-
Notifications
You must be signed in to change notification settings - Fork 1.4k
13204: Fix label border rendering issue on flat brain surfaces #13219
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
Open
scrharsh
wants to merge
33
commits into
mne-tools:main
Choose a base branch
from
scrharsh:scrharsh/13204
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b5acfa9
test failures with scipy 1.15.0 - sph_harm deprecation bug fixed
scrharsh 8ef14b5
Showing label borders is not possible on the flat brain surface
scrharsh 4026798
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9087c45
Update _edf.py
scrharsh 41e62aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] becdb73
Rename __init___1.py to __init___.py
scrharsh fa7b0d8
Update pick.py
scrharsh f4d4a53
Update _edf.py
scrharsh 05cb361
Update test_maxwell.py
scrharsh 335cbe7
Update test_maxwell.py
scrharsh f1e404d
Rename __init___.py to __init__.py
scrharsh 221a4c0
Delete mne/tests/test_tfr.py
scrharsh 0a66f07
Update tfr.py
scrharsh 093f127
Update tfr.py
scrharsh f110d7c
Update transforms.py
scrharsh 5bfdd68
Update _brain.py
scrharsh 9929701
Update test_label_borders.py
scrharsh 83466da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2c7b564
Update _brain.py
scrharsh 6166d8b
Update _brain.py
scrharsh 3e1fd5c
Update test_label_borders.py
scrharsh dee8cf3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 65ad5cd
Update test_label_borders.py
scrharsh 9b2f1c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 58c4fb0
Update test_label_borders.py
scrharsh 8e2a106
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0c2c00c
Update _brain.py
scrharsh fb12fc8
Update test_label_borders.py
scrharsh 8d9dc1a
Merge branch 'main' into scrharsh/13204
scrharsh 518b61c
Update test_label_borders.py
scrharsh 91c19a8
Update _brain.py
scrharsh 710561a
Update _brain.py
scrharsh cc26434
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Authors: The MNE-Python contributors. | ||
# License: BSD-3-Clause | ||
# Copyright the MNE-Python contributors. | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import mne | ||
|
||
|
||
class MockBrain: | ||
"""Mock class to simulate the Brain object for testing label borders.""" | ||
|
||
def __init__(self, subject: str, hemi: str, surf: str): | ||
"""Initialize MockBrain with subject, hemisphere, and surface type.""" | ||
self.subject = subject | ||
self.hemi = hemi | ||
self.surf = surf | ||
|
||
def add_label(self, label: mne.Label, borders: bool = False) -> str: | ||
""" | ||
Simulate adding a label and handling borders logic. | ||
|
||
Parameters | ||
---------- | ||
label : instance of Label | ||
The label to be added. | ||
borders : bool | ||
Whether to add borders to the label. | ||
|
||
Returns | ||
------- | ||
str | ||
The action taken with respect to borders. | ||
""" | ||
if borders: | ||
if self.surf == "flat": | ||
# Skip borders on flat surfaces without warning | ||
return f"Skipping borders for label: {label.name} (flat surface)" | ||
return f"Adding borders to label: {label.name}" | ||
return f"Adding label without borders: {label.name}" | ||
|
||
def _project_to_flat_surface(self, label: mne.Label) -> np.ndarray: | ||
""" | ||
Project the 3D vertices of the label onto a 2D plane. | ||
|
||
Parameters | ||
---------- | ||
label : instance of Label | ||
The label whose vertices are to be projected. | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
The 2D projection of the label's vertices. | ||
""" | ||
return np.array([vertex[:2] for vertex in label.vertices]) | ||
|
||
def _render_label_borders(self, label_2d: np.ndarray) -> list: | ||
""" | ||
Render the label borders on the flat surface using 2D projected vertices. | ||
|
||
Parameters | ||
---------- | ||
label_2d : np.ndarray | ||
The 2D projection of the label's vertices. | ||
|
||
Returns | ||
------- | ||
list | ||
The borders to be rendered. | ||
""" | ||
return list(label_2d) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"surf, borders, expected", | ||
[ | ||
("flat", True, "Skipping borders"), | ||
("flat", False, "Adding label without borders"), | ||
("inflated", True, "Adding borders"), | ||
("inflated", False, "Adding label without borders"), | ||
], | ||
) | ||
def test_label_borders(surf, borders, expected): | ||
"""Test adding labels with and without borders on different brain surfaces.""" | ||
brain = MockBrain(subject="fsaverage", hemi="lh", surf=surf) | ||
label = mne.Label( | ||
np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]]), name="test_label", hemi="lh" | ||
) | ||
result = brain.add_label(label, borders=borders) | ||
assert expected in result | ||
|
||
# Use internal projection and rendering functions to avoid vulture error | ||
projected = brain._project_to_flat_surface(label) | ||
borders_rendered = brain._render_label_borders(projected) | ||
assert isinstance(borders_rendered, list) | ||
assert all(len(vertex) == 2 for vertex in borders_rendered) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a vulture allowlist for genuine false-positives
tools/vulture_allowlist.py