Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrow implementation and improvements to TimeseriesOneByOne #1156

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"RelativePermeability = webviz_subsurface.plugins:RelativePermeability",
"ReservoirSimulationTimeSeries = webviz_subsurface.plugins:ReservoirSimulationTimeSeries",
"ReservoirSimulationTimeSeriesOneByOne = webviz_subsurface.plugins:ReservoirSimulationTimeSeriesOneByOne",
"SimulationTimeSeriesOneByOne = webviz_subsurface.plugins:SimulationTimeSeriesOneByOne",
"ReservoirSimulationTimeSeriesRegional = webviz_subsurface.plugins:ReservoirSimulationTimeSeriesRegional",
"RftPlotter = webviz_subsurface.plugins:RftPlotter",
"RunningTimeAnalysisFMU = webviz_subsurface.plugins:RunningTimeAnalysisFMU",
Expand Down
2 changes: 2 additions & 0 deletions webviz_subsurface/_components/tornado/_tornado_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
lambda x: str(len(x))
)
self._table["Response"] = tornado_data.response_name
self._table["Reference"] = tornado_data.reference_average
self._table.rename(
columns={
"sensname": "Sensitivity",
Expand Down Expand Up @@ -64,6 +65,7 @@ def columns(self) -> List[Dict]:
"True high",
"Low #reals",
"High #reals",
"Reference",
]
]

Expand Down
127 changes: 115 additions & 12 deletions webviz_subsurface/_figures/timeseries_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import numpy as np
import pandas as pd

from webviz_subsurface._utils.colors import find_intermediate_color, rgba_to_str
from webviz_subsurface._utils.colors import (
find_intermediate_color,
rgba_to_str,
rgb_to_str,
scale_rgb_lightness,
hex_to_rgb,
)
from webviz_subsurface._utils.simulation_timeseries import (
get_simulation_line_shape,
set_simulation_line_shape_fallback,
Expand All @@ -31,20 +37,31 @@ def __init__(
line_shape_fallback: str,
historical_vector_df: Optional[pd.DataFrame] = None,
dateline: Optional[datetime.datetime] = None,
):
discrete_color_map: dict = None,
groupby: str = "REAL",
) -> None:
self.dframe = dframe
self.color_col = color_col
self.groupby = groupby
if color_col is not None and discrete_color_map is None:
self.dframe = self.normalize_parameter_value(self.dframe)
self.dframe = self.dframe.rename(columns={color_col: "VALUE"})
self.vector = vector
self.ensemble = ensemble
self.color_col = color_col
self.visualization = visualization
self.historical_vector_df = historical_vector_df
self.date = dateline
self.line_shape = self.get_line_shape(line_shape_fallback)
self.continous_color = self.color_col is not None and discrete_color_map is None
self.colormap = discrete_color_map if discrete_color_map is not None else {}

self.create_traces()

@property
def figure(self) -> dict:
title = self.vector
if self.color_col is not None:
title += f" colored by {self.color_col}"
return {
"data": self.traces,
"layout": {
Expand All @@ -56,7 +73,7 @@ def figure(self) -> dict:
"plot_bgcolor": "white",
"showlegend": False,
"uirevision": self.vector,
"title": {"text": f"{self.vector} colored by {self.color_col}"},
"title": {"text": title},
"shapes": self.shapes,
"annotations": self.annotations,
},
Expand All @@ -65,11 +82,17 @@ def figure(self) -> dict:
def create_traces(self) -> None:
self.traces: List[dict] = []

if self.visualization != "statistics":
self._add_realization_traces()
if self.groupby == "SENSNAME_CASE":
if self.visualization == "realizations":
self._add_sensitivity_traces_real()
else:
self._add_sensitivity_traces_stat()

if self.visualization != "realizations":
self._add_statistic_traces()
else:
if self.visualization == "realizations":
self._add_realization_traces()
if self.visualization == "statistics":
self._add_statistic_traces()

self._add_history_trace()

Expand Down Expand Up @@ -116,7 +139,8 @@ def _add_statistic_traces(self) -> None:

def _add_realization_traces(self) -> None:
"""Renders line trace for each realization"""
mean = self.dframe["VALUE_NORM"].mean()

mean = self.dframe["VALUE_NORM"].mean() if self.continous_color else None
self.traces.extend(
[
{
Expand All @@ -125,21 +149,93 @@ def _add_realization_traces(self) -> None:
"color": self.set_real_color(
real_df["VALUE_NORM"].iloc[0], mean
)
if self.visualization == "realizations"
else "gainsboro",
if self.visualization == "realizations" and self.continous_color
else self.colormap.get(real_df[self.color_col].iloc[0], "grey"),
},
"mode": "lines",
"x": real_df["DATE"],
"y": real_df[self.vector],
"name": self.ensemble,
"legendgroup": self.ensemble,
"hovertext": self.create_hovertext(real_df["VALUE"].iloc[0], real),
"hovertext": self.create_hovertext(real_df["VALUE"].iloc[0], real)
if self.continous_color
else f"Real: {real} {real_df[self.color_col].iloc[0]}",
"showlegend": real_idx == 0,
}
for real_idx, (real, real_df) in enumerate(self.dframe.groupby("REAL"))
]
)

def _add_sensitivity_traces_stat(self) -> None:
"""Renders line trace for each realization"""

self.dframe["dash"] = np.where(
self.dframe["t"] == 1, "dashdot", "solid"
) # dot, dashdot

self.traces.extend(
[
{
"line": {
"dash": "dash" if real_df["t"].iloc[0] == 1 else "solid",
"shape": self.line_shape,
"color": self.colormap.get(
real_df[self.color_col].iloc[0], "grey"
),
"width": 3 if real_df["t"].iloc[0] == 1 else 2,
},
"mode": "lines",
"x": real_df["DATE"],
"y": real_df[self.vector],
"name": sens,
"legendgroup": sens,
"hovertext": f"Sens: {sens}",
}
for real_idx, (sens, real_df) in enumerate(
self.dframe.groupby("SENSNAME_CASE")
)
]
)

def _add_sensitivity_traces_real(self) -> None:
"""Renders line trace for each realization"""

self.dframe["dash"] = np.where(
self.dframe["t"] == 1, "longdash", "solid"
) # dot, dashdot

self.traces.extend(
[
{
"line": {
"dash": "dash" if real_df["t"].iloc[0] == 1 else "solid",
"shape": self.line_shape,
"color": rgb_to_str(
scale_rgb_lightness(
hex_to_rgb(
self.colormap.get(
real_df[self.color_col].iloc[0], "grey"
)
),
130 if real_df["t"].iloc[0] == 1 else 90,
)
),
"width": 3 if real_df["t"].iloc[0] == 1 else 2,
},
"mode": "lines",
"x": real_df["DATE"],
"y": real_df[self.vector],
"name": sens,
"legendgroup": sens,
"hovertext": f"Real: {real}, Sens: {sens}",
"showlegend": real_idx == 0,
}
for real_idx, ((sens, real), real_df) in enumerate(
self.dframe.groupby(["SENSNAME_CASE", "REAL"])
)
]
)

@property
def daterange(self) -> list:
active_dates = self.dframe["DATE"][self.dframe[self.vector] != 0]
Expand Down Expand Up @@ -226,3 +322,10 @@ def set_real_color(norm_value: float, mean_param_value: float) -> str:
return find_intermediate_color(Colors.MID, Colors.GREEN, intermed)

return Colors.MID

def normalize_parameter_value(self, df: pd.DataFrame) -> pd.DataFrame:

df["VALUE_NORM"] = (df[self.color_col] - df[self.color_col].min()) / (
df[self.color_col].max() - df[self.color_col].min()
)
return df
7 changes: 4 additions & 3 deletions webviz_subsurface/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
from ._pvt_plot import PvtPlot
from ._relative_permeability import RelativePermeability
from ._reservoir_simulation_timeseries import ReservoirSimulationTimeSeries
from ._reservoir_simulation_timeseries_onebyone import (
ReservoirSimulationTimeSeriesOneByOne,
)
from ._reservoir_simulation_timeseries_regional import (
ReservoirSimulationTimeSeriesRegional,
)
Expand All @@ -54,6 +51,10 @@
from ._segy_viewer import SegyViewer
from ._seismic_misfit import SeismicMisfit
from ._simulation_time_series import SimulationTimeSeries
from ._simulation_timeseries_onebyone import SimulationTimeSeriesOneByOne
from ._reservoir_simulation_timeseries_onebyone import (
ReservoirSimulationTimeSeriesOneByOne,
)
from ._structural_uncertainty import StructuralUncertainty
from ._subsurface_map import SubsurfaceMap
from ._surface_viewer_fmu import SurfaceViewerFMU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ def _update_graphs(
scatter_fig.update_color(color, options["opacity"])

# Make timeseries graph
df_value_norm = parametermodel.get_real_and_value_df(
ensemble, parameter=parameter, normalize=True
param_df = parametermodel.get_parameter_df_for_ensemble(
ensemble, reals=realizations
)

timeseries_fig = TimeSeriesFigure(
dframe=merge_dataframes_on_realization(
vector_df[["DATE", "REAL", vector]], df_value_norm
vector_df[["DATE", "REAL", vector]], param_df[["REAL", parameter]]
),
visualization=visualization,
vector=vector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,6 @@ def get_stat_value(self, parameter: str, ensemble: str, stat_column: str):
& (self.statframe["ENSEMBLE"] == ensemble)
].iloc[0][stat_column]

def get_real_and_value_df(
self, ensemble: str, parameter: str, normalize: bool = False
) -> pd.DataFrame:
"""
Return dataframe with ralization and values for selected parameter for an ensemble.
A column with normalized parameter values can be added.
"""
df = self.dataframe_melted.copy()
df = df[["VALUE", "REAL"]].loc[
(df["ENSEMBLE"] == ensemble) & (df["PARAMETER"] == parameter)
]
if normalize:
df["VALUE_NORM"] = (df["VALUE"] - df["VALUE"].min()) / (
df["VALUE"].max() - df["VALUE"].min()
)
return df.reset_index(drop=True)

def get_parameter_df_for_ensemble(self, ensemble: str, reals: list):
return self._dataframe[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._plugin import SimulationTimeSeriesOneByOne
Loading