diff --git a/momepy/functional/tests/test_diversity.py b/momepy/functional/tests/test_diversity.py index cabb9101..3e8a4526 100644 --- a/momepy/functional/tests/test_diversity.py +++ b/momepy/functional/tests/test_diversity.py @@ -304,6 +304,29 @@ def test_na_results(self): assert_frame_equal(pandas_agg_vals, numba_agg_vals) + def test_density(self): + graph = ( + Graph.build_contiguity(self.df_tessellation, rook=False) + .higher_order(k=3, lower_order=True) + .assign_self_weight() + ) + fl_area = graph.describe(self.df_buildings["fl_area"])["sum"] + tess_area = graph.describe(self.df_tessellation["area"])["sum"] + dens_new = fl_area / tess_area + dens_expected = { + "count": 144, + "mean": 1.6615871155383324, + "max": 2.450536855278486, + "min": 0.9746481727569978, + } + assert_result( + dens_new, + dens_expected, + self.df_tessellation, + exact=False, + check_names=False, + ) + @pytest.mark.skipif( not PD_210, reason="aggregation is different in previous pandas versions" ) diff --git a/momepy/functional/tests/test_intensity.py b/momepy/functional/tests/test_intensity.py index 2e305cd1..dc7ae702 100644 --- a/momepy/functional/tests/test_intensity.py +++ b/momepy/functional/tests/test_intensity.py @@ -123,6 +123,34 @@ def test_courtyards(self): new_courtyards, old_courtyards, check_names=False, check_dtype=False ) + def test_density(self): + sw = mm.sw_high(k=3, gdf=self.df_tessellation, ids="uID") + graph = ( + Graph.build_contiguity(self.df_tessellation, rook=False) + .higher_order(k=3, lower_order=True) + .assign_self_weight() + ) + + fl_area = graph.describe(self.df_buildings["fl_area"])["sum"] + tess_area = graph.describe(self.df_tessellation["area"])["sum"] + dens_new = fl_area / tess_area + + dens_old = mm.Density( + self.df_tessellation, + self.df_buildings["fl_area"], + sw, + "uID", + self.df_tessellation.area, + ).series + + assert_series_equal( + dens_new, + dens_old, + check_names=False, + check_dtype=False, + check_index_type=False, + ) + def test_node_density(self): nx = mm.gdf_to_nx(self.df_streets, integer_labels=True) nx = mm.node_degree(nx)