daq

Protocol and threaded interface for data acquisition.

class axopy.daq.DaqStream(device)[source]

Asynchronous interface to an input device.

Runs a persistent while loop wherein the device is repeatedly polled for data. When the data becomes available, it is emitted and the loop continues.

There are effectively two methods of this class: start and stop. These methods do as their names suggest – they start and stop the underlying device from sampling new data.

The device used to create the DaqStream is also accessible via the device attribute so you can change settings on the underlying device any time (e.g. sampling rate, number of samples per update, etc.).

Parameters:

device (daq) – Any object implementing the AxoPy data acquisition interface. See NoiseGenerator for an example.

updated

Transmitted when the latest chunk of data is available. The data type depends on the underlying input device, but it is often a numpy ndarray.

Type:

Transmitter

disconnected

Transmitted if the device cannot be read from (it has disconnected somehow).

Type:

Transmitter

finished

Transmitted when the device has stopped and samping is finished.

Type:

Transmitter

run()[source]

Implementation for the underlying QThread.

Don’t call this method directly – use start() instead.

property running

Boolean value indicating whether or not the stream is running.

start()[source]

Start the device and begin reading from it.

stop(wait=True)[source]

Stop the stream.

Parameters:

wait (bool, optional) – Whether or not to wait for the underlying device to stop before returning.

class axopy.daq.Keyboard(rate=10, keys=None)[source]

Keyboard input device.

The keyboard device works by periodically sampling (with the rate specified) whether or not the watched keys have been pressed since the last sampling event. The output is a numpy array of shape (n_keys, 1), where the numerical values are booleans indicating whether or not the corresponding keys have been pressed.

Parameters:
  • rate (int, optional) – Sampling rate, in Hz.

  • keys (container of str, optional) – Keys to watch and use as input signals. The keys used here should not conflict with the key used by the Experiment to start the next task.

Notes

There are a couple reasonable alternatives to the way the keyboard device is currently implemented. One way to do it might be sampling the key states at a given rate and producing segments of sampled key state data, much like a regular data acquisition device. One issue is that actual key state (whether the key is being physically pressed or not) doesn’t seem to be feasible to find out with Qt. You can hook into key press and key release events, but these are subject to repeat delay and repeat rate.

Another possible keyboard device would be responsive to key press events themselves rather than an input sampling event. While Qt enables event-based keyboard handling, the method used here fits the input device model, making it easily swappable with other input devices.

eventFilter(self, a0: QObject, a1: QEvent) bool[source]
read()[source]

Read which keys have just been pressed.

Returns:

data – A boolean array with a 1 indicating the corresponding key has been pressed and a 0 indicating it has not.

Return type:

ndarray, shape (n_keys, 1)

reset()[source]

Reset the input device.

start()[source]

Start the keyboard input device.

stop()[source]

Stop the keyboard input device.

You may need to stop the device in case you want to be able to use the keys watched by the device for another purpose.

class axopy.daq.Mouse(rate=10, position=False)[source]

Mouse input device.

The mouse device works by periodically sampling (with the rate specified) the mouse position within the AxoPy experiment window. The output is in the form of a numpy array of shape (2, 1), representing either the change in position (default) or the absolute position in the window.

Parameters:
  • rate (int, optional) – Sampling rate, in Hz.

  • position (bool, optional) – Whether or not to return the mouse’s position (instead of the position difference from the prevoius sample).

Notes

In Qt’s coordinate system, the positive y direction is downward. Here, this is inverted as a convenience (upward movement of the mouse produces a positive “velocity”).

Mouse events are intercepted here but they are not consumed, meaning you can still use the mouse to manipulate widgets in the experiment window.

eventFilter(self, a0: QObject, a1: QEvent) bool[source]
read()[source]

Read the last-updated mouse position.

Returns:

data – The mouse “velocity” or position (x, y).

Return type:

ndarray, shape (2, 1)

reset()[source]

Clear the input device.

start()[source]

Start sampling mouse movements.

stop()[source]

Stop sampling mouse movements.

class axopy.daq.NoiseGenerator(rate=1000, num_channels=1, amplitude=1.0, read_size=100)[source]

An emulated data acquisition device which generates random data.

Each sample of the generated data is sampled from a zero-mean Gaussian distribution with variance determined by the amplitude specified, which corresponds to three standard deviations. That is, approximately 99.7% of the samples should be within the desired peak amplitude.

NoiseGenerator is meant to emulate data acquisition devices that block on each request for data until the data is available. See read() for details.

Parameters:
  • rate (int, optional) – Sample rate in Hz. Default is 1000.

  • num_channels (int, optional) – Number of “channels” to generate. Default is 1.

  • amplitude (float, optional) – Approximate peak amplitude of the signal to generate. Specifically, the amplitude represents three standard deviations for generating the Gaussian distributed data. Default is 1.

  • read_size (int, optional) – Number of samples to generate per read() call. Default is 100.

read()[source]

Generates zero-mean Gaussian data.

This method blocks (calls time.sleep()) to emulate other data acquisition units which wait for the requested number of samples to be read. The amount of time to block is calculated such that consecutive calls will always return with constant frequency, assuming the calls occur faster than required (i.e. processing doesn’t fall behind).

Returns:

data – The generated data.

Return type:

ndarray, shape (num_channels, read_size)

reset()[source]

Reset the device back to its initialized state.

start()[source]

Does nothing for this device. Implemented to follow device API.

stop()[source]

Does nothing for this device. Implemented to follow device API.