Skip to content

Commit

Permalink
Make sure there are no additional compute()s in DNC tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Oct 25, 2023
1 parent 7070cfb commit c580551
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from pyresample import AreaDefinition

import satpy
from satpy.tests.utils import CustomScheduler

# NOTE:
# The following fixtures are not defined in this file, but are used and injected by Pytest:
Expand Down Expand Up @@ -431,28 +432,34 @@ def setUp(self):
def test_daynight_sza(self):
"""Test compositor with both day and night portions when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_night")
res = comp((self.data_a, self.data_b, self.sza))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_night")
res = comp((self.data_a, self.data_b, self.sza))
res = res.compute()
expected = np.array([[0., 0.22122352], [0.5, 1.]])
np.testing.assert_allclose(res.values[0], expected)

def test_daynight_area(self):
"""Test compositor both day and night portions when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_night")
res = comp((self.data_a, self.data_b))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_night")
res = comp((self.data_a, self.data_b))
res = res.compute()
expected_channel = np.array([[0., 0.33164983], [0.66835017, 1.]])
for i in range(3):
np.testing.assert_allclose(res.values[i], expected_channel)

def test_night_only_sza_with_alpha(self):
"""Test compositor with night portion with alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp((self.data_b, self.sza))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp((self.data_b, self.sza))
res = res.compute()
expected_red_channel = np.array([[np.nan, 0.], [0.5, 1.]])
expected_alpha = np.array([[0., 0.33296056], [1., 1.]])
np.testing.assert_allclose(res.values[0], expected_red_channel)
Expand All @@ -461,19 +468,23 @@ def test_night_only_sza_with_alpha(self):
def test_night_only_sza_without_alpha(self):
"""Test compositor with night portion without alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()
expected = np.array([[0., 0.11042631], [0.66835017, 1.]])
np.testing.assert_allclose(res.values[0], expected)
assert 'A' not in res.bands

def test_night_only_area_with_alpha(self):
"""Test compositor with night portion with alpha band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp((self.data_b,))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp((self.data_b,))
res = res.compute()
expected_l_channel = np.array([[np.nan, 0.], [0.5, 1.]])
expected_alpha = np.array([[np.nan, 0.], [0., 0.]])
np.testing.assert_allclose(res.values[0], expected_l_channel)
Expand All @@ -482,19 +493,23 @@ def test_night_only_area_with_alpha(self):
def test_night_only_area_without_alpha(self):
"""Test compositor with night portion without alpha band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp((self.data_b,))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp((self.data_b,))
res = res.compute()
expected = np.array([[np.nan, 0.], [0., 0.]])
np.testing.assert_allclose(res.values[0], expected)
assert 'A' not in res.bands

def test_day_only_sza_with_alpha(self):
"""Test compositor with day portion with alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_a, self.sza))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_a, self.sza))
res = res.compute()
expected_red_channel = np.array([[0., 0.33164983], [0.66835017, 1.]])
expected_alpha = np.array([[1., 0.66703944], [0., 0.]])
np.testing.assert_allclose(res.values[0], expected_red_channel)
Expand All @@ -503,9 +518,11 @@ def test_day_only_sza_with_alpha(self):
def test_day_only_sza_without_alpha(self):
"""Test compositor with day portion without alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()
expected_channel_data = np.array([[0., 0.22122352], [0., 0.]])
for i in range(3):
np.testing.assert_allclose(res.values[i], expected_channel_data)
Expand All @@ -514,9 +531,11 @@ def test_day_only_sza_without_alpha(self):
def test_day_only_area_with_alpha(self):
"""Test compositor with day portion with alpha_band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_a,))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_a,))
res = res.compute()
expected_l_channel = np.array([[0., 0.33164983], [0.66835017, 1.]])
expected_alpha = np.array([[1., 1.], [1., 1.]])
np.testing.assert_allclose(res.values[0], expected_l_channel)
Expand All @@ -525,9 +544,11 @@ def test_day_only_area_with_alpha(self):
def test_day_only_area_with_alpha_and_missing_data(self):
"""Test compositor with day portion with alpha_band when SZA data is not provided and there is missing data."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_b,))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_b,))
res = res.compute()
expected_l_channel = np.array([[np.nan, 0.], [0.5, 1.]])
expected_alpha = np.array([[np.nan, 1.], [1., 1.]])
np.testing.assert_allclose(res.values[0], expected_l_channel)
Expand All @@ -536,9 +557,11 @@ def test_day_only_area_with_alpha_and_missing_data(self):
def test_day_only_area_without_alpha(self):
"""Test compositor with day portion without alpha_band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp((self.data_a,))
res = res.compute()

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp((self.data_a,))
res = res.compute()
expected = np.array([[0., 0.33164983], [0.66835017, 1.]])
np.testing.assert_allclose(res.values[0], expected)
assert 'A' not in res.bands
Expand Down

0 comments on commit c580551

Please sign in to comment.