nirspipe.io.derivatives#
Write BIDS Derivatives output structure.
Module Contents#
Functions#
Keep only task, acq and run from source entities so output filenames mirror the input. |
|
A derivative’s path, with its directory already created. |
|
Where the raw QC page keeps a run’s per-channel keep/drop decisions. |
|
A subject’s own folder, holding their HTML reports. The mirror of |
|
A group’s own folder, holding its HTML reports. The mirror of |
|
A dyad’s label, |
|
One recorded occasion of a group, |
|
A group’s tables, sidecars and quality record: |
|
A dyad derivative’s path, with its directory already created. |
|
Shape of the signal as written, so a sidecar records what the step left behind. |
|
The REQUIRED |
|
A stage’s sidecar, refused when written before sidecars carried the |
|
Write a JSON sidecar next to out_path (same name, .json extension). |
|
A JSON object off disk, or {} when the file is missing, unreadable or not an object. |
|
The sidecar of one post-stage output: the step, the files it came from, its settings, the bads. |
|
Input files as BIDS URIs, and the absolute paths of those no linked dataset holds. |
|
The runs a |
|
Read one BIDS entity back out of a filename, or None when it carries no such key. |
|
Narrow a list of candidate recordings to exactly one, or say why it cannot. |
|
The bare labels of a tree’s |
|
Every |
|
Locate the desc-{desc} snirf for subject_id under output_dir. |
|
|
|
Register the paths BIDS has no say over, so a validator skips rather than flags them. |
|
The nearest folder above |
|
|
|
The file a Sources entry of a sidecar at |
|
Write dataset_description.json for the derivatives dataset. |
Data#
API#
- nirspipe.io.derivatives.logger = 'get_logger(...)'#
- nirspipe.io.derivatives.RUN_ENTITIES = ('task', 'acquisition', 'run')#
- nirspipe.io.derivatives.carry_entities(source_entities: dict[str, str] | None) dict[str, str]#
Keep only task, acq and run from source entities so output filenames mirror the input.
- nirspipe.io.derivatives.build_output_path(output_dir: pathlib.Path, subject: str, entities: dict[str, str], suffix: str, extension: str, session: str | None = None) pathlib.Path#
A derivative’s path, with its directory already created.
entities={"task": "rest", "desc": "preproc"}, suffix="nirs", extension=".snirf" -> <out>/sub-01/nirs/sub-01_task-rest_desc-preproc_nirs.snirf
The name comes from
nirspipe.io.naming, which reads it off the one config every reader parses against. This is the writing half: it exists so a caller that is about to write a file does not have to remember to create the folder, and so a caller that only wants a name can ask naming directly and leave no tree behind.
- nirspipe.io.derivatives.channel_decisions_path(output_dir: pathlib.Path, subject: str, task: str | None = None, session: str | None = None, rater: str | None = None) pathlib.Path#
Where the raw QC page keeps a run’s per-channel keep/drop decisions.
Re-exported from
nirspipe.io.naming, which builds it.
- nirspipe.io.derivatives.subject_report_dir(output_dir: pathlib.Path, subject_id: str) pathlib.Path#
A subject’s own folder, holding their HTML reports. The mirror of
group-<id>/.
- nirspipe.io.derivatives.group_report_dir(output_dir: pathlib.Path, group_id: str) pathlib.Path#
A group’s own folder, holding its HTML reports. The mirror of
sub-<id>/.
- nirspipe.io.derivatives.group_label(group_id: str, task: str, session: str | None = None) str#
A dyad’s label,
group-G1_task-restorgroup-G1_ses-2_task-rest.
- nirspipe.io.derivatives.occasion_label(group_id: str, session: str | None = None) str#
One recorded occasion of a group,
"G1"or"G1 ses-2".
- nirspipe.io.derivatives.group_data_dir(output_dir: pathlib.Path, group_id: str, session: str | None = None) pathlib.Path#
A group’s tables, sidecars and quality record:
group-<id>/[ses-<s>/]nirs.The mirror of a subject’s
sub-<id>/[ses-<s>/]nirs.
- nirspipe.io.derivatives.group_output_path(output_dir: pathlib.Path, group_id: str, entities: dict, suffix: str, extension: str, session: str | None = None) pathlib.Path#
A dyad derivative’s path, with its directory already created.
entities={"task": "rest", "statistic": "wtc"}, suffix="relmat", extension=".tsv" -> <out>/group-G1/nirs/group-G1_task-rest_stat-wtc_relmat.tsv
The mirror of
build_output_path()for the group side. It builds the whole name rather than handing out a prefix to append to, so each dimension stays its own entity.
- nirspipe.io.derivatives.data_state(raw: Any) dict[str, Any]#
Shape of the signal as written, so a sidecar records what the step left behind.
- A 56-channel 10 Hz recording with 16 channels marked bad ->
{“n_channels”: 56, “n_bad”: 16, “sfreq”: 10.0, “duration_s”: 595.2}
Cheap: everything here is already on the Raw being written.
- nirspipe.io.derivatives.CARRIED_NIRS_FIELDS = ('TaskName', 'NIRSSourceOptodeCount', 'NIRSDetectorOptodeCount')#
- nirspipe.io.derivatives.nirs_required_fields(raw: Any, source: dict[str, Any]) dict[str, Any]#
The REQUIRED
_nirs.jsonfields a derivative carries from its source’s sidecar.source {"TaskName": "rest", "NIRSSourceOptodeCount": 8, ...}, raw at 5 Hz with 40 channels -> {"TaskName": "rest", "NIRSSourceOptodeCount": 8, "NIRSDetectorOptodeCount": 8, "SamplingFrequency": 5.0, "NIRSChannelCount": 40}
A field the source lacks stays absent. The aux-channel counts are left out: no derivative SNIRF holds those channels.
- nirspipe.io.derivatives.require_nirs_fields(source_path: pathlib.Path) dict[str, Any]#
A stage’s sidecar, refused when written before sidecars carried the
_nirs.jsonfields.
- nirspipe.io.derivatives.write_sidecar_json(out_path: pathlib.Path, provenance: dict[str, Any]) None#
Write a JSON sidecar next to out_path (same name, .json extension).
- provenance should include:
pipeline_version
step (e.g. ‘od_conversion’)
parameters (dict of relevant config values)
Sources (BIDS field: source paths, written as BIDS URIs; see to_bids_uri)
data (channel count, sampling rate, duration; see data_state)
timestamp (ISO-8601, auto-added if missing)
- nirspipe.io.derivatives.read_json(path: pathlib.Path) dict[str, Any]#
A JSON object off disk, or {} when the file is missing, unreadable or not an object.
- nirspipe.io.derivatives.write_step_sidecar(path: pathlib.Path, step: str, source: str | None, bads: list[str], excluded_spans: list[dict] | None = None, inputs: Iterable[str | Path] = (), description: str | None = None, columns: dict | None = None, **params: Any) None#
The sidecar of one post-stage output: the step, the files it came from, its settings, the bads.
inputsare tables the step read besidesource, such as an events table.descriptionsays what the values are;columnsare a table’s BIDS column entries.
- nirspipe.io.derivatives.split_sources(paths: Iterable[str | Path], written: pathlib.Path) tuple[list[str], list[str]]#
Input files as BIDS URIs, and the absolute paths of those no linked dataset holds.
[<raw>/sub-01/nirs/sub-01_events.tsv, /home/me/design.tsv]->
(["bids:raw:sub-01/nirs/sub-01_events.tsv"], ["/home/me/design.tsv"])
An input table may live anywhere, and a file outside every dataset has no BIDS URI; its path is still recorded rather than the write refused.
- nirspipe.io.derivatives.joined_runs(path: Path | str) list#
The runs a
--combine-runsseries was joined from, as its sidecar names them; [] for a run’s own file.
- nirspipe.io.derivatives.entity_of(path: Path | str, name: str) str | None#
Read one BIDS entity back out of a filename, or None when it carries no such key.
entity_of("sub-01_task-rest_desc-preproc_nirs.snirf", "desc")->"preproc"The one place the package parses an entity out of a name, so a rename cannot leave one reader matching and another silently finding nothing. A label is alphanumeric by BIDS definition, so the match ends at the underscore or dot that follows it.
- nirspipe.io.derivatives.select_one_run(candidates: list[pathlib.Path], *, what: str, subject_id: str, task: str, session: str | None = None, run: str | None = None) pathlib.Path#
Narrow a list of candidate recordings to exactly one, or say why it cannot.
[sub-01_ses-a_x, sub-01_ses-b_x], session="a"-> the ses-a one[sub-01_ses-a_x, sub-01_ses-b_x], session=None-> raises, naming a and bOne recording per group member is what every inter-brain metric assumes, so ambiguity is refused rather than resolved to the first match.
- nirspipe.io.derivatives.subject_labels(root: pathlib.Path) list[str]#
The bare labels of a tree’s
sub-*folders, sorted:sub-01/,sub-02/-> [“01”, “02”].
- nirspipe.io.derivatives.subject_nirs_dirs(output_dir: pathlib.Path, subject_id: str, session: str | None = None) list[pathlib.Path]#
Every
nirs/a subject’s derivatives can sit in, session level included.sub-01/nirs and sub-01/ses-a/nirs on disk, session=None -> both the same tree, session="a" -> the ses-a one only
derivatives_path()writes a session to its own folder, so a subject recorded over two sessions has nosub-01/nirsat all. Readers that build the path by hand find nothing there and report the subject as missing. Only directories that exist are returned, so a caller that gets an empty list is looking at a subject with no output.
- nirspipe.io.derivatives.find_preproc_snirf(output_dir: pathlib.Path, subject_id: str, task: str, desc: str = 'preproc', session: str | None = None, run: str | None = None) pathlib.Path#
Locate the desc-{desc} snirf for subject_id under output_dir.
Every pipeline step writes one snirf per desc, so desc is what selects a stage: “preproc” is Beer-Lambert output, “filtered” the bandpassed one, “errts” the GLM residual. Raises MissingDerivativesError if the directory or the file is absent.
Searched across
subject_nirs_dirs(), so a session either names its folder or, unnamed, has every session’s folder offered toselect_one_runat once. That is what lets a multi-session tree raise “which session” rather than “no such directory”.
- nirspipe.io.derivatives.read_dataset_description(root: pathlib.Path) dict[str, Any]#
root’s dataset_description.json, or {} when absent or not JSON.Another run can be replacing the file at the instant it is opened, which Windows reports as a permission error; that is retried, never read as an empty description, since an empty one has no links and every Sources URI of the run would then be refused.
- nirspipe.io.derivatives.JSON_ONLY_DESCS = ('sqm', 'sqmraw', 'rating', 'rawrating', 'rawdecision', 'rawspans')#
- nirspipe.io.derivatives.RAW_QC_STAGES = 'stages'#
- nirspipe.io.derivatives.write_bidsignore(output_dir: pathlib.Path) None#
Register the paths BIDS has no say over, so a validator skips rather than flags them.
Reports, logs, the figures inside them, the JSON-only records, and raw QC’s stage files, a dataset of their own. Every other table and file with a sidecar stays on the validator’s books, dyad tables included.
- nirspipe.io.derivatives.dataset_root_of(path: pathlib.Path) Path | None#
The nearest folder above
pathholding a dataset_description.json, or None.bids/sub-01/nirs/sub-01_task-rest_nirs.snirf->bids
- nirspipe.io.derivatives.LINK_RAW = 'raw'#
- nirspipe.io.derivatives.LINK_PREPROCESSED = 'preprocessed'#
- nirspipe.io.derivatives.to_bids_uri(path: str | Path, written: pathlib.Path) str#
pathas a BIDS URI, relative to the datasetwrittensits in.out/sub-01/nirs/sub-01_desc-od_nirs.snirf -> bids::sub-01/nirs/sub-01_desc-od_nirs.snirf bids/sub-01/nirs/sub-01_nirs.snirf, raw: ../bids -> bids:raw:sub-01/nirs/sub-01_nirs.snirf
The nearest dataset wins, so a tree inside its raw dataset still names its own files with
bids::. A string that is already a BIDS URI passes through, as the BIDS apps leave them. A path in no linked dataset raises: it has no BIDS URI.
- nirspipe.io.derivatives.bids_uris(paths: Iterable[str | Path], written: pathlib.Path) list[str]#
- nirspipe.io.derivatives.resolve_bids_uri(uri: str, written: pathlib.Path) pathlib.Path#
The file a Sources entry of a sidecar at
writtennames.bids:raw:sub-01/nirs/sub-01_nirs.snirf-><the raw link>/sub-01/nirs/sub-01_nirs.snirf
- nirspipe.io.derivatives.write_dataset_description(output_dir: pathlib.Path, *, name: str = 'nirspipe output', generated_by: str = 'nirspipe', source: Path | None = None, link: str | None = None) None#
Write dataset_description.json for the derivatives dataset.
Every run writes this, so two started against one output directory write the same path. Reading what is there and writing the merge happen under one lock, or two runs adding different links would each write back only their own. Where it does have to be written it goes to a temporary first, since the plain write truncates and a reader in between sees an empty file and reports the tree as bad BIDS. A reader can still be told the path is busy at the instant it flips, which Windows offers no way around, but that is a retry rather than a tree that looks invalid.
sourceis the tree this one was computed from. It is what lets a reader of a dyad result recover which preprocessing produced its inputs, and it has no correct value while a tool writes back into the tree it read. It goes intoDatasetLinksunderlink(by defaultrawfor a raw dataset,preprocessedfor a derivative), which is what the BIDS URIs in everySourcesresolve against. Links and source datasets already in the file are kept, so tools writing into one tree add to it rather than take turns; a link that would point elsewhere raises, since every URI written under it would move.