Skip to content

Commit

Permalink
Fix for avoiding serialization error when having datetimes larger tha…
Browse files Browse the repository at this point in the history
…n 2263
  • Loading branch information
frode-aarstad committed Jan 18, 2024
1 parent f346b4e commit 953b0f1
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/ert/analysis/_es_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,15 @@ def _get_obs_and_measure_data(
for obs_key, obs_active_list in selected_observations:
observation = observations[obs_key]
group = observation.attrs["response"]

if obs_active_list:
index = observation.coords.to_index()[obs_active_list]
sub_selection = {
name: list(set(index.get_level_values(name))) for name in index.names
}
observation = observation.sel(sub_selection)
ds = source_fs.load_responses(group, tuple(iens_active_index))

try:
filtered_ds = observation.merge(ds, join="left")
except KeyError as e:
Expand Down
5 changes: 5 additions & 0 deletions src/ert/config/summary_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ def read_from_file(self, run_path: str, iens: int) -> xr.Dataset:
summary_data.sort(key=lambda x: x[0])
data = [d for _, d in summary_data]
keys = [k for k, _ in summary_data]

# Internally we need to use strings to represent time because numpy.datetime64[ns] only allows time until 2262 (for now)
# Convert the time_map from datetime.datetime to string
time_map = [datetime.isoformat(t, timespec="microseconds") for t in time_map]
ds = xr.Dataset(
{"values": (["name", "time"], data)},
coords={"time": time_map, "name": keys},
)

return ds.drop_duplicates("time")
1 change: 1 addition & 0 deletions src/ert/data/_measured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def _get_data(
raise ResponseError(_msg)
except KeyError as e:
raise ResponseError(_msg) from e

ds = obs.merge(
response,
join="left",
Expand Down
6 changes: 5 additions & 1 deletion src/ert/storage/local_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def load_responses(
ds = xr.open_dataset(input_path, engine="scipy")
loaded.append(ds)
response = xr.combine_nested(loaded, concat_dim="realization")

assert isinstance(response, xr.Dataset)
return response

Expand All @@ -381,9 +382,12 @@ def load_all_summary_data(
summary_keys = self.get_summary_keyset()

try:
df = self.load_responses("summary", tuple(realizations)).to_dataframe()
df = self.load_responses("summary", tuple(realizations)).to_dataframe(
["time", "name", "realization"]
)
except (ValueError, KeyError):
return pd.DataFrame()

df = df.unstack(level="name")
df.columns = [col[1] for col in df.columns.values]
df.index = df.index.rename(
Expand Down
19 changes: 14 additions & 5 deletions src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,20 @@ def response_configuration(self) -> Dict[str, ResponseConfig]:

@property
def observations(self) -> Dict[str, xr.Dataset]:
observations = list(self.mount_point.glob("observations/*"))
return {
observation.name: xr.open_dataset(observation, engine="scipy")
for observation in observations
}
observations = {}
for observation in list(self.mount_point.glob("observations/*")):
ds = xr.open_dataset(observation, engine="scipy")
if "time" in ds.coords:
# Internally we need to use strings to represent time because numpy.datetime64[ns] only allows time until 2262 (for now)
# Convert the time for the observation from numpy.datetime64[ns] (nanosecords) to datetime64[us] (microseconds) then to string
ds.coords["time"] = [
t
for t in ds.coords["time"]
.values.astype("datetime64[us]")
.astype(str)
]
observations[observation.name] = ds
return observations


class LocalExperimentAccessor(LocalExperimentReader):
Expand Down

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions tests/unit_tests/data/test_integration_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def test_summary_obs(create_measured_data):
summary_obs.remove_inactive_observations()
assert all(summary_obs.data.columns.get_level_values("data_index").values == [71])
# Only one observation, we check the key_index is what we expect:
assert summary_obs.data.columns.get_level_values("key_index").values[
0
] == np.datetime64("2011-12-21")
assert (
summary_obs.data.columns.get_level_values("key_index").values[0]
== "2011-12-21T00:00:00.000000"
)


@pytest.mark.filterwarnings("ignore::ert.config.ConfigWarning")
Expand Down Expand Up @@ -106,7 +107,8 @@ def test_gen_obs_and_summary(create_measured_data):


def test_gen_obs_and_summary_index_range(create_measured_data):
df = create_measured_data(["WPR_DIFF_1", "FOPR"], [[800], [datetime(2010, 4, 20)]])
t = datetime.isoformat(datetime(2010, 4, 20), timespec="microseconds")
df = create_measured_data(["WPR_DIFF_1", "FOPR"], [[800], [t]])
df.remove_inactive_observations()

assert df.data.columns.get_level_values(0).to_list() == [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Realization,Date,"BPR:1,3,8","BPR:5,5,5",FGIP,FGIPH,FGOR,FGORH,FGPR,FGPRH,FGPT,FGPTH,FOIP,FOIPH,FOPR,FOPRH,FOPT,FOPTH,FWCT,FWCTH,FWIP,FWIPH,FWPR,FWPRH,FWPT,FWPTH,WGOR:OP1,WGOR:OP2,WGORH:OP1,WGORH:OP2,WGPR:OP1,WGPR:OP2,WGPRH:OP1,WGPRH:OP2,WOPR:OP1,WOPR:OP2,WOPRH:OP1,WOPRH:OP2,WWCT:OP1,WWCT:OP2,WWCTH:OP1,WWCTH:OP2,WWPR:OP1,WWPR:OP2,WWPRH:OP1,WWPRH:OP2
0,2010-01-10,0.9996,0.9996,2499.4473,2499.9956,1.0,1.0,0.0557,0.0012,0.5528,0.0044,1999.4462,1999.994,0.056,0.0017,0.5538,0.0059,0.1776,0.0002,2249.4492,2249.9998,0.0551,0.0,0.5507,0.0001,1.0,1.0,1.0,1.0,0.0557,0.0,0.0006,0.0006,0.056,0.0,0.0008,0.0008,0.3552,0.0,0.0001,0.0002,0.0551,0.0,0.0,0.0
1,2010-01-10,0.9996,0.9996,2499.8467,2499.9956,1.0,1.0,0.0157,0.0012,0.1533,0.0044,1999.8458,1999.994,0.016,0.0017,0.1542,0.0059,0.0657,0.0002,2249.8489,2249.9998,0.0151,0.0,0.1512,0.0001,1.0,1.0,1.0,1.0,0.0,0.0157,0.0006,0.0006,0.0,0.016,0.0008,0.0008,0.0,0.1314,0.0001,0.0002,0.0,0.0151,0.0,0.0
2,2010-01-10,0.9996,0.9996,2500.0,2499.9956,1.0,1.0,0.0,0.0012,0.0,0.0044,2000.0,1999.994,0.0,0.0017,0.0,0.0059,0.0,0.0002,2250.0,2249.9998,0.0,0.0,0.0,0.0001,1.0,1.0,1.0,1.0,0.0,0.0,0.0006,0.0006,0.0,0.0,0.0008,0.0008,0.0,0.0,0.0001,0.0002,0.0,0.0,0.0,0.0
3,2010-01-10,0.9996,0.9996,2497.1733,2499.9956,0.9994,1.0,0.2835,0.0012,2.8267,0.0044,1997.1715,1999.994,0.284,0.0017,2.8285,0.0059,0.4825,0.0002,2247.1775,2249.9998,0.2823,0.0,2.8224,0.0001,1.0,0.9987,1.0,1.0,0.0879,0.1956,0.0006,0.0006,0.0882,0.1958,0.0008,0.0008,0.4661,0.4989,0.0001,0.0002,0.0873,0.195,0.0,0.0
0,2010-01-10T00:00:00.000000,0.9996,0.9996,2499.4473,2499.9956,1.0,1.0,0.0557,0.0012,0.5528,0.0044,1999.4462,1999.994,0.056,0.0017,0.5538,0.0059,0.1776,0.0002,2249.4492,2249.9998,0.0551,0.0,0.5507,0.0001,1.0,1.0,1.0,1.0,0.0557,0.0,0.0006,0.0006,0.056,0.0,0.0008,0.0008,0.3552,0.0,0.0001,0.0002,0.0551,0.0,0.0,0.0
1,2010-01-10T00:00:00.000000,0.9996,0.9996,2499.8467,2499.9956,1.0,1.0,0.0157,0.0012,0.1533,0.0044,1999.8458,1999.994,0.016,0.0017,0.1542,0.0059,0.0657,0.0002,2249.8489,2249.9998,0.0151,0.0,0.1512,0.0001,1.0,1.0,1.0,1.0,0.0,0.0157,0.0006,0.0006,0.0,0.016,0.0008,0.0008,0.0,0.1314,0.0001,0.0002,0.0,0.0151,0.0,0.0
2,2010-01-10T00:00:00.000000,0.9996,0.9996,2500.0,2499.9956,1.0,1.0,0.0,0.0012,0.0,0.0044,2000.0,1999.994,0.0,0.0017,0.0,0.0059,0.0,0.0002,2250.0,2249.9998,0.0,0.0,0.0,0.0001,1.0,1.0,1.0,1.0,0.0,0.0,0.0006,0.0006,0.0,0.0,0.0008,0.0008,0.0,0.0,0.0001,0.0002,0.0,0.0,0.0,0.0
3,2010-01-10T00:00:00.000000,0.9996,0.9996,2497.1733,2499.9956,0.9994,1.0,0.2835,0.0012,2.8267,0.0044,1997.1715,1999.994,0.284,0.0017,2.8285,0.0059,0.4825,0.0002,2247.1775,2249.9998,0.2823,0.0,2.8224,0.0001,1.0,0.9987,1.0,1.0,0.0879,0.1956,0.0006,0.0006,0.0882,0.1958,0.0008,0.0008,0.4661,0.4989,0.0001,0.0002,0.0873,0.195,0.0,0.0
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Realization,Date,WWCT:OP1,WWCT:OP2
0,2010-01-10,0.3551691770553589,0.0
1,2010-01-10,0.0,0.13138170540332794
2,2010-01-10,0.0,0.0
3,2010-01-10,0.46612998843193054,0.49889394640922546
0,2010-01-10T00:00:00.000000,0.3551691770553589,0.0
1,2010-01-10T00:00:00.000000,0.0,0.13138170540332794
2,2010-01-10T00:00:00.000000,0.0,0.0
3,2010-01-10T00:00:00.000000,0.46612998843193054,0.49889394640922546
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Realization,Date,WWCT:OP1,WWCT:OP2
4,2010-01-10,0.19479288160800934,0.0
4,2010-01-20,0.19516849517822266,0.0
4,2010-01-30,0.19581304490566254,0.0
4,2010-02-09,0.19672726094722748,0.0
4,2010-01-10T00:00:00.000000,0.19479288160800934,0.0
4,2010-01-20T00:00:00.000000,0.19516849517822266,0.0
4,2010-01-30T00:00:00.000000,0.19581304490566254,0.0
4,2010-02-09T00:00:00.000000,0.19672726094722748,0.0
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
,FOPR,FOPR,FOPR,FOPR,FOPR
Realization,0,1,2,3,4
Date,,,,,
2010-01-10,0.05596115440130234,0.015982799232006073,0.0,0.28399229049682617,0.02509653940796852
2010-01-20,0.05905991047620773,0.018984779715538025,0.0,0.2900899052619934,0.02827547676861286
2010-01-30,0.06433762609958649,0.024110613390803337,0.0,0.30051347613334656,0.03370004892349243
2010-02-09,0.07174286246299744,0.03132806345820427,0.0009306804859079421,0.31520578265190125,0.041331253945827484
2010-02-19,0.08120841532945633,0.040592338889837265,0.009585856460034847,0.3340320289134979,0.051114898175001144
2010-01-10T00:00:00.000000,0.05596115440130234,0.015982799232006073,0.0,0.28399229049682617,0.02509653940796852
2010-01-20T00:00:00.000000,0.05905991047620773,0.018984779715538025,0.0,0.2900899052619934,0.02827547676861286
2010-01-30T00:00:00.000000,0.06433762609958649,0.024110613390803337,0.0,0.30051347613334656,0.03370004892349243
2010-02-09T00:00:00.000000,0.07174286246299744,0.03132806345820427,0.0009306804859079421,0.31520578265190125,0.041331253945827484
2010-02-19T00:00:00.000000,0.08120841532945633,0.040592338889837265,0.009585856460034847,0.3340320289134979,0.051114898175001144
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
,FOPR,FOPR,FOPR,FOPR,FOPR
Realization,0,1,2,3,4
Date,,,,,
2015-05-14,0.05978194996714592,0.02822822891175747,0.0,0.2988854944705963,0.031847789883613586
2015-05-24,0.06022725999355316,0.02732691913843155,0.0,0.29930371046066284,0.03208010271191597
2015-06-03,0.060678575187921524,0.026679888367652893,0.0,0.29959961771965027,0.032258305698633194
2015-06-13,0.06101493909955025,0.026284758001565933,0.0,0.29972729086875916,0.032372280955314636
2015-06-23,0.06115317344665527,0.026137633249163628,0.0,0.2997667193412781,0.03241586685180664
2015-05-14T00:00:00.000000,0.05978194996714592,0.02822822891175747,0.0,0.2988854944705963,0.031847789883613586
2015-05-24T00:00:00.000000,0.06022725999355316,0.02732691913843155,0.0,0.29930371046066284,0.03208010271191597
2015-06-03T00:00:00.000000,0.060678575187921524,0.026679888367652893,0.0,0.29959961771965027,0.032258305698633194
2015-06-13T00:00:00.000000,0.06101493909955025,0.026284758001565933,0.0,0.29972729086875916,0.032372280955314636
2015-06-23T00:00:00.000000,0.06115317344665527,0.026137633249163628,0.0,0.2997667193412781,0.03241586685180664
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Realization,Date,FGIP,FGIR,FGIRH,FGIT,FGITH,FGLIR,FGLR,FGOR,FGORH,FGPP,FGPR,FGPRF,FGPRH,FGPRS,FGPT
0,2020-07-02,5737580544.0,0.0,0.0,0.0,0.0,0.0,68.79610443115234,146.78567504882812,146.78567504882812,2777220.0,853880.5625,165187.046875,853880.5625,688693.5625,1168540544.0
0,2020-08-01,5711617536.0,0.0,0.0,0.0,0.0,0.0,68.93971252441406,151.33901977539062,151.33901977539062,2757425.0,866019.9375,192419.359375,866019.9375,673600.5625,1194503808.0
0,2020-09-01,5684979200.0,0.0,0.0,0.0,0.0,0.0,68.25880432128906,154.72657775878906,154.72657775878906,2712226.0,856132.875,208552.921875,856132.875,647579.9375,1221141888.0
0,2020-10-01,5659591680.0,0.0,0.0,0.0,0.0,0.0,66.95653533935547,156.4752960205078,156.4752960205078,2654457.75,837970.5,214351.671875,837970.5,623618.875,1246529280.0
0,2020-11-01,5634260480.0,0.0,0.0,0.0,0.0,0.0,64.8019790649414,155.9357147216797,155.9357147216797,2580463.0,809597.9375,207660.796875,809597.9375,601937.125,1271860352.0
0,2020-12-01,5610946048.0,0.0,0.0,0.0,0.0,0.0,62.26524353027344,153.91632080078125,153.91632080078125,2505382.5,777148.4375,193741.40625,777148.4375,583407.0625,1295174912.0
0,2020-07-02T00:00:00.000000,5737580544.0,0.0,0.0,0.0,0.0,0.0,68.79610443115234,146.78567504882812,146.78567504882812,2777220.0,853880.5625,165187.046875,853880.5625,688693.5625,1168540544.0
0,2020-08-01T00:00:00.000000,5711617536.0,0.0,0.0,0.0,0.0,0.0,68.93971252441406,151.33901977539062,151.33901977539062,2757425.0,866019.9375,192419.359375,866019.9375,673600.5625,1194503808.0
0,2020-09-01T00:00:00.000000,5684979200.0,0.0,0.0,0.0,0.0,0.0,68.25880432128906,154.72657775878906,154.72657775878906,2712226.0,856132.875,208552.921875,856132.875,647579.9375,1221141888.0
0,2020-10-01T00:00:00.000000,5659591680.0,0.0,0.0,0.0,0.0,0.0,66.95653533935547,156.4752960205078,156.4752960205078,2654457.75,837970.5,214351.671875,837970.5,623618.875,1246529280.0
0,2020-11-01T00:00:00.000000,5634260480.0,0.0,0.0,0.0,0.0,0.0,64.8019790649414,155.9357147216797,155.9357147216797,2580463.0,809597.9375,207660.796875,809597.9375,601937.125,1271860352.0
0,2020-12-01T00:00:00.000000,5610946048.0,0.0,0.0,0.0,0.0,0.0,62.26524353027344,153.91632080078125,153.91632080078125,2505382.5,777148.4375,193741.40625,777148.4375,583407.0625,1295174912.0
37 changes: 37 additions & 0 deletions tests/unit_tests/test_load_forward_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import pytest
import xarray as xr
from resdata.summary import Summary

from ert.config import ErtConfig
Expand Down Expand Up @@ -111,6 +112,42 @@ def test_load_inconsistent_time_map_summary(caplog):
assert loaded == 1


@pytest.mark.usefixtures("copy_snake_oil_case_storage")
def test_datetime_2500():
"""
Test that we are able to work with dates past year 2263 in summary files.
"""
cwd = os.getcwd()

# Get rid of GEN_DATA as we are only interested in SUMMARY
with fileinput.input("snake_oil.ert", inplace=True) as fin:
for line in fin:
if line.startswith("GEN_DATA"):
continue
print(line, end="")

facade = LibresFacade.from_config_file("snake_oil.ert")
storage_path = ErtConfig.from_file("snake_oil.ert").ens_path
storage = open_storage(storage_path, mode="w")
ensemble = storage.get_ensemble_by_name("default_0")

# Create a result that is incompatible with the refcase
run_path = Path("storage") / "snake_oil" / "runpath" / "realization-0" / "iter-0"
os.chdir(run_path)
ecl_sum = run_simulator(100, datetime(2500, 1, 1))
ecl_sum.fwrite()
os.chdir(cwd)

realizations = [False] * facade.get_ensemble_size()
realizations[0] = True
facade.load_from_forward_model(ensemble, realizations, 0)

dataset = ensemble.load_responses("summary", tuple([0]))
assert dataset.coords["time"].data.dtype == np.dtype("object")
assert dataset.coords["time"].values[0] == "2500-01-10T00:00:00.000000"


@pytest.mark.usefixtures("copy_snake_oil_case_storage")
def test_load_forward_model(snake_oil_default_storage):
"""
Expand Down

0 comments on commit 953b0f1

Please sign in to comment.