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
|
IntervalSet-like, tuple, list, or ndarray
|
Epochs in any form accepted by :func: |
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
|
IntervalSet-like, tuple, list, or ndarray
|
Epochs in any form accepted by :func: |
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
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
restrict_spike_trains
¶
restrict_spike_trains(trains: 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
trains].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trains
|
sequence of ndarray, or SpikeTrains
|
Per-unit 1-D spike-time arrays. A
:class: |
required |
epochs
|
IntervalSet-like, tuple, list, or ndarray
|
Epochs in any form accepted by :func: |
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
>>> trains = [np.array([0.1, 1.5, 2.9]), np.array([0.5, 3.0, 6.0])]
>>> restrict_spike_trains(trains, (1.0, 3.5))
[array([1.5, 2.9]), array([3.])]