Repository: mne-tools/mne-bids

Issue:
get_anat_landmarks() should accept BIDSPath as image param
The `image` parameter of `mne_bids.get_anat_landmarks()` currently cannot be a `BIDSPath`, even though it would feel very natural to pass a `BIDSPath` here. In fact, that's precisely what I did, only to run into an issue with `nibabel` I believe (I don't have the error message at hand). Workaround for me was to pass `image=bids_path.fpath`, but that's really something we could do automatically, don't you think?

cc @alexrockhill 


Discussion:
Yes, sound very reasonable and like a quick PR. I can do it if you're busy
That would be great! But no pressure :)
Ah in writing the tests, I think the reason it's not that way already is it that the image shouldn't likely be in BIDS format yet because you're going to pass it to `write_anat`. Similarly `write_anat` doesn't accept a `BIDSPath` either because it's a bit circular to ask for a BIDSPath when the goal of the function is to write something in BIDS. I can still make the PR and I think it's fine to support this but it makes confusing tests where you write to BIDS and then use that BIDS directory to write another one.

Resolving patch:
diff --git a/examples/convert_mri_and_trans.py b/examples/convert_mri_and_trans.py
--- a/examples/convert_mri_and_trans.py
+++ b/examples/convert_mri_and_trans.py
@@ -29,7 +29,7 @@
 
 """
 # Authors: Stefan Appelhoff <stefan.appelhoff@mailbox.org>
-#          Alex Rockhill <aprockhill206@gmail.com>
+#          Alex Rockhill <aprockhill@mailbox.org>
 #          Alex Gramfort <alexandre.gramfort@inria.fr>
 #
 # License: BSD (3-clause)
diff --git a/mne_bids/write.py b/mne_bids/write.py
--- a/mne_bids/write.py
+++ b/mne_bids/write.py
@@ -1620,7 +1620,7 @@ def get_anat_landmarks(image, info, trans, fs_subject, fs_subjects_dir=None):
 
     Parameters
     ----------
-    image : str | pathlib.Path | NibabelImageObject
+    image : str | pathlib.Path | mne_bids.BIDSPath | NibabelImageObject
         Path to an MRI scan (e.g. T1w) of the subject. Can be in any format
         readable by nibabel. Can also be a nibabel image object of an
         MRI scan. Will be written as a .nii.gz file.
@@ -1674,6 +1674,8 @@ def get_anat_landmarks(image, info, trans, fs_subject, fs_subjects_dir=None):
     landmarks = _mri_landmarks_to_mri_voxels(landmarks, t1w_mgh)
     # go to T1 scanner space from T1 voxel space
     landmarks = _mri_voxels_to_mri_scanner_ras(landmarks, t1w_mgh)
+    if isinstance(image, BIDSPath):
+        image = image.fpath
     img_nii = _load_image(image, name='image')
     img_mgh = nib.MGHImage(img_nii.dataobj, img_nii.affine)
     landmarks = _mri_scanner_ras_to_mri_voxels(landmarks, img_mgh)


Test patch:
diff --git a/mne_bids/tests/test_write.py b/mne_bids/tests/test_write.py
--- a/mne_bids/tests/test_write.py
+++ b/mne_bids/tests/test_write.py
@@ -1808,6 +1808,10 @@ def test_write_anat(_bids_validate, tmpdir):
     fvox2 = flash2.get_fdata()
     assert_array_equal(fvox1, fvox2)
 
+    # test that we can now use a BIDSPath to use the landmarks
+    landmarks = get_anat_landmarks(bids_path, raw.info, trans_fname,
+                                   'sample', op.join(data_path, 'subjects'))
+
 
 def test_write_raw_pathlike(tmpdir):
     data_path = testing.data_path()