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))
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)
def __init__(self): self._PREPROC_IMGS_SUBDIR_NAME = 'preproc_images' self._OUT_README_FNAME = '0.README_reconstruction.txt' self._OUT_SLICES_FILENAME_PREFIX='out_recon_slice' self._OUT_HORIZ_SLICES_SUBDIR='out_recon_horiz_slice' self.preproc_cfg = tomocfg.PreProcConfig() self.alg_cfg = tomocfg.ToolAlgorithmConfig self.postproc_cfg = tomocfg.PostProcConfig()
def grab_preproc_options(args): """ Get pre-proc options from the command line (through an argument parser) @param parser :: arguments parser already, set up with pre-processing options Returns:: a pre-processing config object set up according to the user inputs in the command line """ import ast pre_config = tomocfg.PreProcConfig() pre_config.input_dir = args.input_path pre_config.input_dir_flat = args.input_path_flat pre_config.input_dir_dark = args.input_path_dark if args.in_img_format: pre_config.in_img_format = args.in_img_format if args.out_img_format: pre_config.out_img_format = args.out_img_format if args.max_angle: pre_config.max_angle = float(args.max_angle) if args.rotation: pre_config.rotation = int(args.rotation) if args.air_region: coords = ast.literal_eval(args.air_region) pre_config.normalize_air_region = [int(val) for val in coords] if args.air_region: coords = ast.literal_eval(args.air_region) pre_config.normalize_air_region = [int(val) for val in coords] if args.region_of_interest: roi_coords = ast.literal_eval(args.region_of_interest) pre_config.crop_coords = [int(val) for val in roi_coords] if 'yes' == args.mcp_corrections: pre_config.mcp_corrections = True if args.median_filter_size: if isinstance(args.median_filter_size, str) and not args.median_filter_size.isdigit(): raise RuntimeError( "The median filter size/width must be an integer") pre_config.median_filter_size = args.median_filter_size if 'wf' == args.remove_stripes: pre_config.stripe_removal_method = 'wavelet-fourier' pre_config.cor = int(args.cor) return pre_config
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")
def test_config_all(self): """ Basic consistency check of the tomographic reconstruction config and some default values """ 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) self.assertEquals(conf.preproc_cfg, pre_conf) self.assertEquals(conf.alg_cfg, alg_conf) self.assertEquals(conf.postproc_cfg, post_conf) print conf
def test_config_pre(self): """ Basic consistency check of the pre-processing config and some default settings """ import IMAT.tomorec.configs as cfgs pre = cfgs.PreProcConfig() self.assertEquals(pre.input_dir, None) self.assertEquals(pre.input_dir_flat, None) self.assertEquals(pre.input_dir_dark, None) self.assertEquals(pre.max_angle, 360) self.assertEquals(pre.normalize_flat_dark, True) self.assertEquals(pre.crop_coords, None) self.assertEquals(pre.scale_down, 0) self.assertEquals(pre.stripe_removal_method, 'wavelet-fourier') self.assertEquals(pre.save_preproc_imgs, True)
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)
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))
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))
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)