cedalion.io.anatomy

Functions for reading and processing anatomical data.

Functions

cell_coordinates(mask, affine[, units])

Get the coordinates of each voxel centre in the transformed mask.

read_parcel_colors(parcel_colors_file)

Read parcel RGB colors from a JSON file.

read_parcellations(parcel_file)

Read parcellation labels from a json file.

read_segmentation_masks(basedir[, mask_files])

Read segmentation masks from NIFTI files.

trimesh_from_freesurfer(lh, rh, crs, units)

Load freesurfer surfaces as a cdc.TrimeshSurface object.

Classes

HeadModelFiles(basedir, mask_files[, name, ...])

Files of a head model, as listed in its headmodel_manifest.json.

class cedalion.io.anatomy.HeadModelFiles(
basedir: Path,
mask_files: dict[str, str],
name: str | None = None,
landmarks_ras_file: str | None = None,
brain_vertex_coordinates: str | None = None,
scalp_surface_obj: str | None = None,
brain_surface_obj: str | None = None,
freesurfer_surface_obj: str | None = None,
inflated_surface_obj: str | None = None,
parcel_colors: str | None = None,
voxel_to_vertex_mapping: str | None = None,
surfaces_are_zero_based: bool = False,
ras_crs: str | None = None,
)[source]

Bases: object

Files of a head model, as listed in its headmodel_manifest.json.

File names are relative to basedir, the folder containing the manifest. Apart from the segmentation masks all files are optional. Missing surfaces and voxel-to-vertex mappings are derived from the segmentation masks when the head model is constructed (see TwoSurfaceHeadModel.from_manifest).

basedir[source]

Folder containing the head model files.

Type:

pathlib.Path

mask_files[source]

Mapping of segmentation types to NIfTI mask files.

Type:

dict[str, str]

name[source]

Name of the head model.

Type:

str | None

landmarks_ras_file[source]

Landmarks in RAS space (Slicer .mrk.json).

Type:

str | None

brain_vertex_coordinates[source]

CSV with additional brain vertex coordinates, e.g. parcel labels.

Type:

str | None

scalp_surface_obj[source]

Scalp surface in voxel coordinates.

Type:

str | None

brain_surface_obj[source]

Brain surface in voxel coordinates.

Type:

str | None

freesurfer_surface_obj[source]

High-resolution pial surface.

Type:

str | None

inflated_surface_obj[source]

Inflated pial surface.

Type:

str | None

parcel_colors[source]

JSON file mapping parcel names to colors.

Type:

str | None

voxel_to_vertex_mapping[source]

Precomputed brain voxel-to-vertex mapping.

Type:

str | None

surfaces_are_zero_based[source]

Whether the voxel coordinates of the surfaces are zero-based (see TwoSurfaceHeadModel.from_surfaces).

Type:

bool

ras_crs[source]

If given, the name of the RAS coordinate system, replacing the name derived from the NIfTI header.

Type:

str | None

basedir: Path[source]
mask_files: dict[str, str][source]
name: str | None = None[source]
landmarks_ras_file: str | None = None[source]
brain_vertex_coordinates: str | None = None[source]
scalp_surface_obj: str | None = None[source]
brain_surface_obj: str | None = None[source]
freesurfer_surface_obj: str | None = None[source]
inflated_surface_obj: str | None = None[source]
parcel_colors: str | None = None[source]
voxel_to_vertex_mapping: str | None = None[source]
surfaces_are_zero_based: bool = False[source]
ras_crs: str | None = None[source]
classmethod from_manifest(
fname: str | Path,
) → HeadModelFiles[source]

Read a head model manifest.

Parameters:

fname – Path to a headmodel_manifest.json.

Returns:

The head model files with basedir set to the manifest’s folder.

path(attr: str) → Path | None[source]

Return the full path of a file entry or None if it is not set.

load_parcel_colors()[source]
load_voxel_to_vertex_mapping()[source]
load_brain_vertex_coordinates()[source]
cedalion.io.anatomy.read_segmentation_masks(
basedir: str,
mask_files: Dict[str, str] = {'csf': 'csf.nii', 'gm': 'gm.nii', 'scalp': 'scalp.nii', 'skull': 'skull.nii', 'wm': 'wm.nii'},
) → Tuple[DataArray, ndarray][source]

Read segmentation masks from NIFTI files.

Parameters:
  • basedir (str) – Directory containing the mask files

  • mask_files (Dict[str, str]) – Dictionary mapping segmentation types to filenames

Returns:

  • masks (xr.DataArray): Concatenated segmentation masks with a new dimension segmentation_type.

  • affine (np.ndarray): Affine transformation matrix associated with the NIFTI files.

Return type:

Tuple[xr.DataArray, np.ndarray]

cedalion.io.anatomy.cell_coordinates(mask, affine, units='mm')[source]

Get the coordinates of each voxel centre in the transformed mask.

Following NIfTI convention, voxel coordinates in ijk-space denote the centre of that voxel and the affine maps it to the centre’s world coordinates. (See also nifti1.h: “The (x,y,z) coordinates refer to the CENTER of a voxel.”)

Parameters:
  • mask (xr.DataArray) – A binary mask of shape (i, j, k).

  • affine (np.ndarray) – Affine transformation matrix.

  • units (str) – Units of the output coordinates.

Returns:

Coordinates of the center of each voxel in the mask.

Return type:

xr.DataArray

cedalion.io.anatomy.read_parcellations(parcel_file: str | Path) → DataFrame[source]

Read parcellation labels from a json file.

Parameters:

parcel_file – The parcels file name

Returns:

Contains vertices’ labels, their appropriate colors

Return type:

pd.DataFrame

cedalion.io.anatomy.read_parcel_colors(parcel_colors_file: str | Path) → dict[str, list][source]

Read parcel RGB colors from a JSON file.

Parameters:

parcel_colors_file – Path to the JSON file mapping parcel names to RGB color lists.

Returns:

Dictionary mapping parcel label strings to RGB color lists.

cedalion.io.anatomy.trimesh_from_freesurfer(
lh: str | Path,
rh: str | Path,
crs: str,
units: str,
offset_hemispheres: bool = True,
) → TrimeshSurface[source]

Load freesurfer surfaces as a cdc.TrimeshSurface object.

Parameters:
  • lh – path of the left hemisphere surface file.

  • rh – path of the right hemisphere surface file.

  • crs – label of the coordinate reference system

  • units – units of vertex coordinates

  • offset_hemispheres – If True, offset the hemispheres along the x-axis such that the left hemisphere’s maximum x-coordinate aligns with the right hemisphere’s minimum x-coordinate, removing the overlap that occurs with inflated surfaces.

Returns:

A single TrimeshSurface containing both hemispheres, with left-hemisphere vertices preceding right-hemisphere vertices in the vertex array.