def test_resample(): # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create example image arr = np.random.rand(64, 32, 30).astype(np.float32) meta = dict(metadata="test") band_info = BandInfo.from_equidistant(30, 400, 700) tmp = SpectralImage(arr, band_info=band_info, meta=meta, dtype=np.float32, copy=False) tmp_subband = tmp.get_resampled_spectrum(17) band_info_down = BandInfo.from_equidistant(17, 400, 700) assert isinstance(tmp_subband, SpectralImage) assert tmp_subband.num_channels == 17 assert meta == tmp_subband.meta assert band_info_down == tmp_subband.band_info finally: # Cleanup temporary folder testing.remove_tmp_folder(id) return
def create_test_camera() -> Tuple[Path, UUID]: """ Helper function to create a test camera folder structure with images. """ testing.needs_internet() # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create folder structure for folder in FOLDERS: try: (path / folder).mkdir() except FileExistsError: pass img_file = testing.get_remote_file(TEST_IMG_FILENAME) img = imageio.imread(img_file) # Mosaic img = mosaic(img) # Save test image, rotated image and BW image imageio.imsave(path / FILES[0], img) imageio.imsave(path / FILES[1], np.flipud(img)) imageio.imsave(path / FILES[2], (255 * img).astype(np.uint8)) imageio.imsave(path / FILES[3], img) imageio.imsave(path / FILES[4], np.flipud(img)) imageio.imsave(path / FILES[5], (255 * img).astype(np.uint8)) except: delete_test_camera(path, id) return path, id
def test_subband(): # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create example image arr = np.random.rand(64, 32, 30).astype(np.float32) meta = dict(metadata="test") band_info = BandInfo.from_equidistant(30, 400, 700) tmp = SpectralImage(arr, band_info=band_info, meta=meta, dtype=np.float32, copy=False) tmp_subband = tmp.get_subband(5, 11) assert isinstance(tmp_subband, SpectralImage) assert tmp_subband.num_channels == 6 assert meta == tmp_subband.meta assert np.array_equal(band_info.centers[5:11], tmp_subband.band_info.centers) finally: # Cleanup temporary folder testing.remove_tmp_folder(id) return
def test_save_rgb(): # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create example image arr = np.random.rand(64, 32, 30).astype(np.float32) tmp = SpectralImage(arr, dtype=np.float32, copy=False) tmp_rgb = tmp.get_rgb() rgb_filename = path / "test_rgb_img.png" tmp.save_rgb(rgb_filename) # Load image loaded = SpectralImage.from_file(path=rgb_filename, dtype=np.float32) # Caution: lossy dtype conversion assert np.allclose(tmp_rgb, loaded, atol=0.01) finally: # Cleanup temporary folder testing.remove_tmp_folder(id) return
def test_from_mat(): testing.needs_internet() # Get a temporary folder path, id = testing.get_tmp_folder() try: path = path / "test_hsi.mat" hsi_file = testing.get_remote_file(TEST_HSI_FILENAME) hsi = np.load(hsi_file) mdict = dict(data=hsi) scipy.io.savemat(path, mdict) # Try loading with and without key for key in [None, 'data']: tmp = SpectralImage.from_mat_file(path=path, key=key) assert np.allclose(hsi, tmp) finally: # Cleanup temporary folder testing.remove_tmp_folder(id) return
def test_save(): # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create example image arr = np.random.rand(64, 32, 30).astype(np.float32) tmp = SpectralImage(arr, dtype=np.float32, copy=False) file = path / "test_img.png" for d in [np.uint8, np.uint16]: tmp.save(path=file, dtype=d) # Load image loaded = SpectralImage.from_file_collection(path=file.parent, dtype=np.float32) # Caution: lossy dtype conversion assert np.allclose(arr, loaded, atol=0.01) # Test error handling with raises(ValueError) as cm: file = path / "test_img" tmp.save(path=file) assert f"Path needs to include an extension." == str(cm.value) with raises(FileNotFoundError) as cm: file = path / "does_not_exist/test_img.png" tmp.save(path=file) assert f"Path '{file.parent}' does not exist." == str(cm.value) tmp.save(path=file, create_dir=True, dtype=np.uint16) # Load image loaded = SpectralImage.from_file_collection(path=file.parent, dtype=np.float32) # Caution: lossy dtype conversion assert np.allclose(arr, loaded, atol=0.01) finally: # Cleanup temporary folder testing.remove_tmp_folder(id) return
def create_test_camera() -> Tuple[Path, UUID]: """ Helper function to create a test camera folder structure with images. """ testing.needs_internet() # Get a temporary folder path, id = testing.get_tmp_folder() try: # Create folder structure for folder in FOLDERS: try: (path / folder).mkdir() except FileExistsError: pass img_file = testing.get_remote_file(TEST_IMG_FILENAME) img = imread(img_file) # Save test image imsave(path / FILES[0], img) # Create greyscale whiteimages whiteimg_1 = (255 * np.ones( (img.shape[0], img.shape[1]))).astype(np.uint8) whiteimg_2 = (127 * np.ones( (img.shape[0], img.shape[1]))).astype(np.uint8) # Save whiteimages imsave(path / FILES[1], whiteimg_1) imsave(path / FILES[2], whiteimg_2) except: delete_test_camera(path, id) return path, id