Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrockhill committed Jul 27, 2021
1 parent 7ac756e commit e250f2a
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions mne/gui/_ieeg_locate_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,27 @@
def _get_mri_head_trans(subject, subjects_dir):
"""Get the head to surface RAS transform using the Freesurfer recon."""
lpa, nasion, rpa = get_mni_fiducials(subject, subjects_dir)
assert lpa['ident'] == FIFF.FIFFV_POINT_LPA
assert nasion['ident'] == FIFF.FIFFV_POINT_NASION
assert rpa['ident'] == FIFF.FIFFV_POINT_RPA
montage = make_dig_montage(
lpa=lpa['r'], nasion=nasion['r'], rpa=rpa['r'], coord_frame='mri')
trans = compute_native_head_t(montage)
return trans


@requires_nibabel()
def _reorient_image(img_data, vox_ras_t):
"""Reorient an image to RAS."""
import nibabel as nib
ornt = nib.orientations.axcodes2ornt(
nib.orientations.aff2axcodes(vox_ras_t)).astype(int)
img_data = nib.orientations.apply_orientation(img_data, ornt)
vox_ras_t[:3, :3] = vox_ras_t[list(ornt[:, 0]), :3]
vox_ras_t[:3, 3] = vox_ras_t[list(ornt[:, 0]), 3]
vox_ras_t[:3, :3] *= ornt[:, 1]
vox_ras_t[:3, 3] *= ornt[:, 1]
return img_data, vox_ras_t


@requires_nibabel()
def _load_image(img, name, reorient=False, verbose=True):
def _load_image(img, name, verbose=True):
"""Load data from a 3D image file (e.g. CT, MR)."""
import nibabel as nib
if not isinstance(img, nib.spatialimages.SpatialImage):
if verbose:
print(f'Loading {img}')
logger.info(f'Loading {img}')
_check_fname(img, overwrite='read', must_exist=True, name=name)
img = nib.load(img)
img_data = np.array(img.dataobj)
vox_ras_t = img.affine
if reorient:
img_data, vox_ras_t = _reorient_image(img_data, vox_ras_t)
# get data reoriented to RAS and transform
orig_data = np.array(img.dataobj)
orig_mgh = nib.MGHImage(orig_data, img.affine)
vox_ras_t = orig_mgh.header.get_vox2ras_tkr()
img_canonical = nib.as_closest_canonical(img_mgh)
img_data, vox_ras_t = _reorient_image(img_data, vox_ras_t)
return img_data, vox_ras_t


Expand Down Expand Up @@ -176,7 +161,7 @@ def _load_image_data(self, ct):
op.join(self._subject_dir, 'mri', 'brain.mgz'),
'MRI Image', reorient=True, verbose=self._verbose)
self._ras_vox_t = np.linalg.inv(self._vox_ras_t)
self._T1_on = True
self._brain_on = True

self._voxel_sizes = np.array(self._mri_data.shape)
self._img_ranges = [[0, self._voxel_sizes[1], 0, self._voxel_sizes[2]],
Expand Down Expand Up @@ -287,14 +272,14 @@ def _plot_images(self):
# Plot sagittal (0), coronal (1) or axial (2) view
self._images = dict(mri=list(), ct=list(), chs=list(),
cursor=list(), cursor2=list())
ct_min, ct_max = np.nanmin(self._ct_data), np.nanmax(self._ct_data)
self._ct_cutoff = int(round(np.quantile(
self._ct_data[~np.isnan(self._ct_data)], _CT_MIN_DEFAULT))) \
if self._T1_on else 0 # only cut off by default if T1 is on
ct_min, ct_max = np.nanmin(self._ct_data), np.nanmax(self._ct_data)
ct_cmap = 'autumn_r' if self._T1_on else 'gray'
if self._brain_on else ct_min # cut off by default if brain is on
ct_cmap = 'autumn_r' if self._brain_on else 'gray'
for axis in range(3):
self._plt._axes[axis].clear()
if self._T1_on:
if self._brain_on:
img_data = np.take(self._mri_data, self._current_slice[axis],
axis=axis).T
self._images['mri'].append(self._plt._axes[axis].imshow(
Expand Down Expand Up @@ -658,7 +643,7 @@ def _update_ch_images(self, axis_selected=None, draw=False):
def _update_mri_images(self, axis_selected=None, draw=False):
"""Update the MRI image(s)."""
for axis in range(3) if axis_selected is None else [axis_selected]:
if self._T1_on:
if self._brain_on:
img_data = np.take(self._mri_data, self._current_slice[axis],
axis=axis).T
else:
Expand All @@ -682,7 +667,7 @@ def _update_ct_images(self, axis_selected=None, draw=False):

def _update_images(self, axis=None, draw=True):
"""Update MRI, CT and channel images when general changes happen."""
if self._T1_on:
if self._brain_on:
self._update_mri_images(axis_selected=axis)
self._update_ct_images(axis_selected=axis)
self._update_ch_images(axis_selected=axis)
Expand Down Expand Up @@ -779,8 +764,8 @@ def _key_press_event(self, event):

if event.text() == 'b':
# Toggle T1 scan on and off
self._T1_on = not self._T1_on
logger.info('Turning T1 ' + ('on' if self._T1_on else 'off'))
self._brain_on = not self._brain_on
logger.info('Turning T1 ' + ('on' if self._brain_on else 'off'))
self._plot_images()
# update slider on toggle
self._ct_min_slider.setValue(self._ct_cutoff)
Expand Down

0 comments on commit e250f2a

Please sign in to comment.