Skip to content

Commit a5e9e7c

Browse files
author
Carwyn Pelley
committed
MAINT: Review changes
1 parent c87fb50 commit a5e9e7c

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

stratify/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
EXTRAPOLATE_NAN, EXTRAPOLATE_NEAREST,
66
EXTRAPOLATE_LINEAR, PyFuncExtrapolator,
77
PyFuncInterpolator)
8-
from ._bounded_vinterp import (interpolate as # noqa: F401
9-
interpolate_conservative)
8+
from ._bounded_vinterp import interpolate_conservative # noqa: F401
109

1110

1211
__version__ = '0.1a3.dev0'

stratify/_bounded_vinterp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from ._conservative import conservative_interpolation
66

77

8-
def interpolate(z_target, z_src, fz_src, axis=-1):
8+
def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
99
"""
10-
Interface for optimised 1d interpolation across multiple dimensions.
10+
1d conservative interpolation across multiple dimensions.
1111
1212
This function provides the ability to perform 1d interpolation on datasets
1313
with more than one dimension. For instance, this function can be used to
@@ -117,7 +117,7 @@ def interpolate(z_target, z_src, fz_src, axis=-1):
117117
z_src_reshaped.shape[-1]])
118118
shape = int(np.product(z_target_reshaped.shape[1:-1]))
119119
z_target_reshaped = z_target_reshaped.reshape(
120-
[z_target_reshaped.shape[0], shape, z_target_reshaped.shape[-1]])
120+
[z_target_reshaped.shape[0], shape, z_target_reshaped.shape[-1]])
121121

122122
result = conservative_interpolation(
123123
z_src_reshaped, z_target_reshaped, fz_src_reshaped)

stratify/_conservative.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ cdef calculate_weights(np.ndarray[np.float64_t, ndim=2] src_point,
1313
# weight = overlap / delta
1414
cdef Py_ssize_t src_ind, tgt_ind
1515
cdef np.float64_t delta, weight
16-
cdef np.ndarray[np.float64_t, ndim = 2] weights
17-
cdef np.ndarray[np.float64_t, ndim = 1] src_cell, tgt_cell
16+
cdef np.ndarray[np.float64_t, ndim=2] weights
17+
cdef np.ndarray[np.float64_t, ndim=1] src_cell, tgt_cell
1818

1919
weights = np.zeros([src_point.shape[0], tgt_point.shape[0]],
2020
dtype=np.float64)
@@ -32,8 +32,8 @@ cdef apply_weights(np.ndarray[np.float64_t, ndim=3] src_point,
3232
np.ndarray[np.float64_t, ndim=3] tgt_point,
3333
np.ndarray[np.float64_t, ndim=3] src_data):
3434
cdef Py_ssize_t ind
35-
cdef np.ndarray[np.float64_t, ndim = 3] results
36-
cdef np.ndarray[np.float64_t, ndim = 2] weights
35+
cdef np.ndarray[np.float64_t, ndim=3] results
36+
cdef np.ndarray[np.float64_t, ndim=2] weights
3737
results = np.zeros(
3838
[src_data.shape[0], tgt_point.shape[0], src_data.shape[2]],
3939
dtype='float64')

stratify/tests/test_bounded_vinterp.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ def gen_bounds(self, start, stop, step):
2727

2828
def test_target_half_resolution(self):
2929
target_bounds = self.gen_bounds(0, 7, 2)
30-
res = bounded_vinterp.interpolate(target_bounds, self.bounds,
31-
self.data, axis=1)
30+
res = bounded_vinterp.interpolate_conservative(target_bounds,
31+
self.bounds, self.data,
32+
axis=1)
3233
target_data = np.ones((4, 3))*2
3334
assert_array_equal(res, target_data)
3435

3536
def test_target_double_resolution(self):
3637
target_bounds = self.gen_bounds(0, 6.5, 0.5)
37-
res = bounded_vinterp.interpolate(target_bounds, self.bounds,
38-
self.data, axis=1)
38+
res = bounded_vinterp.interpolate_conservative(target_bounds,
39+
self.bounds, self.data,
40+
axis=1)
3941
target_data = np.ones((4, 12))*.5
4042
assert_array_equal(res, target_data)
4143

@@ -45,8 +47,9 @@ def test_no_broadcasting(self):
4547
# [1, axis_interpolation, 1].
4648
data = self.data[0]
4749
target_bounds = self.gen_bounds(0, 7, 2)
48-
res = bounded_vinterp.interpolate(target_bounds, self.bounds, data,
49-
axis=0)
50+
res = bounded_vinterp.interpolate_conservative(target_bounds,
51+
self.bounds, data,
52+
axis=0)
5053
target_data = np.ones((3))*2
5154
assert_array_equal(res, target_data)
5255

@@ -55,8 +58,9 @@ def test_target_extends_above_source(self):
5558
# |-|-|-|-|-|-|-|-| - Target
5659
source_bounds = self.gen_bounds(0, 7, 1)
5760
target_bounds = self.gen_bounds(0, 8, 1)
58-
res = bounded_vinterp.interpolate(target_bounds, source_bounds,
59-
self.data, axis=1)
61+
res = bounded_vinterp.interpolate_conservative(target_bounds,
62+
source_bounds,
63+
self.data, axis=1)
6064
target_data = np.ones((4, 7))
6165
target_data[:, -1] = 0
6266
assert_array_equal(res, target_data)
@@ -66,8 +70,9 @@ def test_target_extends_below_source(self):
6670
# |-|-|-|-|-|-|-|-| - Target
6771
source_bounds = self.gen_bounds(0, 7, 1)
6872
target_bounds = self.gen_bounds(-1, 7, 1)
69-
res = bounded_vinterp.interpolate(target_bounds, source_bounds,
70-
self.data, axis=1)
73+
res = bounded_vinterp.interpolate_conservative(target_bounds,
74+
source_bounds,
75+
self.data, axis=1)
7176
target_data = np.ones((4, 7))
7277
target_data[:, 0] = 0
7378
assert_array_equal(res, target_data)
@@ -90,8 +95,9 @@ def gen_bounds(self, start, stop, step):
9095

9196
def test_target_half_resolution(self):
9297
target_bounds = self.gen_bounds(0, 7, 2)
93-
res = bounded_vinterp.interpolate(target_bounds, self.bounds,
94-
self.data, axis=1)
98+
res = bounded_vinterp.interpolate_conservative(target_bounds,
99+
self.bounds, self.data,
100+
axis=1)
95101

96102
target_data = np.ones((2, 3, 4, 3))*2
97103
assert_array_equal(res, target_data)
@@ -103,14 +109,16 @@ def test_target_half_resolution_alt_axis(self):
103109
target_bounds = self.gen_bounds(0, 7, 2)
104110
target_bounds = target_bounds.transpose((1, 0, 2, 3))
105111

106-
res = bounded_vinterp.interpolate(target_bounds, bounds, data, axis=2)
112+
res = bounded_vinterp.interpolate_conservative(target_bounds, bounds,
113+
data, axis=2)
107114
target_data = np.ones((2, 4, 3, 3))*2
108115
assert_array_equal(res, target_data)
109116

110117
def test_target_double_resolution(self):
111118
target_bounds = self.gen_bounds(0, 6.5, 0.5)
112-
res = bounded_vinterp.interpolate(target_bounds, self.bounds,
113-
self.data, axis=1)
119+
res = bounded_vinterp.interpolate_conservative(target_bounds,
120+
self.bounds,
121+
self.data, axis=1)
114122
target_data = np.ones((2, 12, 4, 3))*.5
115123
assert_array_equal(res, target_data)
116124

@@ -123,7 +131,8 @@ def test_mismatch_source_target_level_dimensionality(self):
123131

124132
msg = 'Expecting source and target levels dimensionality'
125133
with self.assertRaisesRegexp(ValueError, msg):
126-
bounded_vinterp.interpolate(target_bounds, source_bounds, data)
134+
bounded_vinterp.interpolate_conservative(target_bounds,
135+
source_bounds, data)
127136

128137
def test_mismatch_source_target_level_shape(self):
129138
# The source and target levels should have identical shape, other than
@@ -134,8 +143,9 @@ def test_mismatch_source_target_level_shape(self):
134143

135144
msg = 'Expecting the shape of the source and target levels'
136145
with self.assertRaisesRegexp(ValueError, msg):
137-
bounded_vinterp.interpolate(target_bounds, source_bounds, data,
138-
axis=0)
146+
bounded_vinterp.interpolate_conservative(target_bounds,
147+
source_bounds, data,
148+
axis=0)
139149

140150
def test_mismatch_between_source_levels_source_data(self):
141151
# The source levels should reflect the shape of the data.
@@ -145,8 +155,9 @@ def test_mismatch_between_source_levels_source_data(self):
145155

146156
msg = 'The provided data is not of compatible shape'
147157
with self.assertRaisesRegexp(ValueError, msg):
148-
bounded_vinterp.interpolate(target_bounds, source_bounds, data,
149-
axis=0)
158+
bounded_vinterp.interpolate_conservative(target_bounds,
159+
source_bounds, data,
160+
axis=0)
150161

151162
def test_unexpected_bounds_shape(self):
152163
# Expecting bounds of size 2 (that is the upper and lower).
@@ -157,8 +168,9 @@ def test_unexpected_bounds_shape(self):
157168

158169
msg = 'Unexpected source and target bounds shape. shape\[-1\] != 2'
159170
with self.assertRaisesRegexp(ValueError, msg):
160-
bounded_vinterp.interpolate(target_bounds, source_bounds, data,
161-
axis=0)
171+
bounded_vinterp.interpolate_conservative(target_bounds,
172+
source_bounds, data,
173+
axis=0)
162174

163175
def test_not_conservative(self):
164176
# Where the target does not cover the full extent of the source.
@@ -175,8 +187,9 @@ def gen_bounds(start, stop, step):
175187

176188
msg = 'Weights calculation yields a less than conservative result.'
177189
with self.assertRaisesRegexp(ValueError, msg):
178-
bounded_vinterp.interpolate(target_bounds, source_bounds, data,
179-
axis=1)
190+
bounded_vinterp.interpolate_conservative(target_bounds,
191+
source_bounds, data,
192+
axis=1)
180193

181194

182195
if __name__ == '__main__':

0 commit comments

Comments
 (0)