Skip to content

Commit e0c0f66

Browse files
committed
Iterate ensembles to look for history to plot
1 parent 4d16ba1 commit e0c0f66

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/ert/gui/tools/plot/plot_api.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -209,25 +209,28 @@ def observations_for_key(self, ensemble_name, key) -> pd.DataFrame:
209209
}
210210
return pd.DataFrame(data_struct).T
211211

212-
def history_data(self, key, ensemble=None) -> pd.DataFrame:
212+
def history_data(self, key, ensembles: Optional[List[str]]) -> pd.DataFrame:
213213
"""Returns a pandas DataFrame with the data points for the history for a
214214
given data key, if any. The row index is the index/date and the column
215215
index is the key."""
216-
217-
if ":" in key:
218-
head, tail = key.split(":", 2)
219-
history_key = f"{head}H:{tail}"
220-
else:
221-
history_key = f"{key}H"
222-
223-
df = self.data_for_key(ensemble, history_key)
224-
225-
if not df.empty:
226-
df = df.T
227-
# Drop columns with equal data
228-
duplicate_cols = [
229-
cc[0] for cc in combi(df.columns, r=2) if (df[cc[0]] == df[cc[1]]).all()
230-
]
231-
return df.drop(columns=duplicate_cols)
216+
if ensembles:
217+
for ensemble in ensembles:
218+
if ":" in key:
219+
head, tail = key.split(":", 2)
220+
history_key = f"{head}H:{tail}"
221+
else:
222+
history_key = f"{key}H"
223+
224+
df = self.data_for_key(ensemble, history_key)
225+
226+
if not df.empty:
227+
df = df.T
228+
# Drop columns with equal data
229+
duplicate_cols = [
230+
cc[0]
231+
for cc in combi(df.columns, r=2)
232+
if (df[cc[0]] == df[cc[1]]).all()
233+
]
234+
return df.drop(columns=duplicate_cols)
232235

233236
return pd.DataFrame()

src/ert/gui/tools/plot/plot_window.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,17 @@ def currentPlotChanged(self):
208208
plot_config = PlotConfig.createCopy(self._plot_customizer.getPlotConfig())
209209
plot_context = PlotContext(plot_config, ensembles, key)
210210

211-
ensemble = plot_context.ensembles()[0] if plot_context.ensembles() else None
212-
213211
# Check if key is a history key.
214212
# If it is it already has the data it needs
215213
if str(key).endswith("H") or "H:" in str(key):
216214
plot_context.history_data = DataFrame()
217215
else:
218216
try:
219-
plot_context.history_data = self._api.history_data(key, ensemble)
217+
plot_context.history_data = self._api.history_data(
218+
key,
219+
plot_context.ensembles(),
220+
)
221+
220222
except (RequestError, TimeoutError) as e:
221223
logger.exception(e)
222224
msg = f"{e}"

tests/unit_tests/gui/tools/plot/test_plot_api.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,24 @@ def test_all_data_type_keys(api):
101101

102102

103103
def test_load_history_data(api):
104-
df = api.history_data(ensemble="default_0", key="FOPR")
104+
df = api.history_data(ensembles=["default_0"], key="FOPR")
105105
assert_frame_equal(
106106
df, pd.DataFrame({1: [0.2, 0.2, 1.2], 3: [1.0, 1.1, 1.2], 4: [1.0, 1.1, 1.3]})
107107
)
108108

109109

110+
def test_load_history_data_searches_until_history_found(api):
111+
df = api.history_data(ensembles=["no-history", "default_0"], key="FOPR")
112+
assert_frame_equal(
113+
df, pd.DataFrame({1: [0.2, 0.2, 1.2], 3: [1.0, 1.1, 1.2], 4: [1.0, 1.1, 1.3]})
114+
)
115+
116+
117+
def test_load_history_data_returns_empty_frame_if_no_history(api):
118+
df = api.history_data(ensembles=["no-history", "still-no-history"], key="FOPR")
119+
assert_frame_equal(df, pd.DataFrame())
120+
121+
110122
def test_plot_api_request_errors_all_data_type_keys(api, mocker):
111123
# Mock the experiment name to be something unexpected
112124
mocker.patch(

0 commit comments

Comments
 (0)