Ejemplo n.º 1
0
def _get_realizations_statistics_plots(
    df_response: pd.DataFrame,
    x_axis: Optional[List[Union[int, str, datetime.datetime]]],
    color: str,
) -> List[PlotModel]:
    data = df_response
    p10 = data.quantile(0.1, axis=1)
    p90 = data.quantile(0.9, axis=1)
    _mean = data.mean(axis=1)
    style = deepcopy(assets.ERTSTYLE["response-plot"]["statistics"])
    style["line"]["color"] = color
    style_mean = deepcopy(style)
    style_mean["line"]["dash"] = "solid"
    mean_data = PlotModel(x_axis=x_axis,
                          y_axis=_mean,
                          text="Mean",
                          name="Mean",
                          **style_mean)
    lower_std_data = PlotModel(x_axis=x_axis,
                               y_axis=p10,
                               text="p10 quantile",
                               name="p10 quantile",
                               **style)
    upper_std_data = PlotModel(x_axis=x_axis,
                               y_axis=p90,
                               text="p90 quantile",
                               name="p90 quantile",
                               **style)
    return [mean_data, lower_std_data, upper_std_data]
Ejemplo n.º 2
0
def _get_observation_plots(observation_df: pd.DataFrame) -> PlotModel:
    data = observation_df["values"]
    stds = observation_df["std"]
    x_axis = observation_df["x_axis"]
    attributes = observation_df["attributes"]
    active_mask = observation_df["active"]

    style = assets.ERTSTYLE["response-plot"]["observation"]
    color = [
        style["color"] if active else "rgb(0, 0, 0)" for active in active_mask
    ]
    style["marker"]["color"] = color

    observation_data = PlotModel(
        x_axis=x_axis,
        y_axis=data,
        text=attributes,
        name="Observation",
        error_y=dict(
            type="data",  # value of error bar given in data coordinates
            array=stds.values,
            visible=True,
        ),
        **style,
    )
    return observation_data
Ejemplo n.º 3
0
def _get_realizations_plots(
    realizations_df: pd.DataFrame,
    x_axis: Optional[List[Union[int, str, datetime.datetime]]],
    color: str,
    style: Optional[Dict] = None,
) -> List[PlotModel]:
    if style:
        _style = style
    else:
        _style = assets.ERTSTYLE["response-plot"]["response"].copy()
    _style.update({"line": {"color": color}})
    _style.update({"marker": {"color": color}})
    realizations_data = list()
    for realization in realizations_df:
        plot = PlotModel(
            x_axis=x_axis,
            y_axis=realizations_df[realization].values,
            text=realization,
            name=realization,
            **_style,
        )
        realizations_data.append(plot)
    return realizations_data
    def update_response_parameter_scatterplot(
        corr_xindex: Dict,
        corr_param_resp: Dict,
        parameters: List[str],
        responses: List[str],
        ensembles: Optional[Mapping[str, Dict]],
    ) -> Optional[Tuple[go.Figure, Component]]:

        if not (parameters and ensembles and responses
                and corr_param_resp["parameter"] in parameters
                and corr_param_resp["response"] in responses):
            raise PreventUpdate

        selected_parameter = corr_param_resp["parameter"]
        selected_response = corr_param_resp["response"]
        _plots = []
        _resp_plots = {}
        _param_plots = {}
        _colors = {}

        for ensemble_id, data in ensembles.items():
            ensemble = load_ensemble(parent, ensemble_id)

            if ensemble.parameters and ensemble.responses:
                y_data = ensemble.parameters[selected_parameter].data_df()
                response = ensemble.responses[selected_response]

                if response.axis is not None:
                    x_index = corr_xindex.get(selected_response, 0)
                    x_data = response.data_df().iloc[x_index]
                    style = deepcopy(
                        assets.ERTSTYLE["response-plot"]["response-index"])
                    style["marker"]["color"] = data["color"]
                    _colors[str(ensemble)] = data["color"]
                    _plots += [
                        PlotModel(
                            x_axis=x_data.values.flatten(),
                            y_axis=y_data.values.flatten(),
                            text="Mean",
                            name=
                            f"{repr(ensemble)}: {selected_response}x{selected_parameter}@{response.axis[x_index]}",
                            **style,
                        )
                    ]
                    _resp_plots[str(ensemble)] = x_data.values.flatten()
                    _param_plots[str(ensemble)] = y_data.values.flatten()

        fig = make_subplots(
            rows=4,
            cols=2,
            specs=[
                [{
                    "colspan": 2,
                    "rowspan": 3
                }, None],
                [None, None],
                [None, None],
                [{
                    "rowspan": 1
                }, {
                    "rowspan": 1
                }],
            ],
        )
        for plot in _plots:
            fig.add_trace(plot.repr, 1, 1)

        for key in _param_plots:
            fig.add_trace(
                {
                    "type": "histogram",
                    "x": _param_plots[key],
                    "showlegend": False,
                    "marker_color": _colors[key],
                },
                4,
                1,
            )
        for key in _resp_plots:
            fig.add_trace(
                {
                    "type": "histogram",
                    "x": _resp_plots[key],
                    "showlegend": False,
                    "marker_color": _colors[key],
                },
                4,
                2,
            )
        fig.update_layout(assets.ERTSTYLE["figure"]["layout"])
        fig.update_layout(showlegend=False)
        final_text = []
        for response_name in responses:
            x_axis = ensemble.responses[response_name].axis
            if x_axis is not None:
                x_value = x_axis[corr_xindex.get(response_name, 0)]
                if response_name == selected_response:
                    res_text = f"**{response_name} @ {x_value}**, "
                else:
                    res_text = f"{response_name} @ {x_value}, "
                final_text.append(dcc.Markdown(res_text))
        final_text += [
            dcc.Markdown(f"parameter: **{corr_param_resp['parameter']}**")
        ]
        return fig, html.Div(final_text)
    def update_response_overview_plot(
        _: Any,
        __: Any,
        ___: Any,
        ____: Any,
        responses: Optional[List[str]],
        ensembles: Optional[Mapping[str, Dict]],
        corr_xindex: Dict,
        corr_param_resp: Dict,
    ) -> Optional[go.Figure]:
        if not (ensembles and responses
                and corr_param_resp["response"] in responses):
            raise PreventUpdate
        selected_response = corr_param_resp["response"]
        _plots = []
        _obs_plots: List[PlotModel] = []

        for ensemble_id, data in ensembles.items():
            ensemble = load_ensemble(parent, ensemble_id)
            response = ensemble.responses[selected_response]
            x_axis = response.axis
            if x_axis is not None:
                if str(x_axis[0]).isnumeric():
                    style = deepcopy(
                        assets.ERTSTYLE["response-plot"]["response-index"])
                else:
                    style = deepcopy(
                        assets.ERTSTYLE["response-plot"]["response"])
            data_df = response.data_df().copy()

            style.update({"marker": {"color": data["color"]}})
            style.update({"line": {"color": data["color"]}})
            _plots += [
                PlotModel(
                    x_axis=x_axis,
                    y_axis=data_df[realization],
                    text=realization,
                    name=realization,
                    **style,
                ) for realization in data_df
            ]

            if response.observations:
                for obs in response.observations:
                    _obs_plots.append(_get_observation_plots(obs.data_df()))

        fig = go.Figure()
        for plot in _plots:
            fig.add_trace(plot.repr)

        _layout = assets.ERTSTYLE["figure"]["layout"].copy()
        _layout.update(dict(showlegend=False))
        fig.update_layout(_layout)

        x_index = corr_xindex.get(selected_response, 0)
        if x_axis is not None:
            fig.add_shape(
                type="line",
                x0=x_axis[x_index],
                y0=0,
                x1=x_axis[x_index],
                y1=1,
                yref="paper",
                line=dict(color="rgb(30, 30, 30)", dash="dash", width=3),
            )
        # draw observations on top
        for plot in _obs_plots:
            fig.add_trace(plot.repr)

        fig.update_layout(clickmode="event+select")
        return fig