示例#1
0
    def test_plot_phase(self, crystal_map_input, phase_list,
                        expected_data_shape):
        cm = CrystalMap(**crystal_map_input)

        assert np.unique(cm.phase_id) == np.array([0])  # Test code assumption
        assert phase_list.ids == [0, 1, 2]
        cm.phases = phase_list
        cm[0, 0].phase_id = 0
        cm[1, 1].phase_id = 2

        fig = plt.figure()
        ax = fig.add_subplot(projection=PLOT_MAP)
        im = ax.plot_map(cm)

        # Expected image data
        phase_id = cm.get_map_data("phase_id")
        unique_phase_ids = np.unique(phase_id[~np.isnan(phase_id)])
        expected_data = np.ones(phase_id.shape + (3, ))
        for i, color in zip(unique_phase_ids, cm.phases_in_data.colors_rgb):
            mask = phase_id == int(i)
            expected_data[mask] = expected_data[mask] * color

        image_data = im.get_array()
        assert np.allclose(image_data.shape, expected_data_shape)
        assert np.allclose(image_data, expected_data)

        plt.close("all")
示例#2
0
    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)
示例#3
0
    def test_get_orientations_array(self, crystal_map_input, phase_list):
        cm = CrystalMap(**crystal_map_input)

        cm[:2, 0].phase_id = 1
        # Test code assumption
        id1 = 0
        id2 = 1
        assert np.allclose(np.unique(cm.phase_id), np.array([id1, id2]))
        cm.phases = phase_list

        # Get all with string
        o = cm.get_map_data("orientations")

        # Get per phase with string
        cm1 = cm[cm.phase_id == id1]
        cm2 = cm[cm.phase_id == id2]
        o1 = cm1.get_map_data("orientations")
        o2 = cm2.get_map_data("orientations")

        expected_o1 = cm1.orientations.to_euler()
        expected_shape = expected_o1.shape
        assert np.allclose(
            o1[~np.isnan(o1)].reshape(expected_shape), expected_o1, atol=1e-3
        )

        expected_o2 = cm2.orientations.to_euler()
        expected_shape = expected_o2.shape
        assert np.allclose(
            o2[~np.isnan(o2)].reshape(expected_shape), expected_o2, atol=1e-3
        )

        # Do calculations "manually"
        data_shape = (cm.size, 3)
        array = np.zeros(data_shape)

        if cm.rotations_per_point > 1:
            rotations = cm.rotations[:, 0]
        else:
            rotations = cm.rotations

        for i, phase in cm.phases_in_data:
            phase_mask = cm._phase_id == i
            phase_mask_in_data = cm.phase_id == i
            array[phase_mask] = (
                Orientation(rotations[phase_mask_in_data])
                .set_symmetry(phase.point_group)
                .to_euler()
            )

        assert np.allclose(o, array.reshape(o.shape), atol=1e-3)
示例#4
0
    def test_plot_masked_phase(self, crystal_map_input, phase_list):
        cm = CrystalMap(**crystal_map_input)

        # Test code assumptions
        assert np.unique(cm.phase_id) == np.array([0])
        assert phase_list.ids == [0, 1, 2]
        assert cm.ndim == 3

        cm.phases = phase_list

        cm[0, :2, :1].phase_id = 1
        cm[1, 2:4, :1].phase_id = 2
        assert np.allclose(np.unique(cm.phase_id), np.array([0, 1, 2]))

        # One phase plot per masked map
        for i, phase in phase_list:
            fig = plt.figure()
            ax = fig.add_subplot(projection=PLOT_MAP)
            _ = ax.plot_map(cm[cm.phase_id == i])

        plt.close("all")
示例#5
0
    def test_orientations(self, crystal_map_input, phase_list):
        x = crystal_map_input["x"]

        # Create phase ID array, ensuring all are present
        phase_ids = np.random.choice(phase_list.ids, x.size)
        for i, unique_id in enumerate(phase_list.ids):
            phase_ids[i] = unique_id

        crystal_map_input.pop("phase_id")
        cm = CrystalMap(phase_id=phase_ids, **crystal_map_input)

        # Set phases and make sure all are in data
        cm.phases = phase_list
        assert cm.phases_in_data.names == cm.phases.names

        # Ensure all points have a valid orientation
        orientations_size = 0
        for phase_id in phase_list.ids:
            o = cm[cm.phase_id == phase_id].orientations
            assert isinstance(o, Orientation)
            orientations_size += o.size

        assert orientations_size == cm.size
示例#6
0
    def test_get_by_phase_name(self, crystal_map_input, phase_list):
        x = crystal_map_input["x"]

        # Create phase ID array, ensuring all are present
        phase_ids = np.random.choice(phase_list.ids, x.size)
        for i, unique_id in enumerate(phase_list.ids):
            phase_ids[i] = unique_id

        # Get number of points with each phase ID
        n_points_per_phase = {}
        for phase_i, phase in phase_list:
            n_points_per_phase[phase.name] = len(np.where(phase_ids == phase_i)[0])

        crystal_map_input.pop("phase_id")
        cm = CrystalMap(phase_id=phase_ids, **crystal_map_input)
        cm.phases = phase_list

        for (_, phase), (expected_phase_name, n_points) in zip(
            phase_list, n_points_per_phase.items()
        ):
            cm2 = cm[phase.name]

            assert cm2.size == n_points
            assert cm2.phases_in_data.names == [expected_phase_name]