def _run_interface(self, runtime): vol1_nii = nb.load(self.inputs.volume1) vol2_nii = nb.load(self.inputs.volume2) if isdefined(self.inputs.mask1): mask1_nii = nb.load(self.inputs.mask1) mask1_nii = nb.Nifti1Image( nb.load(self.inputs.mask1).get_data() == 1, mask1_nii.get_affine(), mask1_nii.get_header()) else: mask1_nii = None if isdefined(self.inputs.mask2): mask2_nii = nb.load(self.inputs.mask2) mask2_nii = nb.Nifti1Image( nb.load(self.inputs.mask2).get_data() == 1, mask2_nii.get_affine(), mask2_nii.get_header()) else: mask2_nii = None histreg = HistogramRegistration(from_img=vol1_nii, to_img=vol2_nii, similarity=self.inputs.metric, from_mask=mask1_nii, to_mask=mask2_nii) self._similarity = histreg.eval(Affine()) return runtime
def _run_interface(self, runtime): from nipy.algorithms.registration.histogram_registration import ( HistogramRegistration, ) from nipy.algorithms.registration.affine import Affine vol1_nii = nb.load(self.inputs.volume1) vol2_nii = nb.load(self.inputs.volume2) if isdefined(self.inputs.mask1): mask1 = nb.load(self.inputs.mask1).get_data() == 1 else: mask1 = None if isdefined(self.inputs.mask2): mask2 = nb.load(self.inputs.mask2).get_data() == 1 else: mask2 = None histreg = HistogramRegistration( from_img=vol1_nii, to_img=vol2_nii, similarity=self.inputs.metric, from_mask=mask1, to_mask=mask2, ) self._similarity = histreg.eval(Affine()) return runtime
def save_nifty(filename, data, origin=(0, 0, 0), vox_scale=(0, 0, 0), angles=(0, 0, 0)): """ save_nifty: write data to given file in nifty format, taking care of the affine transform. Inputs ------ filename : string the file in which to put the output. If not given, the extension .nii will be added. data : 3d numpy array the volume data to save. A single slice will be considered as a 3d volume that is only one voxel thick in the through-plane direction. origin : 3-element sequence =(0,0,0) the (x,y,z) physical coordinates of element (0,0,0) of the data. vox_scale : 3-element sequence the physical size of each voxel along the three axes, in the order that the input data is given, i.e., the first value is the length step along the first data axis. Typically in mm units. angles : 3-element sequence the (Euler) rotation angles about the three (data) axes, defining the rotation data-coordinates->physical coordinates. See nipy.algorithms.registration.affine.rotation_vec2mat for how the calculation is performed. If my assumption about what angles mean is wrong, alternative explicit conversions are here: http://nipy.org/nibabel/generated/nibabel.eulerangles.html """ aff = Affine() aff.translation = origin aff.scaling = vox_scale aff.rotation = angles nifti = nibabel.Nifti1Image(data, aff) if not ('.nii' in filename): filename = filename + '.nii' nifti.to_filename(filename)
def save_nifty(filename, data, origin=(0,0,0), vox_scale=(0,0,0), angles=(0,0,0) ): """ save_nifty: write data to given file in nifty format, taking care of the affine transform. Inputs ------ filename : string the file in which to put the output. If not given, the extension .nii will be added. data : 3d numpy array the volume data to save. A single slice will be considered as a 3d volume that is only one voxel thick in the through-plane direction. origin : 3-element sequence =(0,0,0) the (x,y,z) physical coordinates of element (0,0,0) of the data. vox_scale : 3-element sequence the physical size of each voxel along the three axes, in the order that the input data is given, i.e., the first value is the length step along the first data axis. Typically in mm units. angles : 3-element sequence the (Euler) rotation angles about the three (data) axes, defining the rotation data-coordinates->physical coordinates. See nipy.algorithms.registration.affine.rotation_vec2mat for how the calculation is performed. If my assumption about what angles mean is wrong, alternative explicit conversions are here: http://nipy.org/nibabel/generated/nibabel.eulerangles.html """ aff = Affine() aff.translation = origin aff.scaling = vox_scale aff.rotation = angles nifti = nibabel.Nifti1Image(data, aff) if not('.nii' in filename): filename = filename + '.nii' nifti.to_filename(filename)
def _run_interface(self, runtime): from nipy.algorithms.registration.histogram_registration import ( HistogramRegistration, ) from nipy.algorithms.registration.affine import Affine vol1_nii = nb.load(self.inputs.volume1) vol2_nii = nb.load(self.inputs.volume2) dims = vol1_nii.get_data().ndim if dims == 3 or dims == 2: vols1 = [vol1_nii] vols2 = [vol2_nii] if dims == 4: vols1 = nb.four_to_three(vol1_nii) vols2 = nb.four_to_three(vol2_nii) if dims < 2 or dims > 4: raise RuntimeError( "Image dimensions not supported (detected %dD file)" % dims) if isdefined(self.inputs.mask1): mask1 = nb.load(self.inputs.mask1).get_data() == 1 else: mask1 = None if isdefined(self.inputs.mask2): mask2 = nb.load(self.inputs.mask2).get_data() == 1 else: mask2 = None self._similarity = [] for ts1, ts2 in zip(vols1, vols2): histreg = HistogramRegistration( from_img=ts1, to_img=ts2, similarity=self.inputs.metric, from_mask=mask1, to_mask=mask2, ) self._similarity.append(histreg.eval(Affine())) return runtime