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_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_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_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)