Skip to content

Commit

Permalink
feature: Metrics automatically calc requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
herbiebradley committed Mar 12, 2021
1 parent f5b80c0 commit b196db1
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/models/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ def _total_area(geo_graph: geograph.GeoGraph) -> Metric:


def _avg_component_area(geo_graph: geograph.GeoGraph) -> Metric:
if not geo_graph.components.has_df:
print("Calculating component polygons...")
geo_graph.components = geo_graph.get_graph_components(calc_polygons=True)
comp_geograph = geo_graph.components
if not comp_geograph.has_df:
raise ValueError(
"This metric is not valid for ComponentGeoGraphs without a dataframe."
)
return Metric(
value=np.mean(comp_geograph.df.area.values),
name="avg_component_area",
Expand All @@ -91,16 +90,21 @@ def _avg_component_area(geo_graph: geograph.GeoGraph) -> Metric:


def _avg_component_isolation(geo_graph: geograph.GeoGraph) -> Metric:
"""Calculate the average distance to the next-nearest component."""
comp_geograph = geo_graph.components
if not comp_geograph.has_df:
raise ValueError(
"This metric is not valid for ComponentGeoGraphs without a dataframe."
"""
Calculate the average distance to the next-nearest component.
Warning: very computationally expensive for graphs with more than ~100
components.
"""
if not geo_graph.components.has_df or not geo_graph.components.has_distance_edges:
print(
"""Warning: very computationally expensive for graphs with more
than ~100 components."""
)
elif not comp_geograph.has_distance_edges:
raise ValueError(
"This metric is not valid for ComponentGeoGraphs without distance edges."
geo_graph.components = geo_graph.get_graph_components(
calc_polygons=True, add_distance_edges=True
)
comp_geograph = geo_graph.components
if len(comp_geograph.components_list) == 1:
val: Any = 0
else:
Expand Down Expand Up @@ -133,6 +137,16 @@ def _avg_component_isolation(geo_graph: geograph.GeoGraph) -> Metric:
"avg_component_isolation": _avg_component_isolation,
}

ALL_METRICS = [
"num_components",
"avg_patch_area",
"total_area",
"avg_component_area",
"avg_component_isolation",
]

STANDARD_METRICS = ["num_components", "avg_patch_area", "total_area"]


def _get_metric(name: str, geo_graph: geograph.GeoGraph) -> Metric:
try:
Expand Down

0 comments on commit b196db1

Please sign in to comment.