Esempio n. 1
0
    def test_espe_params(self, mock_exec):
        esp = EnergySpectrumParamsDialog(Mock(),
                                         "measurement",
                                         measurement=mo.get_measurement())
        esp.close()

        # ValueError is raised if spectrum type is wrong
        self.assertRaises(ValueError,
                          lambda: EnergySpectrumParamsDialog(Mock(), "simu"))

        # AttributeError is raised if simulation has no sample
        self.assertRaises(
            AttributeError, lambda: EnergySpectrumParamsDialog(
                Mock(),
                "simulation",
                simulation=mo.get_simulation(),
                element_simulation=mo.get_element_simulation(),
                recoil_widget=Mock()))

        sample = mo.get_sample()
        sim = mo.get_simulation()
        sim.sample = sample
        elem_sim = sim.add_element_simulation(mo.get_recoil_element(),
                                              save_on_creation=False)
        esp = EnergySpectrumParamsDialog(Mock(),
                                         "simulation",
                                         simulation=sim,
                                         element_simulation=elem_sim,
                                         recoil_widget=Mock())
        esp.close()

        assert mock_exec.call_count == 2
Esempio n. 2
0
 def test_raises_exception_if_recoil_added_twice(self):
     sim = mo.get_simulation()
     rec = mo.get_recoil_element()
     sim.add_element_simulation(rec, save_on_creation=False)
     self.assertRaises(
         ValueError,
         lambda: sim.add_element_simulation(rec, save_on_creation=False))
Esempio n. 3
0
    def test_add_element_simulation(self):
        sim = mo.get_simulation()
        rec_c = mo.get_recoil_element(symbol="C")
        rec_16c = mo.get_recoil_element(symbol="C", isotope=16)

        elem_sim_c = sim.add_element_simulation(rec_c, save_on_creation=False)
        elem_sim_16c = sim.add_element_simulation(rec_16c,
                                                  save_on_creation=False)

        self.assertEqual([elem_sim_c, elem_sim_16c], sim.element_simulations)
Esempio n. 4
0
    def test_get_recoils(self):
        sim = mo.get_simulation()
        n = 10
        elems = set(
            mo.get_element(randomize=True, amount_p=0) for _ in range(n))
        rec_elems = [mo.get_recoil_element(element=elem) for elem in elems]
        for rec in rec_elems:
            sim.add_element_simulation(rec, save_on_creation=False)

        self.assertEqual(rec_elems, sim.get_recoil_elements())
Esempio n. 5
0
    def test_can_add_recoil(self):
        sim = mo.get_simulation()
        rec = mo.get_recoil_element()
        self.assertTrue(sim.can_add_recoil(rec))
        sim.add_element_simulation(rec, save_on_creation=False)

        rec2 = mo.get_recoil_element()
        self.assertFalse(sim.can_add_recoil(rec2))

        rec2.element.isotope = 1
        self.assertTrue(sim.can_add_recoil(rec2))
Esempio n. 6
0
    def test_has_element(self):
        sim = mo.get_simulation()
        elem = mo.get_element()

        self.assertFalse(sim.has_element(elem))

        rec = mo.get_recoil_element(symbol=elem.symbol)

        sim.add_element_simulation(rec, save_on_creation=False)

        self.assertTrue(sim.has_element(elem))
Esempio n. 7
0
    def test_calculate_spectrum(self, mock_run, mock_get_espe):
        """Tests that the file paths generated during energy spectrum
        calculation are correct depending on the type of optimization.
        """
        self.main_rec.name = "main_rec"
        optim_recoil = mo.get_recoil_element()
        optim_recoil.prefix = "C"
        optim_recoil.name = "optimized"
        espe_recoil = mo.get_recoil_element()
        espe_recoil.prefix = "H"
        espe_recoil.name = "spectrum"

        self.elem_sim.optimization_recoils = [optim_recoil]
        self.elem_sim.simulation = mo.get_simulation()

        with tempfile.TemporaryDirectory() as tmp_dir:
            self.elem_sim.directory = tmp_dir

            # No optimization
            rec_file = Path(tmp_dir, f"{espe_recoil.get_full_name()}.recoil")
            erd_file = Path(tmp_dir, f"{self.main_rec.get_full_name()}.*.erd")
            espe_file = Path(tmp_dir, f"{espe_recoil.get_full_name()}.simu")

            kwargs = {"recoil_element": espe_recoil}
            self.assert_files_equal(mock_get_espe, kwargs, rec_file, erd_file,
                                    espe_file)

            # Recoil optimization
            rec_file = Path(tmp_dir, f"{espe_recoil.get_full_name()}.recoil")
            erd_file = Path(tmp_dir, f"{optim_recoil.prefix}-opt.*.erd")
            espe_file = Path(tmp_dir, f"{espe_recoil.get_full_name()}.simu")

            kwargs = {
                "recoil_element": espe_recoil,
                "optimization_type": OptimizationType.RECOIL
            }
            self.assert_files_equal(mock_get_espe, kwargs, rec_file, erd_file,
                                    espe_file)

            # Fluence optimization
            rec_file = Path(tmp_dir, f"{espe_recoil.prefix}-optfl.recoil")
            erd_file = Path(tmp_dir, f"{self.main_rec.prefix}-optfl.*.erd")
            espe_file = Path(tmp_dir, f"{espe_recoil.prefix}-optfl.simu")

            kwargs = {
                "recoil_element": espe_recoil,
                "optimization_type": OptimizationType.FLUENCE
            }
            self.assert_files_equal(mock_get_espe, kwargs, rec_file, erd_file,
                                    espe_file)

            self.assertEqual(mock_run.call_count, 3)
Esempio n. 8
0
    def test_initialization(self, mock_exec):
        """A simple test to see if various dialogs get initialized properly.
        """
        e = ElementLossesDialog(Mock(), mo.get_measurement())
        e.close()

        c = CalibrationDialog([mo.get_measurement()], mo.get_detector(),
                              mo.get_run())
        c.close()

        # AttributeError is raised if simulation has no sample
        self.assertRaises(
            AttributeError,
            lambda: OptimizationDialog(mo.get_simulation(), Mock()))

        sample = mo.get_sample()
        sim = mo.get_simulation()
        sim.sample = sample
        o = OptimizationDialog(sim, Mock())
        self.assertFalse(o.pushButton_OK.isEnabled())
        o.close()

        assert mock_exec.call_count == 3
Esempio n. 9
0
    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])
Esempio n. 10
0
    def test_get_active_simulation(self, mock_run):
        sim = mo.get_simulation()

        sim.add_element_simulation(mo.get_recoil_element(),
                                   save_on_creation=False)
        self.assertEqual(([], [], [], []), sim.get_active_simulations())

        elem_sim = sim.element_simulations[0]
        elem_sim.use_default_settings = False
        elem_sim.start(1, 1).run()
        self.assertEqual(([], [elem_sim], [], []),
                         sim.get_active_simulations())
        mock_run.assert_called_once()

        elem_sim.optimization_recoils = [mo.get_recoil_element()]
        elem_sim.start(1, 1, optimization_type=OptimizationType.RECOIL).run()
        self.assertEqual(([], [elem_sim], [], [elem_sim]),
                         sim.get_active_simulations())
Esempio n. 11
0
    def test_initialization(self):
        tab = Mock()
        sim = mo.get_simulation()
        target = mo.get_target()
        icons = IconManager()
        settings = mo.get_global_settings()

        widget = TargetWidget(tab,
                              sim,
                              target,
                              icons,
                              settings,
                              auto_save=False)
        rec_dist = widget.recoil_distribution_widget

        self.assertFalse(rec_dist.main_recoil_selected())
        self.assertIsNone(rec_dist.get_current_main_recoil())

        widget.close()
Esempio n. 12
0
 def test_simulation_has_slots(self):
     """Tests that __slots__ work correctly for Simulation."""
     sim = mo.get_simulation()
     utils.assert_has_slots(sim)