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")
def test_get_rotations_array(self, crystal_map_input): cm = CrystalMap(**crystal_map_input) # Get with string r = cm.get_map_data("rotations") new_shape = cm.shape + (3,) if cm.rotations_per_point > 1: expected_array = cm.rotations[:, 0].to_euler().reshape(*new_shape) else: expected_array = cm.rotations.to_euler().reshape(*new_shape) assert np.allclose(r, expected_array, atol=1e-3) # Get with array (RGB) new_shape2 = (cm.size, 3) r2 = cm.get_map_data(r.reshape(*new_shape2)) assert np.allclose(r2, expected_array, atol=1e-3)
def test_get_coordinate_array(self, crystal_map_input, to_get, expected_array): cm = CrystalMap(**crystal_map_input) # Get via string data_via_string = cm.get_map_data(to_get) assert np.allclose(data_via_string, expected_array) # Get via numpy array if to_get == "x": data_via_array = cm.get_map_data(cm.x) elif to_get == "y": data_via_array = cm.get_map_data(cm.y) else: # to_get == "z" data_via_array = cm.get_map_data(cm.z) assert np.allclose(data_via_array, expected_array) # Make sure they are the same assert np.allclose(data_via_array, data_via_string)
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)
def test_get_unknown_string_raises(self, crystal_map_input, to_get): xmap = CrystalMap(**crystal_map_input) with pytest.raises(ValueError, match=f"{to_get} is None."): _ = xmap.get_map_data(to_get)
def test_get_phase_id_array_from_3d_data(self, crystal_map_input): cm = CrystalMap(**crystal_map_input) _ = cm.get_map_data(cm.phase_id)