Skip to content

Commit

Permalink
Factorize test data creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Jun 2, 2022
1 parent 1b03e05 commit e2d7641
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions pyresample/test/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,36 +305,37 @@ def test_resampler_returns_the_right_shape(self):
assert res.shape == (3, ) + self.dst_area.shape

def test_resampler_returns_a_dataarray(self):
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
dims=['y', 'x'])
data = self.create_2d_src_data()
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
assert isinstance(res, xr.DataArray)

def test_resampler_returns_a_dataarray_with_correct_area_attribute(self):
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
dims=['y', 'x'])
data = self.create_2d_src_data()
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
assert res.attrs["area"] == self.dst_area

def test_resampler_returns_a_dataarray_with_input_attributes(self):
attrs = {"sky": "blue", "grass": "green"}
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
dims=['y', 'x'], attrs=attrs)
data = self.create_2d_src_data(attrs)
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
new_attrs = res.attrs.copy()
new_attrs.pop("area")
assert new_attrs == attrs

def test_resampler_returns_a_dataarray_with_input_dims(self):
attrs = {"sky": "blue", "grass": "green"}
def create_2d_src_data(self, attrs=None):
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
dims=['y', 'x'], attrs=attrs)
return data

def test_resampler_returns_a_dataarray_with_input_dims(self):
attrs = {"sky": "blue", "grass": "green"}
data = self.create_2d_src_data(attrs)
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
Expand All @@ -343,19 +344,21 @@ def test_resampler_returns_a_dataarray_with_input_dims(self):

def test_resampler_returns_a_dataarray_with_input_coords(self):
attrs = {"sky": "blue", "grass": "green"}
data = xr.DataArray(da.ones((3,) + self.src_area.shape, dtype=np.float64),
dims=["bands", 'y', 'x'], attrs=attrs,
coords={"bands": ["R", "G", "B"]})
data = self.create_3d_rgb_src_data(attrs)
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')

assert all(res.coords["bands"] == data.coords["bands"])

def test_resampler_returns_a_dataarray_with_correct_xy_coords(self):
def create_3d_rgb_src_data(self, attrs=None):
data = xr.DataArray(da.ones((3,) + self.src_area.shape, dtype=np.float64),
dims=["bands", 'y', 'x'],
dims=["bands", 'y', 'x'], attrs=attrs,
coords={"bands": ["R", "G", "B"]})
return data

def test_resampler_returns_a_dataarray_with_correct_xy_coords(self):
data = self.create_3d_rgb_src_data()
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
Expand All @@ -365,9 +368,7 @@ def test_resampler_returns_a_dataarray_with_correct_xy_coords(self):
assert "y" in res.coords

def test_resampler_can_take_random_dim_order(self):
data = xr.DataArray(da.ones((3,) + self.src_area.shape, dtype=np.float64),
dims=["bands", 'y', 'x'],
coords={"bands": ["R", "G", "B"]}).transpose("x", "bands", "y")
data = self.create_3d_rgb_src_data().transpose("x", "bands", "y")
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
Expand All @@ -382,8 +383,7 @@ def test_resampler_accepts_only_bilinear_or_nn(self):

def test_resample_area_to_area_2d(self):
"""Resample area to area, 2d."""
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
dims=['y', 'x'])
data = self.create_2d_src_data()
self.resampler.precompute()
res = self.resampler.compute(
data, method='bilinear').compute(scheduler='single-threaded')
Expand All @@ -392,7 +392,7 @@ def test_resample_area_to_area_2d(self):

def test_resample_area_to_area_2d_fill_value(self):
"""Resample area to area, 2d, use fill value."""
data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64), dims=['y', 'x'])
data = self.create_2d_src_data()
dst_area = AreaDefinition('outside', 'outside', None,
{'proj': 'stere', 'lon_0': 180.0,
'lat_0': 90.0, 'lat_ts': 60.0,
Expand Down

0 comments on commit e2d7641

Please sign in to comment.