Skip to content

Commit 1e41d1c

Browse files
author
Joan Massich
committed
wip
1 parent 0024040 commit 1e41d1c

2 files changed

Lines changed: 122 additions & 6 deletions

File tree

mne/channels/tests/test_standard_montage.py

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,140 @@ def test_standard_montages_in_head(kind, tol):
115115

116116

117117
from mne.channels.montage import transform_to_head
118-
119-
@pytest.mark.parametrize('kind', _BUILT_IN_MONTAGES)
120-
def test_foo(kind):
118+
from pytest import approx
119+
120+
@pytest.mark.parametrize('kind, foo', [
121+
# XXX All should be 0.085 but they are not !!
122+
['EGI_256', 0.08500001],
123+
['easycap-M1', 0.08499999999999999],
124+
['easycap-M10', 0.08499999999999999],
125+
['GSN-HydroCel-128', 9.763325532616348],
126+
['GSN-HydroCel-129', 9.781833508100744],
127+
['GSN-HydroCel-256', 10.53120179308986],
128+
['GSN-HydroCel-257', 10.542564039112401],
129+
['GSN-HydroCel-32', 9.334690825727204],
130+
['GSN-HydroCel-64_1.0', 11.375727506868348],
131+
['GSN-HydroCel-65_1.0', 11.41411195568285],
132+
['biosemi128', 103.13293097944218],
133+
['biosemi16', 102.54836114601703],
134+
['biosemi160', 103.24734353529684],
135+
['biosemi256', 102.31834042785782],
136+
['biosemi32', 102.66433014370907],
137+
['biosemi64', 101.87617188729301],
138+
['mgh60', 0.11734227421583884],
139+
['mgh70', 0.11808759279592418],
140+
['standard_1005', 0.1171808880579489],
141+
['standard_1020', 0.11460403303216726],
142+
['standard_alphabetic', 0.12012639557866846],
143+
['standard_postfixed', 0.11887390168465949],
144+
['standard_prefixed', 0.11675854869450944],
145+
['standard_primed', 0.11887390168465949],
146+
])
147+
def test_foo(kind, foo):
121148
"""Test standard montage properties (ie: they form a head)."""
122149
montage = read_standard_montage(kind)
123150
# import pdb; pdb.set_trace()
124151
montage = transform_to_head(montage) if montage._coord_frame != 'head' else montage # noqa
125152
eeg_loc = np.array([ch['r'] for ch in _get_dig_eeg(montage.dig)])
126-
# print(np.linalg.norm(eeg_loc, axis=1))
127153

128-
print(np.linalg.norm(eeg_loc, axis=1).mean())
129154
# assert_allclose(
130155
# actual=np.linalg.norm(eeg_loc, axis=1),
131156
# desired=np.full((eeg_loc.shape[0], ), EXPECTED_HEAD_SIZE),
132157
# atol=1e-2 # Use a high tolerance for now # tol,
133158
# )
159+
assert np.linalg.norm(eeg_loc, axis=1).mean() == approx(foo)
160+
161+
162+
import matplotlib.pyplot as plt
163+
from mne.channels._dig_montage_utils import _get_fid_coords
164+
from mne.transforms import _sph_to_cart
165+
166+
167+
def _plot_dig_transformation(transformed, original):
168+
EXPECTED_HEAD_SIZE = 0.085
169+
from mne.viz.backends.renderer import _Renderer
170+
def get_data(montage):
171+
data, coord_frame = _get_fid_coords(montage.dig)
172+
data['eeg'] = np.array([ch['r'] for ch in _get_dig_eeg(montage.dig)])
173+
data['coord_frame'] = coord_frame
174+
175+
return data
176+
177+
def _plot_fid_coord(renderer, data, color):
178+
renderer.tube(
179+
# origin=data.lpa, # XXX: why I cannot pas a (3,) ?
180+
origin=np.atleast_2d(data.lpa),
181+
destination=np.atleast_2d(data.rpa),
182+
# color='red', # XXX: why I cannot do that?
183+
color=color,
184+
radius=0.001, # XXX: why radious=1 which is default does not work?
185+
)
186+
renderer.tube(
187+
origin=np.atleast_2d((data.lpa+data.rpa)/2),
188+
destination=np.atleast_2d(data.nasion),
189+
color=color,
190+
radius=0.001, # XXX: why radious=1 which is default does not work?
191+
)
192+
193+
ren = _Renderer()
194+
ren.sphere(
195+
center=np.array([0, 0, 0]),
196+
# color=(100, 100, 100), # XXX: is color (R,G,B) 0-255? doc needs rev.
197+
color=(1.0, 1.0, 1.0), # XXX: doc don't say [0-1 or 0-255] ??
198+
scale=EXPECTED_HEAD_SIZE, # XXX: why I cannot put radius a value in mm?? # noqa
199+
opacity=0.5,
200+
resolution=8, # XXX: why this is not callen n_poligons??
201+
backface_culling=False,
202+
)
203+
N_RAND_PTS = 50
204+
ren.sphere(
205+
center=_sph_to_cart(np.stack(
206+
[np.full((N_RAND_PTS,), EXPECTED_HEAD_SIZE),
207+
np.random.rand(N_RAND_PTS) * 3 * 3.1415,
208+
np.random.rand(N_RAND_PTS) * 3 * 3.1415,
209+
],
210+
axis=-1,
211+
)),
212+
color=(1.0, 1.0, 1.0),
213+
scale=0.001
214+
)
215+
216+
orig_data = get_data(original)
217+
trans_data = get_data(transformed)
218+
219+
for oo, tt in zip(orig_data.eeg, trans_data.eeg):
220+
ren.tube(
221+
origin=np.atleast_2d(oo),
222+
destination=np.atleast_2d(tt),
223+
color=(.0, .1, .0),
224+
radius=0.0005,
225+
)
226+
227+
_plot_fid_coord(ren, orig_data, (1.0, 0, 0))
228+
ren.sphere(center=orig_data.eeg, color=(1.0, .0, .0), scale=0.001)
229+
230+
_plot_fid_coord(ren, trans_data, (0, 0, 1.0))
231+
ren.sphere(center=trans_data.eeg, color=(.0, .0, 1.0), scale=0.001)
232+
233+
234+
ren.show()
235+
236+
# import pdb; pdb.set_trace()
237+
pass
238+
134239

135240

136241
def test_bar():
137242
"""Test bar."""
243+
plt.switch_backend('Qt5Agg')
244+
245+
# kind = 'EGI_256'
138246
kind = 'mgh60'
247+
montage = read_standard_montage(kind)
248+
trf_montage = transform_to_head(montage)
249+
250+
eeg_loc = np.array([ch['r'] for ch in _get_dig_eeg(montage.dig)])
251+
252+
_plot_dig_transformation(trf_montage, montage)
253+
254+
import pdb; pdb.set_trace()

mne/viz/_3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
FIDUCIAL_ORDER = (FIFF.FIFFV_POINT_LPA, FIFF.FIFFV_POINT_NASION,
4949
FIFF.FIFFV_POINT_RPA)
5050

51-
51+
# XXX: to unify with digitization
5252
def _fiducial_coords(points, coord_frame=None):
5353
"""Generate 3x3 array of fiducial coordinates."""
5454
points = points or [] # None -> list

0 commit comments

Comments
 (0)