Ejemplo n.º 1
0
def _get_colors_for_allowed_bands(
    phase: Phase,
    highest_hkl: Union[List[int], np.ndarray, None] = None,
    color_cycle: Optional[List[str]] = None,
):
    """Return an array of Miller indices of allowed Kikuchi bands for a
    point group and a corresponding color.

    The idea with this function is to always get the same color for the
    same band in the same point group.

    Parameters
    ----------
    phase
        A phase container with a space and point group describing the
        allowed symmetry operations.
    highest_hkl
        Highest Miller indices to consider. If None (default), [9, 9, 9]
        is used.
    color_cycle
        A list of color names recognized by Matplotlib. If None
        (default), a color palette based on EDAX TSL's coloring of bands
        is cycled through.

    Returns
    -------
    hkl_color
        Array with Miller indices and corresponding colors of shape
        (nhkl, 2, 3), with hkl and color in index 0 and 1 along axis=1,
        respectively.
    """
    if highest_hkl is None:
        highest_hkl = [9, 9, 9]
    rlp = ReciprocalLatticePoint.from_highest_hkl(
        phase=phase,
        highest_hkl=highest_hkl,
    )

    rlp2 = rlp[rlp.allowed]
    # TODO: Replace this ordering with future ordering method in
    #  diffsims
    g_order = np.argsort(rlp2.gspacing)
    new_hkl = np.atleast_2d(rlp2.hkl.data)[g_order]
    rlp3 = ReciprocalLatticePoint(phase=rlp.phase, hkl=new_hkl)
    hkl = np.atleast_2d(rlp3.hkl.data)
    families, families_idx = _get_hkl_family(hkl=hkl, reduce=True)

    if color_cycle is None:
        color_cycle = TSL_COLORS
    n_families = len(families)
    n_times = int(np.ceil(n_families / len(color_cycle)))
    colors = (color_cycle * n_times)[:n_families]
    colors = [mcolors.to_rgb(i) for i in colors]

    hkl_colors = np.zeros(shape=(rlp3.size, 2, 3))
    for hkl_idx, color in zip(families_idx.values(), colors):
        hkl_colors[hkl_idx, 0] = hkl[hkl_idx]
        hkl_colors[hkl_idx, 1] = color

    return hkl_colors
Ejemplo n.º 2
0
 def test_symmetrise(self):
     assert np.allclose(
         ReciprocalLatticePoint(phase=Phase(space_group=225), hkl=[1, 1, 1])
         .symmetrise()
         .hkl.data,
         np.array(
             [
                 [1, 1, 1],
                 [-1, 1, 1],
                 [-1, -1, 1],
                 [1, -1, 1],
                 [1, -1, -1],
                 [1, 1, -1],
                 [-1, 1, -1],
                 [-1, -1, -1],
             ]
         ),
     )
     rlp2, multiplicity = ReciprocalLatticePoint(
         phase=Phase(space_group=186), hkl=[2, 2, 0]
     ).symmetrise(return_multiplicity=True)
     assert multiplicity == 12
     assert np.allclose(
         rlp2.hkl.data,
         [
             [2, 2, 0],
             [-2, 0, 0],
             [0, -2, 0],
             [-2, -2, 0],
             [2, 0, 0],
             [0, 2, 0],
             [-2, 2, 0],
             [0, -2, 0],
             [2, 0, 0],
             [2, -2, 0],
             [0, 2, 0],
             [-2, 0, 0],
         ],
     )
     rlp3 = ReciprocalLatticePoint(
         phase=Phase(space_group=186), hkl=[2, 2, 0]
     ).symmetrise(antipodal=False)
     assert np.allclose(
         rlp3.hkl.data,
         [[2, 2, 0], [-2, 0, 0], [0, -2, 0], [-2, -2, 0], [2, 0, 0], [0, 2, 0]],
     )
Ejemplo n.º 3
0
 def test_unique(self, ferrite_phase):
     hkl = [[-1, -1, -1], [1, 1, 1], [1, 0, 0], [0, 0, 1]]
     rlp = ReciprocalLatticePoint(phase=ferrite_phase, hkl=hkl)
     assert isinstance(rlp.unique(), ReciprocalLatticePoint)
     assert np.allclose(rlp.unique(use_symmetry=False).hkl.data, hkl)
     assert np.allclose(
         rlp.unique(use_symmetry=True).hkl.data, [[1, 1, 1], [1, 0, 0]]
     )
Ejemplo n.º 4
0
 def test_allowed_b_centering(self):
     """B centering will never fire since no diffpy.structure space group has
     'B' first in the name.
     """
     phase = Phase(space_group=15)
     phase.space_group.short_name = "B"
     rlp = ReciprocalLatticePoint(
         phase=phase, hkl=[[1, 2, 2], [1, 1, -1], [1, 1, 2]]
     )
     assert np.allclose(rlp.allowed, [False, True, False])
Ejemplo n.º 5
0
 def test_init_rlp(self, nickel_phase, hkl):
     rlp = ReciprocalLatticePoint(phase=nickel_phase, hkl=hkl)
     assert rlp.phase.name == nickel_phase.name
     assert isinstance(rlp.hkl, Vector3d)
     assert rlp.structure_factor[0] is None
     assert rlp.theta[0] is None
     assert rlp.size == 2
     assert rlp.shape == (2, 3)
     assert rlp.hkl[0].shape == (1,)
     assert rlp.hkl.data[0].shape == (3,)
     assert np.issubdtype(rlp.hkl.data.dtype, int)
 def test_many_zone_axes_labels_as_markers(
     self, nickel_ebsd_simulation_generator, nickel_phase
 ):
     simgen = nickel_ebsd_simulation_generator
     rlp = ReciprocalLatticePoint(
         phase=nickel_phase,
         hkl=[[1, 1, 1], [2, 0, 0], [2, 2, 0], [3, 1, 1], [1, 2, 3]],
     )
     rlp2 = rlp.symmetrise()
     sim = simgen.geometrical_simulation(rlp2)
     _ = sim.zone_axes_labels_as_markers()
Ejemplo n.º 7
0
def nickel_rlp(request, nickel_phase):
    """A set of reciprocal lattice points for a Nickel crystal
    structure with a minimum interplanar spacing.
    """
    return ReciprocalLatticePoint(phase=nickel_phase, hkl=request.param)
class TestCrystallographicComputations:
    @pytest.mark.parametrize(
        "hkl, desired_uvw",
        [
            ([1, 1, 1], np.array([]).reshape((0, 3))),
            ([[1, 1, 1], [2, 2, 2]], np.array([]).reshape((0, 3))),
            ([[1, 1, 1], [1, 1, -1]], [[-1, 1, 0], [1, -1, 0]]),
        ],
    )
    def test_get_uvw_from_hkl(self, hkl, desired_uvw):
        """Desired uvw from the cross product of hkl."""
        assert np.allclose(_get_uvw_from_hkl(hkl), desired_uvw)

    @pytest.mark.parametrize(
        ("hkl, desired_family_keys, desired_family_values, desired_indices, "
         "reduce"),
        [
            ([1, 1, 1], [[1, 1, 1]], [1, 1, 1], [0], False),
            ([1, 1, 1], [[1, 1, 1]], [1, 1, 1], [0], True),
            (
                [[1, 1, 1], [2, 0, 0]],
                [[1, 1, 1], [2, 0, 0]],
                [[[1, 1, 1]], [[2, 0, 0]]],
                [[0], [1]],
                False,
            ),
            (
                ReciprocalLatticePoint(phase=Phase(space_group=225),
                                       hkl=[1, 1, 1]).symmetrise().hkl.data,
                [1, 1, 1],
                [[
                    [1, 1, 1],
                    [-1, 1, 1],
                    [-1, -1, 1],
                    [1, -1, 1],
                    [1, -1, -1],
                    [1, 1, -1],
                    [-1, 1, -1],
                    [-1, -1, -1],
                ]],
                [np.arange(8)],
                False,
            ),
            (
                ReciprocalLatticePoint(phase=Phase(space_group=225),
                                       hkl=[[1, 1, 1], [2, 0, 0]
                                            ]).symmetrise().hkl.data,
                [[1, 1, 1], [2, 0, 0]],
                [
                    [
                        [1, 1, 1],
                        [-1, 1, 1],
                        [-1, -1, 1],
                        [1, -1, 1],
                        [1, -1, -1],
                        [1, 1, -1],
                        [-1, 1, -1],
                        [-1, -1, -1],
                    ],
                    [
                        [2, 0, 0],
                        [0, 2, 0],
                        [-2, 0, 0],
                        [0, -2, 0],
                        [0, 0, 2],
                        [0, 0, -2],
                    ],
                ],
                [np.arange(8).tolist(),
                 np.arange(8, 14).tolist()],
                False,
            ),
            (
                ReciprocalLatticePoint(phase=Phase(space_group=225),
                                       hkl=[[1, 1, 1], [2, 2, 2]
                                            ]).symmetrise().hkl.data,
                [1, 1, 1],
                [[
                    [1, 1, 1],
                    [-1, 1, 1],
                    [-1, -1, 1],
                    [1, -1, 1],
                    [1, -1, -1],
                    [1, 1, -1],
                    [-1, 1, -1],
                    [-1, -1, -1],
                    [2, 2, 2],
                    [-2, 2, 2],
                    [-2, -2, 2],
                    [2, -2, 2],
                    [2, -2, -2],
                    [2, 2, -2],
                    [-2, 2, -2],
                    [-2, -2, -2],
                ]],
                [np.arange(16)],
                True,
            ),
        ],
    )
    def test_get_hkl_family(self, hkl, desired_family_keys,
                            desired_family_values, desired_indices, reduce):
        """Desired sets of families and indices."""
        families, families_idx = _get_hkl_family(hkl, reduce=reduce)

        for i, (k, v) in enumerate(families.items()):
            assert np.allclose(k, desired_family_keys[i])
            assert np.allclose(v, desired_family_values[i])
            assert np.allclose(families_idx[k], desired_indices[i])

    @pytest.mark.parametrize(
        "highest_hkl, color_cycle, desired_hkl_colors",
        [
            ([1, 1, 1], ["C0", "C1"], [[1, 1, 1], [0.12, 0.47, 0.71]]),
            (
                [2, 2, 2],
                ["g", "b"],
                [
                    [[1, 1, 1], [0, 0.5, 0]],
                    [[2, 0, 0], [0, 0, 1]],
                    [[2, 2, 0], [0, 0.5, 0]],
                    [[2, 2, 2], [0, 0.5, 0]],
                ],
            ),
            (
                [2, 2, 2],
                None,
                [
                    [[1, 1, 1], [1, 0, 0]],
                    [[2, 0, 0], [1, 1, 0]],
                    [[2, 2, 0], [0, 1, 0]],
                    [[2, 2, 2], [1, 0, 0]],
                ],
            ),
        ],
    )
    def test_get_colors_for_allowed_bands(self, nickel_phase, highest_hkl,
                                          color_cycle, desired_hkl_colors):
        """Desired colors for bands."""
        hkl_colors = _get_colors_for_allowed_bands(phase=nickel_phase,
                                                   highest_hkl=highest_hkl,
                                                   color_cycle=color_cycle)

        assert np.allclose(hkl_colors, desired_hkl_colors, atol=1e-2)

    def test_get_colors_for_allowed_bands_999(self, nickel_phase):
        """Not passing `highest_hkl` works fine."""
        hkl_colors = _get_colors_for_allowed_bands(phase=nickel_phase)

        assert np.shape(hkl_colors) == (69, 2, 3)
Ejemplo n.º 9
0
    def test_get_allowed_without_space_group_raises(self):
        phase = Phase(point_group="432")
        rlp = ReciprocalLatticePoint(phase=phase, hkl=[1, 1, 1])

        with pytest.raises(ValueError, match=f"The phase {phase} must have a"):
            _ = rlp.allowed
Ejemplo n.º 10
0
 def test_init_without_point_group_raises(self):
     phase = Phase()
     with pytest.raises(ValueError, match=f"The phase {phase} must have a"):
         _ = ReciprocalLatticePoint(phase=phase, hkl=[1, 1, 1])
Ejemplo n.º 11
0
    def test_one_point(self, ferrite_phase):
        rlp = ReciprocalLatticePoint(phase=ferrite_phase, hkl=[1, 1, 0])

        assert rlp.size == 1
        assert np.allclose(rlp.allowed, True)
Ejemplo n.º 12
0
 def test_calculate_theta(self, ferrite_phase, voltage, hkl, desired_theta):
     rlp = ReciprocalLatticePoint(phase=ferrite_phase, hkl=hkl)
     rlp.calculate_theta(voltage=voltage)
     assert np.allclose(rlp.theta, desired_theta)
Ejemplo n.º 13
0
 def test_calculate_structure_factor_raises(self, ferrite_phase):
     rlp = ReciprocalLatticePoint(phase=ferrite_phase, hkl=[1, 0, 0])
     with pytest.raises(ValueError, match="method=man must be among"):
         rlp.calculate_structure_factor(method="man")
     with pytest.raises(ValueError, match="'voltage' parameter must be set when"):
         rlp.calculate_structure_factor(method="doyleturner")
Ejemplo n.º 14
0
 def test_calculate_structure_factor(
     self, ferrite_phase, method, voltage, hkl, desired_factor
 ):
     rlp = ReciprocalLatticePoint(phase=ferrite_phase, hkl=hkl)
     rlp.calculate_structure_factor(method=method, voltage=voltage)
     assert np.allclose(rlp.structure_factor, desired_factor)
Ejemplo n.º 15
0
 def test_allowed(self, space_group, hkl, centering, desired_allowed):
     rlp = ReciprocalLatticePoint(phase=Phase(space_group=space_group), hkl=hkl)
     assert rlp.phase.space_group.short_name[0] == centering
     assert np.allclose(rlp.allowed, desired_allowed)