Ejemplo n.º 1
0
    def test_identities(self):
        # Assert that lists are cloned
        c_isos1 = masses.get_isotopes("C")
        c_isos2 = masses.get_isotopes("C")
        self.assertEqual(c_isos1, c_isos2)
        self.assertIsNot(c_isos1, c_isos2)

        for c1, c2 in itertools.product(c_isos1, c_isos2):
            self.assertIsNot(c1, c2)

        self.assertNotEqual(masses.get_isotopes("C"),
                            masses.get_isotopes("He"))
Ejemplo n.º 2
0
    def test_filtering_unlikely(self):
        # By default, unlikely isotopes (i.e. ones with natural abundance of 0)
        # are filtered out
        default = masses.get_isotopes("O")
        filtered = masses.get_isotopes("O", filter_unlikely=True)
        self.assertEqual(default, filtered)

        unfiltered = masses.get_isotopes("O", filter_unlikely=False)
        self.assertNotEqual(filtered, unfiltered)
        self.assertLess(len(filtered), len(unfiltered))

        for iso in filtered:
            self.assertIn(iso, unfiltered)
Ejemplo n.º 3
0
    def test_rare_and_unknown_isotopes(self):
        # Assert that 'foo' is not in the dictionary, and neither it gets
        # added to it.
        self.assertNotIn("foo", masses._ISOTOPES)
        self.assertEqual([], masses.get_isotopes("foo", filter_unlikely=False))
        self.assertNotIn("foo", masses._ISOTOPES)

        # Uranium has some isotopes, but no abundances so they get filtered out
        self.assertIn("U", masses._ISOTOPES)
        u_all_isos = masses.get_isotopes("U", filter_unlikely=False)
        self.assertNotEqual([], u_all_isos)

        u_filtered = masses.get_isotopes("U", filter_unlikely=True)
        self.assertEqual([], u_filtered)
Ejemplo n.º 4
0
def get_element(randomize=False,
                isotope_p=0.5,
                amount_p=0.5,
                symbol="He",
                **kwargs) -> Element:
    """Returns either a random Element or a Helium element.

    Args:
        randomize: whether a random Element is returned
        isotope_p: the likelihood of the random Element having an isotope
        amount_p: likelihood that the Element is provided a random amount
            argument.
        symbol: element's symbol as a string for a non-randomized element
        kwargs: keyword arguments passed down to non-randomized element

    Return:
        Element object.
    """
    if randomize:
        symbol = random.choice(list(masses._ISOTOPES.keys()))
        if random.random() < isotope_p:
            isotope = random.choice(
                masses.get_isotopes(symbol, filter_unlikely=False))["number"]
        else:
            isotope = None

        if random.random() < amount_p:
            amount = random.uniform(0, 100)
        else:
            amount = 0

        return Element(symbol, isotope=isotope, amount=amount)

    return Element(symbol, **kwargs)
Ejemplo n.º 5
0
    def test_sort_by_abundance(self):
        # By default, isotopes are sorted by abundance
        default = masses.get_isotopes("Li", filter_unlikely=False)
        sorted_isos = masses.get_isotopes("Li",
                                          sort_by_abundance=True,
                                          filter_unlikely=False)
        self.assertEqual(default, sorted_isos)

        unsorted = masses.get_isotopes("Li",
                                       sort_by_abundance=False,
                                       filter_unlikely=False)
        self.assertEqual(len(sorted_isos), len(unsorted))
        self.assertNotEqual(sorted_isos, unsorted)
        for iso in sorted_isos:
            self.assertLessEqual(iso["abundance"], sorted_isos[0]["abundance"])
            self.assertIn(iso, unsorted)
Ejemplo n.º 6
0
    def test_get_most_common(self):
        elems = ["C", "He", "Mn", "Li"]
        for elem in elems:
            isotopes = masses.get_isotopes(elem,
                                           sort_by_abundance=False,
                                           filter_unlikely=False)

            most_common = masses.get_most_common_isotope(elem)
            self.assertIn(most_common, isotopes)

            for iso in isotopes:
                self.assertLessEqual(iso["abundance"],
                                     most_common["abundance"])

        self.assertIsNone(masses.get_most_common_isotope("U"))
        self.assertIsNone(masses.get_most_common_isotope("foo"))