From f5b80c004850bee04cd1f771866c50412838b85b Mon Sep 17 00:00:00 2001 From: Herbie Bradley Date: Thu, 11 Mar 2021 11:42:31 +0000 Subject: [PATCH] fix: avg_comp_isolation = 0 if only one component --- src/models/metrics.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/models/metrics.py b/src/models/metrics.py index b35b5c9..ab94c17 100644 --- a/src/models/metrics.py +++ b/src/models/metrics.py @@ -101,20 +101,24 @@ def _avg_component_isolation(geo_graph: geograph.GeoGraph) -> Metric: raise ValueError( "This metric is not valid for ComponentGeoGraphs without distance edges." ) - - dist_set = set() - for comp in comp_geograph.graph.nodes: - for nbr in comp_geograph.graph.adj[comp]: - dist_set.update( - [ - dist - for u, v, dist in comp_geograph.graph.edges(nbr, data="distance") - if v != comp - ] - ) - mean_patch_isolation = np.mean(np.fromiter(dist_set, np.float32, len(dist_set))) + if len(comp_geograph.components_list) == 1: + val: Any = 0 + else: + dist_set = set() + for comp in comp_geograph.graph.nodes: + for nbr in comp_geograph.graph.adj[comp]: + dist_set.update( + [ + dist + for u, v, dist in comp_geograph.graph.edges( + nbr, data="distance" + ) + if v != comp + ] + ) + val = np.mean(np.fromiter(dist_set, np.float32, len(dist_set))) return Metric( - value=mean_patch_isolation, + value=val, name="avg_component_isolation", description="The average distance to the next-nearest component", variant="component",