Skip to content

Commit

Permalink
Fix index for clicking on observation
Browse files Browse the repository at this point in the history
Closes #369
  • Loading branch information
valentin-krasontovitsch committed Aug 18, 2022
1 parent 98fb09a commit f70d429
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
5 changes: 4 additions & 1 deletion webviz_ert/controllers/multi_response_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def _get_realizations_statistics_plots(
return [mean_data, lower_std_data, upper_std_data]


def _get_observation_plots(observation_df: pd.DataFrame) -> PlotModel:
def _get_observation_plots(
observation_df: pd.DataFrame, metadata: List[str] = None
) -> PlotModel:
data = observation_df["values"]
stds = observation_df["std"]
x_axis = observation_df["x_axis"]
Expand All @@ -100,6 +102,7 @@ def _get_observation_plots(observation_df: pd.DataFrame) -> PlotModel:
array=stds.values,
visible=True,
),
meta=metadata,
**style,
)
return observation_data
Expand Down
56 changes: 54 additions & 2 deletions webviz_ert/controllers/response_correlation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from webviz_ert.models import (
load_ensemble,
BarChartPlotModel,
EnsembleModel,
PlotModel,
Response,
)
Expand Down Expand Up @@ -236,7 +237,11 @@ def update_response_overview_plot(

if response.observations:
for obs in response.observations:
obs_plots.append(_get_observation_plots(obs.data_df()))
observation_dataframe = obs.data_df()
metadata = len(observation_dataframe) * ["observation"]
obs_plots.append(
_get_observation_plots(observation_dataframe, metadata)
)

fig = go.Figure()
for plot in response_plots:
Expand Down Expand Up @@ -481,7 +486,12 @@ def update_corr_index(
)
elif click_data:
active_response = active_resp_param["response"]
corr_xindex[active_response] = click_data["points"][0]["pointIndex"]
x_index = _get_index_after_click_on_response_plot(
click_data["points"][0], active_response, loaded_ensemble
)
if x_index is None:
corr_xindex[active_response] = DEFAULT_X_INDEX
corr_xindex[active_response] = x_index
return corr_xindex

def _get_default_x_index(response: Response) -> int:
Expand Down Expand Up @@ -741,3 +751,45 @@ def _generate_active_info_piece(title: str, value: str) -> Component:
),
]
)


def _get_index_after_click_on_response_plot(
clicked_point: Dict, response: str, loaded_ensemble: EnsembleModel
) -> Optional[int]:
"""checks whether a click in the response overview plot was right on an
observation, and tries to handle that case, grabbing the wanted x index, or
handles the simple case of not clicking on an observation."""

loaded_response = loaded_ensemble.responses[response]

# we check the metadata to identify an observation point
if (
loaded_response.observations
and "meta" in clicked_point
and clicked_point["meta"] == "observation"
):
# case of clicking on observation point - we cannot use the points
# index, as it refers to the axis of observations, which has
# potentially fewer points than the response axis, and have to reverse
# lookup the clicked value in the response axis instead
clicked_x_value = clicked_point["x"]
response_x_axis = loaded_response.axis
# need to be defensive, for typing reasons
if response_x_axis is None:
return None
if isinstance(clicked_x_value, str):
# if the clicked value is a string representing a datetime, we only
# get a date, as we only display a date in the plot. As the value
# in the axis is a datetime with timestamp, we need to use a hack
# (prefix-match) - we hope that this filter returns a unique
# element
return [
index
for index, x_value in enumerate(response_x_axis)
if x_value.startswith(clicked_x_value)
][0]
# clicked x value is numerical, we can do a simple reverse lookup to
# find the index
return response_x_axis.get_loc(clicked_x_value)
# simple case of not an observation point, index refers to right axis
return clicked_point["pointIndex"]
2 changes: 2 additions & 0 deletions webviz_ert/models/plot_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def __init__(self, **kwargs: Any):
self._marker = kwargs["marker"]
self._error_y = kwargs.get("error_y")
self._hoverlabel = kwargs.get("hoverlabel")
self._meta = kwargs["meta"] if "meta" in kwargs else None
self.selected = True

@property
Expand All @@ -218,6 +219,7 @@ def repr(self) -> go.Scattergl:
error_y=self._error_y,
connectgaps=True,
hoverlabel=self._hoverlabel,
meta=self._meta,
)
if self._line:
repr_dict["line"] = self._line
Expand Down
2 changes: 1 addition & 1 deletion webviz_ert/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def ensemble_id(self) -> str:
return self._ensemble_id

@property
def axis(self) -> Optional[List[Union[int, str, datetime.datetime]]]:
def axis(self) -> pd.Index:
return self.data.index

@property
Expand Down

0 comments on commit f70d429

Please sign in to comment.