braintools.input.Input#

class braintools.input.Input(duration)#

Base class for composable input currents.

This class provides a composable API for building complex input currents by combining simpler components using operators and transformations.

Parameters:

duration (float | Quantity) – The total duration of the input signal. If float, assumes the same unit as the global dt setting.

duration#

The total duration of the input.

Type:

float or Quantity

dt#

The time step, retrieved from global environment.

Type:

Quantity

n_steps#

Number of time steps in the generated array.

Type:

int

shape#

Shape of the generated input array.

Type:

tuple

__call__(recompute=False)[source]#

Generate and return the input current array.

scale(factor)[source]#

Scale the input by a constant factor.

shift(time_shift)[source]#

Shift the input in time (delay or advance).

clip(min_val, max_val)[source]#

Clip the input values to a specified range.

smooth(tau)[source]#

Apply exponential smoothing with time constant tau.

repeat(n_times)[source]#

Repeat the input pattern multiple times.

apply(func)[source]#

Apply a custom transformation function.

Notes

The Input class supports the following operators: - Addition (+): Add two inputs or add a constant - Subtraction (-): Subtract inputs or constants - Multiplication (*): Multiply inputs or scale by constant - Division (/): Divide inputs or divide by constant - Negation (-): Negate the input values - Sequential (&): Concatenate inputs in time - Overlay (|): Take maximum of two inputs at each point

Examples

Basic arithmetic operations:

>>> import brainunit as u
>>> import brainstate
>>> from braintools.input import Ramp, Sinusoidal, Step
>>> brainstate.environ.set(dt=0.1 * u.ms)

>>> # Add two inputs
>>> ramp = Ramp(0, 1, 500 * u.ms)
>>> sine = Sinusoidal(0.5, 10 * u.Hz, 500 * u.ms)
>>> combined = ramp + sine


>>> # Scale an input
>>> scaled_ramp = ramp * 2.0
>>> half_sine = sine.scale(0.5)


>>> # Subtract a baseline
>>> centered = sine - 0.25

Complex compositions:

>>> # Amplitude modulation
>>> carrier = Sinusoidal(1.0, 100 * u.Hz, 1000 * u.ms)
>>> envelope = Ramp(0, 1, 1000 * u.ms)
>>> am_signal = carrier * envelope


>>> # Sequential stimulation protocol
>>> baseline = Step([0], [0 * u.ms], 200 * u.ms)
>>> stim = Step([1], [0 * u.ms], 500 * u.ms)
>>> recovery = Step([0], [0 * u.ms], 300 * u.ms)
>>> protocol = baseline & stim & recovery


>>> # Overlay (maximum) for redundant stimulation
>>> stim1 = Step([0, 1, 0], [0, 100, 400] * u.ms, 500 * u.ms)
>>> stim2 = Step([0, 0.8, 0], [0, 200, 450] * u.ms, 500 * u.ms)
>>> combined_stim = stim1 | stim2

Transformations:

>>> # Time shifting for delayed responses
>>> delayed_sine = sine.shift(50 * u.ms)
>>> advanced_ramp = ramp.shift(-20 * u.ms)


>>> # Clipping for saturation effects
>>> clipped = (ramp * 2).clip(0, 1.5)


>>> # Smoothing for filtering
>>> smooth_steps = Step([0, 1, 0.5, 1, 0],
...                     [0, 100, 200, 300, 400] * u.ms,
...                     500 * u.ms).smooth(10 * u.ms)


>>> # Repeating patterns
>>> burst = Step([0, 1, 0], [0, 10, 20] * u.ms, 50 * u.ms)
>>> repeated_bursts = burst.repeat(10)


>>> # Custom transformations
>>> import jax.numpy as jnp
>>> rectified = sine.apply(lambda x: jnp.maximum(x, 0))
>>> squared = sine.apply(lambda x: x ** 2)

Advanced protocols:

>>> # Complex experimental protocol
>>> pre_baseline = Step([0], [0 * u.ms], 1000 * u.ms)
>>> conditioning = Sinusoidal(0.5, 5 * u.Hz, 2000 * u.ms)
>>> test_pulse = Step([2], [0 * u.ms], 100 * u.ms)
>>> post_baseline = Step([0], [0 * u.ms], 1000 * u.ms)
>>>
>>> protocol = (pre_baseline &
...            (conditioning + 0.5).clip(0, 1) &
...            test_pulse &
...            post_baseline)


>>> # Noisy modulated signal
>>> from braintools.input import WienerProcess
>>> signal = Sinusoidal(1.0, 20 * u.Hz, 1000 * u.ms)
>>> noise = WienerProcess(1000 * u.ms, sigma=0.1)
>>> modulator = (Ramp(0.5, 1.5, 1000 * u.ms) +
...             Sinusoidal(0.2, 2 * u.Hz, 1000 * u.ms))
>>> noisy_modulated = (signal + noise) * modulator
__init__(duration)[source]#

Initialize the Input base class.

Parameters:

duration (float | Quantity) – The total duration of the input.

Methods

__init__(duration)

Initialize the Input base class.

apply(func)

Apply a custom function to the input.

clip([min_val, max_val])

Clip the input values to a range.

generate()

Generate the input current array.

repeat(n_times)

Repeat the input pattern n times.

scale(factor)

Scale the input by a factor.

shift(time_shift)

Shift the input in time.

smooth(tau)

Apply exponential smoothing to the input.

Attributes

dt

Get the time step from global environment.

n_steps

Get the number of time steps.

shape

Get the shape of the input array.