Skip to content

Commit 4ab2aae

Browse files
committed
Address review: exact-equality auto-extent tests, silverman and GeoDataFrame NaN coverage (#3628)
1 parent baeaed6 commit 4ab2aae

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

xrspatial/tests/test_kde.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,33 @@ def test_nan_point_auto_extent_not_poisoned(self):
234234
width=8, height=8)
235235
assert np.isfinite(result.x.values).all()
236236
assert np.isfinite(result.y.values).all()
237+
# The filter runs before the extent is derived, so the result must
238+
# be identical to calling kde on the finite points only.
239+
clean = kde([0.0, 1.0], [0.0, 1.0], bandwidth=1.0, width=8, height=8)
240+
np.testing.assert_array_equal(result.values, clean.values)
241+
np.testing.assert_array_equal(result.x.values, clean.x.values)
242+
np.testing.assert_array_equal(result.y.values, clean.y.values)
243+
244+
def test_nan_point_silverman_bandwidth_not_poisoned(self):
245+
"""Silverman's rule must see only the finite points (#3628)."""
246+
result = kde([0.0, 1.0, 2.0, np.nan], [0.0, 1.0, 2.0, 0.5],
247+
bandwidth='silverman', width=8, height=8)
248+
clean = kde([0.0, 1.0, 2.0], [0.0, 1.0, 2.0],
249+
bandwidth='silverman', width=8, height=8)
250+
np.testing.assert_array_equal(result.values, clean.values)
237251
assert float(result.sum()) > 0.0
238252

253+
def test_geodataframe_nan_point_dropped(self, simple_grid):
254+
gpd = pytest.importorskip("geopandas")
255+
shapely_geometry = pytest.importorskip("shapely.geometry")
256+
Point = shapely_geometry.Point
257+
gdf = gpd.GeoDataFrame(
258+
geometry=[Point(0.0, 0.0), Point(1.0, 1.0), Point(np.nan, 0.5)])
259+
clean = kde([0.0, 1.0], [0.0, 1.0], bandwidth=1.0,
260+
template=simple_grid)
261+
result = kde(gdf, bandwidth=1.0, template=simple_grid)
262+
np.testing.assert_allclose(result.values, clean.values, rtol=1e-12)
263+
239264
def test_all_nan_points_raises(self, simple_grid):
240265
with pytest.raises(ValueError, match='finite'):
241266
kde([np.nan, np.nan], [0.0, 1.0], bandwidth=1.0,
@@ -274,7 +299,12 @@ def test_line_density_nan_endpoint_auto_extent(self):
274299
[1.0, 1.0], bandwidth=0.5, width=16, height=16)
275300
assert np.isfinite(result.x.values).all()
276301
assert np.isfinite(result.y.values).all()
277-
assert float(result.sum()) > 0.0
302+
# Identical to the finite-only call, extent included.
303+
clean = line_density([0.0], [0.0], [1.0], [1.0],
304+
bandwidth=0.5, width=16, height=16)
305+
np.testing.assert_array_equal(result.values, clean.values)
306+
np.testing.assert_array_equal(result.x.values, clean.x.values)
307+
np.testing.assert_array_equal(result.y.values, clean.y.values)
278308

279309
def test_line_density_all_nan_raises(self):
280310
with pytest.raises(ValueError, match='finite'):

0 commit comments

Comments
 (0)