Exemplo n.º 1
0
def main_tomo_rec():
    # several dependencies (numpy, scipy) are too out-of-date in standard Python 2.6
    # distributions, as found for example on rhel6
    vers = sys.version_info
    if vers < (2, 7, 0):
        raise RuntimeError(
            "Not running this test as it requires Python >= 2.7. Version found: {0}"
            .format(vers))

    import inspect

    import IMAT.tomorec.io as tomoio

    arg_parser = setup_cmd_options()
    args = arg_parser.parse_args()

    # Save myself early. Save command this command line script and all packages/subpackages
    tomoio.self_save_zipped_scripts(
        args.output_path, os.path.abspath(inspect.getsourcefile(lambda: 0)))

    # Grab and check pre-processing options + algorithm setup + post-processing options
    preproc_config = grab_preproc_options(args)
    alg_config = grab_tool_alg_options(args)
    postproc_config = grab_postproc_options(args)

    cmd_line = " ".join(sys.argv)
    cfg = tomocfg.ReconstructionConfig(preproc_config, alg_config,
                                       postproc_config)
    # Does all the real work
    cmd = tomocmd.ReconstructionCommand()
    cmd.do_recon(cfg, cmd_line=cmd_line)
Exemplo n.º 2
0
    def test_recon_fails_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        with self.assertRaises(ValueError):
            cmd.do_recon('', cmd_line='')

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()
        alg_conf = cfgs.ToolAlgorithmConfig()
        post_conf = cfgs.PostProcConfig()
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)
        with self.assertRaises(ValueError):
            cmd.do_recon(conf, cmd_line='irrelevant')

        pre_conf.input_dir = self.test_input_dir
        import IMAT.tomorec.io as tomoio
        tomoio.make_dirs_if_needed(self.test_input_dir)
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)
        with self.assertRaises(ValueError):
            cmd.do_recon(conf, cmd_line='irrelevant')

        post_conf.output_dir = self.test_output_dir
        tomoio.make_dirs_if_needed(self.test_output_dir)
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)
        # should fail because no images found in input dir
        with self.assertRaises(RuntimeError):
            cmd.do_recon(conf, cmd_line='irrelevant')

        import os
        self.assertTrue(os.path.exists(self.test_input_dir))
        self.assertTrue(os.path.exists(os.path.join(self.test_output_dir,
                                                    '0.README_reconstruction.txt')))
        self.assertTrue(os.path.exists(self.test_output_dir))
Exemplo n.º 3
0
    def test_normalize_flat_raises(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()
        alg_conf = cfgs.ToolAlgorithmConfig()
        post_conf = cfgs.PostProcConfig()
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)

        # absolutely invalid data
        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark([], pre_conf, np.ones((10, 23)), None)

        # wrong data dimensions
        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark(np.ones((3, 2)), pre_conf, np.ones((10, 23)), None)

        # wrong dimensions of the flat image
        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark(self.data_vol, pre_conf, np.ones((10, 23)), None)

        # invalid configurations
        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark(self.data_vol, alg_conf, None, None)

        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark(self.data_vol, post_conf, None, None)

        with self.assertRaises(ValueError):
            cmd.normalize_flat_dark(self.data_vol, conf, None, None)
Exemplo n.º 4
0
    def test_read_stack_fails_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        # The images are not .tiff but .fits so the loader should raise
        with self.assertRaises(RuntimeError):
            cmd.read_in_stack(self.test_input_dir, 'tiff')
Exemplo n.º 5
0
    def test_normalize_air_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()

        normalized = cmd.normalize_air_region(self.data_vol, pre_conf)
        np.testing.assert_allclose(
            normalized,
            self.data_vol,
            err_msg="Epected normalized data volume not to changed")
Exemplo n.º 6
0
    def test_normalize_air_raises(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()
        alg_conf = cfgs.ToolAlgorithmConfig()
        post_conf = cfgs.PostProcConfig()
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)

        normalized = cmd.normalize_air_region(self.data_vol, pre_conf)
        np.testing.assert_allclose(normalized, self.data_vol,
                                   err_msg="Epected normalized data volume not to changed")

        # absolutely invalid data
        with self.assertRaises(ValueError):
            cmd.normalize_air_region([], pre_conf)

        # wrong data dimensions
        with self.assertRaises(ValueError):
            cmd.normalize_air_region(np.ones((3, 2)), pre_conf)

        # invalid configurations
        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, alg_conf)

        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, post_conf)

        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, conf)

        # wrong air-regions
        pre_conf.normalize_air_region = [3]
        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, pre_conf)

        pre_conf.normalize_air_region = (3, 0, 100, 10)
        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, pre_conf)

        pre_conf.normalize_air_region = [3, 0, 100]
        with self.assertRaises(ValueError):
            cmd.normalize_air_region(self.data_vol, pre_conf)
Exemplo n.º 7
0
    def test_normalize_flat_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()

        # ignored, with just info message
        norm = cmd.normalize_flat_dark(self.data_vol, pre_conf, None, None)

        # ignored, with just info message
        norm = cmd.normalize_flat_dark(self.data_vol, pre_conf, 45, None)

        for img_idx in range(0, self.data_vol.shape[0]):
            fake_white = self.data_vol[img_idx, :, :]
            norm = cmd.normalize_flat_dark(self.data_vol, pre_conf, fake_white, None)
            np.testing.assert_allclose(norm[img_idx, : :], np.ones(fake_white.shape),
                                       err_msg="Epected normalized data volume not to changed "
                                       "wheh using fake flat image, with index {0}".format(img_idx))
Exemplo n.º 8
0
    def test_rotate_imgs_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()
        pre_conf.rotation = 1

        (rotated, white, dark) = cmd.rotate_stack(self.data_vol, pre_conf)
        np.testing.assert_allclose(
            rotated,
            self.data_vol,
            err_msg="Epected rotated data volume not to change when "
            "the rotation option is disabled")
        self.assertEquals(
            white,
            None,
            msg="When the white stack is None, it should still be "
            "None after rotation")
        self.assertEquals(
            dark,
            None,
            msg="When the dark stack is None, it should still be "
            "None after rotation")

        pre_conf.rotation = 1
        (rotated_90, white, dark) = cmd.rotate_stack(self.data_vol, pre_conf)
        coordinates = [(3, 510, 0), (2, 2, 3), (1, 0, 0), (0, 500, 5)]
        expected_vals = [
            -0.810005187988, 0.656108379364, -0.531451165676, 0.430478185415
        ]
        for coord, expected in zip(coordinates, expected_vals):
            real_val = rotated_90[coord]
            self.assertAlmostEquals(
                real_val,
                expected,
                msg="Rotation: wrong value found at coordinate {0},{1},{2}. "
                "Expected: {3}, found: {4}".format(coord[0], coord[1],
                                                   coord[2], expected,
                                                   real_val))
Exemplo n.º 9
0
    def test_rotate_raises(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        import IMAT.tomorec.configs as cfgs
        pre_conf = cfgs.PreProcConfig()
        alg_conf = cfgs.ToolAlgorithmConfig()
        post_conf = cfgs.PostProcConfig()
        conf = cfgs.ReconstructionConfig(pre_conf, alg_conf, post_conf)

        pre_conf.rotation = 1
        # absolutely invalid data
        with self.assertRaises(ValueError):
            cmd.rotate_stack([], pre_conf)

        # wrong data type or dimensions (for samples / flats / darks
        with self.assertRaises(ValueError):
            cmd.rotate_stack(np.ones((3, 2)), pre_conf)

        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, pre_conf, [1])

        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, pre_conf, None, np.zeros((3, 3)))

        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, pre_conf, None, [0, 1])

        # invalid configurations
        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, None)

        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, [])

        with self.assertRaises(ValueError):
            cmd.rotate_stack(self.data_vol, conf)
Exemplo n.º 10
0
    def test_read_stack_fails_ok(self):
        import IMAT.tomorec.reconstruction_command as cmd
        cmd = cmd.ReconstructionCommand()

        with self.assertRaises(RuntimeError):
            cmd.read_in_stack(self.test_input_dir, 'tiff')