Skip to content

Commit

Permalink
condense reading of results - utils.read_results() (#218)
Browse files Browse the repository at this point in the history
* condense reading of results - utils.read_results()

* refactor core.eval.read_method_outputs()
  • Loading branch information
jGaboardi authored Dec 8, 2024
1 parent 2210d76 commit fed852f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 153 deletions.
17 changes: 1 addition & 16 deletions core/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,4 @@


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
return utils.read_results(fua, method, proj_crs)
4 changes: 2 additions & 2 deletions core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas
import shapely

from .utils import read_manual, read_original
from .utils import read_original, read_results
from .viz.protocol import plot_case

__all__ = [
Expand Down Expand Up @@ -266,7 +266,7 @@ def process_case(
in_data = read_original(city)
remove_false_nodes = True
else:
in_data = read_manual(city, read_original(city).crs)
in_data = read_results(city, "manual", read_original(city).crs)
remove_false_nodes = False

# generate the type-case roads & intersections
Expand Down
6 changes: 4 additions & 2 deletions core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def osm_auckland():
@pytest.fixture
def manual_auckland():
"""Helper for setup of 'manual' Auckland network."""
roads = core.utils.read_manual(pytest.auckland, _osm_auckland().crs)
roads = core.utils.read_results(pytest.auckland, "manual", _osm_auckland().crs)
graph = momepy.gdf_to_nx(roads, length="length", integer_labels=True)
nodes, edges = momepy.nx_to_gdf(graph)
return graph, nodes, edges
Expand All @@ -55,7 +55,9 @@ def manual_auckland():
@pytest.fixture
def parenx_auckland():
"""Helper for setup of parenx voronoi Auckland network."""
roads = core.utils.read_parenx(pytest.auckland, "voronoi", _osm_auckland().crs)
roads = core.utils.read_results(
pytest.auckland, "parenx-voronoi", _osm_auckland().crs
)
graph = momepy.gdf_to_nx(roads, length="length", integer_labels=True)
nodes, edges = momepy.nx_to_gdf(graph)
return graph, nodes, edges
Expand Down
2 changes: 1 addition & 1 deletion core/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_generate_case(protocol_type, remove_false_nodes, known_lines, known_ver

in_data = core.utils.read_original(city)
if protocol_type != "original":
in_data = core.utils.read_manual(city, in_data.crs)
in_data = core.utils.read_results(city, "manual", in_data.crs)

observed_lines, observed_verts = core.protocol.generate_case(
in_data,
Expand Down
117 changes: 35 additions & 82 deletions core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@ def test_read_sample_data():


cities = list(core.utils.city_fua.keys())
cityseer_records = [40591, 10083, 14659, 28940, 14893, 12796, 26070]
methods = [
"cityseer",
"manual",
"neatnet",
"osmnx",
"parenx-skeletonize",
"parenx-voronoi",
]

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]
parenx_voronoi_records = [42_302, 9_569, 16_844, 29_446, 15_516, 14_242, 35_694]
parenx_skeletonize_records = [44_294, 9_561, 17_469, 30_641, 16_075, 14_784, 37_557]
osmnx_records = [43_451, 15_770, 24_025, 37_954, 21_755, 17_704, 30_156]

# results records
cityseer_records = [40591, 10083, 14659, 28940, 14893, 12796, 26070]
man_records = [38_772, 8_640, 14_170, 29_252, 13_508, 11_032, 30_225]
neatnet_records = [39_489, 8_229, 13_587, 29_028, 12_395, 11_059, 16_298]
osmnx_records = [43_451, 15_770, 24_025, 37_954, 21_755, 17_704, 30_156]
parenx_skeletonize_records = [44_294, 9_561, 17_469, 30_641, 16_075, 14_784, 37_557]
parenx_voronoi_records = [42_302, 9_569, 16_844, 29_446, 15_516, 14_242, 35_694]

method_records = [
*cityseer_records,
*man_records,
*neatnet_records,
*osmnx_records,
*parenx_skeletonize_records,
*parenx_voronoi_records,
]


@pytest.mark.parametrize("city, n_records", zip(cities, osm_records, strict=True))
Expand Down Expand Up @@ -49,86 +69,19 @@ def test_read_no_degree_2(city, n_records):


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

gdf_1 = core.utils.read_manual(fua, pytest.epsg_4326)
gdf_2 = core.utils.read_manual(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, 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]

gdf_1 = core.utils.read_osmnx(fua, pytest.epsg_4326)
gdf_2 = core.utils.read_osmnx(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, parenx_voronoi_records, strict=True)
"city, method, n_records",
zip(
cities * len(methods),
[m for m in methods for _ in range(len(cities))],
method_records,
strict=True,
),
)
def test_read_parenx_voronoi(city, n_records):
fua = core.utils.city_fua[city]
option = "voronoi"
gdf_1 = core.utils.read_parenx(fua, proj_crs=pytest.epsg_4326, option=option)
gdf_2 = core.utils.read_parenx(
core.utils.fua_city[fua], proj_crs=pytest.epsg_4326, option=option
)

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, parenx_skeletonize_records, strict=True)
)
def test_read_parenx_skeletonize(city, n_records):
fua = core.utils.city_fua[city]
option = "skeletonize"
gdf_1 = core.utils.read_parenx(fua, proj_crs=pytest.epsg_4326, option=option)
gdf_2 = core.utils.read_parenx(
core.utils.fua_city[fua], proj_crs=pytest.epsg_4326, option=option
)

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, neatnet_records, strict=True))
def test_read_neatnet(city, n_records):
def test_read_results(city, method, n_records):
fua = core.utils.city_fua[city]

gdf_1 = core.utils.read_neatnet(fua, pytest.epsg_4326)
gdf_2 = core.utils.read_neatnet(core.utils.fua_city[fua], pytest.epsg_4326)
gdf_1 = core.utils.read_results(fua, method, pytest.epsg_4326)
gdf_2 = core.utils.read_results(core.utils.fua_city[fua], method, pytest.epsg_4326)

geopandas.testing.assert_geodataframe_equal(gdf_1, gdf_2)

Expand Down
67 changes: 17 additions & 50 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
"read_sample_data",
"read_original",
"read_no_degree_2",
"read_manual",
"read_cityseer",
"read_osmnx",
"read_parenx",
"read_neatnet",
"read_results",
"graph_size",
"load_usecases",
"make_grid",
Expand Down Expand Up @@ -82,62 +78,33 @@ def read_no_degree_2(fua: int | str) -> geopandas.GeoDataFrame:
return geopandas.read_parquet(_fua_path(fua, "no_degree_2"))


def read_manual(fua: int, proj_crs: str | int | pyproj.CRS) -> geopandas.GeoDataFrame:
"""Read in manually prepared simplified road data."""
return (
geopandas.read_parquet(_fua_path(fua, "manual"))[["geometry"]]
.explode(ignore_index=True, index_parts=False)
.to_crs(proj_crs)
)


def read_cityseer(
fua: int | str, proj_crs: str | int | pyproj.CRS
def read_results(
fua: int | str, method: 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:
"""Read OSM roads from parquet format; return bare columns."""
return (
geopandas.read_parquet(_fua_path(fua, "osmnx"))
.explode(ignore_index=True, index_parts=False)
.to_crs(proj_crs)
)
"""Read results parquet format.
Parameters
-----------
fua : int | str
Code or name of FUA.
method : str
Simplification method used.
proj_crs : str | int | pyproj.CRS
Projected CRS information.
def read_parenx(
fua: int, option: str, proj_crs: str | int | pyproj.CRS
) -> geopandas.GeoDataFrame:
"""
Read in prepared parenx data.
option is one of: voronoi, skeletonize
Returns
--------
geopandas.GeoDataFrame
Simplified results from ``fua`` by ``method``.
"""

return (
geopandas.read_parquet(_fua_path(fua, f"parenx-{option}"))
geopandas.read_parquet(_fua_path(fua, method))
.explode(ingore_index=True, index_parts=False)
.to_crs(proj_crs)
)


def read_neatnet(fua: int, proj_crs: str | int | pyproj.CRS) -> geopandas.GeoDataFrame:
"""Read in prepared neatnet data."""

return (
geopandas.read_parquet(_fua_path(fua, "neatnet"))
.explode(ignore_index=True, index_parts=False)
.to_crs(proj_crs)
)


def graph_size(info: str, g: networkx.Graph) -> str:
return f"{info}\n\t* {g}"

Expand Down

0 comments on commit fed852f

Please sign in to comment.