Skip to content

_LayeredMesh._compute_over can produce NaN colors when composed alpha is zero #13712

Closed

Description

@PragnyaKhandelwal

Description

When composing overlays in _LayeredMesh._compute_over, RGB channels are divided by resulting alpha.
If resulting alpha is zero for any vertex, this can create NaN values in colors and runtime warnings.

Minimal Repro

  • Compose two overlays where final alpha is zero for some vertices.
  • Call layered update path (add_overlay / update).

Expected

No NaNs in composed RGBA; zero-alpha vertices should remain stable.

Actual

NaN values can appear in RGB channels for zero-alpha vertices.

Suspected Location

  • mne/viz/_3d_overlay.py (_LayeredMesh._compute_over)

Proposed Fix

Guard division for zero-alpha rows (e.g., divide only where alpha > 0, set RGB=0 otherwise), then clip.

Acceptance Criteria

  • No NaNs in composed colors.
  • Existing overlay behavior unchanged for alpha > 0.
  • Regression test added for zero-alpha composition path.

Steps to reproduce

Paste this MWE (no dataset required; it reproduces at the core compositing function level):

import numpy as np
from mne.viz._3d_overlay import _LayeredMesh

mesh = _LayeredMesh(
    renderer=None,
    vertices=np.zeros((3, 3), float),
    triangles=np.array([[0, 1, 2]], int),
    normals=np.zeros((3, 3), float),
)

# Both layers have alpha=0 at all vertices
B = np.array([[1.0, 0.0, 0.0, 0.0],
              [0.0, 1.0, 0.0, 0.0],
              [0.0, 0.0, 1.0, 0.0]])
A = np.array([[0.2, 0.2, 0.2, 0.0],
              [0.4, 0.4, 0.4, 0.0],
              [0.6, 0.6, 0.6, 0.0]])

C = mesh._compute_over(B, A)
print(C)
print("Has NaN:", np.isnan(C).any())

Link to data

No response

Expected results

  • No runtime warning.
  • For vertices with resulting alpha = 0, output should stay finite (e.g., RGB set to 0) and alpha remain 0.
  • Has NaN should be False.

Actual results

  • Runtime warning appears from division by zero during compositing.
  • Output can contain NaN values in RGB channels for zero-alpha vertices.
  • Has NaN is True.

Additional information

Platform Windows-10-10.0.19045-SP0
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
Executable c:\MyData\2nd Year\SEM_4\GSOC-26\mne-python.venv\Scripts\python.exe
CPU 11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz (4 cores)
Memory 7.7 GiB

Core
X mne 1.1.0.dev1458+gc8132bea6 (outdated, release 1.11.0 is available!)

  • numpy 2.2.6 (unknown linalg bindings (threadpoolctl module not found: No module named 'threadpoolctl'))
  • scipy 1.13.1
  • matplotlib 3.10.8 (backend=qtagg)

Numerical (optional)

  • nibabel 5.3.3
  • unavailable sklearn, numba, nilearn, dipy, openmeeg, cupy, pandas, h5io, h5py

Visualization (optional)

  • pyvista 0.47.1 (OpenGL 4.5.0 - Build 27.20.100.8935 via Intel(R) UHD Graphics)
  • pyvistaqt 0.11.3
  • vtk 9.6.0
  • qtpy 2.4.3 (PyQt5=5.15.2)
  • unavailable ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)

  • mffpy 0.10.0
  • defusedxml 0.7.1
  • unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, curryreader, pybv, pymef, antio

Activity

  1. PragnyaKhandelwal commented on Mar 2, 2026

    @PragnyaKhandelwal
    ContributorAuthor

    I’ve started working on this bug and opened a fix branch: fix-overlay-composite-zero-alpha-nan. I reproduced the issue on both main and stable 1.11.0 (NaNs from divide-by-zero in _LayeredMesh._compute_over when composed alpha is zero), implemented a fix in the compositing path, and added a regression test to ensure zero-alpha composition stays finite (no NaNs). I’ll open a PR shortly and link it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions