fascicle
Fascicle is a Python framework for reading, writing and managing neuroscience datasets. It is built around the BIDS standard and supports outputs from FreeSurfer, MNE-Python and SimNIBS.
The core of Fascicle reads and writes raw BIDS datasets. Extensions handle common neuroimaging derivatives, such as those from FreeSurfer, MNE-Python and SimNIBS CHARM. These extensions use the same small framework that you can use to write your own extensions, and you can replace any reader or writer with your own.
Install
pip install mindmap-fascicle # raw BIDS + MRI/surfaces/tractograms, FreeSurfer, SimNIBS charm
pip install "mindmap-fascicle[mne]" # + raw MEG/EEG and MNE-Python source-analysis derivatives
The package installs as mindmap-fascicle but is imported as fascicle.
Quickstart
from fascicle import DatasetManager
from fascicle.extensions import freesurfer, mne
dm = DatasetManager("/data/my_study/rawdata") # no config needed
dm.get_subjects() # ['01', '02', ...]
img = dm.load("01", "T1w", session="test") # nibabel image
dm.save(img, "01", "T1w", session="retest") # writes sub-01/ses-retest/anat/sub-01_ses-retest_T1w.nii.gz
dm.add_datatype("bold", "raw", dm.bids_namer("bold")) # any other raw BIDS suffix
bold = dm.load("01", "bold", session="test", task="rest")
freesurfer.register(dm, root_path="/data/my_study/derivatives/freesurfer")
lh_coords, lh_faces = dm.load("01", "fs-pial", hemi="lh")
mne.register(dm, root_path="/data/my_study/derivatives/mne")
dm.save(cov, "01", "mne-cov", task="rest") # sub-01/cov/sub-01_task-rest_cov.fif
info = dm.load("01", "meg-info", session="meg", task="rest", run=1) # raw MEG, BIDS sidecars applied
mne.register_subjects_dir(dm) # trans/head keys on the FreeSurfer subjects dir
dm.save(trans, "01", "fs-trans") # sub-01/sub-01-trans.fif
dm.get_path(...) takes the same arguments as load and returns the path without touching the file (except for raw modalities, below, which are looked up in the BIDS index). Index entities (run, echo, ...) may be zero-padded to any width in BIDS, so raw keys find the file whatever its padding: run=1 finds run-01 or run-001, split=1 finds split-01. save writes a new file with the values as given (run=1 → run-1, run="01" → run-01), and refuses to create run-1 next to an existing run-01.
Concepts
-
Subjects: pass
"01"or"sub-01", both name the same files everywhere;dm.get_subjects()returns bare labels (["01", ...]). -
Roots: named folders (
dm.roots)."raw"is the BIDS dataset; extensions add their own ("fs","mne", ...) withadd_root. -
Datatypes: every key in
dm.registry("T1w","fs-pial","mne-cov") is aDatatypewith- a namer,
namer(root, subject, **inputs) -> Path, which only builds the path, so the same namer works for loading and saving. Inputs are BIDS entities (session,task, ...) or tool-specific ones (hemi); - a reader,
reader(path) -> data; - a writer,
writer(data, path).
A missing reader or writer means "use the one registered for the file extension".
dm.describe("fs-pial")shows all of this for one key. - a namer,
-
Raw modalities: data a toolbox reads from the raw dataset itself, rather than from its own derivatives folder (MEG/EEG for MNE, or a modality BIDS doesn't define yet).
dm.add_raw(key, suffix, reader, extensions=...)registers one read-only key that finds the file in the pybids index, so it works for any BIDS-shaped name (sub-01[_ses-a]_task-x_<suffix>.<ext>) whatever its extension, matchesrun=1torun-01, and returns folder recordings such as CTF.ds. The key needn't be the suffix: MNE'smeg-inforeads themegfiles with a different reader. Extensions list these in aRAWtable ofRawDatatypes, registered unprefixed withregister_raw. For files whose names aren't BIDS-shaped, useadd_datatype(key, "raw", your_namer). -
Sidecars: the small files describing a raw recording (
channels.tsv,meg.json,coordsystem.json, ...), which BIDS lets sit higher up the tree (inheritance).add_raw(key, suffix, sidecar_of="meg-file")registers thesuffixsidecar of a raw recording, found from the recording's entities byfascicle.utils.find_sidecar, e.g. a session-levelchannels.tsvfromrun=1. The MNE extension registers one key per sidecar (meg-channels,meg-coordsystem, ...), and itsmeg/eeg/meg-infokeys apply the fields BIDS defines the same way everywhere: channel types and bad channels fromchannels.tsv, line frequency frommeg.json. What changed is reported ininfo["temp"]["bids_sidecars"];meg-file/eeg-file/meg-info-filereturn the file exactly as stored. Keys built withbids_namer(T1w, ...) follow inheritance too, for metadata files only (.json,.tsv,.bval,.bvec):dm.load("01", "T1w", extension=".json")falls back tosub-01/sub-01_T1w.jsonor a dataset-levelT1w.json.savealways writes the subject's own file, never a shared one. -
File extensions:
dm.ext_readers/dm.ext_writersmap extensions to default readers/writers: nibabel for images and tractograms (.nii,.nii.gz,.mgh,.mgz,.gii,.trk,.tck), the standard library for.jsonsidecars (read as dicts), numpy for.bval/.bvec, and pandas for.tsv/.tsv.gztables (read as DataFrames, with BIDS'sn/aas missing values). -
References: single files outside any subject (templates, atlases, lookup tables) loaded with
load_ref. Each reference is read by file extension unless you give it its own reader, withadd_reference(key, path, reader=...)or, for references from the config file,set_ref_reader(key, reader). References have no writer: they're inputs, not outputs. -
Config file (optional): the declarative way to set up roots and references:
DIR_RAW: /data/my_study/rawdata # -> "raw" root (a raw_root argument wins if both are given) DIR_TEMPLATES: /data/templates # available as dm.config["DIR_TEMPLATES"] for add_root MRI_REF: /data/templates/MNI152.nii.gz # any key containing "REF" becomes a reference: dm.load_ref("MRI_REF")
DatasetManager(config="config.yaml"). Reading a config withenv=True(theread_configdefault) also exports every top-level key as an environment variable.
save refuses to replace an existing file unless you pass overwrite=True, creates missing folders, and raises TypeError for read-only datatypes.
Extensions
| extension | keys (default prefix) | extra dependency |
|---|---|---|
fascicle.extensions.freesurfer |
fs-t1, fs-brain, fs-orig, fs-aseg, fs-white/pial/inflated/sphere, fs-area/curv/thickness/sulc, fs-cortex (read-only), fs-ras2tkras (read-only), BEM surfaces fs-iskull/oskull/skin (bem/inner_skull.surf, outer_skull.surf, outer_skin.surf, as MNE's watershed/FLASH write them) |
none |
fascicle.extensions.charm |
charm-seg, charm-t1 |
none |
fascicle.extensions.mindmap |
BEM surfaces from mindmap-bem: mm-iskull, mm-oskull, mm-skin |
none |
fascicle.extensions.mne |
raw (unprefixed, read-only): meg, eeg, meg-info (sidecars applied), meg-file, eeg-file, meg-info-file (as stored), sidecars meg-/eeg- + channels, json, events, coordsystem, plus meg-headshape, eeg-electrodes; derivatives: mne-src, mne-fwd, mne-ave, mne-i-ave, mne-v-ave, mne-cov, mne-stc; subjects dir (register_subjects_dir, on the fs root by default): fs-trans (read/write), fs-head, fs-head-dense (read-only) |
mne |
Every extension has the same register(dm, root_key=..., root_path=None, namespace=None, readers=None, writers=None). Keys are prefixed with namespace, which defaults to root_key; raw keys (MNE's meg, ...) aren't, and registering an extension twice keeps one set of them. To compare two FreeSurfer runs, register twice: freesurfer.register(dm, root_key="fs_run2", root_path=...) gives fs_run2-t1, ... The same works for BEM methods kept in separate subjects directories: freesurfer.register(dm, root_key="watershed", root_path=...) gives watershed-iskull.
Swapping a reader or writer
import ants
def ants_reader(path):
return ants.image_read(str(path))
# One datatype:
dm.set_reader("T1w", ants_reader)
freesurfer.register(dm, root_path=..., readers={"t1": ants_reader}) # at registration
# Every file of one type (raw BIDS, FreeSurfer, charm, ... alike):
dm.add_extension(".nii.gz", reader=ants_reader)
set_writer, writers={...} and add_extension(..., writer=...) work the same way. Mark a datatype read-only with Datatype.READ_ONLY as its writer.
Writing your own extension
An extension is a table of Datatypes plus a register function. This is exactly how the built-in extensions are written. For BIDS-style names, build the namers with derivative_namer:
# my_lab/resection.py
from fascicle import Datatype, derivative_namer, register_extension
def read_notes(path):
return path.read_text()
def write_notes(text, path):
path.write_text(text)
DATATYPES = {
# nibabel by extension; the .json sidecar goes through the JSON reader/writer
"mask": Datatype(derivative_namer("mask", datatype="anat", extensions=(".nii.gz", ".json"))),
"notes": Datatype(
derivative_namer("notes", datatype="anat", extensions=(".txt",), entities=("session", "desc")),
read_notes,
write_notes,
),
}
def register(dm, root_key="resec", root_path=None, namespace=None, readers=None, writers=None):
register_extension(dm, DATATYPES, root_key=root_key, root_path=root_path,
namespace=namespace, readers=readers, writers=writers)
resection.register(dm, root_path="/data/my_study/derivatives/resection")
dm.save(mask_img, "01", "resec-mask", session="postop", desc="manual")
# -> derivatives/resection/sub-01/ses-postop/anat/sub-01_ses-postop_desc-manual_mask.nii.gz
mask = dm.load("01", "resec-mask", session="postop", desc="manual")
BIDS-style names with derivative_namer
derivative_namer(suffix, datatype=None, extensions=(".nii.gz",), entities=None, extra_entities=(), fixed=None) accepts any suffix, folder and extension:
datatypeis the folder undersub-XX[/ses-YY]/. PassNoneto put files directly in it.extensions: the first one is the default; ask for another withextension=, e.g. a.jsonsidecar.Nonebuilds extensionless stems.entitiesrestricts which BIDS entities the datatype takes. By default it takes all of them, using pybids' names (session,acquisition,desc, ...).extra_entities=("cut",)adds your own entities, written as_cut-<value>just beforedesc.fixed={"signal": "virtual"}gives an entity one value for this datatype: it's always written and callers can't pass it. Use it to give variants of a file their own keys while keeping one suffix. For example, the MNE extension'smne-i-ave/mne-v-aveareavefiles withsignal-intracranial/signal-virtual.
Entities are always written in the BIDS spec's order, whatever order you pass them in. Anything the namer wasn't declared with raises ValueError instead of being dropped. So do values the spec forbids, such as desc="brain-mask", run="one" or hemi="lh" (BIDS uses L/R). fascicle.bids_pattern(...) shows the pybids pattern a namer fills.
dm.bids_namer(suffix) is the raw-BIDS counterpart: it knows the raw spec (T1w lives in anat/, bold in func/), but only accepts raw suffixes and entities. A derivative entity such as desc raises and points you to derivative_namer.
Namers don't have to be BIDS-style: any namer(root, subject, **inputs) -> Path works (see fascicle.extensions.freesurfer). For a one-off datatype, skip the module: dm.add_root(...) and dm.add_datatype(key, root_key, namer, reader, writer). You can also subclass DatasetManager and register everything in __init__.
Caveats
- Not picklable.
DatasetManagerwraps abids.BIDSLayout, which can't be pickled. Don't pass a manager tomultiprocessing/joblibworkers. Resolve paths withget_path(or load data) in the parent process and pass those instead. - The BIDS index isn't refreshed after
save. Files you write are loadable by path straight away, butget_subjects()anddm.layoutqueries only see them in a newDatasetManager. - Extensions don't index their folders. Namers build paths without looking at the disk, so they can write into folders that don't exist yet. For pybids queries on a derivatives folder, also call
dm.add_derivatives(key, path)once it has adataset_description.json. pybids doesn't parse custom entities (such as MNE'ssignal), so you can't filter on them in pybids queries; Fascicle's ownload/savehandle them fully.
Example
examples/tutorial.ipynb downloads one subject of OpenNeuro's ds000114, loads its T1w and BOLD with nibabel, then switches T1w to ANTsPy. Run pip install -e ".[examples]" first.
Metadata
Release files for mindmap-fascicle 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mindmap_fascicle-0.4.0.tar.gz | 59.7 kB | With provenance | Details for mindmap_fascicle-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mindmap_fascicle-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for mindmap_fascicle-0.4.0-py3-none-any.whl |
Total release size: 99.7 kB
Release files / mindmap_fascicle-0.4.0.tar.gz
| Download URL | mindmap_fascicle-0.4.0.tar.gz |
|---|---|
| Size | 59.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ee7941d55e6e69c202c93fd3fca3201858338e97037f302e876810bc649050f2
|
|
BLAKE2b-256 checksum How to use checksums |
385878339ee31cfd80fb52e36f91062ed0a56a407e9535df2eeddf6ba61ad020
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mindmap_fascicle-0.4.0-py3-none-any.whl
| Download URL | mindmap_fascicle-0.4.0-py3-none-any.whl |
|---|---|
| Size | 40.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9bd5fc4b0eeccca7acd532685e4d8123b68fde93c74f91a08e881d1bc5545658
|
|
BLAKE2b-256 checksum How to use checksums |
aa5a3b0e20d8adeaefc6ee8bcbcd5f7a76cc5fe0238f90b773b2c044c3ce76af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for mindmap-fascicle 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mindmap_fascicle-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.