Data models

This model defines classes that allow easy integration of streaming data with the pre-processing and analysis pipelines. Data models provide a uniform

KSTAR ECEi data

General 2d image data

Implements classes and methods for channels located within an a 2d array.

class data_models.channels_2d.channel_2d(ch_v, ch_h, chnum_v, chnum_h, order)

Abstraction of a channel in 2d array.

__init__(ch_v, ch_h, chnum_v, chnum_h, order)

Initializes channel_2d class.

Args:
ch_v (int):

Horizontal channel number

ch_h (int):

Vertical channel number

chnum_v (int):

Total count of vertical channels

chnum_h (int):

Total count of vertical channels

order (string):

Either ‘horizontal’ or ‘vertical’. Denotes whether horizontal or vertical channels are arranged consecutively

Returns:

None

get_idx()

Returns the linear, ZERO-BASED, index corresponding to ch_h and ch_v.

Returns:
index (int):

Linear, zero-based index corresponding to ch_h and ch_v in the 2d array.

get_num()

Returns linear channel index.

class data_models.channels_2d.channel_pair(ch1, ch2)

Custom defined channel pair.

This is just a tuple with an overloaded equality operator. It’s also hashable so one can use it in conjunction with sets.

>>> ch1 = channel_2d(13, 7, 24, 8, 'horizontal')
>>> ch2 = channel_2d(12, 7, 24, 8, 'horizontal')
>>> ch_pair = channel_pair(ch1, c2)
>>> channel_pair(ch1, ch2) == channel_pair(ch2, c1)
True

The hash is that of a tuple consisting of the ordered channel indices of ch1 and ch2.

__init__(ch1, ch2)

Initializes channel pair with 2 channels.

Args:
ch1 (channel_2d):

First channel

ch2 (channel_2d):

Second channel

Returns:

None

class data_models.channels_2d.channel_range(ch_start, ch_end)

Defines iterators over a 2d sub-array.

This class defines an iterator over a rectangular selection in a 2d sub-array, as defined by vertical and horizontal initial and final position (vi, hi), and (vf, hf).

    v
    ^
    |
6 | oooooo
5 | ooxxxo
4 | ooxxxo
3 | ooxxxo
2 | ooxxxo
1 | oooooo
    +--------> h
    123456

The rectangular selection above shows (vi,hi) = (2,3) and (vf, hf) = (5,5). Iteration over this selection with horizontal channels consecutively ordered gives the index series

(3,2), (4,2), (5,2),
(3,3), (4,3), (5,3),
(3,4), (4,4), (5,4),
(4,5), (4,5), (5,5).
__init__(ch_start, ch_end)

Initializes the channel_range object.

Args:

ch_start (channel_2d): Initial channel for iteration ch_end (channel_2d): Stop channel for iteration

length()

Returns the number of channels in the range.

Returns:

int: Number of channels in the range

class data_models.channels_2d.num_to_vh(chnum_v: int, chnum_h: int, order: str)

Functor that returns a tuple (ch_v, ch_h) for a channel number.

Note that these are 1-based numbers.

>>> obj = num_to_vh(24, 8, "vertical")
>>> ch_num_to_vh(17)
     (3, 1)
__init__(chnum_v: int, chnum_h: int, order: str)

Initializes with number of vertical and horizontal views.

Args:
chnum_v (int):

Number of vertical views in the diagnostic

chnum_h (int):

Number of horizontal views in the diagnostic

order (string) :

Either ‘horizontal’ or ‘vertical’ denotes whether horizontal or vertical channels are ordered consecutively.22

class data_models.channels_2d.vh_to_num(chnum_v, chnum_h, order='horizontal')

Returns the linear channel number for a tuple of channel indices.

>>> obj = vh_to_num(24, 8, order='horizontal')
>>> obj(2, 4)
12
>>> obj = vh_to_num(24, 8, order='vertical')
>>> obj(2, 4)
28
__init__(chnum_v, chnum_h, order='horizontal')

Initializes the functor class.

Args:
ch_v (int):

vertical channel number

ch_h (int):

horizontal channel number

order (str) :

Either ‘horizontal’ or ‘vertical’. Identifies the whether the horizontal or vertical ordered consecutively.

Returns:

None

Timebase

Defines time-base classes to be used in streaming settings.

class data_models.timebase.timebase_streaming(t_start: float, t_end: float, f_sample: float, samples_per_chunk: int, chunk_idx: int)

Defines a timebase for a time-chunk of data in a stream.

The start and end time refer to the entire data stream. To describe the position of the time-chunk in the stream, the member function analysis.timebase.timebase_streaming.time_to_idx() returns an integer index if the time argument falls within the current time chunk.

>>> t_start = -0.1   # This is the start time of the stream
>>> t_end = 9.9  # This is the stop time of the stream
>>> f_sample = 5e5  # Sampling frequency
>>> chunk_size = 10_000  # Number of samples per time-chunk
>>> chunk_idx = 12  # We want to describe the 12th time-chunk.

>>> tb_stream = timebase_streaming(t_start, t_end, f_sample, chunk_size, chunk_idx)

>>> for t, target in range(chunk_idx * chunk_size - 2,
                           chunk_idx * chunk_size + 2)):
        time = tb_stream.t_start + t / tb_stream.f_sample
        print(tb_stream.time_to_idx(time))

    [None, None, 0, 1, 2]

The first two sample times fall before the described time-chunk. Here time_to_idx returns None. The last three sample times are within time-chunk and time_to_idx returns the time index of the relevant sample.

__init__(t_start: float, t_end: float, f_sample: float, samples_per_chunk: int, chunk_idx: int)

Defines a timebase for a data chunk in the stream.

Args:
t_start (float):

Start time of the data stream, in seconds

t_end (float):

End time of the data stream, in seconds

f_sample (float):

Sampling frequency, in Hz

samples_per_chunk (int):

Number of samples per chunk

chunk_idx (int):

Index of the chunk that this timebase is used used

Returns:

None

get_trange()

Returns start and end time in this chunk.

Returns:
(t0, t1): (tuple[int]):

Start and end time in this chunk.

time_to_idx(time: float)

Generates an index suitable to index the current data chunk in time.

Args:
time (float):

Absolute time we wish to get an index for

Returns:
tidx_rel (int):

Relative index in the current time chunk. Returns None of time is outside of current chunk.

Data Model Helper functions