Skip to content

Commit

Permalink
change arg name to empty_bucket_value and add replacement check
Browse files Browse the repository at this point in the history
  • Loading branch information
ameraner committed Jun 18, 2024
1 parent 512a265 commit 6fc18c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions pyresample/bucket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ def _get_indices(self):
target_shape = self.target_area.shape
self.idxs = self.y_idxs * target_shape[1] + self.x_idxs

def get_sum(self, data, fill_value=np.nan, skipna=True,
set_empty_bucket_to=0):
def get_sum(self, data, fill_value=np.nan, skipna=True, empty_bucket_value=0):
"""Calculate sums for each bin with drop-in-a-bucket resampling.
Parameters
Expand All @@ -215,12 +214,12 @@ def get_sum(self, data, fill_value=np.nan, skipna=True,
Default: np.nan
skipna : boolean (optional)
If True, skips missing values (as marked by NaN or `fill_value`) for the sum calculation
(similarly to Numpy's `nansum`). Buckets containing only missing values are set to `set_empty_bucket_to`.
(similarly to Numpy's `nansum`). Buckets containing only missing values are set to `empty_bucket_value`.
If False, sets the bucket to fill_value if one or more missing values are present in the bucket
(similarly to Numpy's `sum`).
In both cases, empty buckets are set to `set_empty_bucket_to`.
In both cases, empty buckets are set to `empty_bucket_value`.
Default: True
set_empty_bucket_to : float
empty_bucket_value : float
Set empty buckets to the given value. Empty buckets are considered as the buckets with value 0.
Note that a bucket could become 0 as the result of a sum
of positive and negative values. If the user needs to identify these zero-buckets reliably,
Expand Down Expand Up @@ -255,7 +254,9 @@ def get_sum(self, data, fill_value=np.nan, skipna=True,

# TODO remove following line in favour of weights = data when dask histogram bug (issue #6935) is fixed
sums = self._mask_bins_with_nan_if_not_skipna(skipna, data, out_size, sums, fill_value)
sums = da.where(sums == 0, set_empty_bucket_to, sums)

if empty_bucket_value != 0:
sums = da.where(sums == 0, empty_bucket_value, sums)

return sums.reshape(self.target_area.shape)

Expand Down
12 changes: 6 additions & 6 deletions pyresample/test/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,24 @@ def test_get_sum_non_default_fill_value_skipna_true(self):
self.assertEqual(np.count_nonzero(result == 255), 0)
self.assertEqual(np.nanmin(result), 0)

def test_nonzero_set_empty_bucket_to_number(self):
"""Test drop-in-a-bucket sum for non-zero set_empty_bucket_to np.nan."""
def test_nonzero_empty_bucket_value_number(self):
"""Test drop-in-a-bucket sum for non-zero empty_bucket_value set as number."""
data = da.from_array(np.array([[2., np.nan], [5., np.nan]]),
chunks=self.chunks)

result = self._get_sum_result(data, skipna=True, set_empty_bucket_to=4095)
result = self._get_sum_result(data, skipna=True, empty_bucket_value=4095)
# 5 is untouched in a single bin
self.assertEqual(np.count_nonzero(result == 5.), 1)
# all-nan and rest is 4095
self.assertEqual(np.count_nonzero(result == 4095), 2048 * 2560 - 2)
self.assertEqual(np.nanmin(result), 2)

def test_nonzero_set_empty_bucket_to_npnan(self):
"""Test drop-in-a-bucket sum for non-zero set_empty_bucket_to np.nan."""
def test_nonzero_empty_bucket_valueo_npnan(self):
"""Test drop-in-a-bucket sum for non-zero empty_bucket_value set as np.nan."""
data = da.from_array(np.array([[2., np.nan], [5., np.nan]]),
chunks=self.chunks)

result = self._get_sum_result(data, skipna=True, set_empty_bucket_to=np.nan)
result = self._get_sum_result(data, skipna=True, empty_bucket_value=np.nan)
# 5 is untouched in a single bin
self.assertEqual(np.count_nonzero(result == 5.), 1)
# all-nan and rest is np.nan
Expand Down

0 comments on commit 6fc18c6

Please sign in to comment.