예제 #1
0
    def testParamToXFM(self):
        def compare_matrices(par, ref):
            if not np.allclose(par, ref):
                print("Matrix mismatch:", ref, par)
                return False
            return True

        ref = minc.xfm_identity_transform_par()
        par_mat = minc.param_to_xfm(ref)

        for i in range(3):
            for r in [-10, -10, 10, 20]:
                ref = minc.xfm_identity_transform_par()
                ref.translations[i] = r
                cmd = ["param2xfm", "-translation"] + [
                    str(j) for j in ref.translations
                ] + [self.tmp, "-clobber"]
                check_call_out(cmd)
                ref_mat = minc.read_xfm(self.tmp)
                par_mat = minc.param_to_xfm(ref)
                self.assertTrue(par_mat.lin)
                self.assertFalse(par_mat.inv)
                self.assertTrue(
                    compare_matrices(par_mat.trans, ref_mat[0].trans),
                    "Error in translations i={} r={}".format(i, r))

        for i in range(3):
            for r in [-10, -10, 10, 20]:
                ref = minc.xfm_identity_transform_par()
                ref.rotations[i] = r
                cmd = ["param2xfm", "-rotations"
                       ] + [str(j)
                            for j in ref.rotations] + [self.tmp, "-clobber"]
                check_call_out(cmd)
                ref_mat = minc.read_xfm(self.tmp)
                par_mat = minc.param_to_xfm(ref)
                self.assertTrue(par_mat.lin)
                self.assertFalse(par_mat.inv)
                self.assertTrue(
                    compare_matrices(par_mat.trans, ref_mat[0].trans),
                    "Error in rotations i={} r={}".format(i, r))

        for i in range(3):
            for r in [0.9, 1.1, 1.2, 1.3]:
                ref = minc.xfm_identity_transform_par()
                ref.scales[i] = r
                cmd = ["param2xfm", "-scales"] + [str(j) for j in ref.scales
                                                  ] + [self.tmp, "-clobber"]
                check_call_out(cmd)
                ref_mat = minc.read_xfm(self.tmp)
                par_mat = minc.param_to_xfm(ref)
                self.assertTrue(par_mat.lin)
                self.assertFalse(par_mat.inv)
                self.assertTrue(
                    compare_matrices(par_mat.trans, ref_mat[0].trans),
                    "Error in scales i={} r={}".format(i, r))
예제 #2
0
 def test_xfm_components(self):
     # TODO: add a test with nonlinear component
     check_call_out(
         ["param2xfm", "-rotations", "30", "0", "0", self.tmp, "-clobber"])
     x = minc.read_xfm(self.tmp)
     N_lin, N_nl = minc.check_xfm_components(x)
     self.assertEqual(N_lin, 1)
     self.assertEqual(N_nl, 0)
     r = minc.reduce_xfm_components(x)
     self.assertEqual(r, x)
예제 #3
0
def xfmavg(inputs, output, verbose=False):
    # TODO: handl inversion flag correctly
    all_linear = True
    all_nonlinear = True
    input_xfms = []

    for j in inputs:
        x = minc.read_xfm(j)
        if x[0].lin and len(x) == 1 and (not x[0].inv):
            # this is a linear matrix
            input_xfms.append(x[0])
        else:
            all_linear &= False
            # strip identity matrixes
            nl = []
            _identity = np.asmatrix(np.identity(4))
            _eps = 1e-6
            for i in x:
                if i.lin:
                    if scipy.linalg.norm(
                            _identity -
                            i.trans) > _eps:  # this is non-identity matrix
                        all_nonlinear &= False
                else:
                    if i.inv:
                        raise Exception(
                            "Trying to average inverted nonlinear transform")
                    nl.append(i)
            if len(nl) != 1:
                all_nonlinear &= False
            else:
                input_xfms.append(nl[0])

    if all_linear:
        acc = np.asmatrix(np.zeros([4, 4], dtype=np.complex))
        for i in input_xfms:
            acc += scipy.linalg.logm(i.trans)
        acc /= len(input_xfms)
        out_xfm = [minc.xfm_entry(True, False, scipy.linalg.expm(acc).real)]
        minc.write_xfm(output, out_xfm)
    elif all_nonlinear:
        input_grids = []
        for i in input_xfms:
            input_grids.append(i.trans)
        output_grid = output.rsplit('.xfm', 1)[0] + '_grid_0.mnc'

        cmds = ['mincaverage', '-clob']
        cmds.extend(input_grids)
        cmds.append(output_grid)
        do_cmd(cmds, verbose=verbose)
        out_xfm = [minc.xfm_entry(False, False, output_grid)]
        minc.write_xfm(output, out_xfm)
    else:
        raise Exception("Mixed XFM files provided as input")
예제 #4
0
 def testXfmRead(self):
     # generate xfm file first
     check_call_out(
         ["param2xfm", "-rotations", "30", "0", "0", self.tmp, "-clobber"])
     xfm = minc.read_xfm(self.tmp)
     #print(xfm)
     reference_matrix = np.array([[1.0, 0.0, 0.0, 0.0],
                                  [0.0, 0.866025388240814, -0.5, 0.0],
                                  [0.0, 0.5, 0.866025388240814, 0.0],
                                  [0.0, 0.0, 0.0, 1.0]])
     self.assertTrue(len(xfm) == 1)
     self.assertTrue(xfm[0].lin)
     self.assertTrue(np.allclose(reference_matrix, xfm[0].trans))
예제 #5
0
    def testXfmToParam(self):
        def compare_parameters(par, ref):
            if not np.allclose(ref.rotations, par.rotations):
                print("rotations mismatch:", ref.rotations, par.rotations)
                return False
            if not np.allclose(ref.translations, par.translations):
                print("translations mismatch:", ref.translations,
                      par.translations)
                return False
            if not np.allclose(ref.scales, par.scales):
                print("scales mismatch:", ref.translations, par.translations)
                return False
            if not np.allclose(ref.translations, par.translations):
                print("translations mismatch:", ref.translations,
                      par.translations)
                return False
            if not np.allclose(ref.shears, par.shears):
                print("shears mismatch:", ref.shears, par.shears)
                return False
            return True

        for i in range(3):
            for r in [-10, -10, 10, 20]:
                ref = minc.xfm_identity_transform_par()
                ref.rotations[i] = r
                cmd = ["param2xfm", "-rotations"
                       ] + [str(j)
                            for j in ref.rotations] + [self.tmp, "-clobber"]

                check_call_out(cmd)
                par = minc.xfm_to_param(minc.read_xfm(self.tmp))

                self.assertTrue(compare_parameters(ref, par),
                                "Error in rotations i={} r={}".format(i, r))

        for i in range(3):
            for r in [-10, -10, 10, 20]:
                ref = minc.xfm_identity_transform_par()
                ref.translations[i] = r
                cmd = ["param2xfm", "-translation"] + [
                    str(j) for j in ref.translations
                ] + [self.tmp, "-clobber"]

                check_call_out(cmd)
                par = minc.xfm_to_param(minc.read_xfm(self.tmp))

                self.assertTrue(compare_parameters(ref, par),
                                "Error in translations i={} r={}".format(i, r))

        for i in range(3):
            for r in [0.9, 1.1, 1.2, 1.3]:
                ref = minc.xfm_identity_transform_par()
                ref.scales[i] = r
                cmd = ["param2xfm", "-scales"] + [str(j) for j in ref.scales
                                                  ] + [self.tmp, "-clobber"]

                check_call_out(cmd)
                par = minc.xfm_to_param(minc.read_xfm(self.tmp))

                self.assertTrue(compare_parameters(ref, par),
                                "Error in scales i={} r={}".format(i, r))