Skip to content

Commit 58098b8

Browse files
author
cpelley
committed
ENH: Return nan values in tgt cells where a src nan cells contribute
1 parent 54bb77a commit 58098b8

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

stratify/_bounded_vinterp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
4949
5050
Note
5151
----
52-
Support for 1D z_target and corresponding ND z_src will be provided in
52+
- Support for 1D z_target and corresponding ND z_src will be provided in
5353
future as driven by user requirement.
54+
- Those cells, where 'nan' values in the source data contribute, a 'nan'
55+
value is returned.
5456
5557
"""
5658
if z_src.ndim != z_target.ndim:

stratify/_conservative.pyx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ cdef apply_weights(np.ndarray[np.float64_t, ndim=3] src_point,
7272
7373
"""
7474
cdef Py_ssize_t ind
75-
cdef np.ndarray[np.float64_t, ndim=3] results
75+
cdef np.ndarray[np.float64_t, ndim=3] results, weighted_contrib
7676
cdef np.ndarray[np.float64_t, ndim=2] weights
77+
cdef np.ndarray[np.int64_t, ndim=2] nan_src_contrib
7778
results = np.zeros(
7879
[src_data.shape[0], tgt_point.shape[0], src_data.shape[2]],
7980
dtype='float64')
@@ -84,10 +85,16 @@ cdef apply_weights(np.ndarray[np.float64_t, ndim=3] src_point,
8485
msg = ('Weights calculation yields a less than conservative '
8586
'result. Aborting.')
8687
raise ValueError(msg)
88+
weighted_contrib = weights * src_data[..., ind][..., None]
8789
results[..., ind] = (
88-
weights * src_data[..., ind][..., None]).sum(axis=1)
89-
# Return np.nan for those target cells where no source contributes.
90-
results[:, weights.sum(axis=0) == 0, :] = np.nan
90+
np.nansum(weighted_contrib, axis=1))
91+
# Return nan values for those target cells, where there is any
92+
# contribution of nan data from the source data.
93+
nan_src_contrib = ((weights > 0) * np.isnan(weighted_contrib)).sum(axis=1)
94+
results[..., ind][nan_src_contrib>0] = np.nan
95+
96+
# Return np.nan for those target cells where no source contributes.
97+
results[:, weights.sum(axis=0) == 0, ind] = np.nan
9198
return results
9299

93100

stratify/tests/test_bounded_vinterp.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ def test_no_broadcasting(self):
5353
target_data = np.ones((3))*2
5454
assert_array_equal(res, target_data)
5555

56+
def test_source_with_nans(self):
57+
# In the case of no broadcasting, transposed and reshaped array of form
58+
# [broadcasting_dims, axis_interpolation, z_varying] should end up
59+
# [1, axis_interpolation, 1].
60+
data = self.data[0]
61+
data[0] = data[-2:] = np.nan
62+
target_bounds = self.gen_bounds(0, 6.5, 0.5)
63+
res = bounded_vinterp.interpolate_conservative(target_bounds,
64+
self.bounds, data,
65+
axis=0)
66+
target_data = np.ones((12))*.5
67+
target_data[:2] = np.nan
68+
target_data[-4:] = np.nan
69+
assert_array_equal(res, target_data)
70+
5671
def test_target_extends_above_source(self):
5772
# |-|-|-|-|-|-|-| - Source
5873
# |-|-|-|-|-|-|-|-| - Target
@@ -62,7 +77,7 @@ def test_target_extends_above_source(self):
6277
source_bounds,
6378
self.data, axis=1)
6479
target_data = np.ones((4, 7))
65-
target_data[:, -1] = 0
80+
target_data[:, -1] = np.nan
6681
assert_array_equal(res, target_data)
6782

6883
def test_target_extends_above_source_non_equally_spaced_coords(self):
@@ -88,7 +103,7 @@ def test_target_extends_below_source(self):
88103
source_bounds,
89104
self.data, axis=1)
90105
target_data = np.ones((4, 7))
91-
target_data[:, 0] = 0
106+
target_data[:, 0] = np.nan
92107
assert_array_equal(res, target_data)
93108

94109

0 commit comments

Comments
 (0)