-
Notifications
You must be signed in to change notification settings - Fork 85
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
[WIP] add plot_anat_landmarks function #824
Draft
agramfort
wants to merge
11
commits into
mne-tools:main
Choose a base branch
from
agramfort:plot_landmarks
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.
Draft
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
94f1356
add plot_anat_landmarks function
agramfort 0593b58
Merge branch 'main' into plot_landmarks
agramfort bc728d9
Apply suggestions from code review
agramfort 8ebeadb
address some comments
agramfort 6dc7c45
Merge branch 'plot_landmarks' of https://github.com/agramfort/mne-bid…
agramfort ca17020
nitpicks
agramfort 3e0deb0
simplify + fix test
agramfort 2e7ee25
flake8
agramfort 19afd3c
Merge branch 'main' into plot_landmarks
sappelhoff f69e55a
Merge branch 'main' into plot_landmarks
sappelhoff 0c8f5d0
Merge branch 'main' into plot_landmarks
sappelhoff 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,38 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import mne | ||
from mne.datasets import testing | ||
from mne.utils import requires_nibabel, requires_version | ||
from mne_bids.viz import plot_anat_landmarks | ||
from mne_bids import BIDSPath, write_anat, get_anat_landmarks | ||
|
||
|
||
@requires_nibabel() | ||
@requires_version('nilearn', '0.6') | ||
def test_plot_anat_landmarks(tmpdir): | ||
"""Test writing anatomical data with pathlib.Paths.""" | ||
data_path = Path(testing.data_path()) | ||
raw_fname = data_path / 'MEG' / 'sample' / 'sample_audvis_trunc_raw.fif' | ||
trans_fname = str(raw_fname).replace('_raw.fif', '-trans.fif') | ||
raw = mne.io.read_raw_fif(raw_fname) | ||
trans = mne.read_trans(trans_fname) | ||
fs_subjects_dir = Path(data_path) / 'subjects' | ||
|
||
bids_root = Path(tmpdir) | ||
t1w_mgh_fname = fs_subjects_dir / 'sample' / 'mri' / 'T1.mgz' | ||
bids_path = BIDSPath(subject='01', session='mri', root=bids_root) | ||
bids_path = write_anat(t1w_mgh_fname, bids_path=bids_path, overwrite=True) | ||
|
||
with pytest.raises(ValueError, match='No landmarks available'): | ||
plot_anat_landmarks(bids_path, show=False) | ||
|
||
landmarks = get_anat_landmarks( | ||
t1w_mgh_fname, raw.info, trans, fs_subject='sample', | ||
fs_subjects_dir=fs_subjects_dir) | ||
|
||
bids_path = write_anat(t1w_mgh_fname, bids_path=bids_path, | ||
landmarks=landmarks, overwrite=True) | ||
fig = plot_anat_landmarks(bids_path, show=False) | ||
assert len(fig.axes) == 12 # 3 subplots + 3 x 3 MRI slices |
This file contains 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,65 @@ | ||
import json | ||
|
||
import numpy as np | ||
from scipy import linalg | ||
import nibabel as nib | ||
|
||
import mne | ||
|
||
|
||
def plot_anat_landmarks(bids_path, vmax=None, show=True): | ||
"""Plot anatomical landmarks attached to an MRI image. | ||
|
||
Parameters | ||
---------- | ||
bids_path : mne_bids.BIDSPath | ||
Path of the MRI image. | ||
vmax : float | ||
Maximum colormap value. | ||
show : bool | ||
Whether to show the figure after plotting. Defaults to ``True``. | ||
|
||
Returns | ||
------- | ||
fig : matplotlib.figure.Figure | ||
The figure object containing the plot. | ||
""" | ||
import matplotlib.pyplot as plt | ||
from nilearn.plotting import plot_anat | ||
|
||
nii = nib.load(str(bids_path)) | ||
|
||
json_path = bids_path.copy().update(extension=".json") | ||
|
||
n_landmarks = 0 | ||
if json_path.fpath.exists(): | ||
json_content = json.load(open(json_path)) | ||
coords_dict = json_content.get("AnatomicalLandmarkCoordinates", dict()) | ||
n_landmarks = len(coords_dict) | ||
|
||
if not n_landmarks: | ||
raise ValueError("No landmarks available with the image") | ||
|
||
for label in coords_dict: | ||
vox_pos = np.array(coords_dict[label]) | ||
ras_pos = mne.transforms.apply_trans(nii.affine, vox_pos) | ||
coords_dict[label] = ras_pos | ||
|
||
######################################################################## | ||
# Plot it with nilearn | ||
fig, axs = plt.subplots( | ||
n_landmarks, 1, figsize=(6, 2.3 * n_landmarks), | ||
facecolor="w") | ||
|
||
for point_idx, (label, ras_pos) in enumerate(coords_dict.items()): | ||
plot_anat( | ||
str(bids_path), axes=axs[point_idx], cut_coords=ras_pos, | ||
title=label, vmax=vmax, | ||
) | ||
|
||
plt.suptitle(bids_path.fpath.name) | ||
|
||
if show: | ||
fig.show() | ||
|
||
return fig |
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.
Should we limit the acceptable range, e.g. to the interval [0, 255]?