eegdash.to_bids.core module#

Bring a source tree to BIDS with an adapter: plan, scan, write, finish.

  • plan: the adapter’s recordings to BIDS paths. Two recordings on one path, a source file named twice, a source outside the source tree, an entity label that is not alphanumeric, a data format EEGDash has no reader for, or (when the source is linked) a source of another format than the BIDS data file stop the run before anything is written. Held-out recordings (the adapter’s table, --exclude) are left out here and reported with their reason.

  • scan: every header is read with the reader the conversion will use (eegdash.io.get_reader(ext)(path, preload=False) under eegdash.io.tolerant()), from the file the tree will hold (the source when it is linked, the adapter’s file when it writes one), so channels.tsv lists that reader’s own channels, in its order.

  • write: per recording, the data file is first put at a temporary name (a symbolic link, a hard link or a copy of the source, or the adapter’s own file), then its sidecars are written, then the data file is renamed into place; a failure removes what this call wrote. Every file goes through a dot-name .<stem>.<pid>.tmp<suffix> and os.replace(), sidecars only when their content changes; stale temporary files of an interrupted run are removed. A data file already in place for the same source is kept; one that holds another source is refused. Subjects are spread over processes (n_jobs) and over job-array tasks (shard, n_shards: subjects sorted, every n-th): a subject belongs to one worker, so no two processes write one folder. The finished subjects’ rows are appended to .eegdash/to_bids/scan-<k>.jsonl (scan-all.jsonl without a shard) every FLUSH_SECONDS, so a cut job resumes without reading those headers again (one append per subject, each flushed to disk, took 0.17 s on a parallel file system: longer than writing the subject).

  • finish, by the call without a shard: the root files (participants, dataset_description, README.md, CHANGES, LICENSE, sourcedata/, task-<t>_events.json, .bidsignore) and .eegdash/to_bids/ (map.csv, report.json, DONE). A data file in the tree that the plan does not name is reported, never deleted.

The tree (every file but .eegdash/) and map.csv depend on nothing but the source, the adapter’s revision and the options: no clock, host or package version. report.json and DONE also count what this call wrote and what it kept.

class eegdash.to_bids.core.BidsReport(dataset_id: str, adapter: str, options: dict[str, ~typing.Any]=<factory>, rows: list[dict] = <factory>, warnings: list[str] = <factory>, problems: list[str] = <factory>, held_out_kept: int = 0, seconds: float = 0.0)[source]

Bases: object

The result of eegdash.to_bids.to_bids(): one row per recording, problems, warnings.

adapter: str
property counts: dict[str, int]

Recordings per status, subjects in the tree (or planned), and hours.

dataset_id: str
held_out_kept: int = 0
labels() → dict[str, dict[str, Any]][source]

Source label -> recordings, its BIDS name and type, whether a rule mapped it.

property ok: bool
options: dict[str, Any]
problems: list[str]
rows: list[dict]
seconds: float = 0.0
to_dict() → dict[str, Any][source]

Everything but the per-recording rows (the CSV holds those); no clock, no absolute path.

warnings: list[str]
write(path: str | Path) → None[source]

The report as JSON at path, and one row per recording in a CSV beside it.

class eegdash.to_bids.core.Context(adapter: Adapter, src: Path, out: Path, link: str, dry_run: bool)[source]

Bases: object

What every worker needs: the adapter (with its plan), the trees, the options.

adapter: Adapter
dry_run: bool
link: str
out: Path
src: Path
eegdash.to_bids.core.bids_path(rec: Recording, extension: str) → str[source]

The data file of rec, relative to the BIDS root.

eegdash.to_bids.core.channels_tsv(channels: Sequence[Channel]) → str[source]

channels.tsv: name type units description, one row per channel.

eegdash.to_bids.core.check_channels(channels: Sequence[Channel], header: Header) → None[source]

The rules of a readable channels.tsv; a ValueError says which one is broken.

One row per channel of the reader’s list, in its order (MNE-BIDS sets no name from a channels.tsv of another length, and relabels by position): each row’s description is the reader’s label at that position. Names unique, types known.

eegdash.to_bids.core.check_paths(src: Path, out: Path) → None[source]

Refuse an output inside the source, or a source inside the output.

eegdash.to_bids.core.convert_recording(ctx: Context, rec: Recording, prior: dict | None = None) → dict[str, Any][source]

Scan one recording and, unless a dry run, write it; returns its report row.

See _convert_recording(). A recording that is not written leaves no empty folder behind.

eegdash.to_bids.core.convert_subject(ctx: Context, recs: Sequence[Recording], prior: dict[str, dict]) → list[dict][source]

The rows of one subject’s recordings: one worker writes one subject folder.

eegdash.to_bids.core.eeg_json(rec: Recording, header: Header, channels: Sequence[Channel], extra: dict) → dict[str, Any][source]

The _eeg.json sidecar: the core’s keys, the adapter’s, then the counts.

eegdash.to_bids.core.events_tsv(events: Sequence[Event]) → str[source]

events.tsv: onset duration trial_type, by onset.

eegdash.to_bids.core.excluded_by(rec: Recording, rules: Sequence[Exclusion]) → Exclusion | None[source]

The first rule that leaves rec out, or None.

eegdash.to_bids.core.finish(adapter: Adapter, out: Path, report: BidsReport, plan_warnings: list[str]) → None[source]

The root files and .eegdash/to_bids/, from the rows of the whole run.

eegdash.to_bids.core.link_state(dest: Path, source: Path, link: str, linkable: bool) → str[source]

absent, same (the data file is the link of source) or other.

For a file the adapter writes, same means only that a regular file is there; whether it is current is the earlier run’s row to say.

eegdash.to_bids.core.make_link(source: Path, path: Path, link: str) → None[source]

Create path as a symbolic link (absolute) to source, a hard link or a copy.

eegdash.to_bids.core.plan(adapter: Adapter) → list[Recording][source]

The adapter’s recordings, sorted by BIDS path; a ValueError before any write.

Refused: a BIDS data format the conversion or EEGDash cannot read, a linked source of another format, an entity label that is not alphanumeric, a source outside the source tree, a source named twice, two sources on one BIDS path.

eegdash.to_bids.core.prior_rows(out: Path) → dict[str, dict][source]

BIDS path -> the last row of an earlier run that put it in the tree (scan-*.jsonl).

eegdash.to_bids.core.read_header(path: Path) → tuple[Header, list[str]][source]

The header of a data file, read with the conversion’s reader; and the reader’s warnings.

eegdash.to_bids.core.run(adapter_cls: type[Adapter], dataset_id: str, src: str | Path, out: str | Path, *, link: str = 'symlink', exclude: str | Path | Sequence[Exclusion] | None = None, keep_held_out: bool = False, dry_run: bool = False, n_jobs: int = 1, shard: int | None = None, n_shards: int | None = None) → BidsReport[source]

Bring the source tree src to the BIDS tree out with adapter_cls.

See eegdash.to_bids.to_bids(), which picks the adapter and out.

eegdash.to_bids.core.split_id(dataset_id: str) → tuple[str, str][source]

tueg-2.0.1 -> ("tueg", "2.0.1"); a ValueError for an id that is not local.

eegdash.to_bids.core.strays(out: Path, rows: Sequence[dict]) → list[str][source]

Data files in the tree that no row of the plan names (a recording that left it).

eegdash.to_bids.core.summary(report: BidsReport, plan_warnings: list[str]) → dict[str, Any][source]

What Adapter.readme() is given: subjects, recordings, hours, warnings.

eegdash.to_bids.core.sweep_tmp(folder: Path, *, recursive: bool = True) → int[source]

Remove the temporary files (_tmp()’s names only) an interrupted run left.

eegdash.to_bids.core.write_bytes(path: Path, data: bytes) → bool[source]

Write data through a temporary dot-name and a rename, unless the file already holds it.

eegdash.to_bids.core.write_text(path: Path, text: str) → bool[source]

write_bytes() for UTF-8 text.