nirspipe.io.auxiliary#

Auxiliary (non-optical) channels carried in a SNIRF file, and getting them onto a haemoglobin time axis without aliasing.

MNE’s SNIRF reader does not touch the aux group at all, so a recording’s accelerometers and gyroscopes are simply absent from read_raw_snirf output. They are read here with h5py instead, written once during preprocessing as a derivative table, and resampled onto whatever time axis the postprocessing stage ends up with.

Two properties of the format shape everything below:

  • The aux clock and the optical clock are independent, and neither the ratio between them nor the aux rate itself is guaranteed constant. Alignment therefore goes through the recorded timestamps rather than sample counts, which drift apart over a long recording.

  • Aux is usually sampled well above the optical rate, so putting it on the optical time axis is decimation, and decimation without a low-pass folds the whole difference back into the band that is kept. resample_to_grid filters before it interpolates for that reason.

Anything that writes a SNIRF through MNE drops the aux group, since MNE’s Raw cannot carry one. write_aux_window puts it back after the write, which is what lets a cropped recording reach postprocessing with its accelerometers intact.

Module Contents#

Classes#

ImuTrace

One sensor’s magnitude over time, carrying the unit an axis should print.

Functions#

read_aux_snirf

Read every aux channel of a SNIRF file.

resample_to_grid

Put a signal sampled at t_src onto the grid t_dst, anti-aliased.

gyro_speed

Angular speed over every gyroscope axis, at the aux rate, or None with no gyroscope.

accel_jerk

Jerk magnitude over every accelerometer axis, at the aux rate, or None without one.

imu_traces

{"gyro": ..., "accel": ...} for whichever sensors the aux group carries.

table_channels

A table written by write_aux_table as the (times, values) read_aux_snirf gives.

aux_table_units

The per-channel units the table’s sidecar records, or {} when it has none.

write_aux_window

Copy a recording’s aux channels into another SNIRF, keeping only windows.

aux_table_path

Where the aux table for a recording sits, given any snirf of the same run.

find_aux_table

write_aux_table

Extract a recording’s aux channels into a gzipped TSV, at the rate they were recorded.

read_aux_table

The table written by write_aux_table, time column included.

Data#

API#

nirspipe.io.auxiliary.logger = 'get_logger(...)'#
nirspipe.io.auxiliary.AUX_DESC = 'aux'#
nirspipe.io.auxiliary.AUX_SUFFIX = 'timeseries'#
nirspipe.io.auxiliary.TIME_COLUMN = 'time'#
nirspipe.io.auxiliary.read_aux_snirf(path: pathlib.Path | str) → tuple[dict[str, numpy.ndarray], dict[str, numpy.ndarray], dict[str, str]]#

Read every aux channel of a SNIRF file.

Returns three dicts keyed by channel name: its timestamps, its values, and its unit. Each channel keeps its own time base because the format permits them to differ; callers that need one grid go through resample_to_grid.

A dataTimeSeries of shape (T, 1), which the spec allows, is squeezed to (T,).

nirspipe.io.auxiliary.resample_to_grid(t_src: numpy.ndarray, x: numpy.ndarray, t_dst: numpy.ndarray) → numpy.ndarray#

Put a signal sampled at t_src onto the grid t_dst, anti-aliased.

A rate change downwards is decimation, and decimation without a low-pass folds every component above the target Nyquist back into the band that survives, landing inside the band a confound regression then works in. The low-pass runs first for that reason; when t_dst is the finer grid there is nothing to fold and only the interpolation happens.

The filter treats t_src as uniform while the interpolation uses the timestamps as recorded: jitter too small to move a filter’s response still accumulates into a real offset over a long recording.

Parameters:
  • t_src (ndarray, shape (n_src,)) -- Timestamps of x, in seconds, ascending.

  • x (ndarray, shape (n_src,)) -- The signal to resample.

  • t_dst (ndarray, shape (n_dst,)) -- Target timestamps, in seconds. Values outside the span of t_src are held at the nearest end rather than extrapolated.

Returns:

x on the target grid.

Return type:

ndarray, shape (n_dst,)

Notes

The anti-alias cutoff is 0.4 times the target rate, i.e. 80% of the target Nyquist, leaving a transition band that a 4th-order Butterworth run forwards and backwards (zero phase, effective order 8) has room to roll off in.

nirspipe.io.auxiliary.GYRO_TOKEN = 'gyro'#
nirspipe.io.auxiliary.ACCEL_TOKEN = '(?<![a-z])acc'#
class nirspipe.io.auxiliary.ImuTrace#

Bases: typing.NamedTuple

One sensor’s magnitude over time, carrying the unit an axis should print.

t: numpy.ndarray = None#
y: numpy.ndarray = None#
unit: str = <Multiline-String>#
nirspipe.io.auxiliary.gyro_speed(times: dict[str, numpy.ndarray], values: dict[str, numpy.ndarray], units: dict[str, str] | None = None) → nirspipe.io.auxiliary.ImuTrace | None#

Angular speed over every gyroscope axis, at the aux rate, or None with no gyroscope.

{GYRO_X_1, GYRO_Y_1, GYRO_Z_1} in o/s  ->  (t, |omega|, "°/s")

Each axis has its median taken off first, since a gyroscope at rest reads a small constant offset rather than zero.

nirspipe.io.auxiliary.accel_jerk(times: dict[str, numpy.ndarray], values: dict[str, numpy.ndarray], units: dict[str, str] | None = None) → nirspipe.io.auxiliary.ImuTrace | None#

Jerk magnitude over every accelerometer axis, at the aux rate, or None without one.

{ACCEL_X_1, ACCEL_Y_1, ACCEL_Z_1} in m/s^2  ->  (t, |da/dt|, "m/s³")

The derivative is what removes gravity, which a still accelerometer reads as a constant 9.8 m/s^2 shared out over its axes by how the head is tilted. Differenced over the recorded timestamps; the first sample repeats the second so the lengths match.

nirspipe.io.auxiliary.imu_traces(times: dict[str, numpy.ndarray], values: dict[str, numpy.ndarray], units: dict[str, str] | None = None) → dict[str, nirspipe.io.auxiliary.ImuTrace]#

{"gyro": ..., "accel": ...} for whichever sensors the aux group carries.

nirspipe.io.auxiliary.table_channels(table: pandas.DataFrame) → tuple[dict[str, numpy.ndarray], dict[str, numpy.ndarray]]#

A table written by write_aux_table as the (times, values) read_aux_snirf gives.

nirspipe.io.auxiliary.aux_table_units(path: pathlib.Path) → dict[str, str]#

The per-channel units the table’s sidecar records, or {} when it has none.

nirspipe.io.auxiliary.write_aux_window(source_path: pathlib.Path | str, dest_path: pathlib.Path | str, windows: list[tuple[float, float]]) → list[str]#

Copy a recording’s aux channels into another SNIRF, keeping only windows.

Cropping goes through MNE, and MNE’s Raw has nowhere to hold an aux channel, so a cropped recording written by write_snirf has no aux group at all. This puts one back afterwards, reading the source with h5py and writing the retained samples straight into the file that was just written.

Each window is rebased so the result starts at zero, matching what cropping does to the optical data and to the markers. Several windows are laid end to end, window i starting at the summed length of those before it, which is how mne.concatenate_raws joins the segments the combined output is made of.

Aux keeps the rate it was recorded at; resample_to_grid resamples it downstream.

source 0 to 600 s at 100 Hz, windows [(100, 400)]

-> one channel of 30001 samples stamped 0 to 300 s

Parameters:
  • source_path (path) -- The uncropped SNIRF, read for its aux group.

  • dest_path (path) -- A SNIRF already written by write_snirf. Its nirs group gains aux1, aux2 and so on, numbered over the channels actually written.

  • windows (list of (float, float)) -- Spans to keep, in seconds on the source recording’s clock, in output order.

Returns:

Names of the channels written. Empty when the source has no aux group, or when no channel had a sample inside any window.

Return type:

list of str

nirspipe.io.auxiliary.aux_table_path(reference: pathlib.Path) → pathlib.Path#

Where the aux table for a recording sits, given any snirf of the same run.

sub-01/nirs/sub-01_task-hold_desc-preproc_nirs.snirf

-> sub-01/nirs/sub-01_task-hold_desc-aux_timeseries.tsv.gz

The desc entity names the stage and every stage of one run shares the rest of the name, so dropping desc and putting the aux one back is what makes any stage able to find it.

nirspipe.io.auxiliary.find_aux_table(reference: pathlib.Path) → pathlib.Path | None#
nirspipe.io.auxiliary.write_aux_table(source_path: pathlib.Path, out_path: pathlib.Path) → tuple[pandas.DataFrame, dict] | None#

Extract a recording’s aux channels into a gzipped TSV, at the rate they were recorded.

Returns the table and the facts about it a sidecar should carry, or None when the file has no aux group. The table is not resampled, so it serves any downstream rate.

nirspipe.io.auxiliary.read_aux_table(path: pathlib.Path) → pandas.DataFrame#

The table written by write_aux_table, time column included.