Exemplo n.º 1
0
 def test_hash_properties(self):
     n = 1000
     for _ in range(n):
         e1 = mo.get_element(randomize=True, amount_p=0.0)
         e2 = mo.get_element(randomize=True, amount_p=0.0)
         if e1.symbol == e2.symbol and e1.isotope == e2.isotope:
             self.assertEqual(hash(e1), hash(e2))
         else:
             self.assertNotEqual(hash(e1), hash(e2))
Exemplo n.º 2
0
 def test_get_rbs_selections(self):
     with tempfile.TemporaryDirectory() as tmp_dir:
         path = Path(tmp_dir, self.rel_dir)
         self._generate_cut_files(path)
         files = [Path(entry.path) for entry in os.scandir(path)]
         rbs = cut_file.get_rbs_selections(files)
         self.assertEqual(
             {
                 "1He.RBS_He.0.cut": mo.get_element(symbol="He"),
                 "He.RBS_Cl.0.cut": mo.get_element(symbol="Cl"),
                 "He.RBS_Cl.1.cut": mo.get_element(symbol="Cl"),
             }, rbs)
Exemplo n.º 3
0
    def assert_settings_equal(self,
                              gs1: GlobalSettings,
                              gs2: GlobalSettings,
                              only_check_same_size=False):
        """Asserts that the two GlobalSettings objects return same values.
        If only_check_same_size is True, the return values of
        get_element_colors are only checked if they are the same size.
        """
        getters = [
            method for method in dir(gs1)
            if method.startswith("get") or method.startswith("is")
        ]

        for getter in getters:
            if getter == "get_element_color":
                args = mo.get_element(randomize=True).symbol,
            elif getter == "get_import_timing":
                args = random.randint(0, 2),
            else:
                args = ()

            val1 = getattr(gs1, getter)(*args)
            val2 = getattr(gs2, getter)(*args)

            if only_check_same_size:
                if getter == "get_element_colors":
                    if len(val1) != len(val2):
                        continue

            self.assertEqual(val1, val2)
Exemplo n.º 4
0
    def test_sorting_points(self):
        """Points should always remain sorted.
        """
        n = 10
        iters = 10
        maximum = 20
        minimum = 0
        rand = lambda: random.uniform(minimum, maximum)
        for _ in range(iters):
            points = [
                Point(rand(), rand())
                for _ in range(n)
            ]
            points_sorted = sorted(points)

            rec_elem = RecoilElement(
                mo.get_element(), points)

            self.assertEqual(points_sorted, rec_elem.get_points())

            p_0 = Point(minimum - 1, rand())
            p_n = Point(maximum + 1, rand())
            rec_elem.add_point(p_0)
            rec_elem.add_point(p_n)

            self.assertIs(p_0, rec_elem.get_first_point())
            self.assertIs(p_n, rec_elem.get_last_point())
Exemplo n.º 5
0
 def test_equals_prop_based(self):
     n = 1000
     for _ in range(n):
         elem1 = mo.get_element(randomize=True)
         elem1_str = str(elem1)
         elem2 = Element.from_string(elem1_str)
         self.assertIsNot(elem1, elem2)
         self.assertEqual(elem1, elem2)
         self.assertEqual(hash(elem1), hash(elem2))
Exemplo n.º 6
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())
Exemplo n.º 7
0
    def _generate_cut_files(self, directory):
        cut = CutFile(directory=directory)
        cut.set_info(mo.get_selection(), self.data)
        cut.save()
        cut.save()

        cut2 = CutFile(directory=directory)
        cut2.set_info(mo.get_selection(), self.data)
        cut2.element = mo.get_element(symbol="He", isotope=1)
        cut2.element_scatter = mo.get_element()
        cut2.save()

        cut3 = CutFile(directory=directory)
        cut3.set_info(mo.get_selection(), self.data)
        cut3.type = "ERD"
        cut3.element_scatter = ""
        cut3.save()
        cut3.save()
        cut3.save(element_count=10)
Exemplo n.º 8
0
    def setUp(self):
        self.p2_args = 1, 1
        self.p1 = Point(0, 0)
        self.p2 = Point(self.p2_args)
        self.p3 = Point(2, 0)

        points = [
            self.p1, self.p2, self.p3
        ]
        self.rec_elem = RecoilElement(mo.get_element(), points)
Exemplo n.º 9
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))
Exemplo n.º 10
0
    def test_sorting(self):
        # Checks that recoil elements are sorted in the same way as elements
        n = 10
        iterations = 10
        for _ in range(iterations):
            elems = [mo.get_element(randomize=True) for _ in range(n)]
            rec_elems = [RecoilElement(elem, []) for elem in elems]
            random.shuffle(elems)
            random.shuffle(rec_elems)

            elems.sort()
            rec_elems.sort()
            for e, r in zip(elems, rec_elems):
                self.assertEqual(e, r.element)
Exemplo n.º 11
0
 def test_adjustable_settings(self):
     beam = mo.get_beam()
     kwargs = {
         "energy": 1,
         "charge": 2,
         "ion": mo.get_element(randomize=True),
         "energy_distribution": 3,
         "spot_size": (1, 2),
         "profile": "foo",
         "divergence": 7
     }
     self.assertNotEqual(kwargs, beam.get_settings())
     beam.set_settings(**kwargs)
     self.assertEqual(kwargs, beam.get_settings())
Exemplo n.º 12
0
 def setUp(self):
     self.timestamp = time.time()
     self.rec_type = "rec"
     self.ch_width = 4
     self.rec_elem = RecoilElement(
         mo.get_element(),
         [Point((0, 4)),
          Point((1, 5)),
          Point((2, 10))],
         color="black",
         description="foo",
         name="bar",
         rec_type="rec",
         reference_density=3,
         channel_width=self.ch_width,
         modification_time=self.timestamp
     )
Exemplo n.º 13
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])
Exemplo n.º 14
0
    def test_most_common_isotope(self):
        self.assertEqual(4, Element("He").get_most_common_isotope())
        self.assertIsNone(Element("U").get_most_common_isotope())

        # get_most_common_isotope just simply returns the number value of
        # masses.get_most_common_isotope
        for _ in range(100):
            e = mo.get_element(randomize=True)

            mi = masses.get_most_common_isotope(e.symbol)

            if mi is None:
                self.assertEqual(mi, e.get_most_common_isotope())
            else:
                self.assertEqual(
                    masses.get_most_common_isotope(
                        e.symbol)[masses.NUMBER_KEY],
                    e.get_most_common_isotope())
Exemplo n.º 15
0
    def test_loading(self):
        with tempfile.TemporaryDirectory() as tmp_dir:
            path = Path(tmp_dir, self.rel_dir)
            cut1 = CutFile(directory=path, split_count=4)
            cut1.set_info(mo.get_selection(), self.data)
            cut1.element = mo.get_element(symbol="F")
            cut1.save()

            fp = path / "mesu1.F.RBS_Cl.0.cut"

            cut2 = CutFile()
            cut2.load_file(fp)
            cut1_d = dict(vars(cut1))
            cut2_d = dict(vars(cut2))

            self.assertIsNone(cut1_d.pop("element_number"))
            self.assertEqual(0, cut2_d.pop("element_number"))

            self.assertTrue(len(cut1_d) > 0)
            self.assertEqual(cut1_d, cut2_d)
Exemplo n.º 16
0
    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)