Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Fix mne.viz.Brain.add_volume_labels matrix ordering bug #11730

Merged
merged 10 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Enhancements
Bugs
~~~~
- Extended test to highlight bug in :func:`mne.stats.permutation_t_test` (:gh:`11575` by :newcontrib:`Joshua Calder-Travis`)
- Fix bug where :meth:`mne.viz.Brain.add_volume_labels` used an incorrect orientation (:gh:`11730` by `Alex Rockhill`_)
- Fix bug with :func:`mne.forward.restrict_forward_to_label` where cortical patch information was not adjusted (:gh:`11694` by `Eric Larson`_)
- Fix bug with PySide6 compatibility (:gh:`11721` by `Eric Larson`_)
- Fix hanging interpreter with matplotlib figures using ``mne/viz/_mpl_figure.py`` in spyder console and jupyter notebooks`(:gh:`11696` by `Mathieu Scheltienne`_)
Expand Down
18 changes: 11 additions & 7 deletions mne/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,14 @@ def _marching_cubes(image, level, smooth=0, fill_hole_size=None, use_flying_edge
f"{level.size} elements"
)

# vtkImageData indexes as slice, row, col (Z, Y, X):
# https://discourse.vtk.org/t/very-confused-about-imdata-matrix-index-order/6608/2
# We can accomplish his by requiring F continuity and raveling later.
# We also pass double as passing integer types directly can be problematic!
image = np.asfortranarray(image, dtype=float)
image_shape = image.shape
assert image.flags.f_contiguous

# fill holes
if fill_hole_size is not None:
image = image.copy() # don't modify original
Expand All @@ -1926,11 +1934,7 @@ def _marching_cubes(image, level, smooth=0, fill_hole_size=None, use_flying_edge
bin_image = binary_dilation(bin_image, iterations=fill_hole_size, mask=mask)
image[bin_image] = val

# force double as passing integer types directly can be problematic!
image_shape = image.shape
# use order='A' to automatically detect when Fortran ordering is needed
data_vtk = numpy_to_vtk(image.ravel(order="A").astype(float), deep=True)
del image
data_vtk = numpy_to_vtk(image.ravel(order="F"), deep=False)

mc = vtkDiscreteFlyingEdges3D() if use_flying_edges else vtkDiscreteMarchingCubes()
# create image
Expand Down Expand Up @@ -1978,8 +1982,8 @@ def _marching_cubes(image, level, smooth=0, fill_hole_size=None, use_flying_edge
polydata = geometry.GetOutput()
rr = vtk_to_numpy(polydata.GetPoints().GetData())
tris = vtk_to_numpy(polydata.GetPolys().GetConnectivityArray()).reshape(-1, 3)
rr = np.ascontiguousarray(rr[:, ::-1])
tris = np.ascontiguousarray(tris[:, ::-1])
rr = np.ascontiguousarray(rr)
tris = np.ascontiguousarray(tris)
out.append((rr, tris))
return out

Expand Down