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

Fix unnecessary Dask compute()s in NDVIHybridGreen compositor #2623

Merged
merged 6 commits into from
Nov 2, 2023
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
5 changes: 1 addition & 4 deletions satpy/composites/spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import logging
import warnings

import dask.array as da

from satpy.composites import GenericCompositor
from satpy.dataset import combine_metadata

Expand Down Expand Up @@ -166,8 +164,7 @@

ndvi = (ndvi_input[1] - ndvi_input[0]) / (ndvi_input[1] + ndvi_input[0])

ndvi.data = da.where(ndvi > self.ndvi_min, ndvi, self.ndvi_min)
ndvi.data = da.where(ndvi < self.ndvi_max, ndvi, self.ndvi_max)
ndvi = ndvi.clip(self.ndvi_min, self.ndvi_max)

Check warning on line 167 in satpy/composites/spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/spectral.py#L167

Added line #L167 was not covered by tests

# Introduce non-linearity to ndvi for non-linear scaling to NIR blend fraction
if self.strength != 1.0: # self._apply_strength() has no effect if strength = 1.0 -> no non-linear behaviour
Expand Down
63 changes: 41 additions & 22 deletions satpy/tests/compositor_tests/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""Tests for spectral correction compositors."""

import dask

Check warning on line 18 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L18

Added line #L18 was not covered by tests
import dask.array as da
import numpy as np
import pytest
import xarray as xr

from satpy.composites.spectral import GreenCorrector, HybridGreen, NDVIHybridGreen, SpectralBlender
from satpy.tests.utils import CustomScheduler

Check warning on line 25 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L25

Added line #L25 was not covered by tests


class TestSpectralComposites:
Expand Down Expand Up @@ -83,34 +85,51 @@

def setup_method(self):
"""Initialize channels."""
self.c01 = xr.DataArray(da.from_array([[0.25, 0.30], [0.20, 0.30]], chunks=25),
dims=("y", "x"), attrs={"name": "C02"})
self.c02 = xr.DataArray(da.from_array([[0.25, 0.30], [0.25, 0.35]], chunks=25),
dims=("y", "x"), attrs={"name": "C03"})
self.c03 = xr.DataArray(da.from_array([[0.35, 0.35], [0.28, 0.65]], chunks=25),
dims=("y", "x"), attrs={"name": "C04"})
self.c01 = xr.DataArray(

Check warning on line 88 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L88

Added line #L88 was not covered by tests
da.from_array(np.array([[0.25, 0.30], [0.20, 0.30]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C02"})
self.c02 = xr.DataArray(

Check warning on line 91 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L91

Added line #L91 was not covered by tests
da.from_array(np.array([[0.25, 0.30], [0.25, 0.35]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C03"})
self.c03 = xr.DataArray(

Check warning on line 94 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L94

Added line #L94 was not covered by tests
da.from_array(np.array([[0.35, 0.35], [0.28, 0.65]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C04"})

def test_ndvi_hybrid_green(self):
"""Test General functionality with linear scaling from ndvi to blend fraction."""
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), prerequisites=(0.51, 0.65, 0.85),
standard_name="toa_bidirectional_reflectance")

# Test General functionality with linear strength (=1.0)
res = comp((self.c01, self.c02, self.c03))
assert isinstance(res, xr.DataArray)
assert isinstance(res.data, da.Array)
assert res.attrs["name"] == "ndvi_hybrid_green"
assert res.attrs["standard_name"] == "toa_bidirectional_reflectance"
data = res.values
with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), prerequisites=(0.51, 0.65, 0.85),

Check warning on line 101 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L100-L101

Added lines #L100 - L101 were not covered by tests
standard_name="toa_bidirectional_reflectance")

# Test General functionality with linear strength (=1.0)
res = comp((self.c01, self.c02, self.c03))
assert isinstance(res, xr.DataArray)
assert isinstance(res.data, da.Array)
assert res.attrs["name"] == "ndvi_hybrid_green"
assert res.attrs["standard_name"] == "toa_bidirectional_reflectance"
data = res.values

Check warning on line 110 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L105-L110

Added lines #L105 - L110 were not covered by tests
np.testing.assert_array_almost_equal(data, np.array([[0.2633, 0.3071], [0.2115, 0.3420]]), decimal=4)

def test_nonliniear_scaling(self):
"""Test non-linear scaling using `strength` term."""
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), strength=2.0, prerequisites=(0.51, 0.65, 0.85),
standard_name="toa_bidirectional_reflectance")
def test_ndvi_hybrid_green_dtype(self):

Check warning on line 113 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L113

Added line #L113 was not covered by tests
"""Test that the datatype is not altered by the compositor."""
with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), prerequisites=(0.51, 0.65, 0.85),

Check warning on line 116 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L115-L116

Added lines #L115 - L116 were not covered by tests
standard_name="toa_bidirectional_reflectance")
res = comp((self.c01, self.c02, self.c03)).compute()
assert res.data.dtype == np.float32

Check warning on line 119 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L118-L119

Added lines #L118 - L119 were not covered by tests

res = comp((self.c01, self.c02, self.c03))
np.testing.assert_array_almost_equal(res.values, np.array([[0.2646, 0.3075], [0.2120, 0.3471]]), decimal=4)
def test_nonlinear_scaling(self):

Check warning on line 121 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L121

Added line #L121 was not covered by tests
"""Test non-linear scaling using `strength` term."""
with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), strength=2.0,

Check warning on line 124 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L123-L124

Added lines #L123 - L124 were not covered by tests
prerequisites=(0.51, 0.65, 0.85),
standard_name="toa_bidirectional_reflectance")

res = comp((self.c01, self.c02, self.c03))
res_np = res.data.compute()
assert res.dtype == res_np.dtype
assert res.dtype == np.float32
np.testing.assert_array_almost_equal(res.data, np.array([[0.2646, 0.3075], [0.2120, 0.3471]]), decimal=4)

Check warning on line 132 in satpy/tests/compositor_tests/test_spectral.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/compositor_tests/test_spectral.py#L128-L132

Added lines #L128 - L132 were not covered by tests

def test_invalid_strength(self):
"""Test using invalid `strength` term for non-linear scaling."""
Expand Down
Loading