braintools.input.constant

Contents

braintools.input.constant#

braintools.input.constant(I_and_duration)#

Format constant input currents with specified durations.

Creates a sequence of constant current pulses, where each pulse has a specified amplitude and duration. This function is similar to section but uses a different input format.

Parameters:

I_and_duration (list of tuples) – List of (current_value, duration) pairs. Each tuple specifies: - current_value: The amplitude (scalar or array, with or without units) - duration: The time duration (with or without time units)

Returns:

  • current (ndarray or Quantity) – The concatenated current array.

  • total_duration (Quantity) – The total duration of all segments.

Examples

>>> import brainunit as u
>>> import brainstate
>>> import numpy as np
>>> brainstate.environ.set(dt=0.1 * u.ms)

Simple two-phase protocol

>>> current, duration = constant([
...     (0 * u.pA, 100 * u.ms),
...     (10 * u.pA, 200 * u.ms)
... ])

Mixed scalar and array values

>>> current, duration = constant([
...     (0, 50 * u.ms),
...     (np.array([1, 2, 3]) * u.nA, 100 * u.ms),
...     (0, 50 * u.ms)
... ])

Complex multi-phase stimulation

>>> phases = [
...     (0 * u.pA, 20 * u.ms),      # baseline
...     (5 * u.pA, 50 * u.ms),      # weak stimulus
...     (10 * u.pA, 100 * u.ms),    # strong stimulus
...     (2 * u.pA, 30 * u.ms),      # recovery
...     (0 * u.pA, 50 * u.ms),      # rest
... ]
>>> current, total_time = constant(phases)
>>> print(f"Total stimulation time: {total_time}")

Using arrays for spatial patterns

>>> spatial_pattern = np.array([[1, 0], [0, 1]]) * u.nA
>>> current, duration = constant([
...     (np.zeros((2, 2)) * u.nA, 100 * u.ms),
...     (spatial_pattern, 200 * u.ms),
...     (np.zeros((2, 2)) * u.nA, 100 * u.ms)
... ])

Ramp-like approximation with many steps

>>> steps = [(i * u.pA, 10 * u.ms) for i in range(11)]
>>> current, duration = constant(steps)

Notes

  • All current values are converted to the same unit as the first value

  • All durations are converted to the dt time unit

  • The function automatically broadcasts arrays to compatible shapes