Skip to content

Commit 6fe298f

Browse files
Add coordinate converters between MNI152 and FSAverage (#212)
* Update changelog.rst * Create coordinate_conversions.py * Update localization __init__ * Update localization.rst * Create test_coordinate_conversions.py * Update tests/test_coordinate_conversions.py * Update tests/test_coordinate_conversions.py * Update test_coordinate_conversions.py
1 parent c8d944b commit 6fe298f

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

docs/changelog.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ Change tags (adopted from `sklearn <https://scikit-learn.org/stable/whats_new/v0
2828

2929
- |API| : you will need to change your code to have the same effect in the future; or a feature will be removed in the future.
3030

31+
Version 2.5.0
32+
-------------
33+
- |Feature| : Add functions to convert electrode coordinates between MNI152 and fsaverage space, with ``localization.mni152_to_fsaverage`` and ``localization.fsaverage_to_mni152``.
34+
35+
Version 2.4.0
36+
-------------
37+
- |Enhancement| : Adds the ability to set vmin and vmax to ``plot_brain_overlay``.
38+
39+
Version 2.3.0
40+
-------------
41+
- |MajorFeature| : Adds the ability to interpolate electrode data onto the brain surface using ``interpolate_electrodes_onto_brain`` as well as enhanced functionality of the ``plot_brain_overlay`` function to plot such data.
42+
43+
Version 2.2.0
44+
-------------
45+
- |Enhancement| : Adds the ``del`` keyword functionality to the Data object and its sub-components.
46+
- |Enhancement| : Enhance the capability of local electrode rereferencing.
47+
48+
49+
Version 2.1.0
50+
-------------
51+
- |Feature| : Allow the ``Brain`` class to work with MNI152 brains.
52+
- |Enhancement| : Allow ``plot_brain_elecs`` to work with varied opacity per electrodes.
53+
- |Fix| : Fix a bug in responsiveness_ttest.
54+
3155
Version 2.0.0
3256
-------------
3357
- |MajorFeature| : Added the ``Brain`` class for representing a brain to the ``naplib.localization`` module. This can be used to plot intracranial electrodes on static or interactive 3D brain plots with the ``naplib.visualization module``, estimate anatomical locations of electrodes, calculate statistics on brains, and more. See Examples for illustrations of use-cases.

docs/references/localization.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ Localization
33

44
.. currentmodule:: naplib.localization
55

6+
Convert from MNI152 to FSAverage
7+
--------------------------------
8+
9+
.. autofunction:: mni152_to_fsaverage
10+
11+
Convert from FSAverage to MNI152
12+
--------------------------------
13+
14+
.. autofunction:: fsaverage_to_mni152
15+
16+
617
Localization on a Freesurfer Brain
718
----------------------------------
819

naplib/localization/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .freesurfer import Brain, find_closest_vertices
2+
from .coordinate_conversions import mni152_to_fsaverage, fsaverage_to_mni152
23

3-
__all__ = ['Brain', 'find_closest_vertices']
4+
__all__ = ['Brain', 'find_closest_vertices', 'mni152_to_fsaverage', 'fsaverage_to_mni152']
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
3+
def mni152_to_fsaverage(coords):
4+
"""
5+
Convert 3D coordinates from MNI152 space to fsaverage space.
6+
7+
Parameters
8+
----------
9+
coords : np.ndarray (elecs, 3)
10+
Coordinates in MNI space
11+
12+
Returns
13+
-------
14+
new_coords : np.ndarray (elecs, 3)
15+
Coordinates in fsaverage space
16+
"""
17+
old_coords = np.concatenate([coords, np.ones((len(coords),1))], axis=1).T
18+
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]])
19+
new_coords = (xform @ old_coords).T
20+
return new_coords
21+
22+
def fsaverage_to_mni152(coords):
23+
"""
24+
Convert 3D coordinates from fsaverage space to MNI152 space.
25+
26+
Parameters
27+
----------
28+
coords : np.ndarray (elecs, 3)
29+
Coordinates in fsaverage space
30+
31+
Returns
32+
-------
33+
new_coords : np.ndarray (elecs, 3)
34+
Coordinates in MNI152 space
35+
"""
36+
old_coords = np.concatenate([coords, np.ones((len(coords),1))], axis=1).T
37+
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]])
38+
new_coords = (xform @ old_coords).T
39+
return new_coords
40+
41+

tests/test_coordinate_conversions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
from naplib.localization import mni152_to_fsaverage, fsaverage_to_mni152
3+
4+
def test_mni152_fsaverage_conversions():
5+
coords_tmp = np.array([[13.987, 36.5, 10.067], [-10.54, 24.5, 15.555]])
6+
coords_tmp2 = mni152_to_fsaverage(coords_tmp)
7+
expected_2 = np.array(
8+
[[ 14.15310394, 34.73510469, 9.41733728],
9+
[-10.60987978, 23.11927315, 14.49010227]]
10+
)
11+
assert np.allclose(coords_tmp2, expected_2, rtol=1e-3)
12+
13+
coords_tmp3 = fsaverage_to_mni152(coords_tmp2)
14+
assert np.allclose(coords_tmp3, coords_tmp, rtol=1e-3)

0 commit comments

Comments
 (0)