pycortex
repository·main·Indexed 20 days ago
https://github.com/gallantlab/pycortexA software library for visualizing volumetric neuroimaging data, such as fMRI, on cortical surfaces. It provides tools for 3D rendering via cortex.webgl, 2D flat projections via cortex.quickflat, and brain alignment through automatic and manual processes in cortex.align. The library supports Linux and macOS, utilizing a flat-file database (filestore) to manage surfaces, transforms, masks, and ROIs.
What's inside pycortex
- Pycortex is a software package designed for generating interactive 3D visualizations of fMRI data projected onto cortical surface models. Additionally, it can generate high-quality 2D flattened cortical visualizations.
What is OpenCTM?
mainOpenCTM is a file format, software library, and toolset designed for the compression of 3D triangle meshes. It aims to compress geometry to a fraction of the size of comparable formats like 3DS, STL, COLLADA, and VRML. The library is written in portable C (C99) and is designed to be compatible with both big-endian and little-endian 32/64-bit systems.Explore the most commonly used pycortex modules
mainThe
cortexpackage is organized into several specialized modules. The most frequently used modules for visualization and data handling are:cortex.quickflat: High-level functions for creating 2D flat projections of cortical data (e.g.,make_figure,add_data,add_rois).cortex.webgl: Tools for interactive 3D visualization (e.g.,show,make_static).cortex.dataset: Core data structures for representing brain data, includingDataset,Volume, andVertexobjects (and their 2D/RGB variants).
Explore WebGL 3-D viewer examples
mainTheexamples/webgldirectory contains examples demonstrating how to create interactive 3-D viewers using WebGL. These examples serve as a reference for implementing high-quality, interactive brain visualizations within a web environment.Understand the segmentation process in pycortex
mainSegmentation is the process of identifying boundaries between white matter/gray matter and gray matter/dura. While tools like Caret estimate a single 'fiducial' surface (the midway point between white matter and pia) using a marching cubes algorithm, pycortex provides a more integrated approach.
Pycortex wraps several Freesurfer segmentation steps to simplify the workflow. Typically, the four functions located in
cortex.segmentare sufficient to transform an anatomical image into a segmented and flattened surface.How pycortex surfaces work
mainPycortex operates on triangular mesh geometry. A surface is any file specifying this geometry. To plot cortical data, a subject must have at least the fiducial and flat geometries available.
Surface Requirements
- Vertex Consistency: The surface files for a specific subject and hemisphere (left
lhor rightrh) must have the same number of vertices across all types (e.g., fiducial, inflated, and flat). If they do not match, mapping from fiducial to flatmap will fail. - Formats: Supported formats are OFF, VTK, and npz. npz is recommended for highest performance as it is binary and compressed. VTK files must be in v. 1 (ASCII) format.
- Naming Convention: Inside the filestore, files are named
{type}_{hemisphere}.{format}(e.g.,fiducial_lh.npz).
Surface Types
fiducial: The undistorted reconstruction in original anatomical space.inflated: An inflated version of the fiducial surface.flat: A 2D flattened version of the surface.
- Vertex Consistency: The surface files for a specific subject and hemisphere (left
How ROI and Sulcus drawing differ in pycortex
mainWhen using
pycortex-roidraw, it is important to distinguish between ROIs and Sulci, as they interact with the pycortex ecosystem differently:ROIs (Regions of Interest)
- Type: Closed curves.
- Data: Carries per-hemisphere vertex membership.
- Export: A portable JSON vertex set containing vertex indices, boundary rings, and editable bezier curves.
- Compatibility: These are independent of the standard Inkscape-based pycortex ROI system. They are not read by
get_roi_masksor similar Python API functions. Use them for quick interactive annotation in the viewer.
Sulci
- Type: Open curves.
- Data: Carries no vertex data; they are display geometry only.
- Export: An
overlays.svgfragment (open, unfilled<path>elements in asulcilayer). - Compatibility: These are compatible with pycortex's native machinery. Once merged into a subject's
overlays.svg, they are read byquickflat, the WebGL viewer, and Inkscape.
How Dataviews work in Pycortex
mainDataviews are the primary objects used to represent neuroimaging data in different spaces.
Datasetacts as a container for these views.Core Dataview types include:
Volume: Volumetric data in original volume space.Vertex: Data projected onto surface vertices.VolumeRGB/VertexRGB: Data containing color channels (Red, Green, Blue, and optionally Alpha).Volume2D/Vertex2D: A pair of volumetric/vertex data displayed using a 2D colormap.
All dataviews support common keyword arguments for visualization:
cmap: Matplotlib colormap name.vmin/vmax: Colormap limits.description: Text shown as a subheading in WebGL views.priority: Order of inclusion in aDataset.**kwargs: Additional string-based attributes (e.g., options forcortex.quickflat.make_figure).
Understand the purpose and direction of transforms
mainIn pycortex, transforms are used to align functional data (e.g., fMRI sequences) with the anatomical surfaces. This is necessary because functional scans often have different scan parameters and larger distortions than the T1 anatomical scans used for surface generation.
Crucial Directionality: All transforms in pycortex always map from fiducial space to functional space.
Choose a brain surface flattening method
mainPyCortex supports three methods for transforming a 3D brain surface into a 2D flatmap. Choose based on your requirements for speed versus distortion:
- Freesurfer (Recommended): Uses Freesurfer's
mris_flatten. Produces high-quality flatmaps with minimal distortion. Time: ~2 hours per hemisphere. - SLIM: An experimental, very fast method using the SLIM algorithm. Tends to have more distortion. Requirement: Requires additional installation of the SLIM dependency.
- Blender: Uses Blender's UV unwrapping. Faster than Freesurfer but may introduce more distortion. Time: ~5-15 minutes per hemisphere.
- Freesurfer (Recommended): Uses Freesurfer's
Choose between Dynamic and Static WebGL viewers
mainPycortex provides two modes for interactive 3D brain visualization in a web browser:
- Dynamic Viewer: A temporary viewer hosted by the Python process that generated it. This is ideal for real-time interaction and bi-directional communication between Python and JavaScript.
- Static Viewer: A viewer saved permanently to disk. It generates a single web page with most resources embedded, making it easy to host on a web server (like nginx) or upload to repositories like Neurovault. This mode is intended for sharing visualizations that persist beyond the lifetime of the Python session.
Understand the subject database structure
mainThe pycortex filestore organizes subject data into a specific directory hierarchy. A typical subject entry (e.g.,
S1) contains the following subdirectories and files:anatomicals/: Raw anatomical scans (e.g.,raw.nii.gz).cache/: Cached computations like flatmasks and flatpixel data.overlays.svg: SVG files for surface overlays.rois.svg: SVG files defining surface ROIs.surface-info/: Quantifications such as Tissot's Indicatrix and flatmap surface distortion (e.g.,distortion[dist_type=areal].npz).surfaces/: Surface files in formats like.gii(e.g.,flat_lh.gii,inflated_lh.gii,pia_lh.gii,wm_lh.gii).transforms/: Transformation matrices and reference volumes (e.g.,fullhead/matrices.xfm).views/: Stored WebGL view parameters.
filestore/db └── S1 ├── anatomicals │ └── raw.nii.gz ├── cache │ ├── flatmask_1024.npz │ ├── flatpixel_fullhead_1024_nearest_l32.npz │ ├── flatverts_1024.npz │ └── fullhead_linenn.npz ├── overlays.svg ├── rois.svg ├── surface-info │ ├── distortion[dist_type=areal].npz │ └── distortion[dist_type=metric].npz ├── surfaces │ ├── flat_lh.gii │ ├── flat_rh.gii │ ├── inflated_lh.gii │ ├── inflated_rh.gii │ ├── pia_lh.gii │ ├── pia_rh.gii │ ├── wm_lh.gii │ └── wm_rh.gii ├── transforms │ ├── fullhead │ │ ├── matrices.xfm │ │ └── reference.nii.gz │ └── retinotopy │ ├── matrices.xfm │ └── reference.nii.gz └── views