nirspipe.pipeline.hyper.alignment#
Putting a group’s recordings on one clock, and saying afterwards what was done.
alignment_params() is stamped onto every inter-brain table, because --no-align and
an alignment whose trigger sits at t=0 both leave every offset at zero and nothing else on
disk can tell the two apart.
Module Contents#
Functions#
What put these recordings on one clock, for a sidecar beside a number read off them. |
|
One member’s aligned recording, its events and its sidecar, beside each other. |
|
Align N recordings by the first shared annotation trigger. |
|
Cut a second copy of the same recordings onto the clock |
|
Seconds into each original recording at which its aligned copy starts. |
|
Each block’s onset on the shared clock, minus the first member’s copy of it. |
|
Each member’s IMU traces moved onto the clock |
|
Trim all recordings to the shortest duration without trigger-based alignment. |
|
Actually cut every aligned recording down to |
|
Validate |
|
Z-score each channel independently per subject (mean=0, std=1 across time). |
Data#
API#
- nirspipe.pipeline.hyper.alignment.logger = 'get_logger(...)'#
- nirspipe.pipeline.hyper.alignment.ALIGN_STAGE = 'aligned'#
- nirspipe.pipeline.hyper.alignment.alignment_params(raws: dict[str, mne.io.Raw]) dict#
What put these recordings on one clock, for a sidecar beside a number read off them.
{"aligned": True, "align_step": "align_recordings", "align_trigger": {"sub-01": "start", "sub-02": "start"}, "align_offset_s": {"sub-01": 0.0, "sub-02": 22.4}, "aligned_duration_s": 3900.0}
Every inter-brain metric assumes the members share a time axis, and this is what says on disk whether they were put on one.
aligned: nullmeans the recordings reached the metric without going through either route, which is the case worth catching: a reader cannot tell it from a successful alignment by looking at the numbers.Read off the lineage stamps rather than passed in, so a sidecar cannot claim an alignment that did not run.
- nirspipe.pipeline.hyper.alignment.write_aligned_member(raw_aligned: mne.io.Raw, snirf_path, out_dir, group_id: str)#
One member’s aligned recording, its events and its sidecar, beside each other.
out_dir/sub-01_task-rest_nirs.snirf, _events.tsv, and _nirs.json gaining {"align_group": "G1", "align_step": "align_recordings", "align_trigger": "start", "align_offset_s": 22.4, "aligned_duration_s": 3900.0}
The offset goes in the member’s own sidecar. The copied BIDS sidecar is added to, not replaced: the fields a raw recording has to carry are still needed there. Read off the lineage stamp, like
alignment_params(), so the sidecar cannot claim an alignment that did not run.
- nirspipe.pipeline.hyper.alignment.align_recordings(raws: dict[str, mne.io.Raw], task: str) tuple[dict[str, mne.io.Raw], dict[str, float]]#
Align N recordings by the first shared annotation trigger.
Crops each raw from its first occurrence of a common trigger description, then trims all to the same duration (shortest post-crop).
Returns (aligned_raws, {subject_id: crop_offset_seconds}). Each output carries an
alignedlineage stamp naming the trigger and the offset it was cut at, whichalignment_params()reads back for the sidecars.Raises AlignmentError if no shared trigger exists across all subjects.
- nirspipe.pipeline.hyper.alignment.align_like(raws: dict[str, mne.io.Raw], aligned_raws: dict[str, mne.io.Raw]) dict[str, mne.io.Raw]#
Cut a second copy of the same recordings onto the clock
aligned_rawssit on.align_like({"sub-01": intensity}, {"sub-01": haemo_aligned}) -> {"sub-01": intensity cropped to the same window}
The dyad raw report converts to haemoglobin before aligning, so the aligned objects are no longer optical density and nothing downstream can take GVTD or a carpet off them. This brings the intensity copy onto the same window instead of aligning it a second time, so it cannot land on a trigger other than the one every other panel is drawn against.
cropaccumulates intofirst_samp, so the shift already applied to a member isaligned.first_time - raw.first_timewhatever produced it, trigger alignment, a plain trim, or a--tstartwindow on top. A member the aligned set does not carry, or one whose window runs past the end of this copy, is dropped rather than returned short.
- nirspipe.pipeline.hyper.alignment.aligned_offsets(raws: dict[str, mne.io.Raw], aligned_raws: dict[str, mne.io.Raw]) dict[str, float]#
Seconds into each original recording at which its aligned copy starts.
trigger at 22.4 s, then a window from 60 s -> {"sub-02": 82.4}
The aligner’s own offsets stop at the trigger. A window cut afterwards moves the shared clock’s zero again, and this is the offset that converts after both.
- nirspipe.pipeline.hyper.alignment.onset_residuals(raws: dict[str, mne.io.Raw], subject_ids: list[str]) list[dict]#
Each block’s onset on the shared clock, minus the first member’s copy of it.
ca at 30.0 s in sub-01 and 30.098 s in sub-02 -> [{"condition": "ca", "label": "ca", "subject_id": "sub-02", "residual_s": 0.098}]
What alignment cannot remove: a trigger that landed late in one member, or two clocks drifting apart. Blocks are matched by description and by order of occurrence, a repeat labelled
ca (2); a block the first member or this member lacks has no row.labelis the namecondition_windows()gives the same block,ca#2.
- nirspipe.pipeline.hyper.alignment.align_imu_like(imu: dict[str, dict[str, ImuTrace]], raws: dict[str, mne.io.Raw], aligned_raws: dict[str, mne.io.Raw]) dict[str, dict[str, ImuTrace]]#
Each member’s IMU traces moved onto the clock
aligned_rawssit on.member aligned 22.4 s in, gyro jolt at 30.0 s on its own clock -> the same jolt at 7.6 s on the shared clock
imuis{sid: {sensor: ImuTrace}}on each member’s own clock, zero at the first sample ofraws[sid], the same assumption the aux regressors make. The shift is the onealign_like()cuts the optical copy by, so the two cannot disagree. Samples outside the aligned window are dropped; a member the aligned set does not carry is left out.
- nirspipe.pipeline.hyper.alignment.trim_to_shortest(raws: dict[str, mne.io.Raw]) tuple[dict[str, mne.io.Raw], dict[str, float]]#
Trim all recordings to the shortest duration without trigger-based alignment.
Use for resting-state data where no shared trigger exists. Returns (trimmed_raws, {subject_id: 0.0}).
The outputs carry an
alignedstamp too, withaligned=Falseon it: the offsets are zero here and can legitimately be zero after a real alignment as well, so the flag is the only thing that tells a reader which of the two produced the numbers.
- nirspipe.pipeline.hyper.alignment.crop_aligned_window(raws: dict[str, mne.io.Raw], tstart: float | None, tend: float | None) dict[str, mne.io.Raw]#
Actually cut every aligned recording down to
[tstart, tend].For a metric with no frequency axis of its own, where a window carries no edge the whole record would not have had: the Welch coherence and the signal overlays of the raw dyad report.
nirspipe-hyperdoes not use this. Its window goes throughresolve_analysis_window()instead and is taken out of the wavelet transform, a cut stretch transformed alone having two edges and a cone of influence of its own.tendpast the end of the data is clipped rather than refused: recordings differ in length and an over-long window is a request for “to the end”, not a mistake.
- nirspipe.pipeline.hyper.alignment.resolve_analysis_window(raws: dict[str, mne.io.Raw], tstart: float | None, tend: float | None) tuple[float, float] | None#
Validate
--tstart/--tendagainst the aligned recordings and return the window.Runs after align_recordings / trim_to_shortest, where t=0 is the shared trigger (or the common start) and all recordings already have one length. That is what makes a single window valid for the whole group: the same [tstart, tend] names the same moment of the task in every subject, which it would not on the raw per-subject clocks.
Example: a 600 s aligned dyad with tstart=60, tend=300 gives
(60.0, 300.0), and the coherence reported is the 240 s between them.It returns a window; it does not cut. The recordings stay whole and the window is taken out of the wavelet transform afterwards, which is the same route
--by-conditiontakes (seewindow_result()).tendpast the end of the data is clipped rather than refused: recordings differ in length and an over-long window is a request for “to the end”, not a mistake. Atstartat or past the end has no data to describe and raises.
- nirspipe.pipeline.hyper.alignment.normalize_raws(raws: dict[str, mne.io.Raw]) dict[str, mne.io.Raw]#
Z-score each channel independently per subject (mean=0, std=1 across time).
Applied after alignment so all subjects share the same time axis. Channels with near-zero variance are left unchanged (divided by 1.0).
Affects signal overlay display only; coherence and ISC values are scale-invariant and unchanged. SCI / bad-channel detection run on raw CW data before normalization.