Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions features/eolearn/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from eolearn.core import EOPatch

pytest.register_assert_rewrite("sentinelhub.testing_utils") # makes asserts in helper functions work with pytest

EXAMPLE_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..", "..", "example_data")
EXAMPLE_EOPATCH_PATH = os.path.join(EXAMPLE_DATA_PATH, "TestEOPatch")

Expand Down
51 changes: 25 additions & 26 deletions features/eolearn/tests/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,50 @@
import copy
import sys

import numpy as np
import pytest
from numpy.testing import assert_array_equal
from pytest import approx
from skimage.feature import blob_dog

from sentinelhub.testing_utils import test_numpy_data

from eolearn.core import FeatureType
from eolearn.core.eodata_io import FeatureIO
from eolearn.features import BlobTask, DoGBlobTask, DoHBlobTask, LoGBlobTask

FEATURE = (FeatureType.DATA, "NDVI", "blob")
BLOB_FEATURE = (FeatureType.DATA, "blob")


def test_blob_feature(small_ndvi_eopatch):
def test_dog_blob_task(small_ndvi_eopatch):
eopatch = small_ndvi_eopatch
BlobTask(FEATURE, blob_dog, sigma_ratio=1.6, min_sigma=1, max_sigma=30, overlap=0.5, threshold=0)(eopatch)
DoGBlobTask((FeatureType.DATA, "NDVI", "blob_dog"), threshold=0)(eopatch)
assert eopatch.data["blob"] == approx(
eopatch.data["blob_dog"]
), "DoG derived class result not equal to base class result"
assert eopatch[BLOB_FEATURE] == approx(eopatch.data["blob_dog"])


BLOB_TESTS = [
[DoGBlobTask(FEATURE, threshold=0), 0.0, 37.9625, 0.0854, 0.0],
(DoGBlobTask(FEATURE, threshold=0), {"exp_min": 0.0, "exp_max": 37.9625, "exp_mean": 0.08545, "exp_median": 0.0}),
]
if sys.version_info >= (3, 8): # For Python 3.7 scikit-image returns less accurate result for this test
BLOB_TESTS.append([DoHBlobTask(FEATURE, num_sigma=5, threshold=0), 0.0, 21.9203, 0.05807, 0.0])
BLOB_TESTS.append([LoGBlobTask(FEATURE, log_scale=True, threshold=0), 0, 42.4264, 0.0977, 0.0])


@pytest.mark.parametrize("task, expected_min, expected_max, expected_mean, expected_median", BLOB_TESTS)
def test_blob(small_ndvi_eopatch, task, expected_min, expected_max, expected_mean, expected_median):
BLOB_TESTS.extend(
[
(
DoHBlobTask(FEATURE, num_sigma=5, threshold=0),
{"exp_min": 0.0, "exp_max": 21.9203, "exp_mean": 0.05807, "exp_median": 0.0},
),
(
LoGBlobTask(FEATURE, log_scale=True, threshold=0),
{"exp_min": 0, "exp_max": 42.4264, "exp_mean": 0.09767, "exp_median": 0.0},
),
]
)


@pytest.mark.parametrize("task, expected_statistics", BLOB_TESTS)
def test_blob_task(small_ndvi_eopatch, task, expected_statistics):
eopatch = copy.deepcopy(small_ndvi_eopatch)
task.execute(eopatch)

# Test that no other features were modified
for feature, value in small_ndvi_eopatch.data.items():
if isinstance(value, FeatureIO):
value = value.load()
assert_array_equal(value, eopatch.data[feature], err_msg=f"EOPatch data feature '{feature}' has changed")

delta = 1e-4
test_numpy_data(eopatch[BLOB_FEATURE], exp_shape=(10, 20, 20, 1), **expected_statistics, delta=1e-4)

blob = eopatch.data[FEATURE[-1]]
assert np.min(blob) == approx(expected_min, abs=delta)
assert np.max(blob) == approx(expected_max, abs=delta)
assert np.mean(blob) == approx(expected_mean, abs=delta)
assert np.median(blob) == approx(expected_median, abs=delta)
del eopatch[BLOB_FEATURE]
assert small_ndvi_eopatch == eopatch, "Other features of the EOPatch were affected."
40 changes: 11 additions & 29 deletions features/eolearn/tests/test_haralick.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,38 @@

import numpy as np
import pytest
from numpy.testing import assert_array_equal
from pytest import approx

from sentinelhub.testing_utils import test_numpy_data

from eolearn.core import FeatureType
from eolearn.core.eodata_io import FeatureIO
from eolearn.features import HaralickTask

FEATURE = (FeatureType.DATA, "NDVI", "haralick")
OUTPUT_FEATURE = (FeatureType.DATA, "haralick")


@pytest.mark.parametrize(
"task, expected_min, expected_max, expected_mean, expected_median",
"task, expected_statistics",
(
[
HaralickTask(FEATURE, texture_feature="contrast", angle=0, levels=255, window_size=3),
3.5,
9079.0,
965.8295,
628.5833,
{"exp_min": 3.5, "exp_max": 9079.0, "exp_mean": 965.8295, "exp_median": 628.5833},
],
[
HaralickTask(FEATURE, texture_feature="sum_of_square_variance", angle=np.pi / 2, levels=8, window_size=5),
0.96899,
48.7815,
23.0229,
23.8987,
{"exp_min": 0.96899, "exp_max": 48.7815, "exp_mean": 23.0229, "exp_median": 23.8987},
],
[
HaralickTask(FEATURE, texture_feature="sum_entropy", angle=-np.pi / 2, levels=8, window_size=7),
0,
1.7463,
0.5657,
0.5055,
{"exp_min": 0, "exp_max": 1.7463, "exp_mean": 0.5657, "exp_median": 0.50558},
],
),
)
def test_haralick(small_ndvi_eopatch, task, expected_min, expected_max, expected_mean, expected_median):
def test_haralick(small_ndvi_eopatch, task, expected_statistics):
eopatch = copy.deepcopy(small_ndvi_eopatch)
task.execute(eopatch)

# Test that no other features were modified
for feature, value in small_ndvi_eopatch.data.items():
if isinstance(value, FeatureIO):
value = value.load()
assert_array_equal(value, eopatch.data[feature], err_msg=f"EOPatch data feature '{feature}' has changed")

delta = 1e-4
test_numpy_data(eopatch[OUTPUT_FEATURE], exp_shape=(10, 20, 20, 1), **expected_statistics, delta=1e-4)

haralick = eopatch.data["haralick"]
assert np.min(haralick) == approx(expected_min, abs=delta)
assert np.max(haralick) == approx(expected_max, abs=delta)
assert np.mean(haralick) == approx(expected_mean, abs=delta)
assert np.median(haralick) == approx(expected_median, abs=delta)
del eopatch[OUTPUT_FEATURE]
assert small_ndvi_eopatch == eopatch, "Other features of the EOPatch were affected."
28 changes: 9 additions & 19 deletions features/eolearn/tests/test_hog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
"""
import copy

import numpy as np
from numpy.testing import assert_array_equal
from pytest import approx
from sentinelhub.testing_utils import test_numpy_data

from eolearn.core import FeatureType
from eolearn.core.eodata_io import FeatureIO
from eolearn.features import HOGTask


Expand All @@ -30,19 +27,12 @@ def test_hog(small_ndvi_eopatch):
eopatch = copy.deepcopy(small_ndvi_eopatch)
task.execute(eopatch)

# Test that no other features were modified
for feature, value in small_ndvi_eopatch.data.items():
if isinstance(value, FeatureIO):
value = value.load()
assert_array_equal(value, eopatch.data[feature], err_msg=f"EOPatch data feature '{feature}' has changed")

delta = 1e-4
for feature, expected_min, expected_max, expected_mean, expected_median in [
("hog", 0.0, 0.5567, 0.0931, 0.0),
("hog_visu", 0.0, 0.3241, 0.0105, 0.0),
for feature_name, expected_statistics in [
("hog", {"exp_min": 0.0, "exp_max": 0.5567, "exp_mean": 0.09309, "exp_median": 0.0}),
("hog_visu", {"exp_min": 0.0, "exp_max": 0.3241, "exp_mean": 0.010537, "exp_median": 0.0}),
]:
hog = eopatch.data[feature]
assert np.min(hog) == approx(expected_min, abs=delta)
assert np.max(hog) == approx(expected_max, abs=delta)
assert np.mean(hog) == approx(expected_mean, abs=delta)
assert np.median(hog) == approx(expected_median, abs=delta)
test_numpy_data(eopatch.data[feature_name], **expected_statistics, delta=1e-4)

del eopatch[(FeatureType.DATA, "hog")]
del eopatch[(FeatureType.DATA, "hog_visu")]
assert small_ndvi_eopatch == eopatch, "Other features of the EOPatch were affected."
Loading