Beispiel #1
0
    def _run_interface(self, runtime):
        transforms = []
        invert_transform_flags = []
        if isdefined(self.inputs.transform_1):
            transforms.append(self.inputs.transform_1)
            invert_transform_flags.append(self.inputs.invert_1)

        if isdefined(self.inputs.transform_2):
            transforms.append(self.inputs.transform_2)
            invert_transform_flags.append(self.inputs.invert_2)

        if isdefined(self.inputs.transform_3):
            transforms.append(self.inputs.transform_3)
            invert_transform_flags.append(self.inputs.invert_3)

        #output file
        split = splitext(os.path.basename(self.inputs.input_image))
        self.inputs.output_image = os.getcwd() + os.sep + split[0] + split[1]
        if '_space-' in self.inputs.output_image:
            self.inputs.output_image = re.sub(
                '_space-[A-z]*_', "_space-" + self.inputs.target_space + "_",
                self.inputs.output_image)

        #combine transformation files and output flags
        transforms_zip = zip(transforms, invert_transform_flags)
        transform_string = ' '.join([
            '-t [ ' + str(t) + ' , ' + str(int(f)) + ' ]'
            for t, f in transforms_zip if t != None
        ])
        cmdline = "antsApplyTransforms --float -v 1 -e 3 -d 3 -n " + self.inputs.interpolation + " -i " + self.inputs.input_image + " " + transform_string + " -r " + self.inputs.reference_image + " -o " + self.inputs.output_image

        cmd(cmdline)

        return runtime
Beispiel #2
0
    def _run_interface(self, runtime):
        normalization_type = self.inputs.normalization_type

        #Setup ANTs command line arguments
        if isdefined(self.inputs.user_ants_command):
            cmdline, self.inputs.normalization_type = self.read_user_command_line(
            )
            self._set_outputs()
            cmdline = self.replace_user_command_line(cmdline)
        else:
            self._set_outputs()
            cmdline = self.default_command_line()
        print(self.inputs)
        #Run antsRegistration on command line
        print("Ants command line:\n", cmdline)
        p = cmd(cmdline)

        if self.inputs.normalization_type in ['rigid', 'affine']:
            #Convert linear transforms from .mat to .txt. antsRegistration produces .mat file based on output
            #prefix, but this format seems to be harder to work with / lead to downstream errors
            #If linear transform, then have to apply transformations to input image
            self.apply_linear_transforms()

        if isdefined(self.inputs.rotation_error) or isdefined(
                self.inputs.translation_error):
            if self.inputs.rotation_error != [
                    0, 0, 0
            ] and self.inputs.translation_error != [0, 0, 0]:
                print('Warning: Applying misalignment')
                print("\tRotation:", self.inputs.rotation_error)
                print("\tTranslation:", self.inputs.translation_error)
                exit(1)
                self.apply_misalignment()

        return runtime
Beispiel #3
0
    def _run_interface(self, runtime):
        #Get extension for input transformation files
        ext_1 = splitext(self.inputs.transform_1)[1]
        ext_2 = splitext(self.inputs.transform_2)[1]

        if ext_1 in ['.mat', '.txt'] and ext_2 in ['.mat', '.txt']:
            self.inputs.out_file = os.getcwd(
            ) + os.sep + 'composite_affine.mat'
        elif ext_1 == '.h5' or ext_2 == '.h5':
            self.inputs.out_file = os.getcwd() + os.sep + 'composite_warp.h5'
        cmd("CompositeTransformUtil --assemble " + ' '.join([
            self.inputs.out_file, self.inputs.transform_1,
            self.inputs.transform_2
        ]))

        return runtime
Beispiel #4
0
    def apply_linear_transforms(self):

        #Command line to
        if not os.path.exists(self.inputs.warped_image):
            cmdline = "antsApplyTransforms -e 3 -d 3 -n Linear  -i " + self.inputs.moving_image + " -t " + self.inputs.out_matrix + " -r " + self.inputs.fixed_image + " -o " + self.inputs.warped_image
            print(cmdline)
            cmd(cmdline)

        if not os.path.exists(self.inputs.out_matrix_inverse):
            cmdline = "antsApplyTransforms -e 3 -d 3  -n Linear -i " + self.inputs.moving_image + " -t " + self.inputs.out_matrix + " -r " + self.inputs.fixed_image + " -o Linear[" + self.inputs.out_matrix_inverse + ",1]"
            print(cmdline)
            cmd(cmdline)

        def mat2txt(self, ii_fn, oo_fn):
            print(ii_fn, oo_fn)
            tfm = sitk.ReadTransform(ii_fn)
            sitk.WriteTransform(tfm, oo_fn)
            return 0
    def _run_interface(self, runtime):

        quantNode = self._quantCommand()
        quantNode.inputs = self.inputs
        init_command = quantNode.cmdline
        modified_command = []
        self.inputs.out_file = quantNode.inputs.out_file
        for f in init_command.split(' '):
            if os.path.exists(f):
                f = check_gz(f)
            elif f == quantNode.inputs.out_file and splitext(
                    f)[1] == '.nii.gz':
                f = splitext(f)[0] + '.nii'
                self.inputs.out_file = f
            modified_command.append(f)

        print(modified_command)
        command = ' '.join(modified_command)
        print(command)
        cmd(command)

        print("Out file", self.inputs.out_file)
        return runtime
Beispiel #6
0
    def apply_misalignment(self):
        com = center_of_mass(nib.load(self.inputs.fixed_image).get_data())

        img = nib.load(self.inputs.fixed_image)
        com_world = [
            img.affine[0, 3] + com[0] * img.affine[0, 2],
            img.affine[1, 3] + com[1] * img.affine[1, 1],
            img.affine[2, 3] + com[2] * img.affine[2, 0]
        ]

        tfm = sitk.VersorRigid3DTransform()
        rotations_radians = list(np.pi * np.array(self.inputs.rotation_error) /
                                 180.)
        tfm.SetParameters(rotations_radians + self.inputs.translation_error)
        tfm.SetFixedParameters(com_world)
        print('Center of Mass :', com_world)
        print(tfm.GetParameters())
        print(tfm.GetFixedParameters())
        misalign_matrix = os.getcwd(
        ) + os.sep + 'misalignment_rot_x-{}_y-{}_z-{}_trans_x-{}_y-{}_z-{}.tfm'.format(
            *self.inputs.rotation_error, *self.inputs.translation_error)
        sitk.WriteTransform(tfm, misalign_matrix)

        print('Warning: misaligning PET to MRI alignment using file',
              misalign_matrix)

        cmdline = "antsApplyTransforms -e 3 -d 3  -n Linear -i " + self.inputs.moving_image + " -t " + misalign_matrix + " " + self.inputs.out_matrix + " -r " + self.inputs.fixed_image + " -o Linear[" + self.inputs.out_matrix + ",0]"
        print(cmdline)
        cmd(cmdline)

        cmdline = "antsApplyTransforms -e 3 -d 3  -n Linear -i " + self.inputs.moving_image + " -t " + misalign_matrix + " " + self.inputs.out_matrix + " -r " + self.inputs.fixed_image + " -o Linear[" + self.inputs.out_matrix_inverse + ",1]"
        print(cmdline)
        cmd(cmdline)

        cmdline = "antsApplyTransforms -e 3 -d 3 -n Linear  -i " + self.inputs.moving_image + " -t " + self.inputs.out_matrix + " -r " + self.inputs.fixed_image + " -o " + self.inputs.warped_image
        print(cmdline)
        cmd(cmdline)

        cmdline = "antsApplyTransforms -e 3 -d 3 -n Linear  -i " + self.inputs.fixed_image + " -t " + self.inputs.out_matrix_inverse + " -r " + self.inputs.moving_image + " -o " + self.inputs.inverse_warped_image
        print(cmdline)
        cmd(cmdline)