From e250f2a8c8c46b6f05dbd36ba7f95e364a203a11 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 27 Jul 2021 14:46:26 -0700 Subject: [PATCH] wip --- mne/gui/_ieeg_locate_gui.py | 49 +++++++++++++------------------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/mne/gui/_ieeg_locate_gui.py b/mne/gui/_ieeg_locate_gui.py index 6de35eeb0bb..e89e6ec9b50 100644 --- a/mne/gui/_ieeg_locate_gui.py +++ b/mne/gui/_ieeg_locate_gui.py @@ -48,9 +48,6 @@ 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) @@ -58,32 +55,20 @@ def _get_mri_head_trans(subject, subjects_dir): @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 @@ -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]], @@ -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( @@ -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: @@ -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) @@ -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)