Skip to content

Commit

Permalink
added datasets with out black or white and warn
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoelles committed Sep 21, 2023
1 parent 6e84d71 commit 5643366
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@ dmypy.json
notebooks/data*

# ignore test data
tests/testdata/specim_data
tests/testdata/specim_data
tests/testdata/no_white
tests/testdata/no_black
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ markers = [
"slow: marks tests as slow",
"unit: fast offline tests",
]
filterwarnings = [
"error",
"ignore::UserWarning",
# note the use of single quote below to denote "raw" strings in TOML
'ignore:function ham\(\) is deprecated:DeprecationWarning',
]

[tool.tox]
legacy_tox_ini = """
Expand Down
5 changes: 5 additions & 0 deletions src/specarray/io/specim_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import xarray as xr
from spectral.io.bilfile import BilFile

import warnings


def _find_files(folder: Path):
"""Find all files in the folder"""
Expand Down Expand Up @@ -85,8 +87,11 @@ def from_specim_folder(
raise ValueError("No capture data found")
elif mode == "black":
black = data_array
# warning that there is no Dark Reference
warnings.warn("No Dark Reference found")
elif mode == "white":
white = data_array
warnings.warn("No White Reference found")
except Exception as exception:
print(f"An error occurred reading {mode}: {exception}")
return capture, metadata, wavelengths, black, white
46 changes: 30 additions & 16 deletions src/specarray/specarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,43 @@ def shape(self):
"""Return the shape of the data"""
return self.capture.shape

@property
def has_black(self) -> bool:
return self.black.shape != ()

@property
def has_white(self) -> bool:
return self.white.shape != ()

@property
def spectral_albedo(self):
"""Calculate and return the spectral albedo. The values are limited to 0 and 1."""
spectral_albedo = (self.capture - self.black.mean(dim="sample")) / (
self.white.mean(dim="sample") - self.black.mean(dim="sample")
)
spectral_albedo = xr.where(spectral_albedo < 0.0, 0.0, spectral_albedo)
spectral_albedo = xr.where(spectral_albedo > 1.0, 1.0, spectral_albedo)
spectral_albedo.name = "spectral albedo"
return spectral_albedo
if self.has_black and self.has_white:
spectral_albedo = (self.capture - self.black.mean(dim="sample")) / (
self.white.mean(dim="sample") - self.black.mean(dim="sample")
)
spectral_albedo = xr.where(spectral_albedo < 0.0, 0.0, spectral_albedo)
spectral_albedo = xr.where(spectral_albedo > 1.0, 1.0, spectral_albedo)
spectral_albedo.name = "spectral albedo"
return spectral_albedo
else:
raise ValueError("No black or white reference")

@property
def broadband_albedo(self):
"""Calculate and return the broadband albedo"""
broadband_albedo = np.trapz(
self.spectral_albedo,
self.spectral_albedo.coords["wavelength"],
) / (
self.spectral_albedo.coords["wavelength"].max().values
- self.spectral_albedo.coords["wavelength"].min().values
)
broadband_albedo = xr.DataArray(broadband_albedo, dims=["sample", "point"], name="broadband_albedo")
return broadband_albedo
if self.has_black and self.has_white:
broadband_albedo = np.trapz(
self.spectral_albedo,
self.spectral_albedo.coords["wavelength"],
) / (
self.spectral_albedo.coords["wavelength"].max().values
- self.spectral_albedo.coords["wavelength"].min().values
)
broadband_albedo = xr.DataArray(broadband_albedo, dims=["sample", "point"], name="broadband_albedo")
return broadband_albedo
else:
raise ValueError("No black or white reference")

def _gen_wavelength_point_df(self, raw_array: ndarray) -> pd.DataFrame:
"""Generate a dataframe with the wavelenghts as index and points as colums"""
Expand Down
30 changes: 27 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from pathlib import Path
import shutil
from os import remove
from time import sleep

import pandas as pd
import spectral.io.envi as envi
Expand All @@ -13,7 +15,10 @@

thisdir = Path(__file__).parent.absolute()
testdatadir = thisdir / "testdata"
testdatadir1 = thisdir / "testdata" / "specim_data" / "capture"
specim_data_dir = thisdir / "testdata" / "specim_data"
no_black_dir = testdatadir / "no_black"
no_white_dir = testdatadir / "no_white"
testdatadir1 = specim_data_dir / "capture"
specim_ex_name = "fake"


Expand All @@ -32,22 +37,41 @@ def _save_image_ndarray(prefix=""):


def gen_data():
shutil.rmtree(testdatadir1, ignore_errors=True)
shutil.rmtree(specim_data_dir, ignore_errors=True)
shutil.rmtree(no_black_dir, ignore_errors=True)
shutil.rmtree(no_white_dir, ignore_errors=True)
testdatadir1.mkdir(parents=True, exist_ok=True)
_save_image_ndarray()
_save_image_ndarray("DARKREF_")
_save_image_ndarray("WHITEREF_")
# rename all files from .img to .raw
for f in testdatadir1.glob("*.img"):
f.rename(f.with_suffix(".raw"))
# copy folder
shutil.copytree(specim_data_dir, no_black_dir)
shutil.copytree(specim_data_dir, no_white_dir)
remove(no_black_dir / "capture" / "DARKREF_fake.hdr")
remove(no_black_dir / "capture" / "DARKREF_fake.raw")
remove(no_white_dir / "capture" / "WHITEREF_fake.raw")
remove(no_white_dir / "capture" / "WHITEREF_fake.hdr")


gen_data()


@pytest.fixture()
def testdata_specim_folder() -> Path:
return Path(__file__).parent.absolute() / "testdata/specim_data"
return specim_data_dir


@pytest.fixture()
def testdata_no_black() -> Path:
return no_black_dir


@pytest.fixture()
def testdata_no_white() -> Path:
return no_white_dir


@pytest.fixture()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
from specarray import SpecArray
from pathlib import Path

from pytest_check import check
import pytest


def test_from_dir(testdata_specim: SpecArray):
check.equal(testdata_specim.shape, (2, 1024, 448))


def test_no_black(testdata_no_black: Path):
with pytest.warns(UserWarning):
no_black = SpecArray.from_folder(testdata_no_black)
check.is_instance(no_black, SpecArray)
check.equal(no_black.shape, (2, 1024, 448))
check.equal(no_black.black.shape, ())


def test_no_white(testdata_no_white: Path):
with pytest.warns(UserWarning):
no_white = SpecArray.from_folder(testdata_no_white)
check.is_instance(no_white, SpecArray)
check.equal(no_white.shape, (2, 1024, 448))
check.equal(no_white.white.shape, ())

0 comments on commit 5643366

Please sign in to comment.