Skip to content

VertexRGB multiframe with NaN raises IndexError in alpha mask #629

@mvdoc

Description

@mvdoc

Summary

VertexRGB cannot be rendered when the channels are multiframe (shape (T, V)) and contain NaNs. The auto-created alpha is constructed as 1-D (V,), but the NaN mask derived from the channels is 2-D (T, V), so indexing fails.

This is an edge case — RGB movies are uncommon — but the failure mode is an unhandled IndexError rather than a clear error or correct behavior, so worth tracking.

Reproducer

import numpy as np
import cortex

T, V = 5, cortex.db.get_surf("S1", "fiducial", merge=True)[0].shape[0]
rng = np.random.default_rng(0)
r = rng.uniform(0, 1, (T, V))
g = rng.uniform(0, 1, (T, V))
b = rng.uniform(0, 1, (T, V))
# inject NaNs in one channel
r[rng.random((T, V)) < 0.2] = np.nan

vrgb = cortex.VertexRGB(r, g, b, "S1")
cortex.webgl.show(vrgb)  # raises

Traceback

File ".../cortex/dataset/viewRGB.py", line 860, in alpha
    alpha.data[mask] = alpha.vmin
    ~~~~~~~~~~^^^^^^
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Root cause

In cortex/dataset/viewRGB.py (VertexRGB.alpha property, ~lines 842–863):

@property
def alpha(self):
    alpha = self._alpha
    if alpha is None:
        alpha = np.ones(self.red.vertices.shape[1])    # 1-D, length V only
        alpha = Vertex(alpha, self.red.subject, vmin=0, vmax=1)
    ...
    rgb = np.array([self.red.data, self.green.data, self.blue.data])
    mask = np.isnan(rgb).any(axis=0)                   # (T, V) for multiframe
    alpha.data[mask] = alpha.vmin                      # IndexError

For multiframe input each channel's .data is (T, V), so rgb is (3, T, V) and mask is (T, V). The auto-built alpha is shape (V,), hence the dimensionality mismatch.

Suggested fix

Build the auto-alpha with the same shape as the channel data, e.g.:

alpha = np.ones_like(self.red.data)

(or some equivalent that handles both 1-D and 2-D channel data). A regression test with a (T, V) VertexRGB containing NaNs would help.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions