Skip to content

Commit

Permalink
Ensure align_calc_nse dates are consistent format
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Mar 7, 2024
1 parent 3699dd6 commit f12a790
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions swmmanywhere/metric_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,29 @@ def align_calc_nse(synthetic_results: pd.DataFrame,
efficiency (NSE) of the variable over time. In cases where the synthetic
data is does not overlap the real data, the value is interpolated.
"""
# Format dates
synthetic_results['date'] = pd.to_datetime(synthetic_results['date'])
real_results['date'] = pd.to_datetime(real_results['date'])

# Extract data
syn_data = extract_var(synthetic_results, variable)
syn_data = syn_data.loc[syn_data.object.isin(syn_ids)]
syn_data = syn_data.groupby('date').value.sum().reset_index()
syn_data = syn_data.groupby('date').value.sum()

real_data = extract_var(real_results, variable)
real_data = real_data.loc[real_data.object.isin(real_ids)]
real_data = real_data.groupby('date').value.sum().reset_index()

real_data = real_data.groupby('date').value.sum()
# Align data
df = pd.merge(syn_data,
real_data,
on='date',
left_index = True,
right_index = True,
suffixes=('_syn', '_real'),
how='outer').sort_values(by='date')
how='outer').sort_index()

# Interpolate to time in real data
df['value_syn'] = df.set_index('date').value_syn.interpolate().to_numpy()
df['value_syn'] = df.value_syn.interpolate().to_numpy()
df = df.dropna(subset=['value_real'])

# Calculate NSE
Expand Down
6 changes: 3 additions & 3 deletions tests/test_metric_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def test_outlet_nse_flow():
results = pd.DataFrame([{'object' : 4253560,
'variable' : 'flow',
'value' : 10,
'date' : pd.to_datetime('2021-01-01 00:00:00')},
'date' : pd.to_datetime('2021-01-01').date()},
{'object' : '',
'variable' : 'flow',
'value' : 5,
'date' : pd.to_datetime('2021-01-01 00:00:00')},
'date' : pd.to_datetime('2021-01-01').date()},
{'object' : 4253560,
'variable' : 'flow',
'value' : 5,
Expand All @@ -136,7 +136,7 @@ def test_outlet_nse_flow():
'variable' : 'flow',
'value' : 2,
'date' : pd.to_datetime('2021-01-01 00:00:05')}])

# Calculate NSE (perfect results)
val = mu.metrics.outlet_nse_flow(synthetic_G = G,
synthetic_results = results,
Expand Down

0 comments on commit f12a790

Please sign in to comment.