def transform(floating, T, reference=None, interp_order=3): # Convert assumed nibabel-like input images to local image class floating = Image(floating) if not reference == None: reference = Image(reference) return asNifti1Image(floating.transform(np.asarray(T), grid_coords=False, reference=reference, interp_order=interp_order))
def resample(self, align_runs=True): """ Return a list of 4d nibabel-like images corresponding to the resampled runs. """ if align_runs: transforms = self._transforms else: transforms = self._within_run_transforms indices = range(len(self._runs)) data = [resample4d(self._runs[i], transforms=transforms[i]) for i in indices] return [asNifti1Image(Image(data[i], self._runs[i].to_world)) for i in indices]
T = GridTransform(I, defmodes.data.reshape(list(I.shape)+[7,3])) # freeze mean deformation component T._param[0] = 1 T._free_param_idx = [1,2,3,4,5,6] if 'defrand' in TYPE: best_param = np.load('t3_def_crl1.npy') best_simi = 0.0 for i in range(NTRIALS): T.param = best_param + std*np.random.normal(std.size) T = R.optimize(T) optimum = R.eval(T) if optimum > best_simi: print(' *** New best ***') best_simi = optimum best_param = T.param else: print(' *** Unbeaten optimum ***') T.param = best_param else: T.param = identity T = R.optimize(T) # Create matcher instance fname = 't'+str(ID)+'_'+TYPE+'_'+SIMI+'_'+INTERP outfile = join(DATADIR, fname+'.nii') print('Saving resampled source in: %s' % outfile) Jt = J.transform(T, reference=I) save(asNifti1Image(Jt), outfile)
import numpy as np from nibabel import load, save from nipy.neurospin.image import Image, asNifti1Image from os.path import join from glob import glob datadir = join('d:\\', 'home', 'Alexis', 'data', 'remy') I = Image(load(join(datadir, '2_axial_FL_AD_ROI.nii'))) defmodes = glob(join(datadir, 'NEW_SDF', 'SDF_*.nii')) data = np.zeros(list(I.shape)+[len(defmodes)]) for i in range(len(defmodes)): print('image: %d (%s)' % (i, defmodes[i])) m = Image(load(defmodes[i])) mm = m.transform(np.eye(4), reference=I) data[:,:,:,i] = mm.data save(asNifti1Image(Image(data, I.affine)), join(datadir, 'deformation_modes.nii'))