poulet_py.utils package#

class Oscilloscope(max_samples=1000, max_points=100, title='Real-time Data', xlabel='X', ylabel='Y', xlim='auto', ylim='auto', xpadding=0.1, ypadding=0.1, animation_interval=33)[source]#

Bases: object

A real-time data visualization tool similar to an oscilloscope.

This class provides a thread-safe, animated plotting interface for visualizing streaming data with automatic downsampling and view adjustment.

Parameters:
  • max_samples (int, optional) – Maximum number of samples to keep in memory (default: 1000).

  • max_points (int, optional) – Maximum number of points to display (downsampling threshold, default: 100).

  • title (str, optional) – Title of the plot (default: “Real-time Data”).

  • xlabel (str, optional) – Label for the x-axis (default: “X”).

  • ylabel (str, optional) – Label for the y-axis (default: “Y”).

  • xlim (tuple[float, float] or "auto", optional) – Fixed x-axis limits or “auto” for automatic adjustment (default: “auto”).

  • ylim (tuple[float, float] or "auto", optional) – Fixed y-axis limits or “auto” for automatic adjustment (default: “auto”).

  • xpadding (float, optional) – Padding factor for x-axis when in auto mode (default: 0.1).

  • ypadding (float, optional) – Padding factor for y-axis when in auto mode (default: 0.1).

  • animation_interval (int, optional) – Refresh interval in milliseconds (default: 33 ~30fps).

fig#

The figure instance containing the plot.

Type:

matplotlib.figure.Figure

ax#

The axes instance for the plot.

Type:

matplotlib.axes.Axes

Examples

>>> osc = Oscilloscope(max_samples=500, title="Sensor Data")
>>> osc.start()
>>> osc.add_data({"temp": 25.3, "humidity": 45.2})
__init__(max_samples=1000, max_points=100, title='Real-time Data', xlabel='X', ylabel='Y', xlim='auto', ylim='auto', xpadding=0.1, ypadding=0.1, animation_interval=33)[source]#
add_data(y, x=None)[source]#

Add new data points to the oscilloscope.

This method is thread-safe and can be called from multiple threads.

Parameters:
  • y (dict) – Dictionary of y-values where keys are series names and values are the data points. Each call adds one sample per series.

  • x (Any, optional) – Corresponding x-value for the y samples. If None, uses an auto-incremented index (default: None).

Return type:

None

Notes

The data is stored in internal buffers and will be displayed during the next animation frame.

force_redraw()[source]#

Force an immediate redraw of the plot.

Useful when you need to update the display outside the normal animation interval.

Return type:

None

start()[source]#

Start the real-time plotting animation.

Enables interactive mode and begins periodic updates of the display. The plot window will be non-blocking.

See also

stop

Stop the animation and close the window.

Return type:

None

stop()[source]#

Stop the animation and clean up resources.

Closes the plot window and clears all internal data buffers.

Return type:

None

class TCSInterface(port, *, maximum_temperature=40, beep=False, trigger_out_channel=255, read_timeout=2, response_timeout=2, n_trials=1, stimuli=None, mode='random', interstimulus_period=0)[source]#

Bases: TCS

Interface class for the Thermal Cutaneous Stimulator (TCS) device.

Handles serial communication with the device and provides methods to configure and execute thermal stimulus experiments.

Parameters:
  • port (str) – Serial port to which the device is connected.

  • maximum_temperature (float, optional) – Maximum allowed temperature in °C (default: 40).

  • beep (bool, optional) – Whether to enable audible beeps (default: False).

  • trigger_out_channel (int, optional) – Output channel for trigger signals (default: 255).

  • read_timeout (float, optional) – Timeout for read operations in seconds (default: 2).

  • response_timeout (float, optional) – Timeout for device responses in seconds (default: 2).

  • n_trials (int, optional) – Number of trials to run (default: 1).

  • stimuli (list[TCSStimulus], optional) – List of stimulus configurations (default: None).

  • mode ({'random', 'fixed'}, optional) – Stimulus presentation mode (default: ‘random’).

  • interstimulus_period (int or list[int], optional) – Time between stimuli in milliseconds (default: 0).

stimuli#

List of configured stimulus sequences.

Type:

list[TCSStimulus]

n_trials#

Number of trials to be executed.

Type:

int

mode#

Stimulus presentation mode (‘random’ or ‘fixed’).

Type:

str

interstimulus_period#

Time between stimuli in milliseconds. assign a list of integers for random selection or a single integer for fixed time.

Type:

int or list[int]

run(plot=False, max_plot_samples=1000)[source]#

Execute the configured experiment.

to_df()[source]#

Convert collected data to a pandas DataFrame.

__init__(port, *, maximum_temperature=40, beep=False, trigger_out_channel=255, read_timeout=2, response_timeout=2, n_trials=1, stimuli=None, mode='random', interstimulus_period=0)[source]#

Initialize TCS interface with validation.

Args:

port: Serial port device path (e.g. ‘/dev/ttyUSB0’ or ‘COM3’) maximum_temperature: Safety limit for maximum allowed temperature (0-60°C) beep: Whether to enable audible beep during stimulation trigger_out_channel: Output channel of trigger signal (1-255) read_timeout: Serial read timeout in seconds response_timeout: Timeout for command responses in seconds

run(*, plot=False, max_plot_samples=1000)[source]#

Execute the configured experiment.

Parameters:
  • plot (bool, optional) – Whether to show real-time plotting (default: False).

  • max_plot_samples (int, optional) – Maximum number of samples to show in the plot (default: 1000).

Returns:

List of readings collected during the experiment.

Return type:

list[dict]

Raises:

ValueError – If no stimuli are configured before running.

Notes

Each reading contains: - timestamp: Time of reading - temperature: Current temperature - trial: Trial number - Other relevant parameters

property stimuli: list[TCSStimulus]#

Get or set the stimulus sequence.

Returns:

Currently configured stimulus sequence.

Return type:

list[TCSStimulus]

Notes

When setting the stimuli, they are automatically validated and a sequence is generated according to the configured mode (random/fixed) and number of trials.

to_df()[source]#

Convert collected data to a pandas DataFrame.

Returns:

Formatted DataFrame with timestamp as index and trial data.

Return type:

pandas.DataFrame

Examples

>>> df = device.to_df()
>>> df.plot()

Submodules#