Skip to content

FIX NaN values in Volume/Vertex rendering as black instead of transparent#612

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/fix-nan-values-rendering
Draft

FIX NaN values in Volume/Vertex rendering as black instead of transparent#612
Copilot wants to merge 7 commits intomainfrom
copilot/fix-nan-values-rendering

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

NaN values in Volume.raw / Vertex.raw render as opaque black instead of transparent. After colormap conversion to uint8, NaN information is lost, so the alpha getter's np.isnan(rgb) check on uint8 data is always False. Additionally, Vertex objects with NaN data render as colored (not transparent) in the WebGL viewer due to missing NaN detection in the surface_vertex shader.

vtx = cortex.Vertex(data_with_nans, subject, vmin=-2, vmax=2, cmap="RdBu_r")
raw = vtx.raw
raw.alpha = np.ones(n_verts) * 0.8  # NaN positions rendered as black

Changes

  • Dataview.raw: Capture NaN mask before uint8 conversion, explicitly zero out alpha for NaN positions
  • Volume.raw / Vertex.raw: Store _nan_mask on the returned RGB object so it survives alpha overrides
  • VolumeRGB.alpha / VertexRGB.alpha getters: Apply stored _nan_mask to enforce alpha=0 for NaN positions, even after user sets a custom alpha channel
  • surface_vertex WebGL shader: Add NaN detection in the colormap lookup using the GLSL idiom cuv.x != cuv.x. When vertex data is NaN, set vColor = vec4(0.) so the fragment shader discards the vertex. Handles both 1D and 2D cases. The surface_pixel shader already had NaN detection via colorlut(), but surface_vertex was missing it.
  • Tests: Four new tests covering default and user-overridden alpha for both Volume and Vertex

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI and others added 5 commits March 27, 2026 19:56
…rtex raw

When using Volume.raw or Vertex.raw, NaN values in data were rendered as
opaque black because: (1) colormap conversion maps NaN to black RGB, and
(2) after uint8 conversion, the NaN info is lost so the alpha getter's
NaN detection fails.

Fix:
- In Dataview.raw: explicitly set alpha=0 for NaN positions before
  returning the RGBA channels
- In Volume.raw/Vertex.raw: store a _nan_mask on the RGB object
- In VolumeRGB/VertexRGB alpha getters: apply stored _nan_mask to
  enforce alpha=0 for NaN positions, even after user alpha override

Agent-Logs-Url: https://github.com/gallantlab/pycortex/sessions/a5d0ee68-41d6-4451-8ed5-a84b060a457f

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NaN values rendering as black in Volume and Vertex Fix NaN values in Volume/Vertex rendering as black instead of transparent Mar 27, 2026
Copilot AI requested a review from mvdoc March 27, 2026 20:05
@mvdoc mvdoc changed the title Fix NaN values in Volume/Vertex rendering as black instead of transparent FIX NaN values in Volume/Vertex rendering as black instead of transparent Mar 27, 2026
@mvdoc mvdoc closed this Mar 28, 2026
@mvdoc mvdoc reopened this Apr 2, 2026
@mvdoc
Copy link
Copy Markdown
Contributor

mvdoc commented Apr 3, 2026

This is possibly related to #455

@mvdoc
Copy link
Copy Markdown
Contributor

mvdoc commented Apr 3, 2026

@copilot check if issue #455 is related and if so address both

…ssue #455)

Add NaN detection in the surface_vertex shader's colormap lookup.
When vertex data contains NaN, the normalized colormap coordinates
(cuv.x/cuv.y) become NaN. Use the GLSL idiom (x != x) to detect
NaN and set vColor to vec4(0.) (fully transparent) so NaN vertices
are discarded instead of rendered as colored.

Agent-Logs-Url: https://github.com/gallantlab/pycortex/sessions/4091acc2-7dc4-4b78-bd80-52e45c73d1ea

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 3, 2026

@copilot check if issue #455 is related and if so address both

Yes, issue #455 is directly related. It describes two problems:

  1. VertexRGB NaN values show as black on quickflat — Already fixed by this PR's Python-side changes (capturing NaN mask before uint8 conversion and enforcing alpha=0).

  2. Vertex NaN values show as colored (not transparent) on WebGL — The surface_pixel shader already had NaN detection in colorlut() (via the valid check), but the surface_vertex shader was missing it. When data0 is NaN, the colormap coordinate cuv.x becomes NaN, and texture2D with NaN coordinates returns an opaque color instead of transparent.

Fixed in 85f1d5b by adding NaN detection in the surface_vertex shader using the GLSL idiom cuv.x != cuv.x (true only for NaN), which sets vColor = vec4(0.) so the fragment shader's existing if (vColor.a < .001) discard; check properly discards NaN vertices. Also handles the 2D case with a cuv.y check.

Copilot finished work on behalf of mvdoc April 3, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants