Skip to main content

module maxwell_env.py

Global Variables

  • fs_ms

function stim_process

stim_process(
stim_process_ready,
stim_units,
stim_ready,
stim_shm_name,
env_done,
stim_amp=400,
stim_length=100,
stim_tag='stim'
)

Params stim_amp mV of square wave stim_length us for each phase of the square wave


function find_process_by_port

find_process_by_port(port)

function stop_process_using_port

stop_process_using_port(port)

function init_maxone

init_maxone(
config,
stim_electrodes,
filt=True,
verbose=1,
gain=512,
cutoff='1Hz',
spike_thresh=5,
dummy=False
)

Initialize MaxOne, set electrodes, and setup subscribers.

Args:

  • config (str): Path to the config file for the electrodes.
  • stim_electrodes (list): List of electrode numbers to stimulate.
  • filt (bool): Whether to use the high-pass filter.
  • verbose (int): Verbosity level. 0: No print statements, 1: Print initialization statements, 2: Print all statements.
  • gain (int): Gain of the amplifier. Options: 512, 1024, 2048.
  • cutoff (str): Cutoff frequency of the high-pass filter. Options: '1Hz', '300Hz'.
  • spike_thresh (int): Threshold for spike detection, in units of standard deviations.
  • dummy (bool): Whether to use dummy data.

Returns:

  • tuple: A tuple containing the subscriber, stimulation units, and stimulation electrodes dictionary.

function init_maxone_settings

init_maxone_settings(
gain=512,
amp_gain=512,
cutoff='1Hz',
spike_thresh=5,
verbose=1
)

Initialize MaxOne and set gain and high-pass filter

Parameters ---------- gain : int, 2048

amp_gain : int

cutoff : str, 300Hz


function setup_subscribers

setup_subscribers(filt, verbose=1)

Setup subscribers for events from MaxOne, this allows us to read the data from the chip.


function select_electrodes

select_electrodes(config, stim_electrodes, verbose=1, dummy=False)

function power_cycle_stim_electrodes

power_cycle_stim_electrodes(stim_units)

"Power up and down again all the stimulation units. It appears this is needed to equilibrate the electrodes"

  • from maxwell code

function disconnect_stim_electrodes

disconnect_stim_electrodes(stim_units)

Disconnect each stimulation unit in the list.


function connect_stim_electrodes

connect_stim_electrodes(stim_units)

Connect each stimulation unit in the list.


function parse_events_list

parse_events_list(events_data)

Parse the raw binary events data into a list of SpikeEvent objects.


function parse_frame

parse_frame(frame_data)

Parse the binary frame data into an array of floating-point voltages.


function receive_packet

receive_packet(subscriber, buffer_size=None)

Use the subscriber to capture the frame and event data from the server. Returns an integer frame_number as well as data buffers for the voltage data frame and the spike events. Also sets the current time.


function socket_worker

socket_worker(data_queue, event_queue, subscriber_args)

Worker function that reads from the ZeroMQ socket.


function plot_worker

plot_worker(queue)

function ignore_first_packet

ignore_first_packet(subscriber, verbose=1)

This first loop ignores any partial packets to make sure the real loop gets aligned to an actual frame. First it spins for as long as recv() fails, then it waits for the RCVMORE flag to be False to check that the last partial frame is over.


function ignore_remaining_packets

ignore_remaining_packets(subscriber, verbose=1)

This function reads and discards all remaining data in the buffer. It uses a non-blocking recv() to ensure that it only reads available data and stops when there are no more packets.


function launch_dummy_server

launch_dummy_server(dummy)

class Config

Class to handle configuration file parsing and management.

function __init__

__init__(filename)

Initialize the Config object.

Args:

  • filename (str): Path to the configuration file.

function get_channels

get_channels()

Get all channels from the configuration.

Returns:

  • list: List of channel numbers.

function get_channels_for_electrodes

get_channels_for_electrodes(electrodes)

Get channels corresponding to given electrodes.

Args:

  • electrodes (list): List of electrode numbers.

Returns:

  • list: List of corresponding channel numbers.

function get_electrodes

get_electrodes()

Get all electrodes from the configuration.

Returns:

  • list: List of electrode numbers.

function get_num_channels

get_num_channels()

Get the total number of channels in the configuration.

Returns:

  • int: Number of channels.

class MaxwellEnv

The MaxwellEnv class extends from the BaseEnv class and implements a specific environment for running experiments on MaxWell's MaxOne system. This class is used to interact with the MaxOne system, receive data, and send stimulation commands.

Attributes:

  • config (str): Stores the config filepath in order to easily reload the array.
  • name (str): Stores the name of the environment instance.
  • max_time_sec (int): Stores the maximum experiment time.
  • save_file (str): The file where the data will be saved.
  • stim_electrodes (list): Stores the list of electrodes for stimulation.
  • verbose (int): Controls the verbosity of the environment's operations.
  • array (None): Initialized as None, to be updated in sub-classes as needed.
  • subscriber (None): Initialized as None, to be updated in sub-classes as needed.
  • save_dir (str): Stores the directory where the simulation data will be saved.
  • is_stimulation (bool): A flag that indicates whether a stimulation is going to occur.
  • stim_log_file (str or None): The file where the log of the stimulation is saved. If no stimulation is going to occur, this is None.
  • stim_units (None): Initialized as None, to be updated in sub-classes as needed.
  • stim_electrodes_dict (None): Initialized as None, to be updated in sub-classes as needed.
  • start_time (float): The time when the environment is initialized.
  • cur_time (float): The current time, updated at each step.
  • last_stim_time (float): The time when the last stimulation occurred.
  • smoke_test (bool): A flag that indicates whether the environment is being used for a smoke test.

function __init__

__init__(
config,
name='',
stim_electrodes=[],
max_time_sec=60,
save_dir='data',
multiprocess=False,
render=False,
filt=False,
observation_type='spikes',
verbose=1,
smoke_test=False,
dummy=None,
start=True
)

Initialize the Maxwell environment.

Args:

  • config (str): A path to the maxwell config file. This is usually made by the Maxwell GUI, and contains the information about the array.
  • name (str): The name of the environment instance. This is used for saving data.
  • stim_electrodes (list): A list of electrodes for stimulation. If no electrodes are specified, no stimulation will occur.
  • max_time_sec (int): The maximum experiment time in seconds.
  • save_dir (str): The directory where the stimulation data will be saved.
  • filt (bool): A flag that indicates whether a filter should be applied to the data. The filter is onboard the chip, and is applied to the data before it is sent to the computer. It adds ~100ms of latency.
  • observation_type (str): A string that indicates the type of observation that the environment should return. 'spikes' returns a list of spike events 'raw' returns the raw datastream frame with shape (ch,1)
  • verbose (int): An integer that controls the verbosity of the environment's operations. 0 is silent, 1 is verbose.
  • smoke_test (bool): A flag that indicates whether the environment is being used for a smoke test. If True, the environment will not save any data, will use dummy logic, and no hardware will be used.
  • dummy (str): A flag that will indicate whether to use a dummy maxwell server. 'sine' will use a sine wave for the data filepath will use the first 30 seconds of data from the filepath None will use the real maxwell server

property dt

Returns time since the last step.


property stim_dt

Returns time since last stimulation.


property stim_dts

Returns time since last stimulation.


function clear_buffer

clear_buffer(
num_successive_waits=10,
min_wait_f=0.5,
buffer_size=10,
samp_freq_hz=20000
)

Clear the ZMQ socket buffer, so self.step() returns latest data.

This is done by waiting until the time to receive buffer_size frames is at least to min_wait_f*buffer_size for num_successive_waits successive method calls. There are two buffers: the ZMQ socket buffer and buffer_size used in self.step(buffer_size=buffer_size).

Args:

  • num_successive_waits (int): Number of successive waits before considering the buffer cleared.
  • min_wait_f (float): Minimum wait factor.
  • buffer_size (int): Size of the buffer.
  • samp_freq_hz (int): Sampling frequency in Hz.

function close

close()

Shuts down the environment and saves the data.


function connect_units

connect_units(units=None, inds=None)

Connect the specified stimulation units.


function disconnect_all

disconnect_all()

Disconnect all stimulation units.


function get_observation

get_observation(buffer_size=None)

Create the observation from the electrodes or spike events.

Args:

  • buffer_size (int, optional): Size of the buffer for raw data observation.

Returns:

  • list or numpy.ndarray: Observation data.

function reset

reset()

Reset the environment


function start

start()

Start the experiment by initializing time management, flushing the buffer, and starting the recording.


function step

step(action=None, tag=None, buffer_size=None)

Receive events published since last time step() was called. This includes spike events and raw datastream.

Args:

  • action (list, optional): A list of stimulation commands. Each command is a tuple of the form (electrode_index, amplitude_mV, phase_length_us).
  • tag (str, optional): A tag for the stimulation log.
  • buffer_size (int, optional): Size of the buffer for observation.

Returns:

  • tuple: A tuple containing the observation and a boolean indicating if the episode is done.

function time_elapsed

time_elapsed()

Returns time since initialization of the environment.


class MaxwellStim

Used for stimulating electrodes in a parallel process

function __init__

__init__(stim_units)

class SpikeEvent

SpikeEvent(frame, channel, amplitude)


This file was automatically generated via lazydocs.