Skip to content

Commit

Permalink
Anvy eval (#211)
Browse files Browse the repository at this point in the history
* add eval funs draft

* add stroke stats to stats_func

* osmnx: add printout on digraph

* add strokes and orig-manual weights to eval

* update eval nb: add multivar ranking for cell weights (WIP)

* add read_cityseer func

* adding cityseer data to nb

* adding sns draft to eval nb

* delete outdated nb

* update nrecord for cityseer readin tests

---------

Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
  • Loading branch information
anastassiavybornova and martinfleis authored Dec 5, 2024
1 parent a4b2bcc commit 2210d76
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 468 deletions.
20 changes: 20 additions & 0 deletions core/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from core import utils


def read_method_outputs(fua, method, proj_crs=None):
match method:
case "cityseer":
gdf = utils.read_cityseer(fua, proj_crs)
case "orig":
gdf = utils.read_original(fua)
case "manual":
gdf = utils.read_manual(fua, proj_crs)
case "osmnx":
gdf = utils.read_osmnx(fua, proj_crs)
case "parenx-voronoi":
gdf = utils.read_parenx(fua, "voronoi", proj_crs)
case "parenx-skeletonize":
gdf = utils.read_parenx(fua, "skeletonize", proj_crs)
case "sgeop":
gdf = utils.read_sgeop(fua, proj_crs)
return gdf
43 changes: 38 additions & 5 deletions core/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import networkx
import shapely

__all__ = [
"add_node_degree",
"get_edge_stats",
"get_node_stats",
]
__all__ = ["add_node_degree", "get_edge_stats", "get_node_stats", "get_stroke_stats"]


def add_node_degree(
Expand Down Expand Up @@ -91,3 +87,40 @@ def get_node_stats(
degree_distr = dict(Counter(cell["degree"]))
avg_degree = _avg_degree(degree_distr)
return node_count, degree_distr, avg_degree


def get_stroke_stats(
edge_gdf: geopandas.GeoDataFrame,
stroke_gdf: geopandas.GeoDataFrame,
geom: shapely.Polygon,
) -> tuple[int, float, float]:
"""Calculate basic stroke stats for a single H3 hex cell.
Parameters
----------
edge_gdf : geopandas.GeoDataFrame
Edge data. Needs to have a column "stroke_id".
stroke_gdf : geopandas.GeoDataFrame
Stroke data (from momepy.COINS(edge_gdf).stroke_gdf)
geom: shapely.Polygon
Single H3 hex cell.
Returns
-------
stroke_count : int
Number of strokes intersecting the cell.
stroke_length_sum : float
Sum of length of all strokes intersecting the cell.
stroke_length_max : float
Maximum of lengths of strokes intersecting the cell.
"""

cell = edge_gdf.clip(geom)
stroke_ids = set(cell.stroke_id)
strokes_cell = stroke_gdf[stroke_gdf.index.isin(stroke_ids)].copy()

stroke_count = len(stroke_ids)
stroke_length_sum = strokes_cell.length.sum()
stroke_length_max = strokes_cell.length.max()

return stroke_count, stroke_length_sum, stroke_length_max
14 changes: 14 additions & 0 deletions core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_read_sample_data():


cities = list(core.utils.city_fua.keys())
cityseer_records = [40591, 10083, 14659, 28940, 14893, 12796, 26070]
osm_records = [78_908, 60_364, 79_317, 84_819, 79_907, 50_917, 92_667]
xnd2_records = [43_233, 12_439, 16_302, 30_552, 16_554, 13_468, 29_314]
man_records = [38_772, 8_640, 14_170, 29_252, 13_508, 11_032, numpy.nan]
Expand Down Expand Up @@ -62,6 +63,19 @@ def test_read_manual(city, n_records):
assert gdf_1.crs == gdf_2.crs == pytest.epsg_4326


@pytest.mark.parametrize("city, n_records", zip(cities, cityseer_records, strict=True))
def test_read_cityseer(city, n_records):
fua = core.utils.city_fua[city]

gdf_1 = core.utils.read_cityseer(fua, pytest.epsg_4326)
gdf_2 = core.utils.read_cityseer(core.utils.fua_city[fua], pytest.epsg_4326)

geopandas.testing.assert_geodataframe_equal(gdf_1, gdf_2)

assert gdf_1.shape[0] == gdf_2.shape[0] == n_records
assert gdf_1.crs == gdf_2.crs == pytest.epsg_4326


@pytest.mark.parametrize("city, n_records", zip(cities, osmnx_records, strict=True))
def test_read_osmnx(city, n_records):
fua = core.utils.city_fua[city]
Expand Down
12 changes: 12 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"read_original",
"read_no_degree_2",
"read_manual",
"read_cityseer",
"read_osmnx",
"read_parenx",
"read_neatnet",
Expand Down Expand Up @@ -90,6 +91,17 @@ def read_manual(fua: int, proj_crs: str | int | pyproj.CRS) -> geopandas.GeoData
)


def read_cityseer(
fua: int | str, proj_crs: str | int | pyproj.CRS
) -> geopandas.GeoDataFrame:
"""Read in cityseer data."""
return (
geopandas.read_parquet(_fua_path(fua, "cityseer"))
.explode(ignore_index=True, index_parts=False)
.to_crs(proj_crs)
)


def read_osmnx(
fua: int | str, proj_crs: str | int | pyproj.CRS
) -> geopandas.GeoDataFrame:
Expand Down
Loading

0 comments on commit 2210d76

Please sign in to comment.