From e0acfb2ffe51c98a30e6dda4b46e34d3473c8b2d Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Wed, 21 Sep 2022 12:36:39 +0200 Subject: [PATCH 1/3] Add a failing test --- pyresample/test/test_gradient.py | 53 +++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/pyresample/test/test_gradient.py b/pyresample/test/test_gradient.py index c28a190c5..b6e06e9c9 100644 --- a/pyresample/test/test_gradient.py +++ b/pyresample/test/test_gradient.py @@ -485,6 +485,57 @@ def test_resample_area_to_area_nn(self): np.testing.assert_allclose(res, expected_resampled_data) assert res.shape == dst_area.shape + def test_regression_449(self): + from datetime import datetime + + import dask.array as da + import numpy as np + import xarray as xr + from satpy import Scene + + from pyresample import create_area_def + + # from pyresample.gradient import ResampleBlocksGradientSearchResampler + + dater = datetime.utcnow() + + lon = np.arange(-180, 180, 0.25) + lat = np.arange(-90, 90 + 0.25, 0.25) + + inv = np.random.uniform(low=0., high=1., size=(lat.shape[0], lon.shape[0])) + + area_ext = (np.nanmin(lon), np.nanmin(lat), np.nanmax(lon), np.nanmax(lat)) + targ_area = create_area_def("source_area", + "EPSG:4326", + area_extent=area_ext, + width=inv.shape[1], + height=inv.shape[0]) + + # dest_area = create_area_def("msg_3km_disk", + # {'a': '6378169', 'h': '35785831', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', + # 'rf': '295.488065897001', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}, + # area_extent=(-5570248.6867, -5567248.2834, 5567248.2834, 5570248.6867), + # width=3712, + # height=3712, + # ) + + # resampler = ResampleBlocksGradientSearchResampler(targ_area, targ_area) + ecm_scn = Scene() + + ecm_scn['test'] = xr.DataArray(da.from_array(inv), + coords={'y': lat, 'x': lon}, + attrs={'start_time': dater}) + + ecm_scn['test'].attrs['area'] = targ_area + + ecm_snm_res = ecm_scn.resample('msg_seviri_fes_3km', resampler='gradient_search') + minval, meanval, maxval = (np.nanmin(ecm_snm_res['test']), + np.nanmean(ecm_snm_res['test']), + np.nanmax(ecm_snm_res['test'])) + assert np.isfinite(minval) + assert np.isfinite(meanval) + assert np.isfinite(maxval) + class TestRBGradientSearchResamplerArea2Swath: """Test RBGradientSearchResampler for the Swath to Area case.""" @@ -812,7 +863,7 @@ def test_concatenate_chunks_stack_calls(dask_da): assert 'axis=2' in str(dask_da.concatenate.mock_calls[-1]) -class TestGradientCython(): +class TestGradientCython: """Test the core gradient features.""" def setup(self): From b07dabb8e548c48b61840ebaea1f2949857dc798 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Mon, 26 Sep 2022 15:19:54 +0200 Subject: [PATCH 2/3] Refactor test --- pyresample/test/test_gradient.py | 45 +++++++++++++++----------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/pyresample/test/test_gradient.py b/pyresample/test/test_gradient.py index b6e06e9c9..7926923e8 100644 --- a/pyresample/test/test_gradient.py +++ b/pyresample/test/test_gradient.py @@ -481,7 +481,6 @@ def test_resample_area_to_area_nn(self): res = self.resampler.compute( data, method='nn', fill_value=2.0).compute(scheduler='single-threaded').values - print(res) np.testing.assert_allclose(res, expected_resampled_data) assert res.shape == dst_area.shape @@ -491,11 +490,9 @@ def test_regression_449(self): import dask.array as da import numpy as np import xarray as xr - from satpy import Scene from pyresample import create_area_def - - # from pyresample.gradient import ResampleBlocksGradientSearchResampler + from pyresample.gradient import ResampleBlocksGradientSearchResampler dater = datetime.utcnow() @@ -511,27 +508,27 @@ def test_regression_449(self): width=inv.shape[1], height=inv.shape[0]) - # dest_area = create_area_def("msg_3km_disk", - # {'a': '6378169', 'h': '35785831', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', - # 'rf': '295.488065897001', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}, - # area_extent=(-5570248.6867, -5567248.2834, 5567248.2834, 5570248.6867), - # width=3712, - # height=3712, - # ) - - # resampler = ResampleBlocksGradientSearchResampler(targ_area, targ_area) - ecm_scn = Scene() - - ecm_scn['test'] = xr.DataArray(da.from_array(inv), - coords={'y': lat, 'x': lon}, - attrs={'start_time': dater}) - - ecm_scn['test'].attrs['area'] = targ_area + dest_area = create_area_def("msg_3km_disk", + {'a': '6378169', 'h': '35785831', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', + 'rf': '295.488065897001', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}, + area_extent=(-5570248.6867, -5567248.2834, 5567248.2834, 5570248.6867), + width=3712, + height=3712, + ) + + data = xr.DataArray(da.from_array(inv), + coords={'y': lat, 'x': lon}, + attrs={'start_time': dater}) + + resampler = ResampleBlocksGradientSearchResampler(targ_area, dest_area) + resampler.precompute() + res = resampler.compute( + data, method='nn', + fill_value=np.nan).compute(scheduler='single-threaded').values - ecm_snm_res = ecm_scn.resample('msg_seviri_fes_3km', resampler='gradient_search') - minval, meanval, maxval = (np.nanmin(ecm_snm_res['test']), - np.nanmean(ecm_snm_res['test']), - np.nanmax(ecm_snm_res['test'])) + minval, meanval, maxval = (np.nanmin(res), + np.nanmean(res), + np.nanmax(res)) assert np.isfinite(minval) assert np.isfinite(meanval) assert np.isfinite(maxval) From e9507e20bfaa4a257990528c00c70ce45caea3e4 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Wed, 15 Jan 2025 19:10:05 +0100 Subject: [PATCH 3/3] Narrow down the error to the slicer --- pyresample/test/test_gradient.py | 49 -------------------------------- pyresample/test/test_slicer.py | 27 ++++++++++++++++++ 2 files changed, 27 insertions(+), 49 deletions(-) diff --git a/pyresample/test/test_gradient.py b/pyresample/test/test_gradient.py index 5a218ac6a..89ae21be9 100644 --- a/pyresample/test/test_gradient.py +++ b/pyresample/test/test_gradient.py @@ -255,55 +255,6 @@ def test_resample_area_to_area_nn(self): np.testing.assert_allclose(res, expected_resampled_data) assert res.shape == dst_area.shape - def test_regression_449(self): - from datetime import datetime - - import dask.array as da - import numpy as np - import xarray as xr - - from pyresample import create_area_def - from pyresample.gradient import ResampleBlocksGradientSearchResampler - - dater = datetime.utcnow() - - lon = np.arange(-180, 180, 0.25) - lat = np.arange(-90, 90 + 0.25, 0.25) - - inv = np.random.uniform(low=0., high=1., size=(lat.shape[0], lon.shape[0])) - - area_ext = (np.nanmin(lon), np.nanmin(lat), np.nanmax(lon), np.nanmax(lat)) - targ_area = create_area_def("source_area", - "EPSG:4326", - area_extent=area_ext, - width=inv.shape[1], - height=inv.shape[0]) - - dest_area = create_area_def("msg_3km_disk", - {'a': '6378169', 'h': '35785831', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', - 'rf': '295.488065897001', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}, - area_extent=(-5570248.6867, -5567248.2834, 5567248.2834, 5570248.6867), - width=3712, - height=3712, - ) - - data = xr.DataArray(da.from_array(inv), - coords={'y': lat, 'x': lon}, - attrs={'start_time': dater}) - - resampler = ResampleBlocksGradientSearchResampler(targ_area, dest_area) - resampler.precompute() - res = resampler.compute( - data, method='nn', - fill_value=np.nan).compute(scheduler='single-threaded').values - - minval, meanval, maxval = (np.nanmin(res), - np.nanmean(res), - np.nanmax(res)) - assert np.isfinite(minval) - assert np.isfinite(meanval) - assert np.isfinite(maxval) - class TestRBGradientSearchResamplerSwath2Area: """Test RBGradientSearchResampler for the Area to Swath case.""" diff --git a/pyresample/test/test_slicer.py b/pyresample/test/test_slicer.py index 5868c3637..654b3c410 100644 --- a/pyresample/test/test_slicer.py +++ b/pyresample/test/test_slicer.py @@ -193,6 +193,33 @@ def test_slicing_works_with_extents_of_different_units(self): assert 60 <= slice_x.stop < 65 assert 50 <= slice_y.stop < 55 + def test_regression_449(self): + import numpy as np + + from pyresample import create_area_def + + lon = np.arange(-180, 180, 0.25) + lat = np.arange(-90, 90 + 0.25, 0.25) + + area_ext = (np.nanmin(lon), np.nanmin(lat), np.nanmax(lon), np.nanmax(lat)) + src_area = create_area_def("source_area", + "EPSG:4326", + area_extent=area_ext, + width=len(lon), + height=len(lat)) + + dest_area = create_area_def("msg_3km_disk", + {'a': '6378169', 'h': '35785831', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', + 'rf': '295.488065897001', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}, + area_extent=(-5570248.6867, -5567248.2834, 5567248.2834, 5570248.6867), + width=3712, + height=3712, + ) + + slicer = create_slicer(src_area, dest_area) + # Should not raise any error + slicer.get_slices() + class TestSwathSlicer(unittest.TestCase): """Test the get_slice function when input is a swath."""