Skip to content

Commit

Permalink
Add new metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Mar 16, 2024
1 parent 44b30ba commit 1373c61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
5 changes: 4 additions & 1 deletion swmmanywhere/metric_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def outlet_nse_flooding(synthetic_G: nx.Graph,
list(sg_syn.nodes),
list(sg_real.nodes))

@metrics.register
def outlet_kstest_diameters(real_G: nx.Graph,
synthetic_G: nx.Graph,
real_results: pd.DataFrame,
Expand All @@ -453,7 +454,6 @@ def outlet_kstest_diameters(real_G: nx.Graph,
'real' network is calculated by dominant_outlet, while the dominant outlet
node of the 'synthetic' network is calculated by best_outlet_match.
"""

# Identify synthetic and real outlet arcs
sg_syn, _ = best_outlet_match(synthetic_G, real_subs)
sg_real, _ = dominant_outlet(real_G, real_results)
Expand All @@ -464,6 +464,7 @@ def outlet_kstest_diameters(real_G: nx.Graph,
return stats.ks_2samp(list(syn_diameters.values()),
list(real_diameters.values())).statistic

@metrics.register
def outlet_pbias_length(real_G: nx.Graph,
synthetic_G: nx.Graph,
real_results: pd.DataFrame,
Expand All @@ -485,6 +486,7 @@ def outlet_pbias_length(real_G: nx.Graph,
real_length = sum([d['length'] for u,v,d in sg_real.edges(data=True)])
return (syn_length - real_length) / real_length

@metrics.register
def outlet_pbias_nmanholes(real_G: nx.Graph,
synthetic_G: nx.Graph,
real_results: pd.DataFrame,
Expand All @@ -504,6 +506,7 @@ def outlet_pbias_nmanholes(real_G: nx.Graph,
return (sg_syn.number_of_nodes() - sg_real.number_of_nodes()) \
/ sg_real.number_of_nodes()

@metrics.register
def outlet_pbias_npipes(real_G: nx.Graph,
synthetic_G: nx.Graph,
real_results: pd.DataFrame,
Expand Down
48 changes: 38 additions & 10 deletions tests/test_metric_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,27 +282,55 @@ def test_outlet_nse_flooding():
def test_design_params():
"""Test the design param related metrics."""
G = load_graph(Path(__file__).parent / 'test_data' / 'graph_topo_derived.json')
nx.set_edge_attributes(G, 0.15, 'diameter')
subs = get_subs()

# Mock results
results = pd.DataFrame([{'object' : 4253560,
# Mock results (only needed for dominant outlet)
results = pd.DataFrame([{'id' : 4253560,
'variable' : 'flow',
'value' : 10,
'date' : pd.to_datetime('2021-01-01 00:00:00')},
{'object' : 4253560,
{'id' : 4253560,
'variable' : 'flow',
'value' : 5,
'date' : pd.to_datetime('2021-01-01 00:00:05')},
])

# Calculate NSE (perfect results)
val = mu.metrics.outlet_kstest_diameters(synthetic_G = G,
synthetic_results = results,
real_G = G,
real_results = results,
real_subs = subs)
assert val == 0.0
# Target results
design_results = {'outlet_kstest_diameters' : 0.0625,
'outlet_pbias_length' : -0.15088965,
'outlet_pbias_nmanholes' : -0.05,
'outlet_pbias_npipes' : -0.15789473}

# Iterate for G = G, i.e., perfect results
metrics = mu.iterate_metrics(synthetic_G = G,
synthetic_subs = None,
synthetic_results = None,
real_G = G,
real_subs = subs,
real_results = results,
metric_list = design_results.keys())
for metric, val in metrics.items():
assert metric in design_results
assert np.isclose(val, 0)

# edit the graph for target results
G_ = G.copy()
G_.remove_node(list(G.nodes)[0])
G_.edges[list(G_.edges)[0]]['diameter'] = 0.3

metrics = mu.iterate_metrics(synthetic_G = G_,
synthetic_subs = None,
synthetic_results = None,
real_G = G,
real_subs = subs,
real_results = results,
metric_list = design_results.keys())

for metric, val in metrics.items():
assert metric in design_results
assert np.isclose(val, design_results[metric]), metric

def test_netcomp_iterate():
"""Test the netcomp metrics and iterate_metrics."""
netcomp_results = {'nc_deltacon0' : 0.00129408,
Expand Down

0 comments on commit 1373c61

Please sign in to comment.