bpod_core.bpod.Bpod

class bpod_core.bpod.Bpod

Bases: SerialDevice, AbstractBpod

Class for interfacing with a Bpod Finite State Machine.

__init__(port=None, serial_number=None, *, remote=False) None
Parameters:
  • port (str | None)

  • serial_number (str | None)

  • remote (bool)

Return type:

None

close() None

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:

  • time – absolute Bpod timestamp (Datetime(time_unit='us'))

  • trial – zero-based trial index (UInt16)

  • state – state name (Categorical)

  • type – event type (Enum)

  • event – input event name (Categorical)

  • channel – channel name (Categorical)

  • value – channel value (UInt8)

Return type:

pl.DataFrame

Raises:

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

open() None

Open the connection to the Bpod.

Raises:
  • SerialException – If the port could not be opened.

  • BpodError – If the handshake fails.

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:

pl.DataFrame or pl.LazyFrame

Raises:

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

reset_session_clock() bool

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, *, validate=True) None

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.

  • 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.

  • validate_callCallHintViolation – 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) None

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) 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) bool

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() None

Stop the currently running state machine.

Return type:

None

update_modules() None

Update the list of connected modules and their configurations.

Return type:

None

validate_state_machine(state_machine) None

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() None

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: NamedTuple

Available input channels.

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

Get the location of the Bpod device.

modules: _ModuleDict

Available modules, keyed by name.

property name: str | None

Get the name of the Bpod device.

outputs: NamedTuple

Available output channels.

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.