Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to be more flexible to CRS and enhancement changes #2002

Merged
merged 1 commit into from
Feb 2, 2022
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
66 changes: 35 additions & 31 deletions satpy/tests/enhancement_tests/test_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
from satpy.enhancements import create_colormap


def run_and_check_enhancement(func, data, expected, **kwargs):
"""Perform basic checks that apply to multiple tests."""
from trollimage.xrimage import XRImage

pre_attrs = data.attrs
img = XRImage(data)
func(img, **kwargs)

assert isinstance(img.data.data, da.Array)
old_keys = set(pre_attrs.keys())
# It is OK to have "enhancement_history" added
new_keys = set(img.data.attrs.keys()) - {"enhancement_history"}
assert old_keys == new_keys

np.testing.assert_allclose(img.data.values, expected, atol=1.e-6, rtol=0)


class TestEnhancementStretch:
"""Class for testing enhancements in satpy.enhancements."""

Expand All @@ -47,27 +64,14 @@ def setup_method(self):
self.rgb = xr.DataArray(rgb_data, dims=('bands', 'y', 'x'),
coords={'bands': ['R', 'G', 'B']})

def _test_enhancement(self, func, data, expected, **kwargs):
"""Perform basic checks that apply to multiple tests."""
from trollimage.xrimage import XRImage

pre_attrs = data.attrs
img = XRImage(data)
func(img, **kwargs)

assert isinstance(img.data.data, da.Array)
assert sorted(pre_attrs.keys()) == sorted(img.data.attrs.keys()), "DataArray attributes were not preserved"

np.testing.assert_allclose(img.data.values, expected, atol=1.e-6, rtol=0)

def test_cira_stretch(self):
"""Test applying the cira_stretch."""
from satpy.enhancements import cira_stretch

expected = np.array([[
[np.nan, -7.04045974, -7.04045974, 0.79630132, 0.95947296],
[1.05181359, 1.11651012, 1.16635571, 1.20691137, 1.24110186]]])
self._test_enhancement(cira_stretch, self.ch1, expected)
run_and_check_enhancement(cira_stretch, self.ch1, expected)

def test_reinhard(self):
"""Test the reinhard algorithm."""
Expand All @@ -80,7 +84,7 @@ def test_reinhard(self):

[[np.nan, 0., 0., 0.93333793, 1.29432402],
[1.55428709, 1.76572249, 1.94738635, 2.10848544, 2.25432809]]])
self._test_enhancement(reinhard_to_srgb, self.rgb, expected)
run_and_check_enhancement(reinhard_to_srgb, self.rgb, expected)

def test_lookup(self):
"""Test the lookup enhancement function."""
Expand All @@ -89,7 +93,7 @@ def test_lookup(self):
[0., 0., 0., 0.333333, 0.705882],
[1., 1., 1., 1., 1.]]])
lut = np.arange(256.)
self._test_enhancement(lookup, self.ch1, expected, luts=lut)
run_and_check_enhancement(lookup, self.ch1, expected, luts=lut)

expected = np.array([[[0., 0., 0., 0.333333, 0.705882],
[1., 1., 1., 1., 1.]],
Expand All @@ -99,7 +103,7 @@ def test_lookup(self):
[1., 1., 1., 1., 1.]]])
lut = np.arange(256.)
lut = np.vstack((lut, lut, lut)).T
self._test_enhancement(lookup, self.rgb, expected, luts=lut)
run_and_check_enhancement(lookup, self.rgb, expected, luts=lut)

def test_colorize(self):
"""Test the colorize enhancement function."""
Expand All @@ -119,46 +123,46 @@ def test_colorize(self):
1.88238767e-01, 1.88238767e-01],
[1.88238767e-01, 1.88238767e-01, 1.88238767e-01,
1.88238767e-01, 1.88238767e-01]]])
self._test_enhancement(colorize, self.ch1, expected, palettes=brbg)
run_and_check_enhancement(colorize, self.ch1, expected, palettes=brbg)

def test_palettize(self):
"""Test the palettize enhancement function."""
from trollimage.colormap import brbg

from satpy.enhancements import palettize
expected = np.array([[[10, 0, 0, 10, 10], [10, 10, 10, 10, 10]]])
self._test_enhancement(palettize, self.ch1, expected, palettes=brbg)
run_and_check_enhancement(palettize, self.ch1, expected, palettes=brbg)

def test_three_d_effect(self):
"""Test the three_d_effect enhancement function."""
from satpy.enhancements import three_d_effect
expected = np.array([[
[np.nan, np.nan, -389.5, -294.5, 826.5],
[np.nan, np.nan, 85.5, 180.5, 1301.5]]])
self._test_enhancement(three_d_effect, self.ch1, expected)
run_and_check_enhancement(three_d_effect, self.ch1, expected)

def test_crefl_scaling(self):
"""Test the crefl_scaling enhancement function."""
from satpy.enhancements import crefl_scaling
expected = np.array([[
[np.nan, 0., 0., 0.44378, 0.631734],
[0.737562, 0.825041, 0.912521, 1., 1.]]])
self._test_enhancement(crefl_scaling, self.ch2, expected, idx=[0., 25., 55., 100., 255.],
sc=[0., 90., 140., 175., 255.])
run_and_check_enhancement(crefl_scaling, self.ch2, expected, idx=[0., 25., 55., 100., 255.],
sc=[0., 90., 140., 175., 255.])

def test_piecewise_linear_stretch(self):
"""Test the piecewise_linear_stretch enhancement function."""
from satpy.enhancements import piecewise_linear_stretch
expected = np.array([[
[np.nan, 0., 0., 0.44378, 0.631734],
[0.737562, 0.825041, 0.912521, 1., 1.]]])
self._test_enhancement(piecewise_linear_stretch,
self.ch2 / 100.0,
expected,
xp=[0., 25., 55., 100., 255.],
fp=[0., 90., 140., 175., 255.],
reference_scale_factor=255,
)
run_and_check_enhancement(piecewise_linear_stretch,
self.ch2 / 100.0,
expected,
xp=[0., 25., 55., 100., 255.],
fp=[0., 90., 140., 175., 255.],
reference_scale_factor=255,
)

def test_btemp_threshold(self):
"""Test applying the cira_stretch."""
Expand All @@ -167,8 +171,8 @@ def test_btemp_threshold(self):
expected = np.array([[
[np.nan, 0.946207, 0.892695, 0.839184, 0.785672],
[0.73216, 0.595869, 0.158745, -0.278379, -0.715503]]])
self._test_enhancement(btemp_threshold, self.ch1, expected,
min_in=-200, max_in=500, threshold=350)
run_and_check_enhancement(btemp_threshold, self.ch1, expected,
min_in=-200, max_in=500, threshold=350)

def test_merge_colormaps(self):
"""Test merging colormaps."""
Expand Down
20 changes: 4 additions & 16 deletions satpy/tests/enhancement_tests/test_viirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import numpy as np
import xarray as xr

from .test_enhancements import run_and_check_enhancement


class TestVIIRSEnhancement(unittest.TestCase):
"""Class for testing the VIIRS enhancement function in satpy.enhancements.viirs."""
Expand Down Expand Up @@ -70,19 +72,5 @@ def test_viirs(self):
from satpy.enhancements.viirs import water_detection
expected = [[[1, 7, 8, 8, 8, 9, 10, 11, 14, 8],
[20, 23, 26, 10, 12, 15, 18, 21, 24, 27]]]
self._test_enhancement(water_detection, self.da, expected,
palettes=self.palette)

def _test_enhancement(self, func, data, expected, **kwargs):
from trollimage.xrimage import XRImage

pre_attrs = data.attrs
img = XRImage(data)
func(img, **kwargs)

self.assertIsInstance(img.data.data, da.Array)
self.assertListEqual(sorted(pre_attrs.keys()),
sorted(img.data.attrs.keys()),
"DataArray attributes were not preserved")

np.testing.assert_allclose(img.data.values, expected, atol=1.e-6, rtol=0)
run_and_check_enhancement(water_detection, self.da, expected,
palettes=self.palette)
2 changes: 1 addition & 1 deletion satpy/tests/reader_tests/test_generic_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setUp(self):

# Create area definition
pcs_id = 'ETRS89 / LAEA Europe'
proj4_dict = {'init': 'epsg:3035'}
proj4_dict = "EPSG:3035"
self.x_size = 100
self.y_size = 100
area_extent = (2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222)
Expand Down