gallantlab / pycortex

Pycortex is a python-based toolkit for surface visualization of fMRI data
https://gallantlab.github.io/pycortex
BSD 2-Clause "Simplified" License
581 stars 137 forks source link

NF Add functions to project volume data to surface while dealing with NaNs. #539

Closed mvdoc closed 3 months ago

mvdoc commented 3 months ago

These new functions can be used to project data from the volume to any arbitrary surface via a combination of pycortex mappers and mri_surf2surf. The projection deals with nans similar to quickshow and nanproject=True.

Example code:

import cortex
from cortex.mapper import vol2surf
import numpy as np

subject = ...  # anonymized
xfm = ...  # anonymized

vol = cortex.Volume.random(subject, xfm, mask="thick")
mask = cortex.db.get_mask(subject, xfm)
data = vol.data[mask]
data[np.random.randn(*data.shape) > 0] = np.nan

new_volume = cortex.Volume(data, subject, xfm, mask="thick")
surface = cortex.Vertex(vol2surf(data, subject, xfm), subject)
fsaverage = cortex.Vertex(vol2surf(data, subject, xfm, target_surface="fsaverage"), "fsaverage")

fig = cortex.quickshow(new_volume, with_colorbar=False, with_curvature=True, with_rois=False, with_labels=False, nanmean=False,)
fig.suptitle("volume, nanmean=False", fontsize=24)
fig = cortex.quickshow(new_volume, with_colorbar=False, with_curvature=True, with_rois=False, with_labels=False, nanmean=True,)
fig.suptitle("volume, nanmean=True", fontsize=24)
fig = cortex.quickshow(surface, with_colorbar=False, with_curvature=True, with_rois=False, with_labels=False,)
fig.suptitle("vol2surf", fontsize=24)
fig = cortex.quickshow(fsaverage, with_colorbar=False, with_curvature=True, with_rois=False, with_labels=False,)
fig.suptitle("fsaverage", fontsize=24)

Outputs:

image image image image