def dump(self, path): nb.save(self.img, os.path.join(path, 'subject.nii')) reg = nr.resample(self.timg, self.T.inv(), reference=self.img) nb.save(self.timg, os.path.join(path, 'template_reg.nii')) hip_reg = self.get_mask('left') + self.get_mask('right') hip_reg = nb.Nifti1Image(hip_reg, self.img.get_affine()) nb.save(hip_reg, os.path.join(path, os.path.join('hip_reg.nii'))) if self.lhip_mask != None and self.rhip_mask != None: hip = self.lhip_mask + self.rhip_mask hip = nb.Nifti1Image(hip.astype('uint8'), self.img.get_affine()) nb.save(hip, os.path.join(path, 'hip_gold.nii')) # Uncache template image to free memory self.timg.uncache()
def get_mask(self, hemi): """ Resample template mask """ if hemi == 'left': if self.tlhip_reg_mask != None: return self.tlhip_reg_mask thip = self.tlhip_mask T = self.Tl elif hemi == 'right': if self.trhip_reg_mask != None: return self.trhip_reg_mask thip = self.trhip_mask T = self.Tr else: raise ValueError('neither left nor right!') tmp = nb.Nifti1Image(thip.astype(float), self.timg.get_affine()) tmp = nr.resample(tmp, T.inv(), reference=self.img, interp_order=1).get_data() if hemi == 'left': self.tlhip_reg_mask = tmp.copy() elif hemi == 'right': self.trhip_reg_mask = tmp.copy() return tmp
import nireg as nr from nidata.task.poldrack_etal_2001 import PoldrackEtal2001Dataset from nilearn.plotting import plot_stat_map from nipy.labs.viz import cm pold_dataset = PoldrackEtal2001Dataset() data_dict = pold_dataset.fetch(preprocess_data=True) print(data_dict['anat'][0:2]) print(data_dict['func'][0:2]) func = nb.load(data_dict['func'][0]) anat = nb.load(data_dict['anat'][0]) reg = nr.HistogramRegistration(func, anat) trns = reg.optimize('rigid') func_trans = nr.resample(func, trns.inv(), reference=anat) fh = plt.figure() plot_stat_map(func, cmap=cm.hot_black_bone, black_bg=True, vmax=100., alpha=0.9, title='Before rigid registration', bg_img=anat, axes=fh.add_subplot(2, 1, 1)) plot_stat_map(func_trans, cmap=cm.hot_black_bone, black_bg=True, vmax=100., alpha=0.9,
import nireg as nr from nidata.task.poldrack_etal_2001 import PoldrackEtal2001Dataset from nilearn.plotting import plot_stat_map from nipy.labs.viz import cm pold_dataset = PoldrackEtal2001Dataset() data_dict = pold_dataset.fetch(preprocess_data=True) print(data_dict['anat'][0:2]) print(data_dict['func'][0:2]) func = nb.load(data_dict['func'][0]) anat = nb.load(data_dict['anat'][0]) reg = nr.HistogramRegistration(func, anat) trns = reg.optimize('rigid') func_trans = nr.resample(func, trns.inv(), reference=anat) fh = plt.figure() plot_stat_map(func, cmap=cm.hot_black_bone, black_bg=True, vmax=100., alpha=0.9, title='Before rigid registration', bg_img=anat, axes=fh.add_subplot(2, 1, 1)) plot_stat_map(func_trans, cmap=cm.hot_black_bone, black_bg=True, vmax=100., alpha=0.9, title='After rigid registration', bg_img=anat,
# Perform affine registration # The output is an array-like object such that # np.asarray(T) is a customary 4x4 matrix print('Setting up registration...') tic = time.time() R = HistogramRegistration(I, J, similarity=similarity, interp=interp, renormalize=renormalize) T = R.optimize('affine', optimizer=optimizer, xtol=tol, ftol=tol) toc = time.time() print(' Registration time: %f sec' % (toc - tic)) # Resample source image print('Resampling source image...') tic = time.time() #It = resample2(I, J.coordmap, T.inv(), J.shape) It = resample(I, T.inv(), reference=J) toc = time.time() print(' Resampling time: %f sec' % (toc - tic)) # Save resampled source outroot = source + '_TO_' + target outimg = outroot + '.nii.gz' print ('Saving resampled source in: %s' % outimg) nb.save(It, outimg) # Save transformation matrix """ outparams = outroot + '.npy' np.save(outparams, np.asarray(T)) """
print('Setting up registration...') tic = time.time() R = HistogramRegistration(I, J, similarity=similarity, interp=interp, renormalize=renormalize) T = R.optimize('affine', optimizer=optimizer, xtol=tol, ftol=tol) toc = time.time() print(' Registration time: %f sec' % (toc - tic)) # Resample source image print('Resampling source image...') tic = time.time() #It = resample2(I, J.coordmap, T.inv(), J.shape) It = resample(I, T.inv(), reference=J) toc = time.time() print(' Resampling time: %f sec' % (toc - tic)) # Save resampled source outroot = source + '_TO_' + target outimg = outroot + '.nii.gz' print('Saving resampled source in: %s' % outimg) nb.save(It, outimg) # Save transformation matrix """ outparams = outroot + '.npy' np.save(outparams, np.asarray(T)) """