eegdash.to_bids package#

Submodules#

Module contents#

Bring a dataset that is not in BIDS to a BIDS tree, one adapter per dataset.

A dataset each user obtains from its data holder under the holder’s terms (a gated archive that may not be redistributed, so not on NEMAR) becomes a BIDS tree at <cache_dir>/<name>-<version>/: the folder a local dataset is read from (see eegdash.corpus), so files, verify, shards, convert and merge take it over unchanged:

from eegdash.to_bids import to_bids
report = to_bids("tueg-2.0.1", "/data/tuh_eeg/v2.0.1", dry_run=True)
report = to_bids("tueg-2.0.1", "/data/tuh_eeg/v2.0.1")

Command line:

python -m eegdash.corpus bids tueg-2.0.1 --src DIR [--cache-dir C]
    [--link symlink|hard|copy] [--exclude CSV] [--keep-held-out] [--dry-run]
    [--n-jobs N] [--shard K --n-shards N] [--adapter module:Class]
    [--json REPORT.json]

Headers are read, never the signal; data files are linked (symbolic links by default), never rewritten when the source format is a BIDS data file; nothing is written under the source. Held-out recordings are left out and reported. The adapters are in ADAPTERS: the TUH EEG Corpus (tueg, from NEDC). One outside the package is named module:Class (a subclass of Adapter).

class eegdash.to_bids.Adapter(src: Path, version: str)[source]

Bases: object

One dataset’s layout and metadata, for eegdash.to_bids.to_bids().

A subclass sets the class attributes and implements recordings(), channels(), sidecar(), participants(), dataset_description() and readme(). It may append to self.warnings (through warn()) in any method: the core collects them per recording into the report. An adapter is pickled to the worker processes after recordings() ran, so what it planned there is available to the per-recording methods.

Parameters:
  • src (Path) – The source tree, never written to.

  • version (str) – The source release, one of versions.

changes() → str | None[source]

CHANGES, or None for no file.

channels(rec: Recording, header: Header) → list[Channel][source]

One channel per label of header, in its order.

check_source() → None[source]

Raise ValueError when the source is not this dataset.

dataset_description() → dict[str, Any][source]

Name, License, Authors, DOI and links of dataset_description.json.

property dataset_id: str

the tree’s folder in the cache.

Type:

<name>-<version>

events(rec: Recording, header: Header) → list[Event] | None[source]

The recording’s events; None writes no events.tsv.

extension: ClassVar[str] = '.edf'
held_out() → list[Exclusion][source]

Recordings and subjects left out: tables/<name>-<version>.heldout.csv when there is one.

license_file() → Path | None[source]

A file of the source (relative to it) copied to LICENSE, or None.

linkable: ClassVar[bool] = True
name: ClassVar[str]
participants(subjects: Sequence[str]) → tuple[list[dict], dict][source]

participants.tsv rows (participant_id first) for subjects, and participants.json.

readme(summary: dict[str, Any]) → str[source]

README.md, from the run’s summary (subjects, recordings, hours, warnings).

recordings() → Iterator[Recording][source]

Every data file of the source: a walk that opens no data file.

revision: ClassVar[int] = 1
sidecar(rec: Recording, header: Header) → dict[str, Any][source]

The adapter’s keys of _eeg.json: EEGReference, PowerLineFrequency, …

sourcedata(subjects: Sequence[str]) → dict[str, bytes][source]

Small text files for sourcedata/: name -> content, about subjects only.

subjects are those with a recording in the tree: a held-out subject’s files, or its lines in a shared table, are left out.

table(suffix: str) → Path[source]

The packaged table tables/<name>-<version><suffix>.

versions: ClassVar[tuple[str, ...]]
warn(message: str) → None[source]

Record a warning for the report (and the README, if the adapter writes them there).

write_data(rec: Recording, dest: Path) → None[source]

Write the BIDS data file of rec to dest (linkable = False only).

class eegdash.to_bids.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.Channel(name: str, type: str, units: str = 'n/a', description: str = '', mapped: bool = True)[source]

Bases: object

One row of channels.tsv.

description: str = ''
mapped: bool = True
name: str
type: str
units: str = 'n/a'
class eegdash.to_bids.ChannelTable(rules: str | Path | Iterable[tuple[str, str, str]])[source]

Bases: object

Source label -> BIDS name and type, by the first matching rule.

Parameters:

rules (str | Path | Iterable[tuple[str, str, str]]) – A CSV with the columns pattern,name,type (the packaged tables/<id>.channels.csv), or the rows themselves. pattern is a regular expression matched against the whole label, name its replacement (\g<0> keeps the label, \1 a group), type one of CHANNEL_TYPES.

channels(header: Header) → list[Channel][source]

One Channel per label of header, in its order.

lookup(label: str) → tuple[str, str, bool][source]

(name, type, mapped) of label; (label, "MISC", False) when no rule matches.

class eegdash.to_bids.Event(onset: float, duration: float, trial_type: str)[source]

Bases: object

One row of events.tsv, seconds from the start of the file.

duration: float
onset: float
trial_type: str
class eegdash.to_bids.Exclusion(key: str, match: Literal['recording', 'subject'], reason: str, hashed: bool = False)[source]

Bases: object

A recording, or every recording of a subject, left out of the tree.

hashed: bool = False
key: str
match: Literal['recording', 'subject']
matches(rec: Recording) → bool[source]
reason: str
class eegdash.to_bids.Header(labels: tuple[str, ...], units: tuple[str, ...], sfreq: float, n_times: int, meas_date: datetime | None)[source]

Bases: object

A data file’s header, read by the core, never by the adapter.

labels: tuple[str, ...]
meas_date: datetime | None
n_times: int
sfreq: float
units: tuple[str, ...]
class eegdash.to_bids.Recording(source: Path, subject: str, task: str, session: str | None = None, acquisition: str | None = None, run: str | None = None, key: str = '', extra: Mapping[str, str]=<factory>)[source]

Bases: object

One source data file and where it goes in the BIDS tree.

acquisition: str | None = None
property entities: list[tuple[str, str]]

The BIDS entities, in file-name order (sub ses task acq run).

extra: Mapping[str, str]
property folder: Path

sub-<s>/[ses-<e>/]eeg, relative to the BIDS root.

key: str = ''
run: str | None = None
session: str | None = None
source: Path
property stem: str

sub-<s>[_ses-<e>]_task-<t>[_acq-<a>][_run-<r>].

subject: str
task: str
eegdash.to_bids.get_adapter(spec: str | type[Adapter]) → type[Adapter][source]

The adapter class of a dataset name (tueg), a module:Class path, or a class.

eegdash.to_bids.read_exclusions(path: str | Path) → list[Exclusion][source]

An exclusion table, key,match,reason[,hashed]: a held-out table or --exclude.

eegdash.to_bids.to_bids(dataset_id: str, src: str | Path, *, cache_dir: str | Path | None = None, 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, adapter: str | type[Adapter] | None = None) → BidsReport[source]

Write the BIDS tree of dataset_id at <cache_dir>/<dataset_id>/ from the source src.

Parameters:
  • dataset_id (str) – <name>-<version> (tueg-2.0.1); the adapter is the one registered for the name, and must know the version.

  • src (str | Path) – The source tree; never written to, and the output may not be in it.

  • cache_dir (str | Path | None) – The EEGDash cache (see eegdash.paths.get_default_cache_dir()).

  • link ({"symlink", "hard", "copy"}) – How a data file enters the tree. A symbolic link (absolute) shows a moved source at once in verify; a hard link or a copy makes a tree that stands alone.

  • exclude (str | Path | sequence of Exclusion | None) – More recordings or subjects to leave out: a CSV key,match,reason (match is recording, matched against a recording’s key, the source’s stem by default, or subject).

  • keep_held_out (bool) – Do not apply the adapter’s held-out table (recorded in the report).

  • dry_run (bool) – Plan and read every header; write nothing.

  • n_jobs (int) – Processes, over subjects. They are spawned: a script that asks for more than one calls to_bids() under if __name__ == "__main__":.

  • shard (int | None) – This job-array task’s subjects: subjects sorted, every n_shards-th from shard. The root files and the report are written by a final call without a shard, which reuses what the tasks wrote.

  • n_shards (int | None) – This job-array task’s subjects: subjects sorted, every n_shards-th from shard. The root files and the report are written by a final call without a shard, which reuses what the tasks wrote.

  • adapter (str | type[Adapter] | None) – Another adapter for the name: module:Class or a class.

Returns:

One row per recording; ok when nothing was unreadable, invalid or refused.

Return type:

BidsReport