neurolang.atlases#
Atlases: a label image, its affine, a label table and a space.
An Atlas is what .load_atlas, add_atlas_set and the
atlases: of an engine load: the regions of the relation
name(label, region) of the engine, the centres of their voxels, and
what the engine tracts indexes streamlines by. This module owns the loaders
(a FreeSurfer colour LUT, a BIDS dseg.tsv, a TSV, a CSV, a nilearn
fetcher, the fetchers of the atlases of the engines), the normalisation of
label names, the inference of the hemisphere of a label, the space tag and
the content hash.
It needs numpy, pandas and nibabel only, and nilearn when an atlas is
fetched, so that the core of the engine tracts may import it: it imports no
other module of NeuroLang but neurolang.utils.nilearn_compat, lazily,
to fetch (.importlinter).
Module Attributes
|
A free-form tag naming a coordinate space, e.g. |
The hemisphere of a label. |
|
The default, ordered rules; the first that matches wins: FreeSurfer |
|
|
FreeSurfer's colours of its labels, those of the Destrieux atlas ( |
|
The millimetres of nilearn's Destrieux atlas to those of the MNI152 template: derived from FreeSurfer's fsaverage, it is 1.3 to 2.6% smaller than the template and off by about a millimetre. |
|
The label files of the authors with the colours of the regions: those of the atlases of the cortex and the subcortex together. |
The fetchers of the deterministic atlases of the engines, by the kind of |
Functions
|
Raise |
|
The Destrieux 2009 atlas of nilearn, on the MNI152 template ( |
|
The Schaefer 2018 atlas of nilearn: |
|
Fetch the Melbourne subcortex atlas (Tian et al., 2020, Nat Neurosci, doi:10.1038/s41593-020-00711-6), 3T, at scale 1 to 4 (16, 32, 50 or 54 regions), on the 2 mm grid of the MNI152 2009c asymmetric template, from the authors' repository. |
|
The colour of each label of FreeSurfer's look-up table, by name, fetched into data_dir/ |
|
The hemisphere of label names, by the first rule that matches. |
|
The colour, |
|
A raw label name as the fetched atlases name their regions: a string, decoded if bytes, whose |
|
The |
|
Voxel coordinates of each label of a labelled array, in one pass. |
Read a BIDS |
|
|
Read a table whose first two columns are the id and the name of each label, whatever their headers, with |
Read a FreeSurfer colour LUT ( |
|
|
Read a TSV with columns |
|
The image path with its millimetres moved by transform, a 4×4 matrix, in a file of its own next to it ( |
Classes
|
A deterministic atlas: label volume + affine + label table + space. |
|
Infer the hemisphere of a label name. |
Exceptions
Two objects, e.g. an atlas and a tractogram, are not in one space. |
- class neurolang.atlases.Atlas(name, labels, affine, table, space='unspecified', background=0, hemisphere_rules=(HemisphereRule(pattern='Left[- ].+', hemi='left'), HemisphereRule(pattern='Right[- ].+', hemi='right'), HemisphereRule(pattern='ctx[-_ ]lh[-_ ].+', hemi='left'), HemisphereRule(pattern='ctx[-_ ]rh[-_ ].+', hemi='right'), HemisphereRule(pattern='.+_L', hemi='left'), HemisphereRule(pattern='.+_R', hemi='right'), HemisphereRule(pattern='lh\\..+', hemi='left'), HemisphereRule(pattern='rh\\..+', hemi='right'), HemisphereRule(pattern='.+\\.left', hemi='left'), HemisphereRule(pattern='.+\\.right', hemi='right'), HemisphereRule(pattern='L .+', hemi='left'), HemisphereRule(pattern='R .+', hemi='right'), HemisphereRule(pattern='.+[- ]lh', hemi='left'), HemisphereRule(pattern='.+[- ]rh', hemi='right'), HemisphereRule(pattern='\\d+Networks[_ ]LH[_ ].+', hemi='left'), HemisphereRule(pattern='\\d+Networks[_ ]RH[_ ].+', hemi='right')), kind=None, image=None)#
A deterministic atlas: label volume + affine + label table + space.
- Parameters:
- namestr
The atlas’s identifier, unique in an engine.
- labelsnumpy.ndarray
3D volume of non-negative integer labels (floats with integral values are accepted);
backgroundmarks voxels outside any region.- affinenumpy.ndarray
4x4, voxel indices (ijk) to the space’s world coordinates in mm (RAS+, as nibabel): the affine maps an index to the centre of its voxel.
- tablepandas.DataFrame
Columns
id,nameand optionallyhemi(left,right,noneor empty). Ids of the table absent from the volume are dropped, with a warning; ids of the volume absent from the table (other than the background) are no region, with a warning (unlisted_labels).- spaceSpace
The space tag,
"unspecified"by default.- backgroundint
The label that means “no region” (default 0): it has no region and no point, but keeps its row of the table if it has one.
- hemisphere_rulesSequence[HemisphereRule]
Rules to infer the hemisphere from the name, where the table does not say it. Defaults to
HEMISPHERE_RULES;()infers nothing.- kindstr, optional
How it was loaded: the
kindof.load_atlas(freesurfer,destrieux…;nilearn:<fetcher>for a nilearn fetcher).- imageoptional
The nibabel image the volume was read from, kept as
image.
- Parameters:
Notes
self.tableis the resolved label table: columnsid(int32),name,hemi, by id, for the ids of the table in the volume. The hemisphere of a label is thehemiof the table, if not empty, else the inference byhemisphere_rules(hemispheres()).- check_same_space(other, allow_mismatch=False)#
Raise
SpaceMismatchErrorunlessother(an atlas, or anything with aspace) is in the same space as this atlas; withallow_mismatch, warn.
- content_hash()#
Hash of the volume, affine, label table, background and space, not of the name nor of the kind.
- Return type:
- classmethod fetch(name, kind, data_dir, space='MNI152', **options)#
The atlas of an engine of kind
destrieux,schaeferortian(FETCHERS), fetched intodata_dirwith the options of its fetcher (n_rois=100ofschaefer,scale=2oftian), its label names normalised (label_name()).
- classmethod from_bids_dseg(name, image, dseg_tsv, space='unspecified', **kwargs)#
From a label image and its BIDS
dseg.tsv.
- classmethod from_csv(name, image, labels_csv, space='unspecified', read_csv=None, **kwargs)#
From a label image and a table whose first two columns are the id and the name of each label, read by
pandas.read_csvwith the optionsread_csv(load_label_table_csv()).
- classmethod from_freesurfer(name, image, lut, space='unspecified', **kwargs)#
From a label image (path or nibabel image) and a FreeSurfer LUT.
Keyword arguments are those of the constructor.
- classmethod from_labels(name, image, labels, space='unspecified', normalise=False, **kwargs)#
From a label image and its labels as nilearn’s fetchers give them: a look-up table (DataFrame with
indexandname), a dict id -> name, a list whose position is the id (with a leadingBackground), or pairs (id, name). The row of the background, if any, is dropped. Withnormalise, the names are those oflabel_name(), as the engines name the regions of the atlases they fetch.
- classmethod from_nilearn(name, fetcher, space='MNI152', background=0, hemisphere_rules=(HemisphereRule(pattern='Left[- ].+', hemi='left'), HemisphereRule(pattern='Right[- ].+', hemi='right'), HemisphereRule(pattern='ctx[-_ ]lh[-_ ].+', hemi='left'), HemisphereRule(pattern='ctx[-_ ]rh[-_ ].+', hemi='right'), HemisphereRule(pattern='.+_L', hemi='left'), HemisphereRule(pattern='.+_R', hemi='right'), HemisphereRule(pattern='lh\\..+', hemi='left'), HemisphereRule(pattern='rh\\..+', hemi='right'), HemisphereRule(pattern='.+\\.left', hemi='left'), HemisphereRule(pattern='.+\\.right', hemi='right'), HemisphereRule(pattern='L .+', hemi='left'), HemisphereRule(pattern='R .+', hemi='right'), HemisphereRule(pattern='.+[- ]lh', hemi='left'), HemisphereRule(pattern='.+[- ]rh', hemi='right'), HemisphereRule(pattern='\\d+Networks[_ ]LH[_ ].+', hemi='left'), HemisphereRule(pattern='\\d+Networks[_ ]RH[_ ].+', hemi='right')), **options)#
The atlas
nilearn.datasets.fetch_atlas_<fetcher>(**options)fetches (destrieux_2009,harvard_oxfordwithatlas_name=...,schaefer_2018withn_rois=...), its label names normalised (label_name()), as the engines name those of the atlases they fetch. Its kind isnilearn:<fetcher>.
- classmethod from_tsv(name, image, tsv, space='unspecified', **kwargs)#
From a label image and a TSV (
id,name``[, ``hemi]).
- hemispheres(hints=None)#
The hemisphere of each label of
table, in its order.It is, in order of precedence: the
hemiof the label table, if not empty; the hint for its name, if any and not empty; the inference by the hemisphere rules. A warning names each label for which two of these disagree (the inference counting only when it is notnone); withhints, only those where a hint is one of them.
- property image#
The nibabel image of the atlas: the one it was read from, if any, else one of its volume and affine.
- label_values()#
The ids of each region, by name (a name may have several).
- label_voxels()#
The voxel indices (ijk) of each region, by id, found in one pass (
label_voxel_indices()).
- points()#
The centre of every voxel of a region, in mm through the affine, and its name: columns
x,y,z,label, in the (C) order of the voxels.- Return type:
DataFrame
- present_labels#
the sorted ids of the labels of the volume (with the background)
- region_names()#
The name of each region, by id: the labels of the table but the background.
- property shape#
The shape of the volume.
- neurolang.atlases.FETCHERS = {'destrieux': <function fetch_destrieux>, 'schaefer': <function fetch_schaefer>, 'tian': <function fetch_tian>}#
The fetchers of the deterministic atlases of the engines, by the kind of
.load_atlas: each returnsmaps(a path) andlabels, as nilearn’s fetchers.
- neurolang.atlases.HEMISPHERES = ('left', 'right', 'none')#
The hemisphere of a label.
- neurolang.atlases.HEMISPHERE_RULES: Sequence[HemisphereRule] = (HemisphereRule(pattern='Left[- ].+', hemi='left'), HemisphereRule(pattern='Right[- ].+', hemi='right'), HemisphereRule(pattern='ctx[-_ ]lh[-_ ].+', hemi='left'), HemisphereRule(pattern='ctx[-_ ]rh[-_ ].+', hemi='right'), HemisphereRule(pattern='.+_L', hemi='left'), HemisphereRule(pattern='.+_R', hemi='right'), HemisphereRule(pattern='lh\\..+', hemi='left'), HemisphereRule(pattern='rh\\..+', hemi='right'), HemisphereRule(pattern='.+\\.left', hemi='left'), HemisphereRule(pattern='.+\\.right', hemi='right'), HemisphereRule(pattern='L .+', hemi='left'), HemisphereRule(pattern='R .+', hemi='right'), HemisphereRule(pattern='.+[- ]lh', hemi='left'), HemisphereRule(pattern='.+[- ]rh', hemi='right'), HemisphereRule(pattern='\\d+Networks[_ ]LH[_ ].+', hemi='left'), HemisphereRule(pattern='\\d+Networks[_ ]RH[_ ].+', hemi='right'))#
The default, ordered rules; the first that matches wins: FreeSurfer
Left-/Right-,ctx-lh-/ctx-rh-and a2009sctx_lh_/ctx_rh_(ctx lh `` as :func:`label_name` writes them), the suffixes ``_L/_R(AAL),lh./rh.,.left/.right, the prefixesLeft ``/``Right `` (Harvard-Oxford) and ``L ``/``R `` (nilearn's Destrieux), and the suffixes ``-lh/-rh(Tian’s subcortex), `` lh``/`` rh`` aslabel_name()writes them, and Schaefer’s17Networks_LH_/_RH_(``17Networks LH ``).
- class neurolang.atlases.HemisphereRule(pattern, hemi)#
Infer the hemisphere of a label name.
- exception neurolang.atlases.SpaceMismatchError#
Two objects, e.g. an atlas and a tractogram, are not in one space.
- add_note()#
Exception.add_note(note) – add a note to the exception
- with_traceback()#
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- neurolang.atlases.check_same_space(atlas_space, other_space, allow_mismatch=False)#
Raise
SpaceMismatchErrorunless the spaces are equal.- Parameters:
- atlas_space, other_spaceSpace
The tags of the atlas and of the other object, e.g. a tractogram.
- allow_mismatchbool
The explicit override: if True, warn instead of raising. The caller asserts that the two are aligned by other means.
- Parameters:
atlas_space (Space)
other_space (Space)
allow_mismatch (bool)
- Return type:
None
- neurolang.atlases.infer_hemispheres(names, rules=(HemisphereRule(pattern='Left[- ].+', hemi='left'), HemisphereRule(pattern='Right[- ].+', hemi='right'), HemisphereRule(pattern='ctx[-_ ]lh[-_ ].+', hemi='left'), HemisphereRule(pattern='ctx[-_ ]rh[-_ ].+', hemi='right'), HemisphereRule(pattern='.+_L', hemi='left'), HemisphereRule(pattern='.+_R', hemi='right'), HemisphereRule(pattern='lh\\..+', hemi='left'), HemisphereRule(pattern='rh\\..+', hemi='right'), HemisphereRule(pattern='.+\\.left', hemi='left'), HemisphereRule(pattern='.+\\.right', hemi='right'), HemisphereRule(pattern='L .+', hemi='left'), HemisphereRule(pattern='R .+', hemi='right'), HemisphereRule(pattern='.+[- ]lh', hemi='left'), HemisphereRule(pattern='.+[- ]rh', hemi='right'), HemisphereRule(pattern='\\d+Networks[_ ]LH[_ ].+', hemi='left'), HemisphereRule(pattern='\\d+Networks[_ ]RH[_ ].+', hemi='right')))#
The hemisphere of label names, by the first rule that matches.
- Returns:
- pandas.Series
"left","right"or"none"for each name, in order.
- Parameters:
rules (Sequence[HemisphereRule])
- Return type:
Series
- neurolang.atlases.label_colours(labels)#
The colour,
#rrggbb, of each label id of a look-up table with acolorcolumn, as nilearn’s fetchers (and those ofFETCHERS) give it inlut;{}for labels without colours.
- neurolang.atlases.label_name(value)#
A raw label name as the fetched atlases name their regions: a string, decoded if bytes, whose
-and_are spaces ("L G_and_S_frontomargin"is"L G and S frontomargin").
- neurolang.atlases.label_table(labels)#
The
id,nametable of labels as nilearn’s fetchers (and those ofFETCHERS) give them: a DataFrame withindexandname(a look-up table), a dict id -> name, a list whose position is the id, or pairs (id, name).- Parameters:
labels (Any)
- Return type:
DataFrame
- neurolang.atlases.label_voxel_indices(data, labels)#
Voxel coordinates of each label of a labelled array, in one pass.
- Parameters:
- datanumpy.ndarray
labelled array.
- labelsiterable
label values to look up.
- Returns:
- dict
mapping each label to an
(n, data.ndim)integer array with the coordinates of the voxels equal to it, in the same (C) order asnumpy.nonzero(data == label).
- neurolang.atlases.load_label_table_bids_dseg(path)#
Read a BIDS
*_dseg.tsv(columnsindexandname).
- neurolang.atlases.load_label_table_csv(path, **read_csv)#
Read a table whose first two columns are the id and the name of each label, whatever their headers, with
pandas.read_csv(path, **read_csv)(what.load_atlasreads from a URL).
- neurolang.atlases.load_label_table_freesurfer_lut(path)#
Read a FreeSurfer colour LUT (
FreeSurferColorLUT.txt).