Skip to content

Commit

Permalink
Stop execution if plotting takes too much time. (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c authored Jul 2, 2024
1 parent 866367e commit 9e91eb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ ignore_missing_imports = True
[mypy-solcx.*]
ignore_missing_imports = True

[mypy-stopit.*]
ignore_missing_imports = True

[mypy-yaml.*]
ignore_missing_imports = True

4 changes: 3 additions & 1 deletion pdr_backend/sim/dash_plots/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def update_graph_live(n, selected_vars, selected_vars_old, selected_tab):
state_options = sim_plotter.aimodel_plotdata.colnames
elements.append(selected_var_checklist(state_options, selected_vars_old))

figures = get_figures_by_state(sim_plotter, selected_vars)
timeout = 2 if ts != "final" or n < 2 else 10

figures = get_figures_by_state(sim_plotter, selected_vars, timeout=timeout)
tabs = get_tabs(figures)
selected_tab_value = selected_tab if selected_tab else tabs[0]["name"]
elements = elements + [get_tabs_component(tabs, selected_tab_value)]
Expand Down
33 changes: 23 additions & 10 deletions pdr_backend/sim/dash_plots/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@
# Copyright 2024 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import plotly.graph_objects as go
import stopit

from pdr_backend.aimodel import aimodel_plotter
from pdr_backend.sim.dash_plots.view_elements import figure_names
from pdr_backend.sim.sim_plotter import SimPlotter


def get_figures_by_state(sim_plotter: SimPlotter, selected_vars):
def get_figures_by_state(sim_plotter: SimPlotter, selected_vars, timeout=2):
figures = {}

for key in figure_names:
if not key.startswith("aimodel"):
fig = getattr(sim_plotter, f"plot_{key}")()
with stopit.ThreadingTimeout(timeout) as context_manager:
fig = getattr(sim_plotter, f"plot_{key}")()

if context_manager.state == context_manager.TIMED_OUT:
fig = go.Figure()
else:
if key in ["aimodel_response", "aimodel_varimps"]:
sweep_vars = []
for var in selected_vars:
sweep_vars.append(sim_plotter.aimodel_plotdata.colnames.index(var))
sim_plotter.aimodel_plotdata.sweep_vars = sweep_vars

func_name = getattr(aimodel_plotter, f"plot_{key}")
fig = func_name(sim_plotter.aimodel_plotdata)
with stopit.ThreadingTimeout(timeout) as context_manager:
if key in ["aimodel_response", "aimodel_varimps"]:
sweep_vars = []
for var in selected_vars:
sweep_vars.append(
sim_plotter.aimodel_plotdata.colnames.index(var)
)
sim_plotter.aimodel_plotdata.sweep_vars = sweep_vars

func_name = getattr(aimodel_plotter, f"plot_{key}")
fig = func_name(sim_plotter.aimodel_plotdata)

if context_manager.state == context_manager.TIMED_OUT:
fig = go.Figure()

figures[key] = fig

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dash_bootstrap_components==1.6.0",
"web3==6.20.0",
"sapphire.py==0.2.3",
"stopit==1.1.2",
"ocean-contracts==2.0.4", # install this last
]

Expand Down

0 comments on commit 9e91eb6

Please sign in to comment.