Skip to content

Commit 7a7ada4

Browse files
BUG: prevent NaNs in layered overlay compositing when resulting alpha is zero (#13714)
Co-authored-by: Marijn van Vliet <w.m.vanvliet@gmail.com>
1 parent c4b1851 commit 7a7ada4

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

.mailmap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ Pierre-Antoine Bannier <pierreantoine.bannier@gmail.com> Pierre-Antoine Bannier
291291
Pierre-Antoine Bannier <pierreantoine.bannier@gmail.com> Pierre-Antoine Bannier <pierre-antoine@MacBook-Pro-de-Pierre-Antoine.local>
292292
Pierre-Antoine Bannier <pierreantoine.bannier@gmail.com> Pierre-Antoine Bannier <pierre-antoine@mbpdepieantoine.home>
293293
Ping-Keng Jao <ping-keng.jao@alumni.epfl.ch> nafraw <ping-keng.jao@alumni.epfl.ch>
294+
Pragnya Khandelwal <prag1704@gmail.com> Pragnya <prag1704@gmail.com>
295+
Pragnya Khandelwal <prag1704@gmail.com> PragnyaKhandelwal <prag1704@gmail.com>
294296
Praveen Sripad <pravsripad@gmail.com> prav <prav@prav-dell.(none)>
295297
Praveen Sripad <pravsripad@gmail.com> prav <pravsripad@gmail.com>
296298
Proloy Das <pdas6@mgh.harvard.edu> pdas6 <pdas6@mgh.harvard.edu>

doc/changes/dev/13714.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug in 3D overlay compositing that could produce NaN RGBA values when the resulting alpha is zero, by `Pragnya Khandelwal`_.

mne/viz/_3d_overlay.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ def _compute_over(self, B, A):
119119
C[:, :3] *= A_w
120120
C[:, :3] += B[:, :3] * B_w
121121
C[:, 3:] += B_w
122-
C[:, :3] /= C[:, 3:]
122+
C_alpha_zero = C[:, 3] == 0
123+
C[~C_alpha_zero, :3] /= C[~C_alpha_zero, 3:]
124+
C[C_alpha_zero, :3] = 0
123125
return np.clip(C, 0, 1, out=C)
124126

125127
def _compose_overlays(self):

mne/viz/_brain/tests/test_brain.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,27 @@ def test_layered_mesh(renderer_interactive_pyvistaqt):
138138
assert len(mesh._overlays) == 0
139139
mesh.add_overlay(
140140
scalars=np.array([0, 1, 1, 0]),
141-
colormap=np.array([(1, 1, 1, 1), (0, 0, 0, 0)]),
141+
colormap=np.array([(255, 255, 255, 255), (0, 0, 0, 0)]),
142142
rng=[0, 1],
143143
opacity=None,
144144
name="test1",
145145
)
146-
assert mesh._current_colors is not None
146+
assert_array_equal(
147+
mesh._current_colors, [[1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1]]
148+
)
147149
assert mesh._cached_colors is None
148150
assert len(mesh._overlays) == 1
149151
assert "test1" in mesh._overlays
150152
mesh.add_overlay(
151-
scalars=np.array([1, 0, 0, 1]),
152-
colormap=np.array([(1, 1, 1, 1), (0, 0, 0, 0)]),
153+
scalars=np.array([1, 1, 0, 0]),
154+
colormap=np.array([(255, 255, 255, 255), (0, 0, 0, 0)]),
153155
rng=[0, 1],
154156
opacity=None,
155157
name="test2",
156158
)
157-
assert mesh._current_colors is not None
159+
assert_array_equal(
160+
mesh._current_colors, [[1, 1, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
161+
)
158162
assert mesh._cached_colors is not None
159163
assert len(mesh._overlays) == 2
160164
assert "test2" in mesh._overlays

0 commit comments

Comments
 (0)