Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set summed bin without data to fill_value #364

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pyresample/bucket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@
target_shape = self.target_area.shape
self.idxs = self.y_idxs * target_shape[1] + self.x_idxs

def get_sum(self, data, skipna=True):
def get_sum(self, data, fill_value=np.nan, skipna=True):
"""Calculate sums for each bin with drop-in-a-bucket resampling.

Parameters
----------
data : Numpy or Dask array
Data to be binned and summed.
fill_value : float
Fill value to mark missing/invalid values in the input data,
as well as in the binned and averaged output data.
Default: np.nan
skipna : boolean (optional)
If True, skips NaN values for the sum calculation
(similarly to Numpy's `nansum`). Buckets containing only NaN are set to zero.
Expand Down Expand Up @@ -180,6 +184,10 @@
# 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)

# set bin without data to fill_value if fill_value exists
if ~np.isnan(fill_value):
sums = da.where(sums == 0, fill_value, sums)

Check warning on line 189 in pyresample/bucket/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyresample/bucket/__init__.py#L189

Added line #L189 was not covered by tests

return sums.reshape(self.target_area.shape)

def _mask_bins_with_nan_if_not_skipna(self, skipna, data, out_size, statistic):
Expand All @@ -190,7 +198,7 @@
statistic = da.where(nan_bins > 0, np.nan, statistic)
return statistic

def _call_pandas_groupby_statistics(self, scipy_method, data, fill_value=None, skipna=None):
def _call_pandas_groupby_statistics(self, scipy_method, data, fill_value=np.nan, skipna=None):
"""Calculate statistics (min/max) for each bin with drop-in-a-bucket resampling."""
import dask.dataframe as dd
import pandas as pd
Expand Down Expand Up @@ -236,8 +244,8 @@
# TODO remove following line in favour of weights = data when dask histogram bug (issue #6935) is fixed
statistics = self._mask_bins_with_nan_if_not_skipna(skipna, data, out_size, statistics)

# set bin without data to fill value
statistics = da.where(counts == 0, fill_value, statistics)
# set bin without data to fill_value
statistics = da.where(np.isin(counts, [0, fill_value]), fill_value, statistics)

return statistics.reshape(self.target_area.shape)

Expand Down