From 58960b04f20d9f27a072713f9bca9cb2bdae3a41 Mon Sep 17 00:00:00 2001 From: Romain Hugonnet Date: Wed, 15 Nov 2023 15:24:43 -0800 Subject: [PATCH] Add test for fix --- tests/test_spatialstats.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_spatialstats.py b/tests/test_spatialstats.py index 2b141f87..ff791318 100644 --- a/tests/test_spatialstats.py +++ b/tests/test_spatialstats.py @@ -269,6 +269,23 @@ def test_interp_nd_binning_artificial_data(self) -> None: ].values[0] ) + # Check that the linear extrapolation respects nearest neighbour and doesn't go negative + + # The following example used to give a negative value + df = pd.DataFrame( + { + "var1": [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], + "var2": [0, 0, 0, 0, 5, 5, 5, 5, 5.5, 5.5, 5.5, 5.5, 6, 6, 6, 6], + "statistic": [0, 0, 0, 0, 1, 1, 1, 1, np.nan, 1, 1, np.nan, np.nan, 0, 0, np.nan], + } + ) + fun = xdem.spatialstats.interp_nd_binning( + df, list_var_names=["var1", "var2"], statistic="statistic", min_count=None + ) + + # Check it is now positive or equal to zero + assert fun((5, 100)) >= 0 + def test_interp_nd_binning_realdata(self) -> None: """Check that the function works well with outputs from the nd_binning function"""