neurospatial.behavior.epochs¶
epochs
¶
Array-native epoch selection (restrict / in_epochs).
"Give me my running periods" / "give me trial N" -- restrict time-series and
spike data to a set of time intervals (epochs). Epochs are plain
(start, end) arrays, so this stays array-first and never imports pynapple;
a pynapple IntervalSet is nonetheless accepted transparently because it
is duck-typed (it exposes .start / .end arrays), not isinstance-d.
Functions:
| Name | Description |
|---|---|
restrict |
The headline one-liner: |
in_epochs |
The boolean mask primitive: |
restrict_spike_trains |
Ragged per-unit spike times are not aligned to a common |
Aligned vs ragged
restrict is for arrays that share one time axis (position samples, a single
spike train). restrict_spike_trains is for a ragged collection where each
unit has its own timestamps and there is no shared times -- so each train is
masked against itself.
Functions¶
in_epochs
¶
Boolean mask: True where each element of t falls in any epoch.
Vectorized over samples (broadcasting, no Python loop) and taking the union across intervals (overlapping intervals behave as their union).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
ndarray
|
Timestamps to test (any shape; the mask has the same shape). |
required |
epochs
|
(start, stop), array-like of shape (n, 2), IntervalSet, or None
|
Windows in seconds, on the same clock as the input timestamps. Rows
are sorted and overlapping or touching rows merge. |
required |
closed
|
('both', 'left', 'right', 'neither')
|
Which endpoints are inclusive. Default |
"both"
|
Returns:
| Type | Description |
|---|---|
ndarray of bool
|
Same shape as |
Notes
NaN timestamps evaluate False at every >=/<=/>/<
comparison, so they are (deliberately) excluded from every epoch.
Examples:
>>> import numpy as np
>>> from neurospatial.behavior import in_epochs
>>> in_epochs(np.array([0.0, 2.5, 5.0, 6.0]), (0.0, 5.0))
array([ True, True, True, False])
Source code in src/neurospatial/behavior/epochs.py
restrict
¶
restrict(times: NDArray[float64], *arrays: NDArray[Any], epochs: Any, closed: _Closed = 'both') -> NDArray[Any] | tuple[NDArray[Any], ...]
Restrict times and time-aligned arrays to a set of epochs.
The headline one-liner::
t, pos = restrict(times, positions, epochs=run_epochs)
times is the reference 1-D time axis. Each of *arrays must be
aligned to times (len(arr) == len(times); e.g. positions of
shape (n, n_dims)). All are sliced by the single mask
in_epochs(times, epochs, closed=closed), so alignment and order are
preserved.
With no extra arrays, restrict restricts an event-time array by its
own timestamps -- restrict(spike_train, epochs=...) returns the in-epoch
spikes (here times is the spike train). For ragged per-unit spikes
(each unit its own timestamps, no shared axis), use
:func:restrict_spike_trains instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
times
|
(ndarray, shape(n))
|
Reference time array. |
required |
*arrays
|
ndarray
|
Zero or more arrays aligned to |
()
|
epochs
|
(start, stop), array-like of shape (n, 2), IntervalSet, or None
|
Windows in seconds, on the same clock as the input timestamps. Rows
are sorted and overlapping or touching rows merge. |
required |
closed
|
('both', 'left', 'right', 'neither')
|
Endpoint inclusivity, forwarded to :func: |
"both"
|
Returns:
| Type | Description |
|---|---|
ndarray or tuple of ndarray
|
If no extra arrays were passed, the masked |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any array's first-axis length differs from |
Examples:
>>> import numpy as np
>>> from neurospatial.behavior import restrict
>>> times = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
>>> positions = np.column_stack([times, times])
>>> t, pos = restrict(times, positions, epochs=(1.0, 3.0))
>>> t
array([1., 2., 3.])
Source code in src/neurospatial/behavior/epochs.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
restrict_spike_trains
¶
restrict_spike_trains(spike_times: Sequence[NDArray[float64]], epochs: Any, *, closed: _Closed = 'both') -> list[NDArray[np.float64]]
Restrict each ragged per-unit spike train by its own timestamps.
Ragged per-unit spike times are not aligned to a common times axis, so
each train is masked against itself: [t[in_epochs(t, epochs)] for t in
spike_times].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spike_times
|
sequence of ndarray, or SpikeTrains
|
Per-unit 1-D spike-time arrays. A
:class: |
required |
epochs
|
(start, stop), array-like of shape (n, 2), IntervalSet, or None
|
Windows in seconds, on the same clock as the input timestamps. Rows
are sorted and overlapping or touching rows merge. |
required |
closed
|
('both', 'left', 'right', 'neither')
|
Endpoint inclusivity, forwarded to :func: |
"both"
|
Returns:
| Type | Description |
|---|---|
list of ndarray
|
One masked train per input train, in the same order. Always a plain
|
Examples:
>>> import numpy as np
>>> from neurospatial.behavior import restrict_spike_trains
>>> spike_times = [np.array([0.1, 1.5, 2.9]), np.array([0.5, 3.0, 6.0])]
>>> restrict_spike_trains(spike_times, (1.0, 3.5))
[array([1.5, 2.9]), array([3.])]