Skip to content

Commit

Permalink
return predictions plots also for non-supported residual type AND imt
Browse files Browse the repository at this point in the history
  • Loading branch information
rizac committed Nov 23, 2024
1 parent 7dd3072 commit 15e2b91
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
20 changes: 3 additions & 17 deletions egsim/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,10 @@ def output(self) -> dict:
'layout': layout
}

# set layout for empty data plots, copying from similar one
# (same model same imt, or at least same imt)
for key, plot in plots.items():
if plot['layout'] is not None:
if plot['layout'] is not None: # empty data plot
continue
layout_to_copy = {}
for key_, plot_ in plots.items():
Expand All @@ -322,22 +324,6 @@ def output(self) -> dict:
break
plot['layout'] = layout_to_copy

# # if we are processing total residuals, also set intra and inter
# # defaults as empty plot. If intra and inter were already (or will be )
# # processed, the skip this
# if res_type == total_res:
# for r_type in (intra_res, inter_res):
# key[-1] = r_type
# plots.setdefault(tuple(key), {
# 'data': [{}],
# 'params': {
# 'model': model,
# 'imt': imt,
# 'residual type': residual_label[r_type]
# },
# 'layout': dict(layout)
# })

# return keys sorted so that the frontend displays them accordingly:
return {'plots': [plots[key] for key in sorted(plots.keys())]}

Expand Down
43 changes: 42 additions & 1 deletion tests/django/test_app_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,46 @@ def test_predictions_residuals_visualize(self):
content_type="application/json")
assert response.status_code == 400

def test_predictions_visualize_no_missing_plots(self):
"""Some models do not produce plots for specific IMTs or residuals type
Test that we return those plots, albeit empty
"""
client = Client()
# SgobbaEtAl not defined for the given SA, AbrahmsonSilva only for total
models = ['SgobbaEtAl2020', 'AbrahamsonSilva1997']
imts = ['SA(0.1)', 'PGA']
data = {
'model': models,
'imt': imts,
'magnitude': [4],
'distance': [10]
}
response1 = client.post(f"/{URLS.PREDICTIONS}.csv",
json.dumps(data | {'format': 'csv'}),
content_type="application/json")
assert response1.status_code == 200
content = b''.join(response1.streaming_content)
dframe = pd.read_csv(BytesIO(content), index_col=0, header=0)
assert f'SA(0.1) {Clabel.median} SgobbaEtAl2020' not in dframe.columns
assert f'SA(0.1) {Clabel.std} AbrahamsonSilva1997' in dframe.columns
assert f'PGA {Clabel.median} SgobbaEtAl2020' in dframe.columns
assert f'SA(0.1) {Clabel.median} AbrahamsonSilva1997' in dframe.columns
# 2 imts for SgobbaEtAl, 4 for Abrahamson et al:
len_c = len([c for c in dframe.columns if not c.startswith(f'{Clabel.input} ')])
assert len_c == 6

response = client.post(f"/{URLS.PREDICTIONS_VISUALIZE}",
json.dumps(data | {'plot_type': 'm'}),
content_type="application/json")
assert response.status_code == 200
json_c = response.json()
plots = json_c['plots']
# first plot has 6 traces (3 Sgobba, 3 Abrhamson)
# second plot has 3 tracs (3 Abrhamson, Sgobba non-implemented for SA)
# the 3 plots per model is because we also have std (upper and lower bound)
assert (len(plots[0]['data']) == 6 and len(plots[1]['data']) == 3) or \
(len(plots[1]['data']) == 6 and len(plots[0]['data']) == 3)

def test_residuals_visualize_no_missing_plots(self):
"""Some models do not produce plots for specific IMTs or residuals type
Test that we return those plots, albeit empty
Expand All @@ -333,6 +373,7 @@ def test_residuals_visualize_no_missing_plots(self):
response1 = client.post(f"/{URLS.RESIDUALS}.csv",
json.dumps(data | {'format': 'csv'}),
content_type="application/json")
assert response1.status_code == 200
content = b''.join(response1.streaming_content)
dframe = pd.read_csv(BytesIO(content), index_col=0, header=0)
assert f'SA(0.1) {Clabel.total_res} SgobbaEtAl2020' not in dframe.columns
Expand All @@ -346,7 +387,7 @@ def test_residuals_visualize_no_missing_plots(self):
response = client.post(f"/{URLS.RESIDUALS_VISUALIZE}",
json.dumps(data),
content_type="application/json")
assert response.status == 200
assert response.status_code == 200
expected_params = set(
product(imts, models, ('Total', 'Inter event', 'Intra event'))
)
Expand Down

0 comments on commit 15e2b91

Please sign in to comment.