|
| 1 | +from pathlib import Path |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from webviz_config import WebvizPluginABC, WebvizSettings |
| 6 | +from webviz_config.utils import StrEnum |
| 7 | + |
| 8 | +from webviz_subsurface._models.parameter_model import ParametersModel |
| 9 | +from webviz_subsurface._providers import ( |
| 10 | + EnsembleTableProvider, |
| 11 | + EnsembleTableProviderFactory, |
| 12 | + Frequency, |
| 13 | +) |
| 14 | +from webviz_subsurface._utils.ensemble_summary_provider_set_factory import ( |
| 15 | + create_lazy_ensemble_summary_provider_set_from_paths, |
| 16 | + create_presampled_ensemble_summary_provider_set_from_paths, |
| 17 | +) |
| 18 | + |
| 19 | +from ._utils import SimulationTimeSeriesOneByOneDataModel |
| 20 | +from ._views._onebyone_view import OneByOneView |
| 21 | + |
| 22 | + |
| 23 | +class SimulationTimeSeriesOneByOne(WebvizPluginABC): |
| 24 | + """Visualizes reservoir simulation time series data for sensitivity studies based \ |
| 25 | +on a design matrix. |
| 26 | +A tornado plot can be calculated interactively for each date/vector by selecting a date. |
| 27 | +After selecting a date individual sensitivities can be selected to highlight the realizations |
| 28 | +run with that sensitivity. |
| 29 | +--- |
| 30 | +**Input arguments** |
| 31 | +* **`ensembles`:** Which ensembles in `shared_settings` to visualize. |
| 32 | +* **`rel_file_pattern`:** Path to `.arrow` files with summary data. |
| 33 | +* **`sampling`:** Time separation between extracted values. Can be e.g. `monthly` (default) or \ |
| 34 | + `yearly`. |
| 35 | +* **`perform_presampling`:** Presample summary data instead of lazy sampling. |
| 36 | +* **`initial_vector`:** Initial vector to display |
| 37 | +* **`line_shape_fallback`:** Fallback interpolation method between points. Vectors identified as \ |
| 38 | + rates or phase ratios are always backfilled, vectors identified as cumulative (totals) are \ |
| 39 | + always linearly interpolated. The rest use the fallback. |
| 40 | + Supported options: |
| 41 | + * `linear` (default) |
| 42 | + * `backfilled` |
| 43 | + * `hv`, `vh`, `hvh`, `vhv` and `spline` (regular Plotly options). |
| 44 | +""" |
| 45 | + |
| 46 | + class Ids(StrEnum): |
| 47 | + ONEBYONE_VIEW = "onebyone-view" |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + webviz_settings: WebvizSettings, |
| 52 | + ensembles: list, |
| 53 | + rel_file_pattern: str = "share/results/unsmry/*.arrow", |
| 54 | + sampling: str = Frequency.MONTHLY.value, |
| 55 | + perform_presampling: bool = False, |
| 56 | + initial_vector: str = None, |
| 57 | + line_shape_fallback: str = "linear", |
| 58 | + ) -> None: |
| 59 | + super().__init__() |
| 60 | + |
| 61 | + # vectormodel: ProviderTimeSeriesDataModel |
| 62 | + table_provider = EnsembleTableProviderFactory.instance() |
| 63 | + resampling_frequency = Frequency(sampling) |
| 64 | + |
| 65 | + if ensembles is not None: |
| 66 | + ensemble_paths: Dict[str, Path] = { |
| 67 | + ensemble_name: webviz_settings.shared_settings["scratch_ensembles"][ |
| 68 | + ensemble_name |
| 69 | + ] |
| 70 | + for ensemble_name in ensembles |
| 71 | + } |
| 72 | + if perform_presampling: |
| 73 | + self._presampled_frequency = resampling_frequency |
| 74 | + summary_provider_set = ( |
| 75 | + create_presampled_ensemble_summary_provider_set_from_paths( |
| 76 | + ensemble_paths, rel_file_pattern, self._presampled_frequency |
| 77 | + ) |
| 78 | + ) |
| 79 | + else: |
| 80 | + summary_provider_set = ( |
| 81 | + create_lazy_ensemble_summary_provider_set_from_paths( |
| 82 | + ensemble_paths, rel_file_pattern |
| 83 | + ) |
| 84 | + ) |
| 85 | + else: |
| 86 | + raise ValueError('Incorrect argument, must provide "ensembles"') |
| 87 | + |
| 88 | + if not summary_provider_set: |
| 89 | + raise ValueError( |
| 90 | + "Initial provider set is undefined, and ensemble summary providers" |
| 91 | + " are not instantiated for plugin" |
| 92 | + ) |
| 93 | + |
| 94 | + parameterproviderset = { |
| 95 | + ens_name: table_provider.create_from_per_realization_parameter_file( |
| 96 | + str(ens_path) |
| 97 | + ) |
| 98 | + for ens_name, ens_path in ensemble_paths.items() |
| 99 | + } |
| 100 | + parameter_df = create_df_from_table_provider(parameterproviderset) |
| 101 | + parametermodel = ParametersModel(dataframe=parameter_df, drop_constants=True) |
| 102 | + |
| 103 | + self.add_view( |
| 104 | + OneByOneView( |
| 105 | + data_model=SimulationTimeSeriesOneByOneDataModel( |
| 106 | + provider_set=summary_provider_set, |
| 107 | + parametermodel=parametermodel, |
| 108 | + webviz_settings=webviz_settings, |
| 109 | + resampling_frequency=resampling_frequency, |
| 110 | + initial_vector=initial_vector, |
| 111 | + line_shape_fallback=line_shape_fallback, |
| 112 | + ), |
| 113 | + ), |
| 114 | + self.Ids.ONEBYONE_VIEW, |
| 115 | + ) |
| 116 | + |
| 117 | + # @property |
| 118 | + # def tour_steps(self) -> List[dict]: |
| 119 | + # return [ |
| 120 | + # { |
| 121 | + # "id": self.uuid("layout"), |
| 122 | + # "content": ( |
| 123 | + # "Dashboard displaying time series from a sensitivity study." |
| 124 | + # ), |
| 125 | + # }, |
| 126 | + # { |
| 127 | + # "id": self.uuid("graph-wrapper"), |
| 128 | + # "content": ( |
| 129 | + # "Selected time series displayed per realization. " |
| 130 | + # "Click in the plot to calculate tornadoplot for the " |
| 131 | + # "corresponding date, then click on the tornado plot to " |
| 132 | + # "highlight the corresponding sensitivity." |
| 133 | + # ), |
| 134 | + # }, |
| 135 | + # { |
| 136 | + # "id": self.uuid("table"), |
| 137 | + # "content": ( |
| 138 | + # "Table statistics for all sensitivities for the selected date." |
| 139 | + # ), |
| 140 | + # }, |
| 141 | + # {"id": self.uuid("vector"), "content": "Select time series"}, |
| 142 | + # {"id": self.uuid("ensemble"), "content": "Select ensemble"}, |
| 143 | + # ] |
| 144 | + |
| 145 | + |
| 146 | +def create_df_from_table_provider( |
| 147 | + providerset: Dict[str, EnsembleTableProvider] |
| 148 | +) -> pd.DataFrame: |
| 149 | + dfs = [] |
| 150 | + for ens, provider in providerset.items(): |
| 151 | + df = provider.get_column_data(column_names=provider.column_names()) |
| 152 | + df["ENSEMBLE"] = ens |
| 153 | + dfs.append(df) |
| 154 | + return pd.concat(dfs) |
0 commit comments