def test_set_phase_point_group(self, point_group, point_group_name, fails): p = Phase() if fails: with pytest.raises(ValueError, match=f"'{point_group}' must be of type"): p.point_group = point_group else: p.point_group = point_group assert p.point_group.name == point_group_name
def test_shallowcopy_phase(self): p = Phase(name="al", point_group="m-3m", color="C1") p2 = p p2.name = "austenite" p2.point_group = 43 p2.color = "C2" assert p.__repr__() == p2.__repr__()
def test_set_phase_color(self, color, color_alias, color_rgb, fails): p = Phase() if fails: with pytest.raises(ValueError, match="Invalid RGBA argument: "): p.color = color else: p.color = color assert p.color == color_alias assert np.allclose(p.color_rgb, color_rgb, atol=1e-6)
def test_is_hexagonal(self): p1 = Phase( point_group="321", structure=Structure(lattice=Lattice(1, 1, 2, 90, 90, 120)), ) p2 = Phase( point_group="m-3m", structure=Structure(lattice=Lattice(1, 1, 1, 90, 90, 90)), ) assert p1.is_hexagonal assert not p2.is_hexagonal
def test_phase_repr_str( self, name, space_group, desired_sg_str, desired_pg_str, desired_ppg_str, ): p = Phase(name=name, space_group=space_group, color="C0") desired = ( f"<name: {name}. " + f"space group: {desired_sg_str}. " + f"point group: {desired_pg_str}. " + f"proper point group: {desired_ppg_str}. " + "color: tab:blue>" ) assert p.__repr__() == desired assert p.__str__() == desired
def test_init_phaselist_from_phases(self, phase_collection): p1 = Phase(name="austenite", point_group=432, color=None) p2 = Phase(name="ferrite", point_group="432", color="C1") if phase_collection == "dict": phases = {1: p1, 2: p2} else: # phase_collection == "list": phases = [p1, p2] pl = PhaseList(phases) assert pl.names == [p.name for p in [p1, p2]] assert pl.point_groups == [p.point_group for p in [p1, p2]] assert pl.space_groups == [p.space_group for p in [p1, p2]] assert pl.colors == [p.color for p in [p1, p2]] assert pl.colors_rgb == [p.color_rgb for p in [p1, p2]]
def test_init_empty_phaselist(self, empty_input): pl = PhaseList(empty_input) assert repr(pl) == "No phases." pl.add(Phase("al", point_group="m-3m")) assert repr(pl) == ( "Id Name Space group Point group Proper point group Color\n" " 0 al None m-3m 432 tab:blue")
def test_add_to_phaselist_raises(self): """Trying to add a Phase with a name already in the PhaseList raises a ValueError. """ pl = PhaseList(names=["a"]) with pytest.raises(ValueError, match="'a' is already in the phase list"): pl.add(Phase("a"))
def test_init_phase( self, name, point_group, space_group, color, color_alias, color_rgb, structure ): p = Phase( name=name, point_group=point_group, space_group=space_group, structure=structure, color=color, ) if name is None: assert p.name == structure.title else: assert p.name == str(name) if space_group is None: assert p.space_group is None else: assert p.space_group.number == space_group if point_group == "43": point_group = "432" if isinstance(point_group, Symmetry): point_group = point_group.name assert p.point_group.name == point_group assert p.color == color_alias assert np.allclose(p.color_rgb, color_rgb, atol=1e-6) if structure is not None: assert p.structure == structure else: assert p.structure == Structure()
def test_init_phaselist_from_phase(self): p = Phase(name="austenite", point_group="432", color="C2") pl = PhaseList(p) assert pl.names == [p.name] assert pl.point_groups == [p.point_group] assert pl.space_groups == [p.space_group] assert pl.colors == [p.color] assert pl.colors_rgb == [p.color_rgb]
def test_shallowcopy_phaselist(self, phase_list): pl2 = phase_list phase_list.add(Phase("d", point_group="m-3m")) assert pl2.names == phase_list.names assert [s2.name for s2 in pl2.point_groups ] == [s.name for s in phase_list.point_groups] assert pl2.colors == phase_list.colors
def test_add_phase_in_empty_phaselist(self): """Add Phase to empty PhaseList.""" sg_no = 10 name = "a" pl = PhaseList() pl.add(Phase(name, space_group=sg_no)) assert pl.ids == [0] assert pl.names == [name] assert pl.space_groups == [GetSpaceGroup(sg_no)] assert pl.structures == [Structure()]
def test_orientations_symmetry(self, point_group, rotation, expected_orientation): r = Rotation(rotation) cm = CrystalMap(rotations=r, phase_id=np.array([0])) cm.phases = PhaseList(Phase("a", point_group=point_group)) o = cm.orientations assert np.allclose( o.data, Orientation(r).set_symmetry(point_group).data, atol=1e-3 ) assert np.allclose(o.data, expected_orientation, atol=1e-3)
def test_deepcopy_phase(self): p = Phase(name="al", space_group=225, color="C1") p2 = p.deepcopy() desired_p_repr = ( "<name: al. space group: Fm-3m. point group: m-3m. proper point group: 432." " color: tab:orange>") assert p.__repr__() == desired_p_repr p.name = "austenite" p.space_group = 229 p.color = "C2" new_desired_p_repr = ( "<name: austenite. space group: Im-3m. point group: m-3m. proper point " "group: 432. color: tab:green>") assert p.__repr__() == new_desired_p_repr assert p2.__repr__() == desired_p_repr
def test_add_list_phases_to_phaselist(self): """Add a list of Phase objects to PhaseList, also ensuring that unique colors are given. """ names = ["a", "b"] sg_no = [10, 20] colors = ["tab:blue", "tab:orange"] pl = PhaseList(names=names, space_groups=sg_no) assert pl.colors == colors new_names = ["c", "d"] new_sg_no = [30, 40] pl.add([Phase(name=n, space_group=i) for n, i in zip(new_names, new_sg_no)]) assert pl.names == names + new_names assert pl.space_groups == ( [GetSpaceGroup(i) for i in sg_no] + [GetSpaceGroup(i) for i in new_sg_no] ) assert pl.colors == colors + ["tab:green", "tab:red"]
def test_deepcopy_phaselist(self, phase_list): names = phase_list.names point_groups = [s.name for s in phase_list.point_groups] colors = phase_list.colors pl2 = phase_list.deepcopy() assert pl2.names == names phase_list.add(Phase("d", point_group="m-3m")) phase_list["d"].color = "g" assert phase_list.names == names + ["d"] assert [s.name for s in phase_list.point_groups] == point_groups + ["m-3m"] assert phase_list.colors == colors + ["g"] assert pl2.names == names assert [s.name for s in pl2.point_groups] == point_groups assert pl2.colors == colors
def test_set_phase_id_with_unknown_id(self, crystal_map, set_phase_id, index_error): cm = crystal_map condition = cm.x > 1.5 phase_ids = cm.phases.ids # Get before adding a new phase if index_error: with pytest.raises(IndexError, match="list index out of range"): # `set_phase_id` ID is not in `self.phases.phase_ids` cm[condition].phase_id = set_phase_id _ = repr(cm) # Add unknown ID to phase list to fix `repr(self)` cm.phases.add(Phase("a", point_group=432)) # Add phase with ID 1 else: cm[condition].phase_id = set_phase_id _ = repr(cm) new_phase_ids = phase_ids + [set_phase_id] new_phase_ids.sort() assert cm.phases.ids == new_phase_ids
def test_set_space_group_raises(self): space_group = "outer-space" with pytest.raises(ValueError, match=f"'{space_group}' must be of type "): p = Phase() p.space_group = space_group
def test_set_phase_name(self, name): p = Phase(name=name) if name is None: name = "" assert p.name == str(name)
def test_set_structure(self, structure): p = Phase() p.structure = structure assert p.structure == structure
def test_set_structure_phase_name(self): name = "al" p = Phase(name=name) p.structure = Structure(lattice=Lattice(*([0.405] * 3 + [90] * 3))) assert p.name == name assert p.structure.title == name
def test_set_structure_raises(self): p = Phase() with pytest.raises(ValueError, match=".* must be a diffpy.structure.Structure"): p.structure = [1, 2, 3, 90, 90, 90]
def test_phase_init_non_matching_space_group_point_group(self): with pytest.warns(UserWarning, match="Setting space group to 'None', as"): _ = Phase(space_group=225, point_group="432")
def test_point_group_derived_from_space_group( self, space_group_no, desired_point_group_name ): p = Phase(space_group=space_group_no) assert p.point_group.name == desired_point_group_name