Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added ModifyAffine
  • Loading branch information
chrisgorgo committed Oct 24, 2010
commit 76f65238b6d0ff0b65ae3c56f96cc139d17bf5de
36 changes: 36 additions & 0 deletions nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,39 @@ def _list_outputs(self):
outputs["thresholded_volumes"].append(base + '_thresholded.nii')
return outputs

class ModifyAffineInputSpec(TraitedSpec):
volumes = InputMultiPath(File(exists=True), desc='volumes which affine matrices will be modified', mandatory=True)
transformation_matrix = traits.Array(value=np.eye(4), shape=(4,4), desc="transformation matrix that will be left multiplied by the affine matrix", usedefault=True)

class ModifyAffineOutputSpec(TraitedSpec):
transformed_volumes = OutputMultiPath(File(exist=True))

class ModifyAffine(BaseInterface):
'''
LEft multiplies the affine matrix with a specified values. Saves the volume as a nifti file.
'''
input_spec = ModifyAffineInputSpec
output_spec = ModifyAffineOutputSpec

def _gen_output_filename(self, name):
_, base, _ = split_filename(name)
return os.path.abspath(base + "_transformed.nii")

def _run_interface(self, runtime):
for fname in self.inputs.volumes:
img = nifti.load(fname)

affine = img.get_affine()
affine = np.dot(self.inputs.transformation_matrix,affine)

nifti.save(nifti.Nifti1Image(img.get_data(), affine, img.get_header()), self._gen_output_filename(fname))

runtime.returncode=0
return runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs['transformed_volumes'] = []
for fname in self.inputs.volumes:
outputs['transformed_volumes'].append(self._gen_output_filename(fname))
return outputs