From 9566aaa21061411f7cc8db1ecb358acd594afbfd Mon Sep 17 00:00:00 2001 From: Florian Nachtigall Date: Mon, 30 Sep 2024 15:26:14 +0200 Subject: [PATCH] Make bootstrapping reproducible & add unit test Set random seed to make sampling of border units and subsequent estimation of the spatial separation index (venables) reproducible. Add unit test for calculating UCI with bootstrapping_border=True. The goal of bootstrapping is to improve the estimate of the maximum spatial separation index (venables) compared to the heuristic approach (of using an equal distribution along the border of the area of interest). Test if the bootstrapping approach succeeds in this regard. Note that for small areas, e.g. the 3x3 polygon grid from the unit test, this may not always be the case. For the given random seed it is though. --- tests/test_uci.py | 8 ++++++++ uci/uci.py | 1 + 2 files changed, 9 insertions(+) diff --git a/tests/test_uci.py b/tests/test_uci.py index 8393dab..9307526 100644 --- a/tests/test_uci.py +++ b/tests/test_uci.py @@ -131,3 +131,11 @@ def test_uci_spatial_link_isolated(self): np.testing.assert_equal(result['UCI'], np.nan) np.testing.assert_equal(result['proximity_index'], np.nan) self.assertAlmostEqual(result['location_coef'], 0.3809523, places=5) + + + def test_bootstrap_border(self): + + result = uci(self.gdf, 'activities') + result_bootstrap = uci(self.gdf, 'activities', bootstrap_border=True) + + self.assertGreater(result_bootstrap['spatial_separation_max'], result['spatial_separation_max']) # Bootstrapping should in most cases increase the maximum estimate of the spatial separation index diff --git a/uci/uci.py b/uci/uci.py index 7bfafa7..ccf2633 100644 --- a/uci/uci.py +++ b/uci/uci.py @@ -186,6 +186,7 @@ def _simulate_border_values(length, n): """ Simulate equal value distributions along random subset of the border. """ + np.random.seed(0) positions = np.random.choice(range(length), size=n) values = _create_weight_vector(length, positions) return values