Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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 delete_test_camera(path, id):
    """ Helper function to delete the test camera created by
    :func:`create_test_camera()`.
    """
    for file in FILES:
        try:
            (path / file).unlink()
        except FileNotFoundError:
            pass

    for folder in reversed(FOLDERS):
        try:
            (path / folder).rmdir()
        except FileNotFoundError:
            pass

    testing.remove_tmp_folder(id)

    return