Skip to content

NaN values in Volume/Vertex render as black instead of transparent #611

@mvdoc

Description

@mvdoc

Description

When using Volume.raw or Vertex.raw with manually set alpha channels, any NaN values in the data are rendered as solid black voxels/vertices instead of being transparent. This happens because the RGBA conversion maps NaN to black (R=0, G=0, B=0), and the alpha channel is set independently — so a voxel with NaN data but non-zero alpha shows up as an opaque black region.

This is particularly problematic when plotting thresholded maps where non-significant regions are set to NaN but should appear transparent (showing curvature only).

Minimal example

import cortex
import numpy as np

subject = "fsaverage"  # or any subject in your pycortex db

# Create random data with some NaN values
n_verts = cortex.db.get_surf(subject, "fiducial", "lh")[0].shape[0] * 2
data = np.random.randn(n_verts)
data[::3] = np.nan  # every 3rd vertex is NaN

# Set up alpha: all vertices semi-transparent
alpha = np.ones(n_verts) * 0.8

vtx = cortex.Vertex(data, subject, vmin=-2, vmax=2, cmap="RdBu_r")
vtx = vtx.raw
vtx.alpha = alpha

# NaN vertices render as black instead of transparent
fig = cortex.quickflat.make_figure(vtx, with_curvature=True)
fig.savefig("/tmp/pycortex_nan_black.png", dpi=150)

Expected behavior

NaN values should render as fully transparent (alpha=0), regardless of the alpha channel value. The curvature pattern should show through where data is NaN.

Current workaround

Manually zero out alpha where data is NaN before setting it:

nan_mask = ~np.isfinite(data)
data = np.where(nan_mask, 0, data)
alpha[nan_mask] = 0

Suggestion

The .raw property (or the RGBA conversion) could automatically set alpha=0 for NaN values, so users don't have to handle this manually every time they plot thresholded data.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions