Skip to content

Commit

Permalink
Refactored code to fix webviz store
Browse files Browse the repository at this point in the history
  • Loading branch information
HansKallekleiv committed Sep 1, 2021
1 parent 1f05ecc commit 6a53a0a
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 271 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Tuple, TYPE_CHECKING
from typing import Tuple, Callable

import pandas as pd
import dash
from dash.dependencies import Input, Output, ALL
import dash_html_components as html
Expand All @@ -9,28 +10,31 @@
from ..views.property_delta_view import table_view, surface_views
from ..utils.surface import surface_from_zone_prop

if TYPE_CHECKING:
# pylint: disable=cyclic-import
from ..property_statistics import PropertyStatistics
from ..models import PropertyStatisticsModel


def property_delta_controller(parent: "PropertyStatistics", app: dash.Dash) -> None:
def property_delta_controller(
get_uuid: Callable,
property_model: PropertyStatisticsModel,
surface_table: pd.DataFrame,
app: dash.Dash,
) -> None:
@app.callback(
Output(parent.uuid("delta-bar-graph"), "figure"),
Output(parent.uuid("delta-table-surface-wrapper"), "children"),
Input(parent.uuid("delta-switch-table-surface"), "value"),
Input(parent.uuid("delta-sort"), "value"),
Input({"id": parent.uuid("ensemble-selector"), "tab": "delta"}, "value"),
Input({"id": parent.uuid("delta-ensemble-selector"), "tab": "delta"}, "value"),
Output(get_uuid("delta-bar-graph"), "figure"),
Output(get_uuid("delta-table-surface-wrapper"), "children"),
Input(get_uuid("delta-switch-table-surface"), "value"),
Input(get_uuid("delta-sort"), "value"),
Input({"id": get_uuid("ensemble-selector"), "tab": "delta"}, "value"),
Input({"id": get_uuid("delta-ensemble-selector"), "tab": "delta"}, "value"),
Input(
{"id": parent.uuid("property-selector"), "tab": "delta"},
{"id": get_uuid("property-selector"), "tab": "delta"},
"value",
),
Input(
{"id": parent.uuid("filter-selector"), "tab": "delta", "selector": ALL},
{"id": get_uuid("filter-selector"), "tab": "delta", "selector": ALL},
"value",
),
Input(parent.uuid("delta-bar-graph"), "clickData"),
Input(get_uuid("delta-bar-graph"), "clickData"),
)
def _update_bars(
plot_type: str,
Expand All @@ -42,7 +46,7 @@ def _update_bars(
clickdata: dict,
) -> Tuple[go.Figure, html.Div]:
# Make bar chart
bars = parent.pmodel.make_delta_bars(
bars = property_model.make_delta_bars(
prop=prop,
ensemble=ensemble,
delta_ensemble=delta_ensemble,
Expand All @@ -59,14 +63,15 @@ def _update_bars(
)
# Make surface div
wrapper = make_surfaces(
parent=parent,
get_uuid=get_uuid,
surface_table=surface_table,
prop=prop,
zone=label.split(" | ")[0],
ensemble=ensemble,
delta_ensemble=delta_ensemble,
)
if plot_type == "table":
columns, data = parent.pmodel.make_delta_table(
columns, data = property_model.make_delta_table(
prop=prop,
ensemble=ensemble,
delta_ensemble=delta_ensemble,
Expand All @@ -79,22 +84,29 @@ def _update_bars(


def make_surfaces(
parent: "PropertyStatistics",
surface_table: pd.DataFrame,
prop: str,
zone: str,
ensemble: str,
delta_ensemble: str,
get_uuid: Callable,
statistic: str = "mean",
) -> html.Div:

sprop = parent.surface_renaming.get(prop, prop)
szone = parent.surface_renaming.get(zone, zone)
try:
ens_surface = surface_from_zone_prop(
parent, zone=szone, prop=sprop, ensemble=ensemble, stype=statistic
surface_table,
zone=zone,
prop=prop,
ensemble=ensemble,
stype=statistic,
)
delta_ens_surface = surface_from_zone_prop(
parent, zone=szone, prop=sprop, ensemble=delta_ensemble, stype=statistic
surface_table,
zone=zone,
prop=prop,
ensemble=delta_ensemble,
stype=statistic,
)
except ValueError:
return html.Div("No surfaces found")
Expand All @@ -117,7 +129,7 @@ def make_surfaces(
diff_layer=SurfaceLeafletModel(diff_surface, name="diff_surface").layer,
ensemble=ensemble,
delta_ensemble=delta_ensemble,
parent=parent,
get_uuid=get_uuid,
prop=prop,
zone=zone,
)
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from typing import Union, List, TYPE_CHECKING
from typing import Union, List, Callable

import dash
from dash.dependencies import Input, Output, ALL
import dash_table
import webviz_core_components as wcc

if TYPE_CHECKING:
# pylint: disable=cyclic-import
from ..property_statistics import PropertyStatistics
from ..models import PropertyStatisticsModel


def property_qc_controller(parent: "PropertyStatistics", app: dash.Dash) -> None:
def property_qc_controller(
get_uuid: Callable, property_model: PropertyStatisticsModel, app: dash.Dash
) -> None:
@app.callback(
Output(parent.uuid("property-qc-wrapper"), "children"),
Input({"id": parent.uuid("ensemble-selector"), "tab": "qc"}, "value"),
Input({"id": parent.uuid("property-selector"), "tab": "qc"}, "value"),
Output(get_uuid("property-qc-wrapper"), "children"),
Input({"id": get_uuid("ensemble-selector"), "tab": "qc"}, "value"),
Input({"id": get_uuid("property-selector"), "tab": "qc"}, "value"),
Input(
{"id": parent.uuid("filter-selector"), "tab": "qc", "selector": ALL},
{"id": get_uuid("filter-selector"), "tab": "qc", "selector": ALL},
"value",
),
Input(parent.uuid("property-qc-plot-type"), "value"),
Input(parent.uuid("property-qc-axis-match"), "value"),
Input(get_uuid("property-qc-plot-type"), "value"),
Input(get_uuid("property-qc-axis-match"), "value"),
)
def _update_bars(
ensembles: Union[str, List[str]],
Expand All @@ -31,7 +31,7 @@ def _update_bars(
) -> Union[dash_table.DataTable, wcc.Graph]:
ensembles = ensembles if isinstance(ensembles, list) else [ensembles]
if plot_type == "table":
columns, dframe = parent.pmodel.make_statistics_table(
columns, dframe = property_model.make_statistics_table(
prop=prop, ensembles=ensembles, selector_values=selectors
)
return dash_table.DataTable(
Expand All @@ -51,10 +51,10 @@ def _update_bars(
merge_duplicate_headers=True,
)
return wcc.Graph(
id=parent.uuid("property-qc-graph"),
id=get_uuid("property-qc-graph"),
config={"displayModeBar": False},
style={"height": "75vh"},
figure=parent.pmodel.make_grouped_plot(
figure=property_model.make_grouped_plot(
prop=prop,
ensembles=ensembles,
selector_values=selectors,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Union, TYPE_CHECKING
from typing import Tuple, Union, Callable

import numpy as np
import pandas as pd
Expand All @@ -10,20 +10,22 @@
from ..utils.colors import find_intermediate_color_rgba
from ..figures.correlation_figure import CorrelationFigure
from ..utils.surface import surface_from_zone_prop
from ..models import SimulationTimeSeriesModel
from ..models import PropertyStatisticsModel, SimulationTimeSeriesModel

if TYPE_CHECKING:
# pylint: disable=cyclic-import
from ..property_statistics import PropertyStatistics


def property_response_controller(parent: "PropertyStatistics", app: dash.Dash) -> None:
def property_response_controller(
get_uuid: Callable,
surface_table: pd.DataFrame,
property_model: PropertyStatisticsModel,
timeseries_model: SimulationTimeSeriesModel,
app: dash.Dash,
) -> None:
@app.callback(
Output({"id": parent.uuid("surface-view"), "tab": "response"}, "layers"),
Output({"id": parent.uuid("surface-name"), "tab": "response"}, "children"),
Input(parent.uuid("property-response-correlation-graph"), "clickData"),
Input({"id": parent.uuid("ensemble-selector"), "tab": "response"}, "value"),
Input({"id": parent.uuid("surface-type"), "tab": "response"}, "value"),
Output({"id": get_uuid("surface-view"), "tab": "response"}, "layers"),
Output({"id": get_uuid("surface-name"), "tab": "response"}, "children"),
Input(get_uuid("property-response-correlation-graph"), "clickData"),
Input({"id": get_uuid("ensemble-selector"), "tab": "response"}, "value"),
Input({"id": get_uuid("surface-type"), "tab": "response"}, "value"),
)
def _update_surface(
clickdata: Union[None, dict], ensemble: str, stype: str
Expand All @@ -32,11 +34,14 @@ def _update_surface(
label = clickdata["points"][0]["y"]
prop = label.split(" | ")[0]
zone = label.split(" | ")[1]
sprop = parent.surface_renaming.get(prop, prop)
szone = parent.surface_renaming.get(zone, zone)

try:
surface = surface_from_zone_prop(
parent, zone=szone, prop=sprop, ensemble=ensemble, stype=stype
surface_table,
zone=zone,
prop=prop,
ensemble=ensemble,
stype=stype,
)
except ValueError: # Surface does not exist
return (
Expand All @@ -48,14 +53,14 @@ def _update_surface(
raise PreventUpdate

@app.callback(
Output(parent.uuid("property-response-correlated-slider"), "min"),
Output(parent.uuid("property-response-correlated-slider"), "max"),
Output(parent.uuid("property-response-correlated-slider"), "step"),
Output(parent.uuid("property-response-correlated-slider"), "value"),
Output(parent.uuid("property-response-correlated-slider"), "marks"),
Output(parent.uuid("property-response-correlated-slider"), "disabled"),
Input(parent.uuid("property-response-correlated-filter"), "value"),
Input({"id": parent.uuid("ensemble-selector"), "tab": "response"}, "value"),
Output(get_uuid("property-response-correlated-slider"), "min"),
Output(get_uuid("property-response-correlated-slider"), "max"),
Output(get_uuid("property-response-correlated-slider"), "step"),
Output(get_uuid("property-response-correlated-slider"), "value"),
Output(get_uuid("property-response-correlated-slider"), "marks"),
Output(get_uuid("property-response-correlated-slider"), "disabled"),
Input(get_uuid("property-response-correlated-filter"), "value"),
Input({"id": get_uuid("ensemble-selector"), "tab": "response"}, "value"),
)
# pylint: disable=protected-access
def _update_correlation_figure(
Expand All @@ -69,7 +74,7 @@ def _update_correlation_figure(
bool,
]:
if label is not None:
values = parent.pmodel.filter_on_label(label=label, ensemble=ensemble)
values = property_model.filter_on_label(label=label, ensemble=ensemble)
return (
values.min(),
values.max(),
Expand All @@ -92,23 +97,23 @@ def _update_correlation_figure(
)

@app.callback(
Output(parent.uuid("property-response-vector-graph"), "figure"),
Output(parent.uuid("property-response-correlation-graph"), "figure"),
Input({"id": parent.uuid("ensemble-selector"), "tab": "response"}, "value"),
Input(parent.uuid("property-response-vector-select"), "value"),
Input(parent.uuid("property-response-vector-graph"), "clickData"),
Input(parent.uuid("property-response-correlation-graph"), "clickData"),
Output(get_uuid("property-response-vector-graph"), "figure"),
Output(get_uuid("property-response-correlation-graph"), "figure"),
Input({"id": get_uuid("ensemble-selector"), "tab": "response"}, "value"),
Input(get_uuid("property-response-vector-select"), "value"),
Input(get_uuid("property-response-vector-graph"), "clickData"),
Input(get_uuid("property-response-correlation-graph"), "clickData"),
Input(
{
"id": parent.uuid("filter-selector"),
"id": get_uuid("filter-selector"),
"tab": "response",
"selector": ALL,
},
"value",
),
Input(parent.uuid("property-response-correlated-slider"), "value"),
State(parent.uuid("property-response-vector-graph"), "figure"),
State(parent.uuid("property-response-correlated-filter"), "value"),
Input(get_uuid("property-response-correlated-slider"), "value"),
State(get_uuid("property-response-vector-graph"), "figure"),
State(get_uuid("property-response-correlated-filter"), "value"),
)
# pylint: disable=too-many-locals
def _update_graphs(
Expand All @@ -131,7 +136,7 @@ def _update_graphs(

# Filter realizations if correlation filter is active
real_filter = (
parent.pmodel.filter_reals_on_label_range(ensemble, label, corr_filter)
property_model.filter_reals_on_label_range(ensemble, label, corr_filter)
if corr_filter[0] is not None and corr_filter[1] is not None
else None
)
Expand All @@ -140,23 +145,23 @@ def _update_graphs(
if any(
substr in ctx
for substr in [
parent.uuid("property-response-vector-select"),
parent.uuid("ensemble-selector"),
parent.uuid("property-response-correlated-slider"),
get_uuid("property-response-vector-select"),
get_uuid("ensemble-selector"),
get_uuid("property-response-correlated-slider"),
]
):

figure = update_timeseries_graph(
parent.vmodel, ensemble, vector, real_filter
timeseries_model, ensemble, vector, real_filter
)

# Get clicked data or last available date initially
date = (
timeseries_clickdata.get("points", [{}])[0].get(
"x", parent.vmodel.get_last_date(ensemble)
"x", timeseries_model.get_last_date(ensemble)
)
if timeseries_clickdata
else parent.vmodel.get_last_date(ensemble)
else timeseries_model.get_last_date(ensemble)
)

# Draw clicked date as a black line
Expand All @@ -167,13 +172,13 @@ def _update_graphs(
]

# Get dataframe with vector and REAL
vector_df = parent.vmodel.get_ensemble_vector_for_date(
vector_df = timeseries_model.get_ensemble_vector_for_date(
ensemble=ensemble, vector=vector, date=date
)
vector_df["REAL"] = vector_df["REAL"].astype(int)

# Get dataframe with properties per label and REAL
prop_df = parent.pmodel.get_ensemble_properties(ensemble, selectors)
prop_df = property_model.get_ensemble_properties(ensemble, selectors)
prop_df["REAL"] = prop_df["REAL"].astype(int)
prop_df = (
prop_df[prop_df["REAL"].isin(real_filter)]
Expand All @@ -198,7 +203,7 @@ def _update_graphs(

# Order realizations sorted on value of property
real_order = (
parent.pmodel.get_real_order(ensemble, series=selected_corr)
property_model.get_real_order(ensemble, series=selected_corr)
if selected_corr is not None
else None
)
Expand Down
Loading

0 comments on commit 6a53a0a

Please sign in to comment.