def test_init(self): # Signal shape array0 = np.zeros(shape=(10, 10, 10, 10)) s0 = EBSD(array0) assert array0.shape == s0.axes_manager.shape # Cannot initialise signal with one signal dimension with pytest.raises(ValueError): _ = EBSD(np.zeros(10)) # Shape of one-image signal array1 = np.zeros(shape=(10, 10)) s1 = EBSD(array1) assert array1.shape == s1.axes_manager.shape # SEM metadata kp_md = ebsd_metadata() sem_node = metadata_nodes("sem") assert_dictionary( kp_md.get_item(sem_node), s1.metadata.get_item(sem_node) ) # Phases metadata assert s1.metadata.has_item("Sample.Phases")
def test_read_cutoff(self, save_path_nordif): scan_size = (10, 3) scan_size_reloaded = (10, 20) pattern_size = (5, 5) data_shape = scan_size + pattern_size s = EBSD((255 * np.random.rand(*data_shape)).astype(np.uint8)) s.save(save_path_nordif, overwrite=True) # Reload data but with a scan_size bigger than available file bytes, # so that the data has to be padded with pytest.warns(UserWarning): # No background or setting files s_reload = load( save_path_nordif, scan_size=scan_size_reloaded[::-1], pattern_size=pattern_size, ) # To check if the data padding works as expected, the original data is # padded and compared to the reloaded data cut_data = s.data.flatten() pw = [ ( 0, (scan_size_reloaded[1] - scan_size[1]) * scan_size[0] * np.prod(pattern_size), ) ] cut_data = np.pad(cut_data, pw, mode="constant") cut_data = cut_data.reshape(scan_size_reloaded + pattern_size) assert np.allclose(cut_data, s_reload.data)
def test_read_patterns(self, save_path_hdf5): s = EBSD((255 * np.random.rand(10, 3, 5, 5)).astype(np.uint8)) s.save(save_path_hdf5) with File(save_path_hdf5, mode="r+") as f: del f["Scan 1/EBSD/Data/patterns"] with pytest.raises(KeyError, match="Could not find patterns"): _ = load(save_path_hdf5)
def test_chunk_bytes_indirectly(self): s = EBSD(np.zeros((10, 10, 8, 8))) array_out0 = get_dask_array(s) array_out1 = get_dask_array(s, chunk_bytes="25KiB") array_out2 = get_dask_array(s, chunk_bytes=25e3) assert array_out0.chunksize != array_out1.chunksize assert array_out1.chunksize == array_out2.chunksize
def test_write_data_single(self, save_path_nordif): pattern_size = (5, 5) s = EBSD((255 * np.random.rand(*pattern_size)).astype(np.uint8)) s.save(save_path_nordif, overwrite=True) with pytest.warns(UserWarning): # No background or setting files s_reload = load( save_path_nordif, scan_size=1, pattern_size=pattern_size ) assert np.allclose(s.data, s_reload.data)
def test_get_dask_array(self): s = EBSD((255 * np.random.rand(10, 10, 120, 120)).astype(np.uint8)) dask_array = _get_dask_array(s) assert dask_array.chunksize == (8, 8, 120, 120) # Make data lazy s.data = dask_array.rechunk((5, 5, 120, 120)) dask_array = _get_dask_array(s) assert dask_array.chunksize == (5, 5, 120, 120)
def test_save_fresh(self, save_path_nordif): scan_size = (10, 3) pattern_size = (5, 5) data_shape = scan_size + pattern_size s = EBSD((255 * np.random.rand(*data_shape)).astype(np.uint8)) s.save(save_path_nordif, overwrite=True) with pytest.warns(UserWarning): # No background or setting files s_reload = load(save_path_nordif, scan_size=scan_size[::-1], pattern_size=pattern_size) assert np.allclose(s.data, s_reload.data)
def test_load_manufacturer(self, save_path_hdf5): s = EBSD((255 * np.random.rand(10, 3, 5, 5)).astype(np.uint8)) s.save(save_path_hdf5) # Change manufacturer with File(save_path_hdf5, mode="r+") as f: manufacturer = f["manufacturer"] manufacturer[()] = "Nope".encode() with pytest.raises( OSError, match="Manufacturer Nope not among recognised manufacturers"): _ = load(save_path_hdf5)
def test_check_h5ebsd(self, save_path_hdf5, delete, error): s = EBSD((255 * np.random.rand(10, 3, 5, 5)).astype(np.uint8)) s.save(save_path_hdf5) with File(save_path_hdf5, mode="r+") as f: if delete == "man_ver": del f["manufacturer"] del f["version"] with pytest.raises(OSError, match=error): check_h5ebsd(f) else: del f["Scan 1"] with pytest.raises(OSError, match=error): check_h5ebsd(f)
def test_save_fresh(self, save_path_hdf5, tmp_path): scan_size = (10, 3) pattern_size = (5, 5) data_shape = scan_size + pattern_size s = EBSD((255 * np.random.rand(*data_shape)).astype(np.uint8)) s.save(save_path_hdf5, overwrite=True) s_reload = load(save_path_hdf5) np.testing.assert_equal(s.data, s_reload.data) # Test writing of signal to file when no file name is passed to save() del s.tmp_parameters.filename with pytest.raises(ValueError, match="Filename not defined"): s.save(overwrite=True) s.metadata.General.original_filename = "an_original_filename" os.chdir(tmp_path) s.save(overwrite=True)