nirspipe.io.optode_space#

Where a recording’s optodes sit on its head template, decided once per run and recorded beside it.

SNIRF does not say which space its optode coordinates are in, and a dataset’s _coordsystem.json is often wrong about it. So the placement is decided from the coordinates: the labelled landmarks (the probe’s, else the _coordsystem.json’s) registered onto the same points on the template, or, when that leaves the cap off the scalp, the reading of the coordinates (a head frame, or already on fsaverage) that puts the optodes on the template’s scalp. Prep writes the result as the run’s channel table, and every later reader takes the placement from that record instead of deciding again.

Module Contents#

Classes#

Placement

How a recording’s optode coordinates reach its template’s surface RAS (fsaverage: MNI305).

Functions#

copy_source_fields

Give a SNIRF rewritten through MNE the fields of its source that MNE drops or rewrites.

place_optodes

Decide where a recording’s optodes sit on the template space, from the SNIRF file.

channel_coords_path

The channel table recording a run’s placement on space, beside any file of that run.

recorded_coords_path

The channel table prep wrote for a run, whichever template it was placed on.

recorded_regions

Each source-detector pair’s region in the run’s channel table; empty when it has none.

write_channel_coords

One row per fNIRS channel at its source-detector midpoint on the template, and the placement.

write_coords_sidecar

The JSON beside a channel position table: space, units, columns, and the placement record.

read_placement

The placement prep recorded for a run, or StageError when the tree predates the record.

copy_placement

Carry a run’s placement record to a file written under another run’s name.

hold_placement

Attach a placement to the Raw, for the life of the object; returns raw.

placement_of

The placement of a Raw’s optodes: held on it, recorded for its run, or read off its file.

Data#

API#

nirspipe.io.optode_space.logger = 'get_logger(...)'#
nirspipe.io.optode_space.SCALP_FIT_MAX_MM = 5.0#
nirspipe.io.optode_space.LANDMARK_AGREE_MM = 5.0#
class nirspipe.io.optode_space.Placement#

How a recording’s optode coordinates reach its template’s surface RAS (fsaverage: MNI305).

method is "landmarks" (the file’s landmarks registered onto the template’s), "head" (the template’s own head frame), "mni" or "fsaverage_head" (fsaverage’s surface RAS or head frame, which an infant template reaches through the 10-10 system), or "none" (no reading puts the cap on the scalp). On fsaverage the two head frames are one, "head". transform takes coordinates as MNE holds them, in metres, to the template in millimetres; space names the template.

method: str = None#
transform: numpy.ndarray | None = None#
scalp_fit_mm: float | None = None#
landmarks: int = 0#
note: str | None = None#
space: str = None#
property placed: bool#
apply(coords_m: numpy.ndarray) → numpy.ndarray#
to_json() → dict#
classmethod from_json(record: dict) → nirspipe.io.optode_space.Placement#
nirspipe.io.optode_space.copy_source_fields(source_path, dest_path) → None#

Give a SNIRF rewritten through MNE the fields of its source that MNE drops or rewrites.

source landmarkLabels [Nz, Cz, F3] in mm -> the same three in dest, in its own unit source sourceLabels [F7, F5] -> dest sourceLabels [F7, F5], not S1, S2 source MeasurementTime 14:19:51+01 -> the same string, not 00:00:00Z

MNE keeps a landmark’s position but not its name (anything but the three fiducials reads back as EEG001, EEG002, …), MNE 1.13 reads one in mm a million times too large, and mne-nirs writes template fiducials where the source had none, so a cropped or re-marked recording would otherwise be placed by landmarks it never had. A source without landmarks leaves the copy without any.

mne-nirs also names every optode S1, D1, …, and MNE sets a date it cannot parse to 2000-01-01 and drops the text tags it does not know. Those come back verbatim. The units, the subject id and the per-sample device arrays stay as written.

nirspipe.io.optode_space.place_optodes(snirf_path, space: str = FSAVERAGE) → nirspipe.io.optode_space.Placement#

Decide where a recording’s optodes sit on the template space, from the SNIRF file.

The probe’s own landmarks are used; a probe without any takes those of the recording’s BIDS _coordsystem.json, when that file has them in the optodes’ frame.

Landmarks first: every landmark label the file shares with the template’s 10-10 positions (10-5 on fsaverage), the three fiducials included, is registered onto its position there, and the result is kept when the optodes land on the scalp. Otherwise the coordinates are read in each frame they could be in, and the reading that lands on the scalp is kept. A file no reading places, or one whose probe cannot be read, gets method "none" with the reason.

10-10 labelled landmarks, cap on the scalp once registered       -> "landmarks"
fiducials from another template tilt the cap, raw values fit     -> "mni"
digitiser coordinates never registered to a head                 -> "none"
nirspipe.io.optode_space.channel_coords_path(run_file, space: str = FSAVERAGE) → pathlib.Path#

The channel table recording a run’s placement on space, beside any file of that run.

.../sub-01_ses-02_task-rest_run-01_desc-preproc_nirs.snirf

-> .../sub-01_ses-02_task-rest_run-01_space-fsaverage_channels.tsv

nirspipe.io.optode_space.recorded_coords_path(run_file) → pathlib.Path#

The channel table prep wrote for a run, whichever template it was placed on.

StageError when there is none (a tree from before the record) or more than one.

nirspipe.io.optode_space.recorded_regions(run_file, atlas: str = 'aparc') → dict[str, str]#

Each source-detector pair’s region in the run’s channel table; empty when it has none.

S1_D1 hbo and S1_D1 hbr in precentral-lh -> {"S1_D1": "precentral-lh"}; an unplaced pair, written n/a, is left out.

nirspipe.io.optode_space.write_channel_coords(raw: mne.io.Raw, placement: nirspipe.io.optode_space.Placement, out_path: pathlib.Path, sources: list[str | Path] = (), population: str | None = None) → pathlib.Path#

One row per fNIRS channel at its source-detector midpoint on the template, and the placement.

S1_D1 hbo with source (0.01, 0.05, 0.06) and detector (0.04, 0.05, 0.06) m

-> name x y z = S1_D1 hbo and the transformed midpoint in mm; n/a when unplaced

Each atlas the template ships adds a column naming the region of the pial vertex nearest that midpoint. population is the run’s --population, which the Methods name the template by.

nirspipe.io.optode_space.write_coords_sidecar(out_path, sources: list[str | Path], placement: dict, space: str | None, regions: list[str] = ()) → None#

The JSON beside a channel position table: space, units, columns, and the placement record.

space None is a table whose rows lie on more than one template, named in its own space column. regions are the table’s atlas columns.

nirspipe.io.optode_space.read_placement(run_file) → nirspipe.io.optode_space.Placement#

The placement prep recorded for a run, or StageError when the tree predates the record.

nirspipe.io.optode_space.copy_placement(source_file, dest_file) → None#

Carry a run’s placement record to a file written under another run’s name.

sub-01_task-rest_desc-preproc cut into sub-01_task-early_desc-preproc

-> sub-01_task-early_space-fsaverage_channels.tsv and .json beside it

nirspipe.io.optode_space.hold_placement(raw: mne.io.Raw, placement: nirspipe.io.optode_space.Placement) → mne.io.Raw#

Attach a placement to the Raw, for the life of the object; returns raw.

nirspipe.io.optode_space.placement_of(raw: mne.io.Raw) → nirspipe.io.optode_space.Placement#

The placement of a Raw’s optodes: held on it, recorded for its run, or read off its file.

A derivative takes the record prep left beside it, placed from the recording itself, so it is never placed a second time from its own copy. A raw file read here is placed on fsaverage; a run on another template holds its placement on the Raw.