Skip to content

Commit

Permalink
[MAINT] ras->mri (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrockhill authored Nov 21, 2023
1 parent a6fa04f commit 8ae9622
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
18 changes: 9 additions & 9 deletions mne_gui_addons/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def _load_image(img, verbose=None):
img_data = nib.orientations.apply_orientation(orig_data, ornt_trans)
orig_mgh = nib.MGHImage(orig_data, img.affine)
vox_scan_ras_t = orig_mgh.header.get_vox2ras()
vox_ras_t = orig_mgh.header.get_vox2ras_tkr()
vox_mri_t = orig_mgh.header.get_vox2ras_tkr()
aff_trans = nib.orientations.inv_ornt_aff(ornt_trans, img.shape)
ras_vox_scan_ras_t = np.dot(vox_scan_ras_t, aff_trans)
return img_data, vox_ras_t, vox_scan_ras_t, ras_vox_scan_ras_t
return img_data, vox_mri_t, vox_scan_ras_t, ras_vox_scan_ras_t


def _make_mpl_plot(
Expand Down Expand Up @@ -170,21 +170,21 @@ def _load_image_data(self, base_image=None):
if op.isfile(op.join(self._subject_dir, "mri", "brain.mgz"))
else "T1"
)
self._mri_data, vox_ras_t, vox_scan_ras_t, ras_vox_scan_ras_t = _load_image(
self._mri_data, vox_mri_t, vox_scan_ras_t, ras_vox_scan_ras_t = _load_image(
op.join(self._subject_dir, "mri", f"{mri_img}.mgz")
)

# ready alternate base image if provided, otherwise use brain/T1
if base_image is None:
assert self._mri_data is not None
self._base_data = self._mri_data
self._vox_ras_t = vox_ras_t
self._vox_mri_t = vox_mri_t
self._vox_scan_ras_t = vox_scan_ras_t
self._ras_vox_scan_ras_t = ras_vox_scan_ras_t
else:
(
self._base_data,
self._vox_ras_t,
self._vox_mri_t,
self._vox_scan_ras_t,
self._ras_vox_scan_ras_t,
) = _load_image(base_image)
Expand All @@ -202,13 +202,13 @@ def _load_image_data(self, base_image=None):
"``subject`` and ``subjects_dir`` arguments"
)

self._ras_vox_t = np.linalg.inv(self._vox_ras_t)
self._mri_vox_t = np.linalg.inv(self._vox_mri_t)
self._scan_ras_vox_t = np.linalg.inv(self._vox_scan_ras_t)
self._scan_ras_ras_vox_t = np.linalg.inv(
self._ras_vox_scan_ras_t
) # to RAS voxels
self._scan_ras_ras_t = np.dot(self._vox_ras_t, self._scan_ras_vox_t)
self._ras_scan_ras_t = np.dot(self._vox_scan_ras_t, self._ras_vox_t)
self._scan_ras_mri_t = np.dot(self._vox_mri_t, self._scan_ras_vox_t)
self._mri_scan_ras_t = np.dot(self._vox_scan_ras_t, self._mri_vox_t)
self._voxel_sizes = np.array(self._base_data.shape)
self._voxel_ratios = self._voxel_sizes / self._voxel_sizes.min()

Expand Down Expand Up @@ -341,7 +341,7 @@ def _plot_images(self):
np.where(self._base_data < np.quantile(self._base_data, 0.95), 0, 1),
[1],
)[0]
rr = apply_trans(self._vox_ras_t, rr)
rr = apply_trans(self._vox_mri_t, rr)
self._renderer.mesh(
*rr.T,
triangles=tris,
Expand Down
20 changes: 13 additions & 7 deletions mne_gui_addons/_ieeg_locate.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__(
if not np.isnan(self._chs[self._ch_names[self._ch_index]]).any():
self._set_ras(
apply_trans(
self._ras_scan_ras_t, self._chs[self._ch_names[self._ch_index]]
self._mri_scan_ras_t, self._chs[self._ch_names[self._ch_index]]
),
update_plots=False,
)
Expand Down Expand Up @@ -229,7 +229,7 @@ def color_ch_radius(ch_image, xf, yf, group, radius):
if np.isnan(surf_ras).any():
continue
xyz = apply_trans(
self._scan_ras_ras_vox_t, apply_trans(self._ras_scan_ras_t, surf_ras)
self._scan_ras_ras_vox_t, apply_trans(self._mri_scan_ras_t, surf_ras)
)
# check if closest to that voxel
dist = np.linalg.norm(xyz - self._current_slice)
Expand Down Expand Up @@ -515,9 +515,15 @@ def _update_lines(self, group, only_2D=False):
)[0]
if self._toggle_show_mip_button.text() == "Hide Max Intensity Proj":
# add 2D lines on each slice plot if in max intensity projection
target_vox = apply_trans(self._ras_vox_t, pos[target_idx])
target_vox = apply_trans(
self._mri_scan_ras_t,
apply_trans(self._scan_ras_ras_vox_t, pos[target_idx]),
)
insert_vox = apply_trans(
self._ras_vox_t, pos[insert_idx] + elec_v * _BOLT_SCALAR
self._mri_scan_ras_t,
apply_trans(
self._scan_ras_ras_vox_t, pos[insert_idx] + elec_v * _BOLT_SCALAR
),
)
lines_2D = list()
for axis in range(3):
Expand Down Expand Up @@ -559,7 +565,7 @@ def _update_ch_selection(self):
self._group_selector.setCurrentIndex(self._groups[name])
self._update_group()
if not np.isnan(self._chs[name]).any():
self._set_ras(apply_trans(self._ras_scan_ras_t, self._chs[name]))
self._set_ras(apply_trans(self._mri_scan_ras_t, self._chs[name]))
self._zoom(sign=0, draw=True)
self._update_camera(render=True)

Expand Down Expand Up @@ -625,7 +631,7 @@ def mark_channel(self, ch=None):
]
if self._snap_button.text() == "Off":
self._chs[name][:] = apply_trans(
self._scan_ras_ras_t, self._ras
self._scan_ras_mri_t, self._ras
) # stored as surface RAS
else:
shape = np.mean(self._voxel_sizes) # Freesurfer shape (256)
Expand All @@ -640,7 +646,7 @@ def mark_channel(self, ch=None):
use_relative=True,
)
self._chs[name][:] = apply_trans( # to surface RAS
self._vox_ras_t, np.array(list(neighbors)).mean(axis=0)
self._vox_mri_t, np.array(list(neighbors)).mean(axis=0)
)
self._color_list_item()
self._update_lines(self._groups[name])
Expand Down
6 changes: 3 additions & 3 deletions mne_gui_addons/_vol_stc.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def __init__(
(
self._src_lut,
self._src_vox_scan_ras_t,
self._src_vox_ras_t,
self._src_vox_mri_t,
self._src_rr,
) = _get_src_lut(src)
self._src_scan_ras_vox_t = np.linalg.inv(self._src_vox_scan_ras_t)
Expand Down Expand Up @@ -349,8 +349,8 @@ def __init__(
# TO DO: add surface source space viewing as elif
if any([this_src["type"] == "vol" for this_src in self._src]):
scalars = np.array(np.where(np.isnan(self._stc_img), 0, 1.0))
spacing = np.diag(self._src_vox_ras_t)[:3]
origin = self._src_vox_ras_t[:3, 3] - spacing / 2.0
spacing = np.diag(self._src_vox_mri_t)[:3]
origin = self._src_vox_mri_t[:3, 3] - spacing / 2.0
center = 0.5 * self._stc_range - self._stc_min
(
self._grid,
Expand Down

0 comments on commit 8ae9622

Please sign in to comment.