Note
This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.
nidl.datasets.OpenMind¶
- class nidl.datasets.OpenMind(root, preprocessing='quasiraw', modality=None, group_by=None, require_all=True, instance='first', subjects=None, split=None, split_file=None, apply_mask=False, return_morphometry=False, streaming=True, max_workers=1, shards='*/*.tar', transforms=None)[source]¶
Bases:
DatasetOpenMind dataset [1], read from WebDataset shards.
The OpenMind dataset is a large-scale 3D MRI dataset of the head and neck region featuring 114k MRI Images. This data was pooled from exactly 800 datasets from the OpenNeuro platform and provides 23 different MRI modalities from over 30 different scanners, representing a highly variable pre-training dataset.
The brain T1w, T2w and FLAIR images were preprocessed with brainprep and are available with two preprocessings (see preprocessing):
“quasiraw”: 42941 T1w, 23935 T2w and 4614 FLAIR images, with minimal preprocessing (skull-stripping, N4 bias field correction, 1mm isotropic resampling and linear registration to MNI space), shape (1, 182, 218, 182).
“cat12vbm”: T1w images processed with CAT12 VBM (v12.8.2). The images are the modulated gray matter maps (“mwp1”) non-linearly registered to MNI space at 1.5mm isotropic, shape (1, 113, 137, 113). Each image comes with its morphometry: the gray matter volume of each region of 16 atlases (see return_morphometry).
The shards contain one sample per (subject, session, modality, run) tuple. Deciding which images go together is done here, at load time, with group_by, require_all and instance.
Warning
The quasi-raw images take ~460GB of disk. Keep streaming=True if you intend to use only a small portion of the dataset.
- Parameters:
- rootstr
Root directory of the dataset, mirroring the Hugging Face repository: one folder per preprocessing (e.g.
<root>/quasiraw-v2.1.1/) containing one folder of.tarshards per OpenNeuro dataset, andval_split.csv. Missing files are fetched from the Hugging Face.- preprocessing{“quasiraw”, “cat12vbm”}, default=”quasiraw”
Which preprocessed images to load.
- modalityNone, str or sequence of str, default=None
Modalities to keep, among “t1”, “t2” and “flair” for the quasi-raw preprocessing and “mwp1” for CAT12 VBM. None keeps all of them. A string returns a single image per item; a sequence returns a dict {modality: image} per item, see group_by.
- group_byNone, {“session”, “subject”} or callable, default=None
How images are regrouped into items:
None: one item per image, whatever its modality.
“session”: one item per (dataset, subject, session), holding one image per requested modality (e.g. T1w + T2w acquired in the same session).
“subject”: same, across all the sessions of a subject. Subject labels are only unique within a dataset, so the dataset id (e.g. “ds000030”, parsed from the shard name) is part of the group.
callable: takes the metadata dict of an image (keys “dataset”, “subject”, “session”, “modality”, “run” and, for the quasi-raw images, “entities”) and returns a hashable group key.
Without grouping, an item is always a single array, even if several modalities are requested. With grouping, an item is a dict {modality: image} unless modality is a single string.
- require_allbool, default=True
Only relevant with group_by. If True, groups that miss one of the requested modalities are dropped. If False, they are kept and the missing modalities are set to None (use a custom collate_fn with a DataLoader).
- instance{“first”, “random”, “all”}, default=”first”
Only relevant with group_by. When a group holds several images of the same modality (several runs, acquisitions…):
“first”: keep the first one in sorted key order (the image without run/acq/… entities comes first).
“random”: draw one at each __getitem__ call.
“all”: one item per combination (cartesian product).
- subjectssequence of str or (str, str), default=None
Keep only these subjects, given either as “sub-01” (matches this label in every dataset) or as (“ds000030”, “sub-10159”). Subject labels are only unique within a dataset. Combined with split (both filters apply).
- split{None, “train”, “val”}, default=None
Which subject-level split to load. None loads everything. The split is read from split_file, which lists the subjects of the “val” split; every other subject belongs to “train”. A subject, with all its sessions, modalities and instances, is entirely in one split, so there is no leakage between splits, including with group_by.
- split_filestr or None, default=None
CSV file listing the “val” subjects, with (at least) the columns
dataset_idandparticipant_id. Other columns (e.g. session, run) are ignored: the split is decided per (dataset, subject). Default is<root>/val_split.csv, downloaded from the Hugging Face if missing. Only used if split is not None.- apply_maskbool, default=False
If True, multiply each image by its brain mask. Images without mask (e.g. CAT12 VBM) are returned unchanged.
- return_morphometrybool, default=False
Only available with
preprocessing="cat12vbm". If True, __getitem__ returns(data, morphometry)where morphometry is a dict {atlas: {region: gray matter volume}} (plus the “total_volumes” TIV, GM, WM and CSF volumes). For grouped dict items, morphometry is a dict {modality: morphometry}.- streamingbool, default=True
If True, the images of the shards missing locally are downloaded on demand from the Hugging Face (when accessed via __getitem__), one image at a time with HTTP range requests: nothing is written to disk. If False, all the shards holding the selected images are downloaded at initialization (whole shards, ≤3GB each).
- max_workersint, default=1
Number of concurrent threads to download the shards with streaming=False (1 thread = 1 shard download). Setting it > 1 can raise Hugging Face 429 errors (too many requests).
- shardsstr, default=”/.tar”
Glob pattern selecting the shards, relative to the preprocessing folder (e.g.
"ds0000*/*.tar"). A warning is raised if the same image appears in several of them.- transformscallable or None, default=None
A function/transform that takes in an image (or a dict of images) and returns a transformed version.
Notes
The data are downloaded exclusively from the OpenMind repository on the Hugging Face either on-the-fly (lazy download) or during initialization (immediate download) if they are not already there.
Images are read by byte offsets, listed in an index file (
<root>/<preprocessing folder>/index.json.gz) released with the shards. If this index is neither available locally nor on the Hugging Face, it is built from the local shards (see build_index). Random access thus works with any sampler and any number of DataLoader workers, and thewebdatasetpackage is not needed to read the data.References
[1]Wald et al., An OpenMind for 3D medical vision self-supervised learning, arXiv 2025
Examples
One quasi-raw T1w image per item:
>>> dataset = OpenMind(root="data/openmind", modality="t1") >>> t1 = dataset[0] >>> print(t1.shape) (1, 182, 218, 182)
Paired T1w/T2w acquired in the same session:
>>> dataset = OpenMind( ... root="data/openmind", ... modality=("t1", "t2"), ... group_by="session", ... ) >>> sample = dataset[0] >>> print(sample["t1"].shape, sample["t2"].shape) (1, 182, 218, 182) (1, 182, 218, 182)
CAT12 VBM gray matter maps with their morphometry:
>>> dataset = OpenMind(root="data/openmind", preprocessing="cat12vbm", ... return_morphometry=True) >>> mwp1, morphometry = dataset[0] >>> print(mwp1.shape, morphometry["total_volumes"]["TIV"]) (1, 113, 137, 113) 1391.08099215686
Subject-level train/validation splits (from
<root>/val_split.csv):>>> train = OpenMind(root="data/openmind", modality="t1", split="train") >>> val = OpenMind(root="data/openmind", modality="t1", split="val")
One image per subject, drawn among all modalities:
>>> dataset = OpenMind( ... root="data/openmind", ... group_by=lambda meta: meta["subject"], ... require_all=False, ... instance="random", ... )
- REPO_ID = 'neurospin/openmind'¶
- REVISION = 'ed0af62645c4e8230f82e28a74b0dfa7bd6bfacb'¶
- __init__(root, preprocessing='quasiraw', modality=None, group_by=None, require_all=True, instance='first', subjects=None, split=None, split_file=None, apply_mask=False, return_morphometry=False, streaming=True, max_workers=1, shards='*/*.tar', transforms=None)[source]¶
- static build_index(data_dir, shards='*/*.tar')[source]¶
Index the local shards of one preprocessing.
Record the byte offsets of every image (and mask, morphometry) of the shards, so that they can be read by random access, and write them to
<data_dir>/index.json.gz. This file is released with the shards on the Hugging Face: it only has to be rebuilt when the shards change.- Parameters:
- data_dirstr
Folder of a preprocessing (e.g.
<root>/quasiraw-v2.1.1).- shardsstr, default=”/.tar”
Glob pattern (relative to data_dir) selecting the shards to index.
- Returns:
- pathstr
Path to the written index.
Examples using nidl.datasets.OpenMind¶
Multimodal pretraining with missing modalities: CoMM on OpenMind