From 71a25b5b92c60615aa5fa041f2405aca66d4d042 Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 13:06:37 -0500 Subject: [PATCH 1/8] Update changelog.rst --- docs/changelog.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index c2c37ab5..72e23508 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -28,6 +28,30 @@ Change tags (adopted from `sklearn Date: Sat, 1 Mar 2025 14:30:52 -0500 Subject: [PATCH 2/8] Create coordinate_conversions.py --- naplib/localization/coordinate_conversions.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 naplib/localization/coordinate_conversions.py diff --git a/naplib/localization/coordinate_conversions.py b/naplib/localization/coordinate_conversions.py new file mode 100644 index 00000000..3f2affcb --- /dev/null +++ b/naplib/localization/coordinate_conversions.py @@ -0,0 +1,41 @@ +import numpy as np + +def mni152_to_fsaverage(coords): + """ + Convert 3D coordinates from MNI152 space to fsaverage space. + + Parameters + ---------- + coords : np.ndarray (elecs, 3) + Coordinates in MNI space + + Returns + ------- + new_coords : np.ndarray (elecs, 3) + Coordinates in fsaverage space + """ + old_coords = np.concatenate([coords, np.ones((len(coords),1))], axis=1).T + xform = np.array([[1.0022, 0.0071, -0.0177, 0.0528], [-0.0146, 0.999, 0.0027, -1.5519], [0.0129, 0.0094, 1.0027, -1.2012]]) + new_coords = (xform @ old_coords).T + return new_coords + +def fsaverage_to_mni152(coords): + """ + Convert 3D coordinates from fsaverage space to MNI152 space. + + Parameters + ---------- + coords : np.ndarray (elecs, 3) + Coordinates in fsaverage space + + Returns + ------- + new_coords : np.ndarray (elecs, 3) + Coordinates in MNI152 space + """ + old_coords = np.concatenate([coords, np.ones((len(coords),1))], axis=1).T + xform = np.array([[0.9975, -0.0073, 0.0176, -0.0429], [0.0146, 1.0009, -0.0024, 1.5496], [-0.013, -0.0093, 0.9971, 1.184]]) + new_coords = (xform @ old_coords).T + return new_coords + + From 94afd1579e589336a9ad279c3f22fe431bac2340 Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 14:31:29 -0500 Subject: [PATCH 3/8] Update localization __init__ --- naplib/localization/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/naplib/localization/__init__.py b/naplib/localization/__init__.py index af90f19b..7253b7cb 100644 --- a/naplib/localization/__init__.py +++ b/naplib/localization/__init__.py @@ -1,3 +1,4 @@ from .freesurfer import Brain, find_closest_vertices +from .coordinate_conversions import mni152_to_fsaverage, fsaverage_to_mni152 -__all__ = ['Brain', 'find_closest_vertices'] +__all__ = ['Brain', 'find_closest_vertices', 'mni152_to_fsaverage', 'fsaverage_to_mni152'] From 60742e1762f7c03875fda1d210fa16421c419e9c Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 14:32:40 -0500 Subject: [PATCH 4/8] Update localization.rst --- docs/references/localization.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/references/localization.rst b/docs/references/localization.rst index 1d7cfa2e..943f2a92 100644 --- a/docs/references/localization.rst +++ b/docs/references/localization.rst @@ -3,6 +3,17 @@ Localization .. currentmodule:: naplib.localization +Convert from MNI152 to FSAverage +-------------------------------- + +.. autofunction:: mni152_to_fsaverage + +Convert from FSAverage to MNI152 +-------------------------------- + +.. autofunction:: fsaverage_to_mni152 + + Localization on a Freesurfer Brain ---------------------------------- From 9698461ccb20af80d0e02b85d7cdca0694e7a95f Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 17:41:25 -0500 Subject: [PATCH 5/8] Create test_coordinate_conversions.py --- tests/test_coordinate_conversions.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/test_coordinate_conversions.py diff --git a/tests/test_coordinate_conversions.py b/tests/test_coordinate_conversions.py new file mode 100644 index 00000000..a19fe069 --- /dev/null +++ b/tests/test_coordinate_conversions.py @@ -0,0 +1,14 @@ +import numpy as np +from naplib.localization import mni152_to_fsaverage, fsaverage_to_mni152 + +def test_mni152_fsaverage_conversions(): + coords_tmp = np.array([[13.987, 36.5, 10.067], [-10.54, 24.5, 15.555]]) + coords_tmp2 = mni152_to_fsaverage(coords_tmp) + expected_2 = np.array( + [[ 14.15310394, 34.73510469, 9.41733728], + [-10.60987978, 23.11927315, 14.49010227]] + ) + assert np.allclose(coords_tmp2, expected_2) + + coords_tmp3 = fsaverage_to_mni152(coords_tmp2) + assert np.allclose(coords_tmp3, coords_tmp) From bd2b887feea6bac06c49a67674bca2b6d52673be Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 17:54:36 -0500 Subject: [PATCH 6/8] Update tests/test_coordinate_conversions.py --- tests/test_coordinate_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_coordinate_conversions.py b/tests/test_coordinate_conversions.py index a19fe069..215be706 100644 --- a/tests/test_coordinate_conversions.py +++ b/tests/test_coordinate_conversions.py @@ -11,4 +11,4 @@ def test_mni152_fsaverage_conversions(): assert np.allclose(coords_tmp2, expected_2) coords_tmp3 = fsaverage_to_mni152(coords_tmp2) - assert np.allclose(coords_tmp3, coords_tmp) + assert np.allclose(coords_tmp3, coords_tmp, rtol=1e-4) From 40decad5637b94f3f937904d886ee32b2d4dcf4c Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 17:54:41 -0500 Subject: [PATCH 7/8] Update tests/test_coordinate_conversions.py --- tests/test_coordinate_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_coordinate_conversions.py b/tests/test_coordinate_conversions.py index 215be706..63b3265b 100644 --- a/tests/test_coordinate_conversions.py +++ b/tests/test_coordinate_conversions.py @@ -8,7 +8,7 @@ def test_mni152_fsaverage_conversions(): [[ 14.15310394, 34.73510469, 9.41733728], [-10.60987978, 23.11927315, 14.49010227]] ) - assert np.allclose(coords_tmp2, expected_2) + assert np.allclose(coords_tmp2, expected_2, rtol=1e-4) coords_tmp3 = fsaverage_to_mni152(coords_tmp2) assert np.allclose(coords_tmp3, coords_tmp, rtol=1e-4) From c9b890d7d99571a1fb168b393873cb6cc02799e4 Mon Sep 17 00:00:00 2001 From: Gavin Mischler Date: Sat, 1 Mar 2025 18:37:21 -0500 Subject: [PATCH 8/8] Update test_coordinate_conversions.py --- tests/test_coordinate_conversions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_coordinate_conversions.py b/tests/test_coordinate_conversions.py index 63b3265b..aa976bbd 100644 --- a/tests/test_coordinate_conversions.py +++ b/tests/test_coordinate_conversions.py @@ -8,7 +8,7 @@ def test_mni152_fsaverage_conversions(): [[ 14.15310394, 34.73510469, 9.41733728], [-10.60987978, 23.11927315, 14.49010227]] ) - assert np.allclose(coords_tmp2, expected_2, rtol=1e-4) + assert np.allclose(coords_tmp2, expected_2, rtol=1e-3) coords_tmp3 = fsaverage_to_mni152(coords_tmp2) - assert np.allclose(coords_tmp3, coords_tmp, rtol=1e-4) + assert np.allclose(coords_tmp3, coords_tmp, rtol=1e-3)