Beispiel #1
0
def test_apply_transform_error():
    flow = ApplyTransformFlow()
    npt.assert_raises(ValueError,
                      flow.run,
                      'my_fake_static.nii.gz',
                      'my_fake_moving.nii.gz',
                      'my_fake_map.nii.gz',
                      transform_type='wrong_type')
Beispiel #2
0
def test_apply_affine_transform():
    with TemporaryDirectory() as temp_out_dir:

        factors = {
            ('TRANSLATION', 3): (2.0, None, np.array([2.3, 4.5, 1.7])),
            ('RIGID', 3):
            (0.1, None, np.array([0.1, 0.15, -0.11, 2.3, 4.5, 1.7])),
            ('RIGIDISOSCALING', 3):
            (0.1, None, np.array([0.1, 0.15, -0.11, 2.3, 4.5, 1.7, 0.8])),
            ('RIGIDSCALING', 3):
            (0.1, None,
             np.array([0.1, 0.15, -0.11, 2.3, 4.5, 1.7, 0.8, 0.9, 1.1])),
            ('AFFINE', 3): (0.1, None,
                            np.array([
                                0.99, -0.05, 0.03, 1.3, 0.05, 0.99, -0.10, 2.5,
                                -0.07, 0.10, 0.99, -1.4
                            ]))
        }

        image_registration_flow = ImageRegistrationFlow()
        apply_trans = ApplyTransformFlow()

        for i in factors.keys():
            static, moving, static_g2w, moving_g2w, smask, mmask, M = \
                setup_random_transform(transform=regtransforms[i],
                                       rfactor=factors[i][0])

            stat_file = str(i[0]) + '_static.nii.gz'
            mov_file = str(i[0]) + '_moving.nii.gz'

            save_nifti(pjoin(temp_out_dir, stat_file),
                       data=static,
                       affine=static_g2w)

            save_nifti(pjoin(temp_out_dir, mov_file),
                       data=moving,
                       affine=moving_g2w)

            static_image_file = pjoin(temp_out_dir,
                                      str(i[0]) + '_static.nii.gz')
            moving_image_file = pjoin(temp_out_dir,
                                      str(i[0]) + '_moving.nii.gz')

            out_moved = pjoin(temp_out_dir, str(i[0]) + "_moved.nii.gz")
            out_affine = pjoin(temp_out_dir, str(i[0]) + "_affine.txt")

            if str(i[0]) == "TRANSLATION":
                transform_type = "trans"
            elif str(i[0]) == "RIGIDISOSCALING":
                transform_type = "rigid_isoscaling"
            elif str(i[0]) == "RIGIDSCALING":
                transform_type = "rigid_scaling"
            else:
                transform_type = str(i[0]).lower()

            image_registration_flow.run(static_image_file,
                                        moving_image_file,
                                        transform=transform_type,
                                        out_dir=temp_out_dir,
                                        out_moved=out_moved,
                                        out_affine=out_affine,
                                        level_iters=[1, 1, 1],
                                        save_metric=False)

            # Checking for the created moved file.
            assert os.path.exists(out_moved)
            assert os.path.exists(out_affine)

        images = pjoin(temp_out_dir, '*moving*')
        apply_trans.run(static_image_file,
                        images,
                        out_dir=temp_out_dir,
                        transform_map_file=out_affine)

        # Checking for the transformed file.
        assert os.path.exists(pjoin(temp_out_dir, "transformed.nii.gz"))
Beispiel #3
0
def test_apply_affine_transform():
    with TemporaryDirectory() as temp_out_dir:

        factors = {
            ('TRANSLATION', 3): (2.0, None, np.array([2.3, 4.5, 1.7])),
            ('RIGID', 3): (0.1, None, np.array([0.1, 0.15, -0.11, 2.3, 4.5,
                                                1.7])),
            ('AFFINE', 3): (0.1, None, np.array([0.99, -0.05, 0.03, 1.3,
                                                 0.05, 0.99, -0.10, 2.5,
                                                 -0.07, 0.10, 0.99, -1.4]))}

        image_registeration_flow = ImageRegistrationFlow()
        apply_trans = ApplyTransformFlow()

        for i in factors.keys():
            static, moving, static_g2w, moving_g2w, smask, mmask, M = \
                setup_random_transform(transform=regtransforms[i],
                                       rfactor=factors[i][0])

            stat_file = str(i[0]) + '_static.nii.gz'
            mov_file = str(i[0]) + '_moving.nii.gz'

            save_nifti(pjoin(temp_out_dir, stat_file), data=static,
                       affine=static_g2w)

            save_nifti(pjoin(temp_out_dir, mov_file), data=moving,
                       affine=moving_g2w)

            static_image_file = pjoin(temp_out_dir,
                                      str(i[0]) + '_static.nii.gz')
            moving_image_file = pjoin(temp_out_dir,
                                      str(i[0]) + '_moving.nii.gz')

            out_moved = pjoin(temp_out_dir,
                              str(i[0]) + "_moved.nii.gz")
            out_affine = pjoin(temp_out_dir,
                               str(i[0]) + "_affine.txt")

            if str(i[0]) == "TRANSLATION":
                transform_type = "trans"
            else:
                transform_type = str(i[0]).lower()

            image_registeration_flow.run(static_image_file, moving_image_file,
                                         transform=transform_type,
                                         out_dir=temp_out_dir,
                                         out_moved=out_moved,
                                         out_affine=out_affine,
                                         level_iters=[1, 1, 1],
                                         save_metric=False)

            # Checking for the created moved file.
            assert os.path.exists(out_moved)
            assert os.path.exists(out_affine)

        images = pjoin(temp_out_dir, '*moving*')
        apply_trans.run(static_image_file, images,
                        out_dir=temp_out_dir,
                        transform_map_file=out_affine)

        # Checking for the transformed file.
        assert os.path.exists(pjoin(temp_out_dir, "transformed.nii.gz"))