Skip to content

Commit

Permalink
Test on-disk caching of bilinear coefficients
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Nov 27, 2018
1 parent eb8cd9d commit 26ae344
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
1 change: 0 additions & 1 deletion satpy/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ def precompute(self, mask=None, radius_of_influence=50000, epsilon=0,
reduce_data=reduce_data)

self.resampler = XArrayResamplerBilinear(**kwargs)

try:
self.load_bil_info(cache_dir, **kwargs)
LOG.debug("Loaded bilinear parameters")
Expand Down
58 changes: 53 additions & 5 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,11 @@ def test_expand_without_dims_4D(self):
class TestBilinearResampler(unittest.TestCase):
"""Test the bilinear resampler."""

@mock.patch('satpy.resample.BilinearResampler.load_bil_info')
@mock.patch('satpy.resample.np.savez')
@mock.patch('satpy.resample.np.load')
@mock.patch('satpy.resample.BilinearResampler._create_cache_filename')
@mock.patch('satpy.resample.XArrayResamplerBilinear')
def test_bil_resampling(self, resampler, create_filename, load, savez,
load_bil_info):
def test_bil_resampling(self, resampler, create_filename, load, savez):
"""Test the bilinear resampler."""
import numpy as np
import dask.array as da
Expand All @@ -406,15 +404,16 @@ def test_bil_resampling(self, resampler, create_filename, load, savez,

# Test that bilinear resampling info calculation is called,
# and the info is saved
load_bil_info.side_effect = IOError()
load.side_effect = IOError()
resampler = BilinearResampler(source_swath, target_area)
resampler.precompute(
mask=da.arange(5, chunks=5).astype(np.bool))
resampler.resampler.get_bil_info.assert_called()
resampler.resampler.get_bil_info.assert_called_with()
self.assertFalse(len(savez.mock_calls), 1)
resampler.resampler.reset_mock()
load_bil_info.reset_mock()
load.reset_mock()
load.side_effect = None

# Test that get_sample_from_bil_info is called properly
data = mock.MagicMock()
Expand All @@ -425,6 +424,55 @@ def test_bil_resampling(self, resampler, create_filename, load, savez,
resampler.resampler.get_sample_from_bil_info.assert_called_with(
data, fill_value=fill_value, output_shape=target_area.shape)

# Test that the resampling info is tried to read from the disk
resampler = BilinearResampler(source_swath, target_area)
resampler.precompute(cache_dir='.')
load.assert_called()

# Test caching the resampling info
try:
the_dir = tempfile.mkdtemp()
resampler = BilinearResampler(source_area, target_area)
create_filename.return_value = os.path.join(the_dir, 'test_cache.npz')
load.reset_mock()
load.side_effect = IOError()

resampler.precompute(cache_dir=the_dir)
savez.assert_called()
# assert data was saved to the on-disk cache
self.assertEqual(len(savez.mock_calls), 1)
# assert that load was called to try to load something from disk
self.assertEqual(len(load.mock_calls), 1)

nbcalls = len(resampler.resampler.get_bil_info.mock_calls)
# test reusing the resampler
load.side_effect = None

class FakeNPZ(dict):
def close(self):
pass

load.return_value = FakeNPZ(bilinear_s=1,
bilinear_t=2,
valid_input_index=3,
index_array=4)
resampler.precompute(cache_dir=the_dir)
# we already have things cached in-memory, no need to save again
self.assertEqual(len(savez.mock_calls), 1)
# we already have things cached in-memory, don't need to load
# self.assertEqual(len(load.mock_calls), 1)
self.assertEqual(len(resampler.resampler.get_bil_info.mock_calls), nbcalls)

# test loading saved resampler
resampler = BilinearResampler(source_area, target_area)
resampler.precompute(cache_dir=the_dir)
self.assertEqual(len(load.mock_calls), 2)
self.assertEqual(len(resampler.resampler.get_bil_info.mock_calls), nbcalls)
# we should have cached things in-memory now
# self.assertEqual(len(resampler._index_caches), 1)
finally:
shutil.rmtree(the_dir)


def suite():
"""The test suite for test_scene.
Expand Down

0 comments on commit 26ae344

Please sign in to comment.