Skip to content

ENH: Speed up and clean up 3D code#14016

Open
larsoner wants to merge 28 commits into
mne-tools:mainfrom
larsoner:cleanup
Open

ENH: Speed up and clean up 3D code#14016
larsoner wants to merge 28 commits into
mne-tools:mainfrom
larsoner:cleanup

Conversation

@larsoner

@larsoner larsoner commented Jul 3, 2026

Copy link
Copy Markdown
Member

While I was digging through mne-tools/mne-kit-gui#68, I realized our codebase uses a lot of older PyVista idioms that we can clean up. But that in turn inspired some other changes:

  1. Use PyVista 0.43+ APIs (like Lights) and remove cruft related to older versions, plus params/modes we never actually use (resolution, polygon_offset)
  2. Single-sensor plotting (e.g., with 300+ sensors) was a huge slowdown -- so now we group sensors by color and concat into a single mesh (very large plot_alignment speedup)
  3. Switched to MNE-based mesh decimation (~5s to plot a brain with a silhouette to ~1.4s) by default that makes use of the existing/known FreeSurfer mesh decimation
  4. Fixed a sneaky issue with foci removal that was causing cleanup errors depending on test run order (sometimes!)
  5. Streamlined the _assert_no_instances by avoiding multiple gc.collect() calls (by using request), and by keeping track of Brain instances directly when in testing mode (which greatly reduces the search surface when
  6. Moved assert_no_instances code out of MNE and into https://github.com/mne-tools/refleak and added them as doc-build and test deps

I used Claude Sonnet 5 to help with this (especially the profiling), but I understand and vouch for all code.

@wmvanvliet

wmvanvliet commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

At some point, it may be nice to split of all the referrer tracking code into its own package. This looks very useful also for debugging other projects with memory leaks.

@wmvanvliet wmvanvliet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some simple testing and plotting sensors is now much faster. Much appreciated. I just need to find a new way now to color selected sensors in the dipole fitting GUI.

@larsoner

larsoner commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

I just need to find a new way now to color selected sensors in the dipole fitting GUI.

Okay when coding this I wasn't 100% sure if this was needed. But it does provide a motivation for implemeting the vtkGlyphActor3D method -- that should make it much simpler to color individual actors. So let's not merge for now, I'll fix that here!

@larsoner

larsoner commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

At some point, it may be nice to split of all the referrer tracking code into its own package. This looks very useful also for debugging other projects with memory leaks.

Yeah pyvista and pyvistaqt use very similar code (I think I adapted it from MNE originally). I'll probably tackle that once this lands

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check the outputs soon and then merge assuming everything still works. @wmvanvliet the vtkGlypthActor3D has been implemented, and there's code in there that shows you can update the colors easily now by updating the point_scalars["colors"] array, and marking the object as modified!

Comment thread pyproject.toml Outdated
"pyvistaqt >= 0.11", # released 2023-06-30, no newer version available
"pyxdf",
"pyzmq != 24.0.0",
"refleak >=0.1",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wmvanvliet I've pushed this to https://github.com/mne-tools/refleak and https://pypi.org/project/refleak/#history and started using it here. I'll iterate until it works but hopefully it should. I'll make corresponding PRs to pyvistaqt and pyvista soon!

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.

2 participants