def test_rename_cuts(self): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir, self.mesu_folder) mesu = Measurement( mo.get_request(), path / f"foo.info", name="foo", measurement_setting_file_name=self.settings_file, save_on_creation=False, enable_logging=False) mesu.create_folder_structure(path) mesu.to_file() cuts = ["foo.H.0.0.cut", "foo.He.0.0.cut"] for cut in cuts: fp = mesu.get_cuts_dir() / cut fp.open("w").close() fp = mesu.get_changes_dir() / cut fp.open("w").close() mesu.name = "bar" mesu.rename_cut_files() expected = sorted(["bar.H.0.0.cut", "bar.He.0.0.cut"]) self.assertEqual(expected, sorted(os.listdir(mesu.get_cuts_dir()))) self.assertEqual(expected, sorted(os.listdir(mesu.get_changes_dir())))
def test_serialization(self): with tempfile.TemporaryDirectory() as tmp_dir: tmp_dir = Path(tmp_dir) elem = mo.get_element() prefix = elem.symbol re1 = mo.get_recoil_element(symbol=prefix) re2 = mo.get_recoil_element(name="secondary", symbol=prefix) o_re_first = mo.get_recoil_element(name="optfirst") o_re_med = mo.get_recoil_element(name="optmed") o_re_last = mo.get_recoil_element(name="optlast") es1 = ElementSimulation( tmp_dir, mo.get_request(), [re1, re2], name_prefix=prefix, save_on_creation=True, optimized_fluence=5e12, optimization_recoils=[o_re_first, o_re_med, o_re_last]) es1.to_file(save_optim_results=True) mcsimu = es1.get_default_file_path() es2 = ElementSimulation.from_file(mo.get_request(), prefix, tmp_dir, mcsimu_file=mcsimu, profile_file=None, simulation=mo.get_simulation(), save_on_creation=False) utils.assert_all_equal("He-Default", es1.get_full_name(), es2.get_full_name()) self.assertEqual("He-Default", es1.get_main_recoil().get_full_name(), es2.get_main_recoil().get_full_name()) utils.assert_all_equal(["Default", "secondary"], [r.name for r in es1.recoil_elements], [r.name for r in es2.recoil_elements]) utils.assert_all_equal(5e12, es1.optimized_fluence, es2.optimized_fluence) utils.assert_all_equal(["optfirst", "optmed", "optlast"], [r.name for r in es1.optimization_recoils], [r.name for r in es2.optimization_recoils])
def test_serialization(self): with tempfile.TemporaryDirectory() as tmp_dir: fp = Path(tmp_dir, "test.simu") sim = Simulation(Path(tmp_dir, "foo.simulation"), mo.get_request(), name="foo", description="bar", save_on_creation=False, run=mo.get_run(), detector=mo.get_detector(), target=mo.get_target(), enable_logging=False, use_request_settings=False) sim.to_file(fp) sim2 = Simulation.from_file(mo.get_request(), fp, detector=mo.get_detector(), target=mo.get_target(), run=mo.get_run(), enable_logging=False, save_on_creation=False) self.assertEqual(sim.name, sim2.name) self.assertEqual(sim.description, sim2.description) self.assertEqual(sim.measurement_setting_file_description, sim2.measurement_setting_file_description) self.assertEqual(sim.measurement_setting_file_name, sim2.measurement_setting_file_name) self.assertEqual(sim.use_request_settings, sim2.use_request_settings) self.assertEqual(sim.detector.name, sim2.detector.name) self.assertEqual(sim.detector.angle_offset, sim2.detector.angle_offset) self.assertEqual(sim.target.scattering_element, sim2.target.scattering_element) self.assertEqual(sim.target.target_type, sim2.target.target_type) self.assertEqual(sim.run.fluence, sim2.run.fluence)
def setUp(self): self.detector = mo.get_detector() self.request = mo.get_request() self.eff_files = [ "H.eff", "1H.eff", "1H-comment.eff", "2H.eff", "O.efff"] self.detector.update_directories(self.DETECTOR_DIR) for eff in self.eff_files: fp = self.DETECTOR_DIR / eff open(fp, "a").close() self.detector.add_efficiency_file(fp)
def setUp(self): self.main_rec = mo.get_recoil_element() self.kwargs = { "minimum_energy": 42.0, "minimum_scattering_angle": 17.0, "minimum_main_scattering_angle": 14.0, "number_of_preions": 3 } self.elem_sim = ElementSimulation(Path(tempfile.gettempdir()), mo.get_request(), [self.main_rec], save_on_creation=False, use_default_settings=False, **self.kwargs)
def test_get_measurement_files(self): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir, self.mesu_folder) mesu = Measurement(mo.get_request(), path / "mesu.info", measurement_setting_file_name=self.mesu_name, save_on_creation=False, enable_logging=False, use_request_settings=False) mesu.create_folder_structure(path) mesu.to_file() profile_file, mesu_file, tgt_file, det_file = \ Measurement.find_measurement_files(path) self.assertEqual(path / f"Default.profile", profile_file) self.assertEqual(path / f"{self.mesu_name}.measurement", mesu_file) self.assertEqual(path / f"Default.target", tgt_file) self.assertEqual(path / "Detector" / f"Default.detector", det_file)
def test_serialization(self): """Tests that a deserialized detector has the same attribute values as the serialized detector did. """ with tempfile.TemporaryDirectory() as tmp_dir: det_file = Path(tmp_dir, "d.detector") det1 = Detector(det_file, name="foo", description="bar", detector_type=DetectorType.TOF, virtual_size=(1, 2), tof_slope=4.4e-10, tof_offset=2, angle_slope=3, angle_offset=4, timeres=251, detector_theta=42, tof_foils=[0, 0], save_on_creation=False, foils=[self.unit_foil, self.rect_foil]) det1.to_file(det_file) det2 = Detector.from_file(det_file, mo.get_request(), save_on_creation=False) self.assertIsNot(det1, det2) self.assertEqual(det1.name, det2.name) self.assertEqual(det1.description, det2.description) self.assertEqual(det1.detector_type, det2.detector_type) self.assertIsInstance(det2.detector_type, DetectorType) self.assertEqual(det1.virtual_size, det2.virtual_size) self.assertEqual(det1.tof_slope, det2.tof_slope) self.assertEqual(det1.tof_offset, det2.tof_offset) self.assertEqual(det1.angle_slope, det2.angle_slope) self.assertEqual(det1.angle_offset, det2.angle_offset) self.assertEqual(det1.detector_theta, det2.detector_theta) self.assertEqual(det1.tof_foils, det2.tof_foils) self.compare_foils(det1.foils, det2.foils)
def test_rename_measurement(self): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir) mesu_path = path / "mesu" mesu_name = "foo" measurement = Measurement(mo.get_request(), path=mesu_path / f"{mesu_name}.info", name=mesu_name, save_on_creation=False, enable_logging=False, use_request_settings=False) measurement.create_folder_structure(mesu_path) measurement.to_file() utils.assert_folder_structure_equal( get_extected_folder_structure("mesu", "foo"), path) measurement.rename("bar") utils.assert_folder_structure_equal( get_extected_folder_structure('Measurement_00-bar', "bar"), path)
def test_serialization(self): t = Target(name="foo", modification_time=random.randint(0, 100), description="bar", target_type="AFM", image_size=(random.randint(0, 99), random.randint(0, 99)), image_file="test", target_theta=random.random(), scattering_element=mo.get_element(randomize=True), layers=[mo.get_layer()]) with tempfile.TemporaryDirectory() as tmp_dir: tgt_file = Path(tmp_dir, ".target") t.to_file(tgt_file) t2 = Target.from_file(tgt_file, mo.get_request()) self.assertIsNot(t, t2) self.assertEqual(t.name, t2.name) self.assertEqual(t.description, t2.description) self.assertEqual(t.layers[0].elements, t2.layers[0].elements) self.assertEqual(t.image_size, t2.image_size) self.assertEqual(t.target_theta, t2.target_theta) self.assertEqual(t.target_type, t2.target_type) self.assertEqual(t.scattering_element, t2.scattering_element)
def test_folder_structure(self): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir, self.mesu_folder) mesu = Measurement( mo.get_request(), path / f"{self.mesu_name}.info", name=self.mesu_name, measurement_setting_file_name=self.settings_file, save_on_creation=False, enable_logging=False, use_request_settings=False) # No files or folders should be created... utils.assert_folder_structure_equal({}, Path(tmp_dir)) # ... until create_folder_structure is called mesu.create_folder_structure(path) utils.assert_folder_structure_equal(self.folder_structure, Path(tmp_dir)) mesu.to_file() utils.assert_folder_structure_equal(self.after_to_file, Path(tmp_dir))