Skip to content

Commit

Permalink
fix tests after bug fixes in scan RAS conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrockhill committed Dec 6, 2023
1 parent dfdc492 commit 5e3804d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
4 changes: 2 additions & 2 deletions mne_gui_addons/_ieeg_locate.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,13 @@ def mark_channel(self, ch=None):
self._chs[name][:] = self._ras
else:
neighbors = _voxel_neighbors(
self._vox,
apply_trans(self._scan_ras_ras_vox_t, self._ras),
self._ct_data,
thresh=_VOXEL_NEIGHBORS_THRESH,
voxels_max=self._radius**3,
use_relative=True,
)
self._chs[name][:] = apply_trans( # to surface RAS
self._chs[name][:] = apply_trans(
self._ras_vox_scan_ras_t, np.array(list(neighbors)).mean(axis=0)
)
self._color_list_item()
Expand Down
59 changes: 16 additions & 43 deletions mne_gui_addons/tests/test_ieeg_locate.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _fake_CT_coords(skull_size=5, contact_size=2):
np.array(np.meshgrid(*[range(-contact_size, contact_size + 1)] * 3)), axis=0
)
ct = nib.MGHImage(ct_data, brain.affine)
coords = apply_trans(ct.header.get_vox2ras_tkr(), np.array(coords))
coords = apply_trans(ct.header.get_vox2ras(), np.array(coords))
return ct, coords


Expand Down Expand Up @@ -157,17 +157,15 @@ def test_ieeg_elec_locate_display(renderer_interactive_pyvistaqt, _fake_CT_coord
gui._ras[:] = coords[0]

# start in the right position
gui.set_RAS(apply_trans(gui._mri_scan_ras_t, coords[0]))
gui.set_RAS(coords[0])
gui.mark_channel()

with pytest.raises(ValueError, match="not found"):
gui.mark_channel("foo")

assert not gui._lines and not gui._lines_2D # no lines for one contact
for ci, coord in enumerate(coords[1:], 1):
coord_vox = apply_trans(
gui._scan_ras_ras_vox_t, apply_trans(gui._mri_scan_ras_t, coord)
)
coord_vox = apply_trans(gui._scan_ras_ras_vox_t, coord)
with use_log_level("debug"):
_fake_click(
gui._figs[2],
Expand All @@ -177,13 +175,13 @@ def test_ieeg_elec_locate_display(renderer_interactive_pyvistaqt, _fake_CT_coord
kind="release",
)
assert_allclose(
apply_trans(gui._mri_scan_ras_t, coord)[:2],
coord[:2],
gui._ras[:2],
atol=0.1,
err_msg=f"coords[{ci}][:2]",
)
assert_allclose(
apply_trans(gui._mri_scan_ras_t, coord)[2],
coord[2],
gui._ras[2],
atol=2,
err_msg=f"coords[{ci}][2]",
Expand All @@ -195,29 +193,16 @@ def test_ieeg_elec_locate_display(renderer_interactive_pyvistaqt, _fake_CT_coord

# test snap to center
gui._ch_index = 0
gui.set_RAS(apply_trans(gui._mri_scan_ras_t, coords[0])) # move to first position
gui.set_RAS(coords[0]) # move to first position
gui.mark_channel()
assert (
abs(
np.linalg.norm(
apply_trans(gui._mri_scan_ras_t, coords[0]) - gui._chs["LAMY 1"]
)
- 1.03
)
< 1e-3
)
assert 0 < np.linalg.norm(coords[0] - gui._chs["LAMY 1"]) < 1
gui._snap_button.click()
assert gui._snap_button.text() == "Off"
# now make sure no snap happens
gui._ch_index = 0
gui.set_RAS(apply_trans(gui._mri_scan_ras_t, coords[1] + 1))
gui.set_RAS(coords[1] + 1)
gui.mark_channel()
assert (
np.linalg.norm(
apply_trans(gui._mri_scan_ras_t, coords[1] + 1) - gui._chs["LAMY 1"]
)
< 1e-3
)
assert np.linalg.norm(coords[1] + 1 - gui._chs["LAMY 1"]) < 1e-3
# check that it turns back on
gui._snap_button.click()
assert gui._snap_button.text() == "On"
Expand Down Expand Up @@ -259,40 +244,28 @@ def test_ieeg_elec_locate_display(renderer_interactive_pyvistaqt, _fake_CT_coord
assert montage is not None
assert_allclose(
montage.get_positions()["ch_pos"]["LAMY 1"],
[5.276672, -9.030582, 27.302032],
[0.007262, 0.017135, 0.041672],
atol=0.01,
)

# check auto find targets
gui.remove_channel("LAMY 1")
target, entry = (
apply_trans(gui._mri_scan_ras_t, coords[0]) / 1000,
apply_trans(gui._mri_scan_ras_t, coords[1]) / 1000,
coords[0] / 1000,
coords[1] / 1000,
)

# test just target
gui.auto_find_contacts(targets={"LAMY ": target})
assert (
np.linalg.norm(apply_trans(gui._mri_scan_ras_t, coords[0]) - gui._chs["LAMY 1"])
< 1e-3
)
assert (
np.linalg.norm(apply_trans(gui._mri_scan_ras_t, coords[1]) - gui._chs["LAMY 2"])
< 1e-3
)
assert np.linalg.norm(coords[0] - gui._chs["LAMY 1"]) < 1e-3
assert np.linalg.norm(coords[1] - gui._chs["LAMY 2"]) < 1e-3

gui.remove_channel("LAMY 1")
gui.remove_channel("LAMY 2")

# test with target and entry
gui.auto_find_contacts(targets={"LAMY ": (target, entry)})
assert (
np.linalg.norm(apply_trans(gui._mri_scan_ras_t, coords[0]) - gui._chs["LAMY 1"])
< 1e-3
)
assert (
np.linalg.norm(apply_trans(gui._mri_scan_ras_t, coords[1]) - gui._chs["LAMY 2"])
< 1e-3
)
assert np.linalg.norm(coords[0] - gui._chs["LAMY 1"]) < 1e-3
assert np.linalg.norm(coords[1] - gui._chs["LAMY 2"]) < 1e-3

gui.close()

0 comments on commit 5e3804d

Please sign in to comment.