pylablib.aux_libs.gui package

Submodules

pylablib.aux_libs.gui.device_thread module

class pylablib.aux_libs.gui.device_thread.DeviceThread(name=None, devargs=None, devkwargs=None, signal_pool=None)[source]

Bases: pylablib.core.gui.qt.thread.controller.QTaskThread

Expansion of QTaskThread equipped to deal with a single device.

Parameters:
  • name – thread name
  • devargs – args supplied to setup_task method
  • devkwargs – keyword args supplied to setup_task method
  • signal_poolSignalPool for this thread (by default, use the default common pool)
device

managed device. Its opening should be specified in an overloaded connect_device() method, and it is actually opened by calling open_device() method (which also handles status updates and duplicate opening issues)

qd

device query accessor, which routes device method call through a command ctl.qd.method(*args,**kwarg) is equivalent to ctl.device.method(args,kwargs) called as a query in the device thread

qdi

device query accessor, ignores and silences any exceptions (including missing /stopped controller); similar to .qi accessor for queries

Methods to overload:
setup_task: executed on the thread startup (between synchronization points "start" and "run") finalize_task: executed on thread cleanup (attempts to execute in any case, including exceptions); by default, close the device connection if it is opened connect_device: create the device class and assign it to .device attribute; if connection failed, can leave the attribute None device_open: re-open currently closed device (by default, call .open method of the device) device_close: close currently opened device (by default, call .close method of the device)
Commands:
open_device: open the device, if not already opened close_device: close the device, if opened get_settings: get device settings get_full_info: get full info of the device
finalize_task()[source]

Finalize the thread (always called on thread termination, regardless of the reason)

connect_device()[source]

Connect the device and assign it to the self.device attribute.

Should be overloaded in subclasses. In case of connection error, can leave self.device as None, which symbolizes connection failure.

device_open()[source]

Open the device which has been previously closed.

By default, call .open method of the device.

device_close()[source]

Close the device which is currently opened.

By default, call .close method of the device.

open_device()[source]

Open the device by calling connect_device().

Return True if connection was a success (or the device is already connected) and False otherwise.

close_device()[source]

Close the device.

Automatically called on the thread finalization, ususally shouldn’t be called explicitly.

get_settings()[source]

Get device settings

setup_full_info_job(period=2.0, nodes=None)[source]

Setup a job which periodically obtains full information (by calling get_full_info method) from the device

Useful if obtaining settings takes a lot of time, and they might be needed by some other thread on a short notice.

Parameters:
  • period – job period
  • node – specifies info nodes to be requested (by default, all available nodes)
update_full_info()[source]

Update full info of the device.

A function for a job which is setup in DeviceThread.setup_full_info_job(). Normally doesn’t need to be called explicitly.

get_full_info()[source]

Get full device info

If the full info job is set up using DeviceThread.setup_full_info_job(), use the last cached version of the full info; otherwise, request a new version from the device.

class DeviceMethodAccessor(parent, ignore_errors=False)[source]

Bases: object

Accessor object designed to simplify calling device commands.

Automatically created by the thread, so doesn’t need to be invoked externally.

class pylablib.aux_libs.gui.device_thread.RemoteDeviceThread(name=None, devargs=None, devkwargs=None, signal_pool=None)[source]

Bases: pylablib.aux_libs.gui.device_thread.DeviceThread

Expansion of DeviceThread equipped to deal with a remote device (via RPyC library).

All arguments, attributes and commands are the same as in DeviceThread.

rpyc_device(remote, module, device, *args, **kwargs)[source]

Create a remote device on a different PC via RPyC.

Can replace straightforward device creation for remote devices, i.e., instead of self.device = DeviceModule.DeviceClass(*args,**kwargs) one would call self.device = self.rpyc_device(host,"DeviceModule","DeviceClass",*args,**kwargs).

Parameters:
  • remote – address of the remote host (it should be running RPyC server; see rpyc.run_device_service() for details)
  • module – device class module name
  • device – device class name
  • args – arguments supplied to the device constructor.
  • kwargs – keyword arguments supplied to the device constructor.
rpyc_obtain(obj)[source]

Obtain (i.e., transfer to the local PC) an object returned by the device.

If current device is local, return obj as is.

finalize_task()[source]

Finalize the thread (always called on thread termination, regardless of the reason)

get_settings()[source]

Get device settings

update_full_info()[source]

Update full info of the device.

A function for a job which is setup in DeviceThread.setup_full_info_job(). Normally doesn’t need to be called explicitly.

get_full_info()[source]

Get full device info

If the full info job is set up using DeviceThread.setup_full_info_job(), use the last cached version of the full info; otherwise, request a new version from the device.

pylablib.aux_libs.gui.gui_contoller module

class pylablib.aux_libs.gui.gui_contoller.IGUIController[source]

Bases: sphinx.ext.autodoc.importer._MockObject

Basic layout of a controller object which coordinates interaction between different widgets working on the same task.

class TWidget(params_path)

Bases: tuple

params_path
add_widget_desc(name, params_path=None)[source]

Add a widget description under a given name.

If params_path` is not None, it specifies path under which the widget is stored in the parameters’ table (by default, same as the widget’s name).

set_widget(name, widget)[source]

Set the widget with the given name.

Description with the given name should be created beforehand (see add_widget_desc()).

get_widget(name, default=None)[source]

Get the widget with the given name

get_all_values()[source]

Get all widget parameter values

set_all_values(params)[source]

Set all widget parameter values

get_all_indicators()[source]

Get all GUI indicators as a dictionary

pylablib.aux_libs.gui.helpers module

class pylablib.aux_libs.gui.helpers.StreamFormerThread(name=None, setupargs=None, setupkwargs=None, signal_pool=None)[source]

Bases: pylablib.core.gui.qt.thread.controller.QTaskThread

Thread that combines data from different sources and aligns it in complete rows.

Channels can be added using add_channel() function. Every time the new row is complete, it is added to the current block. When the block is complete (determined by block_period attribute), on_new_block() is called. Accumulated data can be accessed with get_data() and pop_data().

Parameters:
  • name – thread name
  • devargs – args supplied to setup method
  • devkwargs – keyword args supplied to setup method
  • signal_poolSignalPool for this thread (by default, use the default common pool)
block_period

size of a row block which causes on_new_block() call

Commands:
get_data: get the completed aligned data pop_data: pop the completed aligned data (return the data and remove it from the internal storage) clear_table: clear the table with the completed aligned data clear_all: remove all data (table and all filled channels) configure_channel: configure a channel behavior (enable or disable) get_channel_status: get channel status (number of datapoints in the queue, maximal queue size, etc.) get_source_status: get lengths of signal queues for all the data sources
Methods to overload:
setup(): set up the thread cleanup(): clean up the thread prepare_new_data(): modify a new data chunk (dictionary of columns) before adding it to the storage
setup()[source]

Set up the thread

prepare_new_data(columns)[source]

Prepare a newly acquired chunk.

column is a dictionary {name: data} of newly acquired data, where name is a channel name, and data is a list of one or more newly acquired values. Returned data should be in the same format. By default, no modifications are made.

on_new_block()[source]

Gets called every time a new block is complete

cleanup()[source]

Clean up the thread

setup_task(*args, **kwargs)[source]

Setup the thread (called before the main task loop)

finalize_task()[source]

Finalize the thread (always called on thread termination, regardless of the reason)

class ChannelQueue(func=None, max_queue_len=1, required='auto', background=False, enabled=True, fill_on='started', latching=True, expand_list=False, pure_func=True, default=None)[source]

Bases: object

Queue for a single channel.

Manages adding and updating new datapoints. For arguments, see StreamFormerThread.add_channel().

class QueueStatus(queue_len, enabled, max_queue_len)

Bases: tuple

enabled
max_queue_len
queue_len
add(value)[source]

Add a new value (or list of values) to the queue

add_from_func(n=1)[source]

Fill the queue from the function (if available)

n specifies number of values to add.

queued_len()[source]

Get queue length

ready()[source]

Check if at leas one datapoint is ready

ready_len()[source]

Return length of the stored data.

Return 0 if no data is ready, or -1 if “infinite” amount of data is ready (e.g., channel is off)

enable(enable=True)[source]

Enable or disable the queue

set_requried(required='auto')[source]

Specify if receiving value is required

get(n=1)[source]

Pop the oldest values

n specifies number of values to pop. Return list of values.

clear()[source]

Clear the queue

get_status()[source]

Get the queue status

Return tuple (queue_len, enabled, max_queue_len)

add_channel(name, func=None, max_queue_len=1, enabled=True, required='auto', background=False, fill_on='started', latching=True, expand_list=False, pure_func=True, default=None)[source]

Add a new channel to the queue.

Parameters:
  • name (str) – channel name
  • func – function used to get the channel value if no data has been suppled
  • max_queue_len (int) – maximal queue length
  • enabled (bool) – determines if the channel is enabled by default (disabled channel always returns None)
  • required – determines if the channel is required to receive the value to complete the row; by default, False if func is specified and True otherwise
  • background – if required==True, determines whether receiving a new sample in this channel starts a new row (if background==False), or if it’s simply added; if all sample-receiving channels have background==True, the func-defined channels will effectively be filled when the row is complete (corresponds to fill_on=="completed" regardless of its actual value).
  • fill_on (str) – determines when func is called to get the channel value; can be either "started" (when the new row is created) or "completed" (when the new row is complete)
  • latching (bool) – determines value of non-required channel if func is not supplied; if True, it is equal to the last received values; otherwise, it is default
  • expand_list (bool) – if True and the received value is list, assume that it contains several datapoints and add them sequentially (note that this would generally required setting max_queue_len>1, otherwise only the last received value will show up)
  • pure_func (bool) – if True, assume that fast consecutive calls to func return the same result, and the function has no side-effects (in this case, several consecutive calls to func are approximated by a single call result repeated necessary number of times)
  • default – default channel value
subscribe_source(name, srcs, dsts='any', tags=None, parse=None, filt=None)[source]

Subscribe a source signal to a channels.

Called automatically for subscribed channels, so it is rarely called explicitly.

Parameters:
  • name (str) – channel name
  • srcs (str or [str]) – signal source name or list of source names to filter the subscription; can be "any" (any source) or "all" (only signals specifically having "all" as a source).
  • dsts (str or [str]) – signal destination name or list of destination names to filter the subscription; can be "any" (any destination) or "all" (only source specifically having "all" as a destination).
  • tags – signal tag or list of tags to filter the subscription (any tag by default).
  • parse – if not None, specifies a parsing function which takes 3 arguments (src, tag and value) and returns a dictionary {name: value} of channel values to add (useful is a single signal contains multiple channel values, e.g., multiple daq channels) The function is called in the signal source thread, so it should be quick and non-blocking
  • filt (callable) – additional filter function which takes 4 arguments: signal source, signal destination, signal tag, signal value, and checks whether signal passes the requirements.
get_data(nrows=None, columns=None, copy=True)[source]

Get accumulated data.

Parameters:
  • nrows – number of rows to get; by default, all complete rows
  • columns – list of channel names to get; by default all channels
  • copy (bool) – if True, return copy of the internal storage table (otherwise the returned data can increase in size).

Return dictionary {name: [value]} of channel value lists (all lists have the same length) if columns are not specified, or a 2D numpy array if the columns are specified.

pop_data(nrows=None, columns=None)[source]

Pop accumulated data.

Same as get_data(), but removes the returned data from the internal storage.

clear_table()[source]

Clear table containing all complete rows

clear_all()[source]

Clear everything: table of complete rows and all channel queues

configure_channel(name, enable=True, required='auto', clear=True)[source]

Reconfigure existing channel.

Parameters:
  • name (str) – channel name
  • enabled (bool) – determines if the channel is enabled by default (disabled channel always returns None)
  • required – determines if the channel is required to receive the value to complete the row; by default, False if func is specified and True otherwise
  • clear (bool) – if True, clear all channels after reconfiguring
get_channel_status()[source]

Get channel status.

Return dictionary {name: status}, where status is a tuple (queue_len, enabled, max_queue_len).

get_source_status()[source]

Get source incoming queues status.

Return dictionary {name: queue_le}.

class pylablib.aux_libs.gui.helpers.TableAccumulator(channels, memsize=1000000)[source]

Bases: object

Data accumulator which receives data chunks and adds them into a common table.

Can receive either list of columns, or dictionary of named columns; designed to work with StreamFormerThread.

Parameters:
  • channels ([str]) – channel names
  • memsize (int) – maximal number of rows to store
class ChannelData(memsize, chunk_size=None)[source]

Bases: object

Single channel data manager.

Manages the internal buffer to keep continuous list, but reduce number of list appends / removals.

add_data(data)[source]

Add data (list of values) to the buffer

reset_data()[source]

Clean the buffer

get_data(l=None)[source]

Get last at most l samples from the buffer (if l is None, get all samples)

add_data(data)[source]

Add new data to the table.

Data can either be a list of columns, or a dictionary {name: [data]} with named columns.

change_channels(channels)[source]

Change channels in the table.

All the accumulated data will be reset.

reset_data(maxlen=0)[source]

Clear all data in the table

get_data_columns(channels=None, maxlen=None)[source]

Get table data as a list of columns.

Parameters:
  • channels – list of channels to get; all channels by default
  • maxlen – maximal column length (if stored length is larger, return last maxlen rows)
get_data_rows(channels=None, maxlen=None)[source]

Get table data as a list of rows.

Parameters:
  • channels – list of channels to get; all channels by default
  • maxlen – maximal column length (if stored length is larger, return last maxlen rows)
get_data_dict(channels=None, maxlen=None)[source]

Get table data as a dictionary {name: column}.

Parameters:
  • channels – list of channels to get; all channels by default
  • maxlen – maximal column length (if stored length is larger, return last maxlen rows)
class pylablib.aux_libs.gui.helpers.TableAccumulatorThread(name=None, setupargs=None, setupkwargs=None, signal_pool=None)[source]

Bases: pylablib.core.gui.qt.thread.controller.QTaskThread

Table accumulator thread which provides async access to TableAccumulator instance.

Parameters:
  • channels ([str]) – channel names
  • data_source (str) – source thread which emits new data signals (typically, a name of StreamFormerThread thread)
  • memsize (int) – maximal number of rows to store
setup_task(channels, data_source, memsize=1000000)[source]

Setup the thread (called before the main task loop)

start_streaming(path, source_trigger=False, append=False)[source]

Start streaming data to the disk.

Parameters:
  • path (str) – path to the file
  • source_trigger (bool) – if True, start streaming only after source "reset" signal; otherwise, start streaming immediately
  • append (bool) – if True, append new data to the existing file; otherwise, overwrite the file
stop_streaming()[source]

Stop streaming data to the disk

preprocess_data(data)[source]

Preprocess data before adding it to the table (to be overloaded)

get_data_sync(channels=None, maxlen=None, fmt='rows')[source]

Get accumulated table data.

Parameters:
  • channels – list of channels to get; all channels by default
  • maxlen – maximal column length (if stored length is larger, return last maxlen rows)
  • fmt (str) – return format; can be "rows" (list of rows), "columns" (list of columns), or "dict" (dictionary of named columns)
reset()[source]

Clear all data in the table

pylablib.aux_libs.gui.script_thread module

exception pylablib.aux_libs.gui.script_thread.ScriptStopException[source]

Bases: Exception

Exception for stopping script execution

class pylablib.aux_libs.gui.script_thread.ScriptThread(name=None, setupargs=None, setupkwargs=None, signal_pool=None)[source]

Bases: pylablib.core.gui.qt.thread.controller.QTaskThread

A script thread.

Designed to provide means of writing code which interacts with multiple device threads, but reads similar to a standard single-threaded script. To do that, it provides a mechanism of signal montors: one can suspend execution until a signal with certain properties has been received. This can be used to implement, e.g., waiting until the next stream_format/daq sample or a next camera frame.

Parameters:
  • name (str) – thread name
  • setupargs – args supplied to setup_script method
  • setupkwargs – keyword args supplied to setup_script method
  • signal_poolSignalPool for this thread (by default, use the default common pool)
executing

shows whether the script is executing right now; useful in interrupt_script() to check whether it is while the script is running and is done / stopped by user / terminated (then it would be True), or if the script was waiting to be executed / done executing (then it would be False) Duplicates interrupt_reason attribute (executing==False if and only if interrupt_reason=="shutdown")

Type:bool
stop_request

shows whether stop has been requested from another thread (by calling stop_execution()).

Type:bool
interrupt_reason

shows the reason for calling interrupt_script(); can be "done" (called in the end of regularly executed script), "stopped" (called if the script is forcibly stopped), "failed" (called if the thread is shut down while the script is active, e.g., due to error in the script or any other thread, or if the application is closing), or "shutdown" (called when the script is shut down while being inactive)

Type:str
Methods to overload:
setup_script: executed on the thread startup (between synchronization points "start" and "run") finalize_script: executed on thread cleanup (attempts to execute in any case, including exceptions); by default, call interrupt_script() run_script: execute the script (can be run several times per script lifetime) interrupt_script: executed when the script is finished or forcibly shut down (including due to exception or application shutdown)
process_message(tag, value)[source]

Process a new message.

If the function returns False, the message is put in the corresponding queue. Otherwise, the the message is considered to be already, and it gets ‘absorbed’.

setup_script(*args, **kwargs)[source]

Setup script thread (to be overloaded in subclasses)

finalize_script()[source]

Finalize script thread (to be overloaded in subclasses)

By default, calls interrupt_script().

run_script()[source]

Execute script (to be overloaded in subclasses)

interrupt_script(kind='default')[source]

Finalize script execution (the thread is still running, i.e., the script might be started again)

check_stop(check_messages=True)[source]

Check if the script stop is requested.

If it is, raise ScriptStopException which effectively stops execution past this point (the exception is properly caught and processed elsewhere in the service code). To only check if the stop has been requested without exception raising, use stop_request attribute. If check_messages==True, check for new messages from other threads first.

setup_task(*args, **kwargs)[source]

Setup the thread (called before the main task loop)

finalize_task()[source]

Finalize the thread (always called on thread termination, regardless of the reason)

class MonitoredSignal(uid)[source]

Bases: object

add_signal_monitor(mon, srcs='any', dsts='any', tags=None, filt=None)[source]

Add a new signal monitor.

The monitoring isn’t started until start_monitoring() is called. mon specifies monitor name; the rest of the arguments are the same as SignalPool.subscribe()

remove_signal_monitor(mon)[source]

Remove signal monitor with a given name

class TWaitResult(monitor, message)

Bases: tuple

message
monitor
wait_for_signal_monitor(mons, timeout=None)[source]

Wait for a signal to be received on a given monitor or several monitors

If several monitors are given (mon is a list), wait for a signal on any of them. After waiting is done, pop and return signal value (see pop_monitored_signal()).

new_monitored_signals_number(mon)[source]

Get number of received signals at a given monitor

pop_monitored_signal(mon, n=None)[source]

Pop data from the given signal monitor queue.

n specifies number of signals to pop (by default, only one). Each signal is a tuple (mon, sig) of monitor name and signal, where sig is in turn tuple (src, tag, value) describing the signal.

reset_monitored_signal(mon)[source]

Reset monitored signal (clean its received signals queue)

pause_monitoring(mon, paused=True)[source]

Pause or un-pause signal monitoring

start_monitoring(mon)[source]

Start signal monitoring

start_execution()[source]

Request starting script execution

stop_execution()[source]

Request stopping script execution

Module contents