braintools.input.section#
- braintools.input.section(values, durations, return_length=False)#
Format an input current with different sections.
Create a piecewise constant current input where each section has a specified value and duration. This is useful for creating experimental protocols with different stimulation phases.
- Parameters:
values (
Sequence) – The current values for each period. Can be scalars or arrays. If arrays, they must be broadcastable to a common shape. Supports units (e.g., pA, nA).durations (
Sequence) – The duration for each period. Must have same length as values. Supports time units (e.g., ms, s).return_length (
bool) – If True, returns a tuple (current, total_duration). If False, returns only the current array. Default is False.
- Returns:
current (ndarray or Quantity) – The formatted current array with shape (n_timesteps,) or (n_timesteps, …) for array values.
total_duration (Quantity, optional) – Total duration (only if return_length=True).
- Raises:
ValueError – If lengths of values and durations don’t match.
Examples
>>> import brainunit as u >>> import brainstate >>> brainstate.environ.set(dt=0.1 * u.ms)
Simple step protocol
>>> current = section( ... values=[0, 10, 0] * u.pA, ... durations=[100, 200, 100] * u.ms ... )
Multiple channel input
>>> import numpy as np >>> values = [np.zeros(3), np.ones(3) * 5, np.zeros(3)] * u.nA >>> current = section( ... values=values, ... durations=[50, 100, 50] * u.ms ... )
Get both current and duration
>>> current, duration = section( ... values=[0, 1, 2, 1, 0] * u.pA, ... durations=[20, 20, 40, 20, 20] * u.ms, ... return_length=True ... ) >>> print(f"Total duration: {duration}")
Complex protocol with different phases
>>> protocol_values = [0, 2, 5, 10, 5, 2, 0] * u.pA >>> protocol_durations = [50, 30, 30, 100, 30, 30, 50] * u.ms >>> current = section(protocol_values, protocol_durations)
Notes
The function handles unit conversions automatically. All values are converted to the same unit as the first value, and all durations are converted to the dt time unit.