Skip to content

Commit b4f5e87

Browse files
committed
Resolve outstanding issues for tests to pass.
Mostly related to realization.get_smry() returning dummy index
1 parent a014abf commit b4f5e87

5 files changed

+91
-36
lines changed

src/fmu/ensemble/observations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def load_smry(self, realization, smryvector, time_index="yearly", smryerror=None
172172
"""
173173
dataseries = realization.get_smry(
174174
column_keys=[smryvector], time_index=time_index
175-
)[smryvector]
175+
)[["DATE", smryvector]].set_index("DATE")[smryvector]
176176

177177
# In the context of this function, datetimes are not supported. Ensure dates:
178178
if isinstance(dataseries.index, pd.DatetimeIndex):

src/fmu/ensemble/virtualensemble.py

-5
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,6 @@ def get_smry(self, column_keys=None, time_index="monthly"):
872872

873873
# Now ask the VirtualRealization to do interpolation
874874
interp = vreal.get_smry(column_keys=column_keys, time_index=time_index)
875-
# Assume we get back a dataframe indexed by the dates from vreal
876-
# We must reset that index, and ensure the index column
877-
# gets a correct name
878-
interp.index = interp.index.set_names(["DATE"])
879-
interp = interp.reset_index()
880875
interp["REAL"] = realidx
881876
smry_interpolated.append(interp)
882877
return pd.concat(smry_interpolated, ignore_index=True, sort=False)

src/fmu/ensemble/virtualrealization.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ def get_smry(self, column_keys=None, time_index="monthly"):
287287
Returns data for those columns that are known, unknown
288288
columns will be issued a warning for.
289289
290+
The returned dataframe will have a dummy index, and the dates in
291+
the column DATE. The DATE column will contain either datetime.datetime
292+
or pandas.Timestamp objects.
293+
290294
BUG: If some columns are available only in certain dataframes,
291295
we might miss them (e.g. we ask for yearly FOPT, and we have
292296
yearly smry with only WOPT data, and FOPT is only in daily
@@ -359,9 +363,10 @@ def get_smry(self, column_keys=None, time_index="monthly"):
359363
)
360364

361365
smry = self.get_df("unsmry--" + chosen_smry)[["DATE"] + column_keys]
366+
# index is dummy, the date is in the DATE column
367+
smry.set_index("DATE", inplace=True)
362368

363369
# Add the extra datetimes to interpolate at.
364-
smry.set_index("DATE", inplace=True)
365370
smry.index = pd.to_datetime(smry.index)
366371
smry = smry.append(
367372
pd.DataFrame(index=pd.to_datetime(time_index_dt)), sort=False
@@ -390,8 +395,9 @@ def get_smry(self, column_keys=None, time_index="monthly"):
390395
smry[noncum_columns].fillna(method="bfill").fillna(value=0)
391396
)
392397

393-
smry.index = smry.index.set_names(["DATE"])
394-
return smry.loc[pd.to_datetime(time_index_dt)]
398+
smry = smry.loc[pd.to_datetime(time_index_dt)]
399+
smry.index.name = "DATE"
400+
return smry.reset_index()
395401

396402
def get_smry_dates(self, freq="monthly", normalize=False):
397403
"""Return list of datetimes available in the realization

tests/test_observations.py

+78-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""Testing observations in fmu-ensemble."""
32

43
import os
@@ -66,24 +65,26 @@ def test_real_mismatch():
6665
)
6766
realmis = obs.mismatch(real)
6867

69-
# Check layout of returned data
70-
assert isinstance(realmis, pd.DataFrame)
71-
assert len(realmis) == 1
68+
pd.testing.assert_frame_equal(
69+
realmis,
70+
pd.DataFrame(
71+
[
72+
{
73+
"OBSTYPE": "txt",
74+
"OBSKEY": "parameters.txt/FWL",
75+
"MISMATCH": -2.0,
76+
"L1": 2.0,
77+
"L2": 4.0,
78+
"SIMVALUE": 1700,
79+
"OBSVALUE": 1702,
80+
"MEASERROR": 1,
81+
"SIGN": -1,
82+
}
83+
]
84+
),
85+
)
7286
assert "REAL" not in realmis.columns # should only be there for ensembles.
73-
assert "OBSTYPE" in realmis.columns
74-
assert "OBSKEY" in realmis.columns
7587
assert "DATE" not in realmis.columns # date is not relevant
76-
assert "MISMATCH" in realmis.columns
77-
assert "L1" in realmis.columns
78-
assert "L2" in realmis.columns
79-
80-
# Check actually computed values, there should only be one row with data:
81-
assert realmis.loc[0, "OBSTYPE"] == "txt"
82-
assert realmis.loc[0, "OBSKEY"] == "parameters.txt/FWL"
83-
assert realmis.loc[0, "MISMATCH"] == -2
84-
assert realmis.loc[0, "SIGN"] == -1
85-
assert realmis.loc[0, "L1"] == 2
86-
assert realmis.loc[0, "L2"] == 4
8788

8889
# Another observation set:
8990
obs2 = Observations(
@@ -96,12 +97,46 @@ def test_real_mismatch():
9697
}
9798
)
9899
realmis2 = obs2.mismatch(real)
99-
assert len(realmis2) == 3
100-
assert "parameters.txt/RMS_SEED" in realmis2["OBSKEY"].values
101-
assert "outputs.txt/top_structure" in realmis2["OBSKEY"].values
102-
assert "npv.txt" in realmis2["OBSKEY"].values
103-
104-
# assert much more!
100+
pd.testing.assert_frame_equal(
101+
realmis2,
102+
pd.DataFrame(
103+
[
104+
{
105+
"OBSTYPE": "txt",
106+
"OBSKEY": "parameters.txt/RMS_SEED",
107+
"MISMATCH": -177148215.0,
108+
"L1": 177148215.0,
109+
"L2": 3.1381490077686224e16,
110+
"SIMVALUE": 422851785,
111+
"OBSVALUE": 600000000,
112+
"MEASERROR": 1,
113+
"SIGN": -1,
114+
},
115+
{
116+
"OBSTYPE": "txt",
117+
"OBSKEY": "outputs.txt/top_structure",
118+
"MISMATCH": 24.0,
119+
"L1": 24.0,
120+
"L2": 576.0,
121+
"SIMVALUE": 3224,
122+
"OBSVALUE": 3200,
123+
"MEASERROR": 1,
124+
"SIGN": 1,
125+
},
126+
{
127+
"OBSTYPE": "scalar",
128+
"OBSKEY": "npv.txt",
129+
"MISMATCH": 44.0,
130+
"L1": 44.0,
131+
"L2": 1936.0,
132+
"SIMVALUE": 3444,
133+
"OBSVALUE": 3400,
134+
"MEASERROR": 1,
135+
"SIGN": 1,
136+
},
137+
]
138+
),
139+
)
105140

106141
# Test that we can write the observations to yaml
107142
# and verify that the exported yaml can be reimported
@@ -215,6 +250,26 @@ def test_smry():
215250
# loaded realization.
216251
mismatch = obs.mismatch(real)
217252

253+
# Assert the first row exactly:
254+
pd.testing.assert_frame_equal(
255+
mismatch.head(1),
256+
pd.DataFrame(
257+
[
258+
{
259+
"OBSTYPE": "smry",
260+
"OBSKEY": "WBP4:OP_1",
261+
"DATE": datetime.date(2001, 1, 1),
262+
"MEASERROR": 4.0,
263+
"MISMATCH": -2.159454345703125,
264+
"OBSVALUE": 251.0,
265+
"SIMVALUE": 248.84054565429688,
266+
"L1": 2.159454345703125,
267+
"L2": 4.663243071176112,
268+
"SIGN": -1,
269+
}
270+
]
271+
),
272+
)
218273
assert len(mismatch) == 21 # later: implement counting in the obs object
219274
assert mismatch.L1.sum() > 0
220275
assert mismatch.L2.sum() > 0
@@ -537,7 +592,6 @@ def test_ensset_mismatch():
537592
== mismatch[mismatch.ENSEMBLE == "iter-1"].L1.sum()
538593
)
539594

540-
# This is quite hard to input in dict-format. Better via YAML..
541595
obs_pr = Observations(
542596
{
543597
"smry": [

tests/test_virtualrealization.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ def test_get_smry():
155155
assert all(vfopt == fopt)
156156
# But note that the dtype of the index in each dataframe differs
157157
# vfopt.index.dtype == datetime, while fopt.index.dtype == object
158-
assert len(fopt.columns) == 1 # DATE is index (unlabeled)
158+
assert len(fopt.columns) == 2 # DATE is the first column
159159

160160
dvfopt = vreal.get_smry(column_keys="FOPT", time_index="daily")
161-
assert all(dvfopt.diff() >= 0)
161+
assert all(dvfopt["FOPT"].diff().dropna() >= 0)
162162
# Linear interpolation should give many unique values:
163163
assert len(dvfopt["FOPT"].unique()) == 1462
164164
# Length is here 1462 while daily smry for the scratchrealization
@@ -256,7 +256,7 @@ def test_get_smry2():
256256

257257
alldefaults = vreal.get_smry()
258258
assert len(alldefaults) == monthly_length
259-
assert len(alldefaults.columns) == 49
259+
assert len(alldefaults.columns) == 50
260260

261261

262262
def test_get_smry_cumulative():

0 commit comments

Comments
 (0)