示例#1
0
class TestLogNormalCSDSDistribution(unittest.TestCase):

    component = None

    def setUp(self):
        self.CSDS = LogNormalCSDSDistribution()

    def tearDown(self):
        del self.CSDS

    def test_not_none(self):
        self.assertIsNotNone(self.CSDS)

    def test_data_object(self):
        self.assertIsNotNone(self.CSDS.data_object)

    test_average = create_object_attribute_test("CSDS", "average", 15)
    test_alpha_scale = create_object_attribute_test("CSDS", "alpha_scale", 0.5)
    test_alpha_offset = create_object_attribute_test("CSDS", "alpha_offset",
                                                     0.6)
    test_beta_scale = create_object_attribute_test("CSDS", "alpha_scale", 0.5)
    test_beta_offset = create_object_attribute_test("CSDS", "alpha_offset",
                                                    0.6)

    pass  # end of class
示例#2
0
class TestR1G2Model(AbstractTestProbModel, unittest.TestCase):

    prob_model_type = R1G2Model

    test_W1 = create_object_attribute_test("prob_model", "W1", 0.7)
    test_P11_or_P22 = create_object_attribute_test("prob_model", "P11_or_P22", 0.7)

    pass # end of class
示例#3
0
class TestAtom(unittest.TestCase):

    atom_type = None

    def setUp(self):
        self.atom = Atom()

    def tearDown(self):
        del self.atom

    def test_not_none(self):
        self.assertIsNotNone(self.atom)

    def test_data_object(self):
        self.assertIsNotNone(self.atom.data_object)

    test_name = create_object_attribute_test("atom", "name", "Test Name")
    test_pn = create_object_attribute_test("atom", "pn", 3)
    test_default_z = create_object_attribute_test("atom", "default_z", 5.3)
    test_stretch_values = create_object_attribute_test("atom",
                                                       "stretch_values", True)

    def test_parent(self):
        parent_atom = Atom(name="Parent")
        self.atom.parent = parent_atom
        self.assertEqual(self.atom.parent, parent_atom)

    def test_z_calculations(self):
        # Checks wether the atom can calculate stretched values:
        # 1. When everything is set up the way it should be:
        default_z = 9.0
        lattice_d = 5.4
        factor = 0.5

        class DummyComponent(object):
            def get_interlayer_stretch_factors(self):
                return lattice_d, factor

        parent = DummyComponent()
        atom = Atom(parent=parent)
        atom.stretch_values = True
        atom.default_z = default_z
        z = atom.z
        self.assertEqual(z, lattice_d + (default_z - lattice_d) * factor)
        # 2. When no component is set, but stretched is True: should not raise an error, but simple ignore the stretching
        atom.parent = None
        z = atom.z

    def test_structure_factors(self):
        import numpy as np
        rng = 2.0 * np.sin(np.arange(30)) / 0.154056
        res = self.atom.get_structure_factors(rng)
        self.assertIsNotNone(res)

    pass  # end of class
示例#4
0
class TestAtomRatio(unittest.TestCase):

    phase = None

    def setUp(self):
        self.atom1 = DummyAtom()
        self.atom2 = DummyAtom()
        self.parent = DummyParent()
        self.atom_ratio = AtomRatio(name="TestRatio",
                                    sum=2,
                                    value=0.5,
                                    atom1=[self.atom1, "attribute"],
                                    atom2=[self.atom2, "attribute"],
                                    parent=self.parent)
        self.atom_ratio.resolve_relations()

    def tearDown(self):
        del self.atom1
        del self.atom2
        del self.atom_ratio

    def test_not_none(self):
        self.assertIsNotNone(self.atom_ratio)
        self.assertIsNotNone(self.atom_ratio.atom1[0])
        self.assertIsNotNone(self.atom_ratio.atom2[0])

    def test_apply_relation(self):
        self.atom_ratio.enabled = True
        self.atom_ratio.apply_relation()
        self.assertEqual(self.atom1.attribute, 1.0)
        self.assertEqual(self.atom2.attribute, 1.0)
        self.atom_ratio.value = 0.1
        self.atom_ratio.apply_relation()
        self.assertEqual(self.atom1.attribute, 0.2)
        self.assertEqual(self.atom2.attribute, 1.8)

    test_name = create_object_attribute_test("atom_ratio", "name", "Test Name")
    test_name = create_object_attribute_test("atom_ratio", "value", 0.5)
    test_name = create_object_attribute_test("atom_ratio", "sum", 6)
    test_name = create_object_attribute_test("atom_ratio", "atom1",
                                             (None, "Test"))
    test_name = create_object_attribute_test("atom_ratio", "atom2",
                                             (None, "Test"))

    pass  # end of class
示例#5
0
class TestPhase(unittest.TestCase):

    phase = None

    def setUp(self):
        self.ucp = UnitCellProperty(name="TestUCP",
                                    value=0.0,
                                    enabled=False,
                                    factor=0.0,
                                    constant=0.0,
                                    prop=None,
                                    parent=None)

    def tearDown(self):
        del self.ucp

    def test_not_none(self):
        self.assertIsNotNone(self.ucp)

    def test_value_of_prop(self):
        class Dummy():
            attribute = "Test123"

        dummy = Dummy()
        self.ucp.prop = (dummy, "attribute")
        self.assertEqual(self.ucp.get_value_of_prop(), dummy.attribute)

        self.ucp.prop = (None, "attribute")
        self.assertEqual(self.ucp.get_value_of_prop(), 0.0)

    def test_update_value(self):
        class Dummy():
            attribute = 0.5

        dummy = Dummy()
        self.ucp.prop = (dummy, "attribute")
        self.ucp.factor = 0.5
        self.ucp.constant = 1.0

        #Check that if the prop is disabled, it can be set manually:
        self.ucp.value = 0.075
        self.assertEqual(self.ucp.value, 0.075)

        #Check that if the prop is enabled, it is calculated automatically:
        self.ucp.enabled = True
        self.assertEqual(self.ucp.value, 0.5 * 0.5 + 1.0)

        #Check that if the prop is enabled, it can't be set manually:
        self.ucp.value = 0.075
        self.assertNotEqual(self.ucp.value, 0.075)

    test_name = create_object_attribute_test("ucp", "name", "Test Name")
    test_name = create_object_attribute_test("ucp", "value", 0.5)
    test_name = create_object_attribute_test("ucp", "factor", 0.5)
    test_name = create_object_attribute_test("ucp", "constant", 0.5)
    test_name = create_object_attribute_test("ucp", "prop", (None, ""))
    test_name = create_object_attribute_test("ucp", "enabled", True)
    test_name = create_object_attribute_test("ucp", "inherited", True)

    pass  # end of class
示例#6
0
class TestR2G3Model(AbstractTestProbModel, unittest.TestCase):

    prob_model_type = R2G3Model

    test_W1 = create_object_attribute_test("prob_model", "W1", 0.7)
    test_P111_or_P212 = create_object_attribute_test("prob_model",
                                                     "P111_or_P212", 0.7)
    test_G1 = create_object_attribute_test("prob_model", "G1", 0.7)
    test_G2 = create_object_attribute_test("prob_model", "G2", 0.7)
    test_G3 = create_object_attribute_test("prob_model", "G3", 0.7)
    test_G4 = create_object_attribute_test("prob_model", "G4", 0.7)

    pass  # end of class
示例#7
0
class TestDritsCSDSDistribution(unittest.TestCase):

    component = None

    def setUp(self):
        self.CSDS = DritsCSDSDistribution()

    def tearDown(self):
        del self.CSDS

    def test_not_none(self):
        self.assertIsNotNone(self.CSDS)

    def test_data_object(self):
        self.assertIsNotNone(self.CSDS.data_object)

    test_average = create_object_attribute_test("CSDS", "average", 15)

    pass  # end of class
示例#8
0
class TestAtomType(unittest.TestCase):

    atom_type = None

    def setUp(self):
        self.atom_type = AtomType()

    def tearDown(self):
        del self.atom_type

    def test_not_none(self):
        self.assertIsNotNone(self.atom_type)

    def test_data_object(self):
        self.assertIsNotNone(self.atom_type.data_object)

    test_name = create_object_attribute_test("atom_type", "name", "Test Name")
    test_charge = create_object_attribute_test("atom_type", "charge", -5)
    test_debye = create_object_attribute_test("atom_type", "debye", 1.0)
    test_weight = create_object_attribute_test("atom_type", "weight", 60.123)
    test_atom_nr = create_object_attribute_test("atom_type", "atom_nr", 20)
    test_par_c = create_object_attribute_test("atom_type", "par_c", 10.2)
    test_par_a1 = create_object_attribute_test("atom_type", "par_a1", 10.2)
    test_par_a2 = create_object_attribute_test("atom_type", "par_a2", 10.2)
    test_par_a3 = create_object_attribute_test("atom_type", "par_a3", 10.2)
    test_par_a4 = create_object_attribute_test("atom_type", "par_a4", 10.2)
    test_par_a5 = create_object_attribute_test("atom_type", "par_a5", 10.2)
    test_par_b1 = create_object_attribute_test("atom_type", "par_b1", 10.2)
    test_par_b2 = create_object_attribute_test("atom_type", "par_b2", 10.2)
    test_par_b3 = create_object_attribute_test("atom_type", "par_b3", 10.2)
    test_par_b4 = create_object_attribute_test("atom_type", "par_b4", 10.2)
    test_par_b5 = create_object_attribute_test("atom_type", "par_b5", 10.2)

    def test_parent(self):
        parent_atom_type = AtomType(name="Parent")
        self.atom_type.parent = parent_atom_type
        self.assertEqual(self.atom_type.parent, parent_atom_type)

    pass  # end of class
示例#9
0
class TestPhase(unittest.TestCase):

    phase = None

    def setUp(self):
        settings.initialize()
        self.phase = Phase(R=0, G=1)

    def tearDown(self):
        del self.phase

    def test_not_none(self):
        self.assertIsNotNone(self.phase)

    def test_data_object(self):
        self.assertIsNotNone(self.phase.data_object)

    def test_R_G(self):
        self.assertIsNotNone(Phase(R=0, G=1))
        self.assertIsNotNone(Phase(R=0, G=2))
        self.assertIsNotNone(Phase(R=0, G=3))
        self.assertIsNotNone(Phase(R=0, G=4))
        self.assertIsNotNone(Phase(R=0, G=5))
        self.assertIsNotNone(Phase(R=0, G=6))

        self.assertIsNotNone(Phase(R=1, G=2))
        self.assertIsNotNone(Phase(R=1, G=3))
        self.assertIsNotNone(Phase(R=1, G=4))

        self.assertIsNotNone(Phase(R=2, G=2))
        self.assertIsNotNone(Phase(R=2, G=3))

        self.assertIsNotNone(Phase(R=3, G=2))

    test_name = create_object_attribute_test("phase", "name", "Test Name")
    test_display_color = create_object_attribute_test("phase", "display_color",
                                                      "#FF00FF")
    test_default_c = create_object_attribute_test("phase", "default_c", 0.646)
    test_sigma_star = create_object_attribute_test("phase", "sigma_star", 12.5)
    test_inherit_display_color = create_object_attribute_test(
        "phase", "inherit_display_color", True)
    test_inherit_CSDS_distribution = create_object_attribute_test(
        "phase", "inherit_CSDS_distribution", True)
    test_inherit_sigma_star = create_object_attribute_test(
        "phase", "inherit_sigma_star", True)
    test_inherit_probabilities = create_object_attribute_test(
        "phase", "inherit_probabilities", True)

    def test_import_export(self):
        from io import BytesIO
        phases = [Phase(R=0, G=1), Phase(R=1, G=2)]
        fn = BytesIO()
        Phase.save_phases(phases, filename=fn)
        loaded_phases = list(JSONPhaseParser.parse(fn))

        def strip_uuid(data):
            new_data = []
            for line in data.split('\n'):
                if "uuid" not in line:
                    new_data.append(line)
            return "\n".join(new_data)

        outp1 = [strip_uuid(phase.dump_object()) for phase in phases]
        outp2 = [strip_uuid(phase.dump_object()) for phase in loaded_phases]
        self.assertEqual(outp1, outp2)

    pass  # end of class
示例#10
0
class TestProject(unittest.TestCase):

    project = None

    def setUp(self):
        mock_settings()
        self.project = Project(name="Test Project")

    def tearDown(self):
        del self.project

    def test_not_none(self):
        self.assertIsNotNone(self.project)

    test_name = create_object_attribute_test("project", "name", "Test Name")
    test_date = create_object_attribute_test("project", "date", "19/09/1987")
    test_description = create_object_attribute_test("project", "description",
                                                    "Test Description")
    test_author = create_object_attribute_test("project", "author",
                                               "Test Author")
    test_layout_mode = create_object_attribute_test("project", "layout_mode",
                                                    "FULL")
    test_display_marker_align = create_object_attribute_test(
        "project", "display_marker_align", "right")
    test_display_marker_color = create_object_attribute_test(
        "project", "display_marker_color", "#FF00FF")
    test_display_marker_base = create_object_attribute_test(
        "project", "display_marker_base", 2)
    test_display_marker_top = create_object_attribute_test(
        "project", "display_marker_top", 1)
    test_display_marker_top_offset = create_object_attribute_test(
        "project", "display_marker_top_offset", 0.5)
    test_display_marker_angle = create_object_attribute_test(
        "project", "display_marker_angle", 45.6)
    test_display_marker_style = create_object_attribute_test(
        "project", "display_marker_style", "dashed")
    test_display_calc_color = create_object_attribute_test(
        "project", "display_calc_color", "#FF0099")
    test_display_exp_color = create_object_attribute_test(
        "project", "display_exp_color", "#9900FF")
    test_display_calc_lw = create_object_attribute_test(
        "project", "display_calc_lw", 5)
    test_display_exp_lw = create_object_attribute_test("project",
                                                       "display_exp_lw", 1)
    test_display_plot_offset = create_object_attribute_test(
        "project", "display_plot_offset", 1.5)
    test_display_group_by = create_object_attribute_test(
        "project", "display_group_by", 3)
    test_display_label_pos = create_object_attribute_test(
        "project", "display_label_pos", 0.75)
    test_axes_xscale = create_object_attribute_test("project", "axes_xscale",
                                                    1)
    test_axes_xmin = create_object_attribute_test("project", "axes_xmin", 15)
    test_axes_xmax = create_object_attribute_test("project", "axes_xmax", 52)
    test_axes_xstretch = create_object_attribute_test("project",
                                                      "axes_xstretch", True)
    test_axes_yscale = create_object_attribute_test("project", "axes_yscale",
                                                    1)
    test_axes_yvisible = create_object_attribute_test("project",
                                                      "axes_yvisible", True)

    # TODO
    #  - addition of phases, specimens & atom_types
    #  - loading of phases, specimens & atom_types
    #  - testing inherit properties (markers)
    #  - testing initialization deprecated keywords etc.

    pass  # end of class
示例#11
0
class TestLineMixin():
    def setUp(self):
        self.line = self.line_type()
        self.observer = LineObserver(model=self.line)

    def tearDown(self):
        self.observer.relieve_model(self.line)
        del self.observer
        del self.line

    def test_not_none(self):
        self.assertIsNotNone(self.line)

    test_lw = create_object_attribute_test('line', 'lw', 5)
    test_color = create_object_attribute_test('line', 'color', '#FF0000')
    test_label = create_object_attribute_test('line', 'label', '#FF0000')

    def _set_some_data(self):
        x = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20
        ]
        y = [
            0, 0, 0, 0, 0, 0, 10, 20, 30, 40, 80, 160, 80, 40, 30, 20, 10, 0,
            0, 0
        ]
        y = list(zip(y, y))
        self.line.set_data(x, y)

    def test_updates(self):
        self.observer.needs_update_recieved = False
        self._set_some_data()
        self.assertTrue(self.observer.needs_update_recieved)

    def test_data(self):
        self._set_some_data()
        self.assertEqual(self.line.num_columns, 3)
        self.assertEqual(self.line.max_intensity, 160)
        self.assertEqual(self.line.size, 20)
        self.assertEqual(self.line.get_y_at_x(7), 10)
        self.assertEqual(self.line.get_y_at_x(10.5), 60)

    def test_names(self):
        self.observer.needs_update_recieved = False
        self._set_some_data()
        names = ["TestName"]
        self.line.y_names = names
        self.assertEqual(self.line.get_y_name(0), names[0])

    def test_append_valid(self):
        self.line.append(0, 0)
        self.assertEqual(self.line[0], (0.0, [0.0]))

    def test_append_valid_multi(self):
        self.line.append(0, [0, 1, 2])
        self.assertEqual(self.line[0], (0.0, [0.0, 1.0, 2.0]))

    def test_signal(self):
        self.observer.needs_update_recieved = False
        self.line.lw = 10
        self.assertTrue(self.observer.needs_update_recieved)

    def test_serialisation(self):
        x = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20
        ]
        y = [
            0, 0, 0, 0, 0, 0, 10, 20, 30, 40, 80, 160, 80, 40, 30, 20, 10, 0,
            0, 0
        ]
        self.line.set_data(x, y)
        serialised1 = self.line._serialize_data()
        self.line._set_from_serial_data(serialised1)
        serialised2 = self.line._serialize_data()
        self.assertEqual(serialised1, serialised2)

    pass  # end of class
示例#12
0
class TestAtomType(unittest.TestCase):

    atom_type = None

    def setUp(self):
        self.atom_type = AtomType()

    def tearDown(self):
        del self.atom_type

    def test_not_none(self):
        self.assertIsNotNone(self.atom_type)

    def test_data_object(self):
        self.assertIsNotNone(self.atom_type.data_object)

    test_name = create_object_attribute_test("atom_type", "name", "Test Name")
    test_charge = create_object_attribute_test("atom_type", "charge", -5)
    test_debye = create_object_attribute_test("atom_type", "debye", 1.0)
    test_weight = create_object_attribute_test("atom_type", "weight", 60.123)
    test_atom_nr = create_object_attribute_test("atom_type", "atom_nr", 20)
    test_par_c = create_object_attribute_test("atom_type", "par_c", 10.2)
    test_par_a1 = create_object_attribute_test("atom_type", "par_a1", 10.2)
    test_par_a2 = create_object_attribute_test("atom_type", "par_a2", 10.2)
    test_par_a3 = create_object_attribute_test("atom_type", "par_a3", 10.2)
    test_par_a4 = create_object_attribute_test("atom_type", "par_a4", 10.2)
    test_par_a5 = create_object_attribute_test("atom_type", "par_a5", 10.2)
    test_par_b1 = create_object_attribute_test("atom_type", "par_b1", 10.2)
    test_par_b2 = create_object_attribute_test("atom_type", "par_b2", 10.2)
    test_par_b3 = create_object_attribute_test("atom_type", "par_b3", 10.2)
    test_par_b4 = create_object_attribute_test("atom_type", "par_b4", 10.2)
    test_par_b5 = create_object_attribute_test("atom_type", "par_b5", 10.2)

    def test_parent(self):
        parent_atom_type = AtomType(name="Parent")
        self.atom_type.parent = parent_atom_type
        self.assertEqual(self.atom_type.parent, parent_atom_type)

    def test_get_atomic_scattering_factors(self):
        import numpy as np
        stl_range = np.array([0.1152, 0.5756, 1.1469])
        self.atom_type.par_a1 = 10.2
        self.atom_type.par_a2 = 10.2
        self.atom_type.par_a3 = 10.2
        self.atom_type.par_a4 = 10.2
        self.atom_type.par_a5 = 10.2
        self.atom_type.par_b1 = 10.2
        self.atom_type.par_b2 = 10.2
        self.atom_type.par_b3 = 10.2
        self.atom_type.par_b4 = 10.2
        self.atom_type.par_b5 = 10.2
        self.atom_type.par_c = 20.2
        self.assertListEqual(
            self.atom_type.get_atomic_scattering_factors(stl_range).tolist(),
            [71.1827439324707, 70.77093939463964, 69.51772020477823])

    pass  # end of class
示例#13
0
class TestComponent(unittest.TestCase):

    component = None

    def setUp(self):
        self.component = Component()

    def tearDown(self):
        del self.component

    def test_not_none(self):
        self.assertIsNotNone(self.component)

    def test_data_object(self):
        self.assertIsNotNone(self.component.data_object)

    test_name = create_object_attribute_test("component", "name", "Test Name")
    test_d001 = create_object_attribute_test("component", "d001", 0.789)
    test_default_c = create_object_attribute_test("component", "default_c",
                                                  0.646)
    test_delta_c = create_object_attribute_test("component", "delta_c", 0.002)
    test_inherit_atom_relations = create_object_attribute_test(
        "component", "inherit_atom_relations", True)
    test_inherit_interlayer_atoms = create_object_attribute_test(
        "component", "inherit_interlayer_atoms", True)
    test_inherit_layer_atoms = create_object_attribute_test(
        "component", "inherit_layer_atoms", True)
    test_inherit_delta_c = create_object_attribute_test(
        "component", "inherit_delta_c", True)
    test_inherit_default_c = create_object_attribute_test(
        "component", "inherit_default_c", True)
    test_inherit_ucp_a = create_object_attribute_test("component",
                                                      "inherit_ucp_a", True)
    test_inherit_ucp_b = create_object_attribute_test("component",
                                                      "inherit_ucp_b", True)
    test_inherit_d001 = create_object_attribute_test("component",
                                                     "inherit_d001", True)

    def _setup_inheritance(self):
        self.component2 = Component()
        self.component2.linked_with = self.component
        self.component2.inherit_atom_relations = True  # TODO
        self.component2.inherit_interlayer_atoms = True  # TODO
        self.component2.inherit_layer_atoms = True  # TODO
        self.component2.inherit_delta_c = True
        self.component2.inherit_default_c = True
        self.component2.inherit_ucp_a = True  # TODO
        self.component2.inherit_ucp_b = True  # TODO
        self.component2.inherit_d001 = True

    def test_inheritance_for_delta_c(self):
        self._setup_inheritance()

        self.component.delta_c = 0.005
        self.assertEqual(self.component2.delta_c, 0.005)

    def test_inheritance_for_default_c(self):
        self._setup_inheritance()

        self.component.default_c = 0.750
        self.assertEqual(self.component2.default_c, 0.750)

    def test_inheritance_for_d001(self):
        self._setup_inheritance()

        self.component.d001 = 0.750
        self.assertEqual(self.component2.d001, 0.750)

    pass  # end of class
示例#14
0
class TestMixture(unittest.TestCase):

    atom_type = None

    def setUp(self):
        mock_settings()
        self.project = Project(name="TestProject")
        self.mixture = Mixture(name="TestMixture", parent=self.project)

    def tearDown(self):
        del self.mixture
        del self.project

    def test_not_none(self):
        self.assertIsNotNone(self.mixture)

    def test_data_object(self):
        self.assertIsNotNone(self.mixture.data_object)

    test_name = create_object_attribute_test("mixture", "name", "Test Name")

    def test_parent(self):
        parent_project = Project(name="Parent2")
        self.mixture.parent = parent_project
        self.assertEqual(self.mixture.parent, parent_project)

    def test_add_phase_slot(self):
        index = self.mixture.add_phase_slot("TestPhase", 0.5)
        self.assertEqual(index, 0, "Adding a phase slot should return the correct index!")

    def test_add_specimen_slot(self):
        index = self.mixture.add_specimen_slot(None, 1.0, 0)
        self.assertEqual(index, 0, "Adding a specimen slot should return the correct index!")

    def test_add_order1(self):
        """Test if addition works when 1st specimen slot is added before 1st phase slot"""
        self.mixture.add_specimen_slot(None, 1.0, 0)
        self.assertEqual(len(self.mixture.specimens), 1)
        self.assertEqual(len(self.mixture.phases), 0)
        self.assertEqual(len(self.mixture.fractions), 0)
        self.assertEqual(self.mixture.phase_matrix.shape, (1, 0))
        self.mixture.add_phase_slot("TestPhase", 0.5)
        self.assertEqual(len(self.mixture.specimens), 1)
        self.assertEqual(len(self.mixture.phases), 1)
        self.assertEqual(len(self.mixture.fractions), 1)
        self.assertEqual(self.mixture.phase_matrix.shape, (1, 1))
        self.assertEqual(self.mixture.phase_matrix[0, 0], None)

    def test_add_order2(self):
        """Test if addition works when 1st phase slot is added before 1st specimen slot"""
        self.mixture.add_phase_slot("TestPhase", 0.5)
        self.assertEqual(len(self.mixture.specimens), 0)
        self.assertEqual(len(self.mixture.phases), 1)
        self.assertEqual(len(self.mixture.fractions), 1)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 1))
        self.mixture.add_specimen_slot(None, 1.0, 0)
        self.assertEqual(len(self.mixture.specimens), 1)
        self.assertEqual(len(self.mixture.phases), 1)
        self.assertEqual(len(self.mixture.fractions), 1)
        self.assertEqual(self.mixture.phase_matrix.shape, (1, 1))
        self.assertEqual(self.mixture.phase_matrix[0, 0], None)

    def test_add_multiple(self):
        """Test if addition for multiple phases and specimens works as expected"""
        self.mixture.add_phase_slot("TestPhase", 0.5)
        self.mixture.add_specimen_slot(None, 1.0, 0)
        self.mixture.add_specimen_slot(None, 1.0, 0)
        self.mixture.add_phase_slot("TestPhase2", 0.5)
        self.mixture.add_phase_slot("TestPhase3", 0.5)
        self.assertEqual(len(self.mixture.specimens), 2)
        self.assertEqual(len(self.mixture.phases), 3)
        self.assertEqual(len(self.mixture.fractions), 3)
        self.assertEqual(self.mixture.phase_matrix.shape, (2, 3))

    def test_del_phase_slot(self):
        """Test if deleting a phase works as expected"""
        self.mixture.add_phase_slot("TestPhase1", 0.1)
        self.mixture.add_phase_slot("TestPhase2", 0.1)
        self.mixture.del_phase_slot(1)
        self.assertEqual(len(self.mixture.phases), 1)
        self.assertEqual(len(self.mixture.fractions), 1)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 1))
        self.mixture.del_phase_slot(0)
        self.assertEqual(len(self.mixture.phases), 0)
        self.assertEqual(len(self.mixture.fractions), 0)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 0))

    def test_del_specimen_slot(self):
        """Test if deleting a specimen works as expected"""
        self.mixture.add_specimen_slot(None, 0.5, 0)
        self.mixture.add_specimen_slot(None, 0.5, 0)
        self.mixture.del_specimen_slot(1)
        self.assertEqual(len(self.mixture.specimens), 1)
        self.assertEqual(self.mixture.phase_matrix.shape, (1, 0))
        self.mixture.del_specimen_slot(0)
        self.assertEqual(len(self.mixture.specimens), 0)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 0))

    def test_del_phase_slot_by_name(self):
        self.mixture.add_phase_slot("TestPhase1", 0.1)
        self.mixture.del_phase_slot_by_name("TestPhase1")
        self.assertEqual(len(self.mixture.phases), 0)
        self.assertEqual(len(self.mixture.fractions), 0)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 0))

    def test_del_specimen_slot_by_object(self):
        dummy = Specimen(name="Test Specimen", parent=self.project)
        self.project.specimens.append(dummy)
        self.mixture.add_specimen_slot(dummy, 0.5, 0)
        self.mixture.del_specimen_slot_by_object(dummy)
        self.assertEqual(len(self.mixture.specimens), 0)
        self.assertEqual(self.mixture.phase_matrix.shape, (0, 0))

    def test_set_specimen(self):
        dummy = Specimen(name="Test Specimen", parent=self.project)
        self.project.specimens.append(dummy)
        self.mixture.add_specimen_slot(None, 0.5, 0)
        self.mixture.set_specimen(0, dummy)
        self.assertEqual(self.mixture.specimens[0], dummy)

    def test_unset_specimen(self):
        dummy = Specimen(name="Test Specimen", parent=self.project)
        self.project.specimens.append(dummy)
        self.mixture.add_specimen_slot(dummy, 0.5, 0)
        self.mixture.unset_specimen(dummy)
        self.assertEqual(self.mixture.specimens[0], None)

    def test_unset_phase(self):
        specimen = Specimen(name="Test Specimen", parent=self.project)
        self.project.specimens.append(specimen)
        self.mixture.add_specimen_slot(specimen, 0.5, 0)
        self.mixture.add_phase_slot("Test Phase1", 0.5)

        dummy = Phase(name="Test Phase", parent=self.project)
        self.project.phases.append(dummy)
        self.mixture.set_phase(0, 0, dummy)
        self.mixture.unset_phase(dummy)
        self.assertEqual(self.mixture.phase_matrix[0, 0], None)

    def test_randomize_empty_mixture(self):
        self.mixture.refinement.randomize()

    def _refinement_setup(self):
        # TODO maybe add some more variation in the type of Phases?
        specimen = Specimen(name="Test Specimen", parent=self.project)
        self.project.specimens.append(specimen)
        phase1 = Phase(name="Test Phase1", parent=self.project)
        self.project.phases.append(phase1)
        phase2 = Phase(name="Test Phase2", parent=self.project)
        self.project.phases.append(phase2)
        self.mixture.add_specimen_slot(specimen, 0.5, 0)
        self.mixture.add_phase_slot("Test Phase1", 0.5)
        self.mixture.add_phase_slot("Test Phase2", 0.5)
        self.mixture.set_phase(0, 0, phase1)
        self.mixture.set_phase(0, 1, phase2)

    def test_randomize(self):
        self._refinement_setup()

        # Mark the attribute(s) for refinement & get their values:
        refinables = []
        for node in self.mixture.refinables.iter_children():
            ref_prop = node.object
            if ref_prop.refinable:
                ref_prop.refine = True
                refinables.append((ref_prop, ref_prop.value))

        # Randomize:
        self.mixture.refinement.randomize()

        # Check all of them have been randomized:
        # It is possible (but unlikely) that the randomized value
        # is the same as the pre-randomized value. If so run this test again
        # to make sure it is really failing.
        for ref_prop, pre_val in refinables:
            self.assertNotEqual(pre_val, ref_prop.value)

    def test_auto_restrict_empy_mixture(self):
        self.mixture.refinement.auto_restrict()

    def test_auto_restrict(self):
        self._refinement_setup()

        # Mark the attribute(s) for refinement & get their values:
        refinables = []
        for node in self.mixture.refinables.iter_children():
            ref_prop = node.object
            if ref_prop.refinable:
                ref_prop.refine = True
                refinables.append((ref_prop, ref_prop.value))

        # Randomize:
        self.mixture.refinement.auto_restrict()

        # Check all of them have been restricted:
        for ref_prop, pre_val in refinables:
            self.assertEqual(pre_val * 0.8, ref_prop.value_min)
            self.assertEqual(pre_val * 1.2, ref_prop.value_max)


    # TODO:
    #  - set_data_object
    #  - optimize
    #  - apply_current_data_object
    #  - update
    #  - get_refinement_method
    #  - setup_refine_options

    pass # end of class
示例#15
0
class TestAtom(unittest.TestCase):

    atom_type = None

    def setUp(self):
        self.atom = Atom()

    def tearDown(self):
        del self.atom

    def get_mocked_hierarchy(self):
        oxygen = Mock()
        oxygen.name = "O1-"
        hydrogen = Mock()
        hydrogen.name = "H+"

        project = Mock()
        project.atom_types = [oxygen, hydrogen]
        phase = Mock()
        phase.attach_mock(project, 'project')
        component = Mock()
        component.attach_mock(phase, 'phase')

        return oxygen, hydrogen, component, phase, project

    def test_not_none(self):
        self.assertIsNotNone(self.atom)

    def test_data_object(self):
        self.assertIsNotNone(self.atom.data_object)

    test_name = create_object_attribute_test("atom", "name", "Test Name")
    test_pn = create_object_attribute_test("atom", "pn", 3)
    test_default_z = create_object_attribute_test("atom", "default_z", 5.3)
    test_stretch_values = create_object_attribute_test("atom",
                                                       "stretch_values", True)

    def test_parent(self):
        parent_atom = Atom(name="Parent")
        self.atom.parent = parent_atom
        self.assertEqual(self.atom.parent, parent_atom)

    def test_set_atom_type(self):
        oxygen, hydrogen, component, _, _ = self.get_mocked_hierarchy()
        atom = Atom(
            parent=component,
            name="O",
            atom_type=oxygen,
        )
        atom.atom_type = hydrogen
        self.assertEqual(atom.atom_type, hydrogen)

    def test_z_calculations(self):
        # Checks wether the atom can calculate stretched values:
        # 1. When everything is set up the way it should be:
        default_z = 9.0
        lattice_d = 5.4
        factor = 0.5

        parent = Mock()
        parent.configure_mock(**{
            'get_interlayer_stretch_factors.return_value': (lattice_d, factor)
        })

        atom = Atom(parent=parent)
        atom.stretch_values = True
        atom.default_z = default_z
        z = atom.z
        self.assertEqual(z, lattice_d + (default_z - lattice_d) * factor)
        # 2. When no component is set, but stretched is True: should not raise an error, but simple ignore the stretching
        atom.parent = None
        z = atom.z

    def test_structure_factors(self):
        import numpy as np
        rng = 2.0 * np.sin(np.arange(30)) / 0.154056
        res = self.atom.get_structure_factors(rng)
        self.assertIsNotNone(res)

    def test_loads_atom_type_by_name(self):
        atom_json_dict = {
            "uuid": "878341b04e9e11e2b238150ae229a525",
            "name": "O",
            "default_z": 0.66,
            "pn": 6.0,
            "atom_type_name": "O1-"
        }

        oxygen, _, component, _, _ = self.get_mocked_hierarchy()

        atom = Atom.from_json(parent=component, **atom_json_dict)
        atom.resolve_json_references()
        self.assertEqual(atom.atom_type, oxygen)

    pass  # end of class
示例#16
0
class TestR1G4Model(AbstractTestProbModel, unittest.TestCase):

    prob_model_type = R1G4Model

    test_W1 = create_object_attribute_test("prob_model", "W1", 0.7)
    test_P11_or_P22 = create_object_attribute_test("prob_model", "P11_or_P22", 0.7)
    test_R1 = create_object_attribute_test("prob_model", "R1", 0.7)
    test_R2 = create_object_attribute_test("prob_model", "R2", 0.7)
    test_G1 = create_object_attribute_test("prob_model", "G1", 0.7)
    test_G2 = create_object_attribute_test("prob_model", "G2", 0.7)
    test_G11 = create_object_attribute_test("prob_model", "G11", 0.7)
    test_G12 = create_object_attribute_test("prob_model", "G12", 0.7)
    test_G21 = create_object_attribute_test("prob_model", "G21", 0.7)
    test_G22 = create_object_attribute_test("prob_model", "G22", 0.7)
    test_G31 = create_object_attribute_test("prob_model", "G31", 0.7)
    test_G22 = create_object_attribute_test("prob_model", "G32", 0.7)

    pass # end of class
示例#17
0
class TestComponent(unittest.TestCase):

    component = None

    def setUp(self):
        self.component = Component()

    def tearDown(self):
        del self.component

    def test_not_none(self):
        self.assertIsNotNone(self.component)

    def test_data_object(self):
        self.assertIsNotNone(self.component.data_object)

    test_name = create_object_attribute_test("component", "name", "Test Name")
    test_d001 = create_object_attribute_test("component", "d001", 0.789)
    test_default_c = create_object_attribute_test("component", "default_c",
                                                  0.646)
    test_delta_c = create_object_attribute_test("component", "delta_c", 0.002)
    test_inherit_atom_relations = create_object_attribute_test(
        "component", "inherit_atom_relations", True)
    test_inherit_interlayer_atoms = create_object_attribute_test(
        "component", "inherit_interlayer_atoms", True)
    test_inherit_layer_atoms = create_object_attribute_test(
        "component", "inherit_layer_atoms", True)
    test_inherit_delta_c = create_object_attribute_test(
        "component", "inherit_delta_c", True)
    test_inherit_default_c = create_object_attribute_test(
        "component", "inherit_default_c", True)
    test_inherit_ucp_a = create_object_attribute_test("component",
                                                      "inherit_ucp_a", True)
    test_inherit_ucp_b = create_object_attribute_test("component",
                                                      "inherit_ucp_b", True)
    test_inherit_d001 = create_object_attribute_test("component",
                                                     "inherit_d001", True)

    pass  # end of class