| Title: | Create Brain Atlases for the 'ggsegverse' Plotting Ecosystem |
|---|---|
| Description: | Create brain atlas data sets compatible with the 'ggsegverse' plotting packages in 'R'. Provides pipelines for building cortical, subcortical, and white-matter tract atlases from 'FreeSurfer' annotation files, 'GIFTI' and 'CIFTI' surface formats, 'neuromaps', and volumetric 'NIfTI' images. |
| Authors: | Athanasia Mo Mowinckel [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-5756-0223>, Github: drmowinckels), Didac Vidal-Piñeiro [ctb, cph] (ORCID: <https://orcid.org/0000-0001-9997-9156>), John Muschelli [ctb] (ORCID: <https://orcid.org/0000-0001-6469-1750>) |
| Maintainer: | Athanasia Mo Mowinckel <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.9.9.9001 |
| Built: | 2026-05-22 09:10:55 UTC |
| Source: | https://github.com/ggsegverse/ggseg.extra |
Converts logical, numeric, or character input to an integer verbosity
level: 0L (silent), 1L (standard), or 2L (debug).
as_verbosity(x)as_verbosity(x)
x |
Value to coerce. Logical |
Integer 0L, 1L, or 2L
as_verbosity(FALSE) as_verbosity(TRUE) as_verbosity(2)as_verbosity(FALSE) as_verbosity(TRUE) as_verbosity(2)
Reduce vertex count in the sf geometry of a ggseg_atlas object using
Douglas-Peucker simplification. Higher tolerance values produce simpler
shapes with fewer vertices. This avoids re-running the full atlas creation
pipeline.
atlas_simplify(atlas, tolerance = 0.5)atlas_simplify(atlas, tolerance = 0.5)
atlas |
A |
tolerance |
Simplification tolerance passed to sf::st_simplify(dTolerance). Typical range 0.1–2. Default 0.5. |
A modified ggseg_atlas with simplified sf geometry.
## Not run: atlas <- atlas_simplify(my_atlas, tolerance = 1) plot(atlas) ## End(Not run)## Not run: atlas <- atlas_simplify(my_atlas, tolerance = 1) plot(atlas) ## End(Not run)
Apply kernel smoothing to the sf geometry of a ggseg_atlas object.
Higher smoothness values produce rounder region boundaries.
This avoids re-running the full atlas creation pipeline.
atlas_smooth(atlas, smoothness = 5)atlas_smooth(atlas, smoothness = 5)
atlas |
A |
smoothness |
Smoothing bandwidth passed to smoothr::smooth(method = "ksmooth"). Typical range 3–15. Default 5. |
A modified ggseg_atlas with smoothed sf geometry.
## Not run: atlas <- atlas_smooth(my_atlas, smoothness = 10) plot(atlas) ## End(Not run)## Not run: atlas <- atlas_smooth(my_atlas, smoothness = 10) plot(atlas) ## End(Not run)
Turn FreeSurfer annotation files into a brain atlas you can plot with ggseg and ggseg3d. Reads the annotation, extracts vertex-to-region assignments, and generates 2D polygon geometry by projecting the inflated mesh triangles to 2D via orthographic projection.
create_cortical_from_annotation( input_annot, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )create_cortical_from_annotation( input_annot, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )
input_annot |
Character vector of paths to annotation files.
Files should follow FreeSurfer naming convention with |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
hemisphere |
Which hemispheres to include: "lh", "rh", or both. |
views |
Which views to include: "lateral", "medial", "superior", "inferior". |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smooth_refinements |
Number of Chaikin corner-cutting refinements
to apply to 2D polygons. Higher values produce smoother region
boundaries (typical range: 0–3). 0 disables smoothing.
If not specified, uses |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
A ggseg_atlas object containing region metadata (core), vertex
indices for 3D rendering, a colour palette, and sf geometry for 2D plots.
## Not run: atlas <- create_cortical_from_annotation( input_annot = c("lh.aparc.DKTatlas.annot", "rh.aparc.DKTatlas.annot") ) ggseg(atlas = atlas) ## End(Not run)## Not run: atlas <- create_cortical_from_annotation( input_annot = c("lh.aparc.DKTatlas.annot", "rh.aparc.DKTatlas.annot") ) ggseg(atlas = atlas) ## End(Not run)
Build a brain atlas from a CIFTI dense label file (.dlabel.nii).
The file must be in fsaverage5 space (10,242 vertices per hemisphere).
create_cortical_from_cifti( cifti_file, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )create_cortical_from_cifti( cifti_file, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )
cifti_file |
Path to a |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
hemisphere |
Which hemispheres to include: "lh", "rh", or both. |
views |
Which views to include: "lateral", "medial", "superior", "inferior". |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smooth_refinements |
Number of Chaikin corner-cutting refinements
to apply to 2D polygons. Higher values produce smoother region
boundaries (typical range: 0–3). 0 disables smoothing.
If not specified, uses |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
A ggseg_atlas object.
## Not run: atlas <- create_cortical_from_cifti( cifti_file = "parcellation.dlabel.nii" ) ## End(Not run)## Not run: atlas <- create_cortical_from_cifti( cifti_file = "parcellation.dlabel.nii" ) ## End(Not run)
Build a brain atlas from GIFTI label files (.label.gii).
Assumes fsaverage5 surface space (10,242 vertices per hemisphere).
create_cortical_from_gifti( gifti_files, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )create_cortical_from_gifti( gifti_files, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )
gifti_files |
Character vector of paths to |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
hemisphere |
Which hemispheres to include: "lh", "rh", or both. |
views |
Which views to include: "lateral", "medial", "superior", "inferior". |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smooth_refinements |
Number of Chaikin corner-cutting refinements
to apply to 2D polygons. Higher values produce smoother region
boundaries (typical range: 0–3). 0 disables smoothing.
If not specified, uses |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
A ggseg_atlas object.
## Not run: atlas <- create_cortical_from_gifti( gifti_files = c("lh.aparc.label.gii", "rh.aparc.label.gii") ) ## End(Not run)## Not run: atlas <- create_cortical_from_gifti( gifti_files = c("lh.aparc.label.gii", "rh.aparc.label.gii") ) ## End(Not run)
Build an atlas from individual FreeSurfer .label files rather than a
complete annotation. Each label file defines a single region by listing
which surface vertices belong to it.
The function detects hemisphere from filename prefixes (lh. or rh.) and
derives region names from the rest of the filename.
create_cortical_from_labels( label_files, atlas_name = NULL, input_lut = NULL, output_dir = NULL, views = c("lateral", "medial"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )create_cortical_from_labels( label_files, atlas_name = NULL, input_lut = NULL, output_dir = NULL, views = c("lateral", "medial"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )
label_files |
Paths to |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
input_lut |
Path to a color lookup table (LUT) file, or a data.frame
with columns |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
views |
Which views to include: "lateral", "medial", "superior", "inferior". |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smooth_refinements |
Number of Chaikin corner-cutting refinements
to apply to 2D polygons. Higher values produce smoother region
boundaries (typical range: 0–3). 0 disables smoothing.
If not specified, uses |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
A ggseg_atlas object.
## Not run: labels <- c("lh.region1.label", "lh.region2.label", "rh.region1.label") atlas <- create_cortical_from_labels(labels) ## End(Not run)## Not run: labels <- c("lh.region1.label", "lh.region2.label", "rh.region1.label") atlas <- create_cortical_from_labels(labels) ## End(Not run)
Build a brain atlas directly from a neuromaps annotation. The annotation
is downloaded via neuromapr::fetch_neuromaps_annotation().
Supports both surface (.func.gii) and volume (.nii/.nii.gz)
annotations. Volume annotations in MNI152 space are automatically
projected to fsaverage5 via FreeSurfer's mri_vol2surf.
create_cortical_from_neuromaps( source, desc, space = "fsaverage", density = "10k", label_table = NULL, n_bins = NULL, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )create_cortical_from_neuromaps( source, desc, space = "fsaverage", density = "10k", label_table = NULL, n_bins = NULL, atlas_name = NULL, output_dir = NULL, hemisphere = c("rh", "lh"), views = c("lateral", "medial", "superior", "inferior"), tolerance = NULL, smooth_refinements = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL )
source |
Neuromaps source identifier (e.g., |
desc |
Neuromaps descriptor key (e.g., |
space |
Coordinate space. Defaults to |
density |
Surface vertex density. Defaults to |
label_table |
Optional data.frame mapping parcel IDs to region names. |
n_bins |
Number of quantile bins for continuous brain maps. |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
hemisphere |
Which hemispheres to include: "lh", "rh", or both. |
views |
Which views to include: "lateral", "medial", "superior", "inferior". |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smooth_refinements |
Number of Chaikin corner-cutting refinements
to apply to 2D polygons. Higher values produce smoother region
boundaries (typical range: 0–3). 0 disables smoothing.
If not specified, uses |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
A ggseg_atlas object.
## Not run: atlas <- create_cortical_from_neuromaps( source = "abagen", desc = "genepc1", n_bins = 7 ) ## End(Not run)## Not run: atlas <- create_cortical_from_neuromaps( source = "abagen", desc = "genepc1", n_bins = 7 ) ## End(Not run)
Turn a subcortical segmentation volume (like aseg.mgz) into a brain
atlas with 3D meshes for each structure. The function extracts each labelled
region from the volume, creates a surface mesh, and smooths it.
For 2D plotting, the function can also generate slice views by taking snapshots at specified coordinates and extracting contours.
Requires FreeSurfer for mesh generation.
create_subcortical_from_volume( input_volume, input_lut = NULL, atlas_name = NULL, views = NULL, output_dir = NULL, vertex_size_limits = NULL, dilate = NULL, tolerance = NULL, smoothness = NULL, verbose = get_verbose(), cleanup = NULL, skip_existing = NULL, decimate = 0.5, steps = NULL )create_subcortical_from_volume( input_volume, input_lut = NULL, atlas_name = NULL, views = NULL, output_dir = NULL, vertex_size_limits = NULL, dilate = NULL, tolerance = NULL, smoothness = NULL, verbose = get_verbose(), cleanup = NULL, skip_existing = NULL, decimate = 0.5, steps = NULL )
input_volume |
Path to the segmentation volume. Supports |
input_lut |
Path to a FreeSurfer-style colour lookup table that maps
label IDs to region names and colours (e.g., |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
views |
A data.frame specifying projection views with columns |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
vertex_size_limits |
Numeric vector of length 2 setting minimum and maximum vertex count for polygons. Polygons outside this range are filtered out. Default NULL applies no limits. |
dilate |
Dilation iterations for 2D polygons. Useful for filling small gaps between structures. |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smoothness |
Smoothing factor for 2D contours. Higher values produce
smoother region boundaries (typical range: 3–15). Passed to
|
verbose |
Verbosity level: |
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
decimate |
Mesh decimation factor between 0 and 1. Reduces the number
of faces in 3D meshes using quadric edge decimation (via
|
steps |
Which pipeline steps to run. Default NULL runs all steps. Steps are:
Use |
A ggseg_atlas object with region metadata (core), 3D meshes,
a colour palette, and optionally sf geometry for 2D slice plots.
## Not run: # Create 3D-only subcortical atlas from aseg atlas <- create_subcortical_from_volume( input_volume = "path/to/aseg.mgz", input_lut = "path/to/FreeSurferColorLUT.txt", steps = 1:3 ) # View with ggseg3d ggseg3d(atlas = atlas, hemisphere = "subcort") # Full atlas with 2D slices atlas <- create_subcortical_from_volume( input_volume = "path/to/aseg.mgz", input_lut = "path/to/ASegStatsLUT.txt" ) # Post-process to remove/modify regions (functions from ggseg.formats) atlas <- atlas |> atlas_region_remove("White-Matter") |> atlas_region_contextual("Cortex") ## End(Not run)## Not run: # Create 3D-only subcortical atlas from aseg atlas <- create_subcortical_from_volume( input_volume = "path/to/aseg.mgz", input_lut = "path/to/FreeSurferColorLUT.txt", steps = 1:3 ) # View with ggseg3d ggseg3d(atlas = atlas, hemisphere = "subcort") # Full atlas with 2D slices atlas <- create_subcortical_from_volume( input_volume = "path/to/aseg.mgz", input_lut = "path/to/ASegStatsLUT.txt" ) # Post-process to remove/modify regions (functions from ggseg.formats) atlas <- atlas |> atlas_region_remove("White-Matter") |> atlas_region_contextual("Cortex") ## End(Not run)
Turn tractography streamlines into a brain atlas where each tract is rendered as a 3D tube. The function computes a centerline from the streamlines and generates a tube mesh around it.
You can provide tract data in several formats: TRK files from TrackVis, TCK files from MRtrix, or coordinate matrices directly in R. The function reads the streamlines, extracts a representative centerline (by averaging or selecting the medoid), and builds a tube mesh for 3D rendering.
For tracts with many streamlines, set tube_radius = "density" to make
the tube thicker where more streamlines pass through.
create_tract_from_tractography( input_tracts, input_aseg = NULL, atlas_name = NULL, input_lut = NULL, tube_radius = 5, tube_segments = 8, n_points = 50, centerline_method = c("mean", "medoid"), views = NULL, output_dir = NULL, verbose = get_verbose(), tolerance = NULL, smoothness = NULL, cleanup = NULL, skip_existing = NULL, dilate = NULL, vertex_size_limits = NULL, steps = NULL )create_tract_from_tractography( input_tracts, input_aseg = NULL, atlas_name = NULL, input_lut = NULL, tube_radius = 5, tube_segments = 8, n_points = 50, centerline_method = c("mean", "medoid"), views = NULL, output_dir = NULL, verbose = get_verbose(), tolerance = NULL, smoothness = NULL, cleanup = NULL, skip_existing = NULL, dilate = NULL, vertex_size_limits = NULL, steps = NULL )
input_tracts |
Paths to tractography files ( |
input_aseg |
Path to a segmentation volume ( |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
input_lut |
Path to a color lookup table (LUT) file, or a data.frame
with columns |
tube_radius |
Controls the tube thickness. Either a single numeric
value for uniform radius, or |
tube_segments |
Number of segments around the tube circumference. Higher values make smoother tubes but larger meshes. Default 8 is a good balance. |
n_points |
Number of points to resample the centerline to. All tracts are resampled to this length for consistent tube generation. |
centerline_method |
How to extract the centerline from multiple
streamlines: |
views |
A data.frame specifying projection views. If NULL, uses
|
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
verbose |
Verbosity level: |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smoothness |
Smoothing factor for 2D contours. Higher values produce
smoother region boundaries (typical range: 3–15). Passed to
|
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
dilate |
Dilation iterations for 2D polygons. Useful for filling small gaps between structures. |
vertex_size_limits |
Numeric vector of length 2 setting minimum and maximum vertex count for polygons. Polygons outside this range are filtered out. Default NULL applies no limits. |
steps |
Which pipeline steps to run. Default NULL runs all steps. Steps are:
Use |
A ggseg_atlas object with type "tract", containing region
metadata, tube meshes for 3D rendering, colours, and optionally sf
geometry for 2D projection plots.
## Not run: # From TRK files (names derived from filenames) atlas <- create_tract_from_tractography( input_tracts = c("cst_left.trk", "cst_right.trk") ) # With custom names and colours via LUT atlas <- create_tract_from_tractography( input_tracts = c("cst_left.trk", "cst_right.trk"), input_lut = "tract_colors.txt" ) # View with ggseg3d ggseg3d(atlas = atlas) ## End(Not run)## Not run: # From TRK files (names derived from filenames) atlas <- create_tract_from_tractography( input_tracts = c("cst_left.trk", "cst_right.trk") ) # With custom names and colours via LUT atlas <- create_tract_from_tractography( input_tracts = c("cst_left.trk", "cst_right.trk"), input_lut = "tract_colors.txt" ) # View with ggseg3d ggseg3d(atlas = atlas) ## End(Not run)
Build a brain atlas from a single volumetric parcellation (NIfTI/MGZ) that
contains both cortical and subcortical regions. Cortical regions are
projected onto the fsaverage5 surface via FreeSurfer's mri_vol2surf
and rendered as surface views, while subcortical regions go through
the mesh-based subcortical pipeline.
Requires FreeSurfer.
create_wholebrain_from_volume( input_volume, input_lut = NULL, atlas_name = NULL, output_dir = NULL, projfrac = 0.5, projfrac_range = c(0, 1, 0.1), subject = "fsaverage5", regheader = TRUE, min_vertices = 50L, cortical_labels = NULL, subcortical_labels = NULL, cortical_views = c("lateral", "medial", "superior", "inferior"), subcortical_views = NULL, decimate = 0.5, tolerance = NULL, smoothness = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL, steps = NULL )create_wholebrain_from_volume( input_volume, input_lut = NULL, atlas_name = NULL, output_dir = NULL, projfrac = 0.5, projfrac_range = c(0, 1, 0.1), subject = "fsaverage5", regheader = TRUE, min_vertices = 50L, cortical_labels = NULL, subcortical_labels = NULL, cortical_views = c("lateral", "medial", "superior", "inferior"), subcortical_views = NULL, decimate = 0.5, tolerance = NULL, smoothness = NULL, cleanup = NULL, verbose = get_verbose(), skip_existing = NULL, steps = NULL )
input_volume |
Path to volumetric parcellation in MNI152 space (.mgz, .nii, .nii.gz). |
input_lut |
Path to FreeSurfer-style colour lookup table, or a
data.frame with columns |
atlas_name |
Name for the atlas. If NULL, derived from the input filename. |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
projfrac |
Cortical depth fraction for projection (0 = white surface,
1 = pial surface). Only used when |
projfrac_range |
Numeric vector |
subject |
Target surface subject. Default "fsaverage5". |
regheader |
If TRUE (default), assumes volume RAS coordinates match
the subject space and uses |
min_vertices |
Minimum total vertex count across hemispheres for a
label to be classified as cortical by the vertex-count heuristic (see
Label classification). Ignored when |
cortical_labels |
Character vector of label names to force as cortical.
Highest priority; overrides LUT |
subcortical_labels |
Character vector of label names to force as
subcortical. Highest priority; overrides LUT |
cortical_views |
Views for cortical sub-pipeline.
Default |
subcortical_views |
Views for subcortical sub-pipeline. Default NULL (auto-detected). |
decimate |
Mesh decimation ratio for subcortical meshes (0-1). Default 0.5. |
tolerance |
Simplification tolerance for 2D polygons. Higher values
produce simpler shapes with fewer vertices (typical range: 0.1–2).
Passed to |
smoothness |
Smoothing factor for 2D contours. Higher values produce
smoother region boundaries (typical range: 3–15). Passed to
|
cleanup |
Remove intermediate files after atlas creation. If not
specified, uses |
verbose |
Verbosity level: |
skip_existing |
Skip generating output files that already exist,
allowing interrupted atlas creation to resume. If not specified, uses
|
steps |
Which pipeline steps to run. Default NULL runs all steps. Steps are:
Use |
A named list with elements cortical and subcortical, each a
ggseg_atlas object (or NULL if no regions of that type exist).
The pipeline must know which labels are cortical (rendered on the surface) and which are subcortical (rendered as 3D meshes / 2D slices). Three mechanisms are available, applied in priority order:
Function arguments (highest priority): cortical_labels and
subcortical_labels override everything for the specified labels.
LUT type column: If the colour lookup table has a type column
with values "cortical" or "subcortical", that classification is
used for any labels not covered by the function arguments. This is the
recommended approach for reproducible atlas creation.
Vertex-count heuristic (fallback): Labels with at least
min_vertices vertices on the surface projection are classified as
cortical; the rest as subcortical.
Before surface projection, the volume is filtered so that only voxel IDs listed in the LUT are kept; all other non-zero voxels are zeroed out. This prevents unlisted structures (e.g. white matter, ventricle masks) from bleeding onto the cortical surface during label dilation.
Cortical voxels are also used to generate the brain-outline reference
geometry for the subcortical pipeline. The volume's orientation matrix
(xform) is used to split cortical voxels by hemisphere: left-hemisphere
voxels map to FreeSurfer label 3 (left cortex) and right-hemisphere to
label 42 (right cortex).
This is the most complex pipeline in ggsegExtra and the one most likely to need manual correction. Recommended workflow:
Run steps = 1:2 first to project the volume and classify labels.
Inspect result$cortical_labels and result$subcortical_labels.
If the automatic split is wrong, either add a type column to the
LUT or use cortical_labels / subcortical_labels to override.
Run the full pipeline once you are satisfied with the split.
Visually inspect the resulting atlas with ggseg() / ggseg3d().
The cortical surface projection uses FreeSurfer's cortex label
({hemi}.cortex.label) to prevent label dilation into the medial wall.
This file ships with fsaverage5 and is always required.
## Not run: # --- Recommended: LUT with type column --- lut <- data.frame( idx = c(10, 11, 49, 50, 101:148), label = c("Left-Thalamus", "Left-Caudate", "Right-Thalamus", "Right-Caudate", paste0("cortical_region_", 1:48)), type = c(rep("subcortical", 4), rep("cortical", 48)), R = sample(50:220, 52, TRUE), G = sample(50:220, 52, TRUE), B = sample(50:220, 52, TRUE), A = 255L ) result <- create_wholebrain_from_volume( input_volume = "my_atlas.nii.gz", input_lut = lut, atlas_name = "my_atlas" ) result$cortical # surface-based cortical atlas result$subcortical # mesh-based subcortical atlas # --- Without type column: automatic classification --- result <- create_wholebrain_from_volume( input_volume = "atlas.nii.gz", input_lut = "atlas_LUT.txt", atlas_name = "auto_atlas" ) # --- Inspect classification before full run --- result <- create_wholebrain_from_volume( input_volume = "atlas.nii.gz", input_lut = "atlas_LUT.txt", steps = 1:2 ) result$cortical_labels result$subcortical_labels ## End(Not run)## Not run: # --- Recommended: LUT with type column --- lut <- data.frame( idx = c(10, 11, 49, 50, 101:148), label = c("Left-Thalamus", "Left-Caudate", "Right-Thalamus", "Right-Caudate", paste0("cortical_region_", 1:48)), type = c(rep("subcortical", 4), rep("cortical", 48)), R = sample(50:220, 52, TRUE), G = sample(50:220, 52, TRUE), B = sample(50:220, 52, TRUE), A = 255L ) result <- create_wholebrain_from_volume( input_volume = "my_atlas.nii.gz", input_lut = lut, atlas_name = "my_atlas" ) result$cortical # surface-based cortical atlas result$subcortical # mesh-based subcortical atlas # --- Without type column: automatic classification --- result <- create_wholebrain_from_volume( input_volume = "atlas.nii.gz", input_lut = "atlas_LUT.txt", atlas_name = "auto_atlas" ) # --- Inspect classification before full run --- result <- create_wholebrain_from_volume( input_volume = "atlas.nii.gz", input_lut = "atlas_LUT.txt", steps = 1:2 ) result$cortical_labels result$subcortical_labels ## End(Not run)
Reads a FreeSurfer color lookup table and adds hex colour codes for use in plotting.
get_ctab(color_lut)get_ctab(color_lut)
color_lut |
Path to a color table file, or a data.frame that
passes |
A data.frame with the original columns plus roi (zero-padded
index) and color (hex colour code).
ct <- data.frame( idx = 0:1, label = c("Unknown", "Region1"), R = c(0L, 205L), G = c(0L, 130L), B = c(0L, 176L), A = c(0L, 0L) ) get_ctab(ct)ct <- data.frame( idx = 0:1, label = c("Unknown", "Region1"), R = c(0L, 205L), G = c(0L, 130L), B = c(0L, 176L), A = c(0L, 0L) ) get_ctab(ct)
Returns the verbosity level from option, environment variable, or default.
Checks in order: ggseg.extra.verbose option, GGSEG_EXTRA_VERBOSE env var,
then defaults to 1L.
get_verbose()get_verbose()
Verbosity levels:
0 — Silent: no console output
1 — Standard (default): pipeline progress and step summaries
2 — Debug: includes FreeSurfer command output
Logical values are accepted for backward compatibility
(FALSE = 0, TRUE = 1).
Integer 0L, 1L, or 2L
get_verbose() options(ggseg.extra.verbose = 0) get_verbose() options(ggseg.extra.verbose = NULL)get_verbose() options(ggseg.extra.verbose = 0) get_verbose() options(ggseg.extra.verbose = NULL)
Check if object is a color table
is_ctab(x)is_ctab(x)
x |
Object to check. |
TRUE if x is a data.frame with the required color table columns.
ct <- data.frame( idx = 0L, label = "Unknown", R = 0L, G = 0L, B = 0L, A = 0L ) is_ctab(ct) is_ctab(data.frame(x = 1))ct <- data.frame( idx = 0L, label = "Unknown", R = 0L, G = 0L, B = 0L, A = 0L ) is_ctab(ct) is_ctab(data.frame(x = 1))
Get verbosity level
is_verbose(verbose = NULL)is_verbose(verbose = NULL)
verbose |
Optional explicit value. If NULL, reads from
option/env via |
Integer 0L, 1L, or 2L
is_verbose() is_verbose(FALSE) is_verbose(2)is_verbose() is_verbose(FALSE) is_verbose(2)
Annotation files are subject specific. Most are registered for fsaverage, but we recommend using fsaverage5 for the mesh plots in ggseg3d, as these contain a decent balance in number of vertices for detailed rendering and speed.
mri_surf2surf_rereg( subject, annot, hemi = c("lh", "rh"), target_subject = "fsaverage5", output_dir = file.path(freesurfer::fs_subj_dir(), subject, "label"), verbose = get_verbose() )mri_surf2surf_rereg( subject, annot, hemi = c("lh", "rh"), target_subject = "fsaverage5", output_dir = file.path(freesurfer::fs_subj_dir(), subject, "label"), verbose = get_verbose() )
subject |
subject the original annotation file is registered to |
annot |
annotation file name (as found in subjects_dir) |
hemi |
hemisphere (one of "lh" or "rh") |
target_subject |
subject to re-register the annotation (default fsaverage5) |
output_dir |
Directory to store intermediate files (screenshots, masks,
contours). Defaults to |
verbose |
Verbosity level: |
nothing
## Not run: # For help see: freesurfer::fs_help("mri_surf2surf") mri_surf2surf_rereg( subject = "bert", annot = "aparc.DKTatlas", target_subject = "fsaverage5" ) ## End(Not run)## Not run: # For help see: freesurfer::fs_help("mri_surf2surf") mri_surf2surf_rereg( subject = "bert", annot = "aparc.DKTatlas", target_subject = "fsaverage5" ) ## End(Not run)
Reads FreeSurfer annotation files and extracts region information including vertices, colours, and labels for both hemispheres.
read_annotation_data(annot_files)read_annotation_data(annot_files)
annot_files |
Character vector of paths to annotation files.
Files should follow FreeSurfer naming convention with |
A tibble with columns: hemi, region, label, colour, vertices
## Not run: atlas_data <- read_annotation_data(c( "path/to/lh.aparc.annot", "path/to/rh.aparc.annot" )) ## End(Not run)## Not run: atlas_data <- read_annotation_data(c( "path/to/lh.aparc.annot", "path/to/rh.aparc.annot" )) ## End(Not run)
Reads a CIFTI dense label file (.dlabel.nii) and extracts region
information for both hemispheres. Returns data in the same format as
read_annotation_data() for use with the cortical atlas pipeline.
read_cifti_annotation(cifti_file)read_cifti_annotation(cifti_file)
cifti_file |
Path to a |
The CIFTI file must be in fsaverage5 space (10,242 vertices per hemisphere). If your file uses a different resolution, resample it first with Connectome Workbench:
wb_command -cifti-resample input.dlabel.nii ...
A tibble with columns: hemi, region, label, colour, vertices
## Not run: atlas_data <- read_cifti_annotation("parcellation.dlabel.nii") ## End(Not run)## Not run: atlas_data <- read_cifti_annotation("parcellation.dlabel.nii") ## End(Not run)
Read a FreeSurfer color lookup table file (e.g., FreeSurferColorLUT.txt
or ASegStatsLUT.txt). These files map label indices to region names
and RGBA colours.
read_ctab(path)read_ctab(path)
path |
Path to the color table file. |
A data.frame with columns: idx, label, R, G, B, A.
get_ctab() to read and add hex colours, write_ctab() to write
ctab_file <- tempfile() writeLines(c( " 0 Unknown 0 0 0 0", " 1 Left-Cerebral-Cortex 205 130 176 0" ), ctab_file) read_ctab(ctab_file)ctab_file <- tempfile() writeLines(c( " 0 Unknown 0 0 0 0", " 1 Left-Cerebral-Cortex 205 130 176 0" ), ctab_file) read_ctab(ctab_file)
Reads GIFTI annotation (.label.gii) files and extracts region
information including vertices, colours, and labels. Returns data in
the same format as read_annotation_data() for use with the cortical
atlas pipeline.
read_gifti_annotation(gifti_files)read_gifti_annotation(gifti_files)
gifti_files |
Character vector of paths to |
Hemisphere is detected from filename patterns: lh., rh., .L., .R.
A tibble with columns: hemi, region, label, colour, vertices
## Not run: atlas_data <- read_gifti_annotation(c( "lh.aparc.label.gii", "rh.aparc.label.gii" )) ## End(Not run)## Not run: atlas_data <- read_gifti_annotation(c( "lh.aparc.label.gii", "rh.aparc.label.gii" )) ## End(Not run)
Reads neuromaps GIFTI metric files (.func.gii) and converts them to
the standard annotation format used by the cortical atlas pipeline.
read_neuromaps_annotation(gifti_files, label_table = NULL, n_bins = NULL)read_neuromaps_annotation(gifti_files, label_table = NULL, n_bins = NULL)
gifti_files |
Character vector of paths to |
label_table |
Optional data.frame mapping integer parcel IDs to
region names. Must have columns |
n_bins |
Number of quantile bins for continuous data. When |
Automatically detects whether data contains integer parcel IDs
(parcellation) or continuous values (brain map). For parcellations,
vertex value 0 is treated as medial wall. For continuous data, NaN
vertices are medial wall and values are discretized into quantile
bins via n_bins.
Files must be in fsaverage5 space (10,242 vertices per hemisphere).
Use space = "fsaverage" with density = "10k" when fetching from
neuromaps.
A tibble with columns: hemi, region, label, colour, vertices
## Not run: files <- neuromapr::fetch_neuromaps_annotation( "abagen", "genepc1", "fsaverage", density = "10k" ) atlas_data <- read_neuromaps_annotation(files, n_bins = 7) ## End(Not run)## Not run: files <- neuromapr::fetch_neuromaps_annotation( "abagen", "genepc1", "fsaverage", density = "10k" ) atlas_data <- read_neuromaps_annotation(files, n_bins = 7) ## End(Not run)
Projects an MNI152-space NIfTI volume onto the fsaverage5 surface via
FreeSurfer's mri_vol2surf, then discretizes the projected per-vertex
values using the same binning logic as read_neuromaps_annotation().
read_neuromaps_volume(nifti_file, n_bins = NULL, output_dir = tempdir())read_neuromaps_volume(nifti_file, n_bins = NULL, output_dir = tempdir())
nifti_file |
Path to a |
n_bins |
Number of quantile bins for continuous data. When |
output_dir |
Directory for intermediate surface overlay files. |
A tibble with columns: hemi, region, label, colour, vertices
## Not run: atlas_data <- read_neuromaps_volume("map.nii.gz", n_bins = 7) ## End(Not run)## Not run: atlas_data <- read_neuromaps_volume("map.nii.gz", n_bins = 7) ## End(Not run)
Load streamlines from a tractography file. Supports TrackVis (.trk) and
MRtrix (.tck) formats. The file format is detected from the extension.
read_tractography(file)read_tractography(file)
file |
Path to a |
A list of matrices, one per streamline. Each matrix has N rows (points along the streamline) and 3 columns (x, y, z coordinates).
read_trk(), read_tck() for format-specific readers
## Not run: streamlines <- read_tractography("bundle.trk") ## End(Not run)## Not run: streamlines <- read_tractography("bundle.trk") ## End(Not run)
Scaffold an R package for distributing a brain atlas. The generated package follows ggseg conventions and includes everything you need: template scripts for atlas creation, documentation stubs, a test suite, and GitHub Actions for automated checking.
setup_atlas_repo( path, atlas_name = NULL, open = rlang::is_interactive(), rstudio = TRUE )setup_atlas_repo( path, atlas_name = NULL, open = rlang::is_interactive(), rstudio = TRUE )
path |
Where to create the package. If the directory exists, it must be empty. |
atlas_name |
Name of the atlas (lowercase, no spaces). The package
name becomes |
open |
If TRUE, opens the new project in RStudio. Default is TRUE when running interactively. |
rstudio |
If TRUE, creates an |
The package will be named ggseg{AtlasName} (e.g., ggsegSchaefer for
a Schaefer parcellation). After creation, edit the files in data-raw/
to build your atlas, then run devtools::document() and devtools::check().
Invisibly returns the path to the created package.
## Not run: # Create atlas package in a new directory setup_atlas_repo("ggsegDkt", "dkt") # Create in current directory, derive name from path setup_atlas_repo("ggsegMyatlas") # Specify full path setup_atlas_repo("~/projects/ggsegSchaefer", "schaefer") # Without opening in RStudio setup_atlas_repo("ggsegHarvard", "harvard", open = FALSE) ## End(Not run)## Not run: # Create atlas package in a new directory setup_atlas_repo("ggsegDkt", "dkt") # Create in current directory, derive name from path setup_atlas_repo("ggsegMyatlas") # Specify full path setup_atlas_repo("~/projects/ggsegSchaefer", "schaefer") # Without opening in RStudio setup_atlas_repo("ggsegHarvard", "harvard", open = FALSE) ## End(Not run)
Performs diagnostic checks to verify that system dependencies and environment variables required by ggseg.extra are properly configured.
setup_sitrep(detail = c("simple", "full"))setup_sitrep(detail = c("simple", "full"))
detail |
Character. Level of detail to display:
|
Invisibly returns a list with check results.
setup_sitrep() setup_sitrep("full")setup_sitrep() setup_sitrep("full")
Write a color table to file in FreeSurfer format.
write_ctab(x, path)write_ctab(x, path)
x |
A data.frame with columns: idx, label, R, G, B, A. |
path |
Path to write to. |
Invisibly returns the lines written.
ct <- data.frame( idx = 0:1, label = c("Unknown", "Region1"), R = c(0L, 205L), G = c(0L, 130L), B = c(0L, 176L), A = c(0L, 0L) ) out <- tempfile() write_ctab(ct, out)ct <- data.frame( idx = 0:1, label = c("Unknown", "Region1"), R = c(0L, 205L), G = c(0L, 130L), B = c(0L, 176L), A = c(0L, 0L) ) out <- tempfile() write_ctab(ct, out)