Bpod

class bpod_core.bpod.Bpod

Bases: SerialDevice, AbstractBpod

Class for interfacing with a Bpod Finite State Machine.

close()

Close the connection to the Bpod.

Waits for any running trial to finish before closing the serial port.

Raises:

SerialException – If the port could not be closed.

Return type:

None

get_data(*, concat=True, rechunk=False, lazy=False)

Return trial data from the data queue.

Parameters:
  • concat (bool, default: True) – If True, pop and concatenate all DataFrames currently in the queue into a single DataFrame, blocking until at least one is available. If False, pop and return one DataFrame, blocking until one is available.

  • rechunk (bool, default: False) – If True, make sure that the result data is in contiguous memory. Only applies when concat=True.

  • lazy (bool, default: False) – If True, return a polars.LazyFrame. If False, return a polars.DataFrame.

Returns:

One trial’s data, or all available trials concatenated when concat=True.

Columns:

Return type:

polars.DataFrame or polars.LazyFrame

Raises:

BpodError – If the queue is empty and no state machine is currently running.

open()

Open the connection to the Bpod.

Raises:
Return type:

None

peek_data(trigger_states=None, *, lazy=False)

Return a snapshot of the current trial’s data.

Parameters:
  • trigger_states (Collection of str, optional) – Block until at least one of the given states has been entered, then return the snapshot. If None (default), returns immediately.

  • lazy (bool, default: False) – If True, return a polars.LazyFrame. If False (default), return a polars.DataFrame.

Returns:

Events recorded so far in the current trial. Returns an empty DataFrame if no trial is running.

Return type:

polars.DataFrame or polars.LazyFrame

Raises:

ValueError – If one or several of the trigger states are not part of the state machine.

reset_session_clock()

Reset the Bpod session clock.

Returns:

True if the Bpod acknowledged the command.

Return type:

bool

Raises:

BpodError – When the method is called while a state machine is running.

run(state_machine=None, *, trial_number=None, validate=True)

Run a state machine on the Bpod.

Validates, compiles, sends, and queues a state machine for immediate execution. If the Bpod is currently running a state machine, the new one is queued to run as soon as the current one finishes, with no inter-trial gap.

If called without an argument, the previously sent state machine is re-sent from the compilation cache and queued for immediate back-to-back execution.

Parameters:
  • state_machine (StateMachine, optional) – The state machine to run. If not provided, the previously sent state machine is repeated.

  • trial_number (int, optional) – The trial number to assign to the state machine. If not provided, the trial number is automatically incremented with each run.

  • validate (bool, default: True) – If False, the state machine will not be validated prior to compilation. This will speed up the process, but may result in errors or unexpected behavior if the state machine is invalid. Use with caution.

Raises:
  • RuntimeError – If called without an argument and no state machine has been run yet.

  • ValueError – If the state machine is invalid or exceeds hardware limitations.

  • ValidationError – If function arguments don’t match type hints.

Return type:

None

Notes

This method returns once the state machine has been queued on the Bpod. The Bpod will then begin executing the state machine as soon as possible — immediately if the device is idle or right after the current state machine finishes. Subsequent calls of this method will result in continuous acquisition and zero inter-trial downtime (as long as the Bpod’s run queue stays filled).

See also

validate_state_machine

Validation of state machines.

wait

Block until the currently running state machine finishes.

send_softcode(softcode)

Send a softcode to the state machine.

Can be used to trigger transitions.

Parameters:

softcode (int) – The softcode value to send.

Raises:

ValueError – If softcode is out of range.

Return type:

None

set_softcode_handler(softcode_handler=None)

Set the handler function for softcodes sent from the Bpod.

Parameters:

softcode_handler (Callable, optional) – The function to call when a softcode is received.

Return type:

None

set_status_led(enable)

Enable or disable the Bpod status LED.

Parameters:

enable (bool) – True to turn the LED on, False to turn it off.

Returns:

True if the Bpod acknowledged the command.

Return type:

bool

stop_state_machine()

Stop the currently running state machine.

Return type:

None

update_modules()

Update the list of connected modules and their configurations.

Return type:

None

validate_state_machine(state_machine)

Validate the provided state machine for compatibility with the hardware.

Parameters:

state_machine (StateMachine) – The state machine to validate.

Raises:

ValueError – If the state machine is invalid or not compatible with the hardware.

Return type:

None

wait()

Wait for the currently running state machine to finish.

Blocks until the state machine thread completes. If no state machine is currently running, this method returns immediately.

Return type:

None

property address: str

The ZeroMQ address of the Bpod.

property input_event_names: list[str]

Names of all hardware input events.

inputs: dict[str, Input]

Dictionary of available input channels, keyed by name.

property is_queued: bool

Check if a state machine is queued to run after the current one.

property is_ready: bool

Check if a compiled state machine is loaded and ready to run.

property is_running: bool

Check if the Bpod is currently running a state machine.

property location: str | None

The Bpod’s user-defined location, or None if not set.

modules: dict[str, Module]

Dictionary of available modules, keyed by name.

property name: str | None

The Bpod’s user-defined name, or None if not set.

outputs: dict[str, Output]

Dictionary of available output channels, keyed by name.

property port: str

The name of the serial port.

Returns:

The device path of the serial port (e.g., ‘/dev/ttyACM0’).

Return type:

str

property serial0: ExtendedSerial

Primary serial device for communication with the Bpod.

serial1: ExtendedSerial | None = None

Secondary serial device for communication with the Bpod.

serial2: ExtendedSerial | None = None

Tertiary serial device for communication with the Bpod - used by Bpod 2+ only.

property serial_number: str

The Bpod’s unique serial number.

property version: VersionInfo

Version information of the Bpod’s firmware and hardware.