-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match observations with 1 second tolerance
Since summary files have precision loss of time, we need to include some tolerance when matching responses to observations. Also contains a workaround for storage not handling datetimes with microseconds due to index overflow in netcdf3. #6952
- Loading branch information
1 parent
cdd505b
commit 68c969d
Showing
8 changed files
with
230 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
""" | ||
Tests behavior of matching response times to observation times | ||
""" | ||
from contextlib import redirect_stderr | ||
from datetime import date, datetime, timedelta | ||
from io import StringIO | ||
from textwrap import dedent | ||
|
||
import hypothesis.strategies as st | ||
import numpy as np | ||
import pytest | ||
from hypothesis import assume, given, settings | ||
|
||
from ert.cli import ES_MDA_MODE | ||
from ert.cli.main import ErtCliError | ||
from tests.unit_tests.config.observations_generator import summary_observations | ||
from tests.unit_tests.config.summary_generator import summaries | ||
|
||
from .run_cli import run_cli | ||
|
||
start = datetime(1969, 1, 1) | ||
observation_times = st.dates( | ||
min_value=date.fromordinal((start + timedelta(hours=1)).toordinal()) | ||
).map(lambda x: datetime.fromordinal(x.toordinal())) | ||
epsilon = 0.1 | ||
|
||
|
||
@settings(max_examples=3) | ||
@given( | ||
responses_observation=observation_times.flatmap( | ||
lambda observation_time: st.fixed_dictionaries( | ||
{ | ||
"responses": st.lists( | ||
summaries( | ||
start_date=st.just(start), | ||
time_deltas=st.just( | ||
[((observation_time - start).total_seconds()) / 3600] | ||
), | ||
summary_keys=st.just(["FOPR"]), | ||
use_days=st.just(False), | ||
), | ||
min_size=2, | ||
max_size=2, | ||
), | ||
"observation": summary_observations( | ||
summary_keys=st.just("FOPR"), | ||
std_cutoff=10.0, | ||
names=st.just("FOPR_OBSERVATION"), | ||
dates=st.just(observation_time), | ||
time_types=st.just("date"), | ||
), | ||
} | ||
) | ||
), | ||
std_cutoff=st.floats(min_value=0.0, max_value=1.0), | ||
enkf_alpha=st.floats(min_value=3.0, max_value=10.0), | ||
epsilon=st.sampled_from([0.0, 1.1, 2.0, -2.0]), | ||
) | ||
def test_small_time_mismatches_are_ignored( | ||
responses_observation, tmp_path_factory, std_cutoff, enkf_alpha, epsilon | ||
): | ||
responses = responses_observation["responses"] | ||
observation = responses_observation["observation"] | ||
tmp_path = tmp_path_factory.mktemp("summary") | ||
(tmp_path / "config.ert").write_text( | ||
dedent( | ||
f""" | ||
NUM_REALIZATIONS 2 | ||
ECLBASE CASE | ||
SUMMARY FOPR | ||
MAX_SUBMIT 1 | ||
GEN_KW KW_NAME prior.txt | ||
OBS_CONFIG observations.txt | ||
STD_CUTOFF {std_cutoff} | ||
ENKF_ALPHA {enkf_alpha} | ||
""" | ||
) | ||
) | ||
|
||
# Add some inprecision to the reported time | ||
for r in responses: | ||
r[1].steps[-1].ministeps[-1].params[0] += epsilon | ||
|
||
(tmp_path / "prior.txt").write_text("KW_NAME NORMAL 0 1") | ||
response_values = np.array( | ||
[r[1].steps[-1].ministeps[-1].params[-1] for r in responses] | ||
) | ||
std_dev = response_values.std(ddof=0) | ||
assume(np.isfinite(std_dev)) | ||
assume(std_dev > std_cutoff) | ||
observation.value = float(response_values.mean()) | ||
for i in range(2): | ||
for j in range(4): | ||
summary = responses[i] | ||
smspec, unsmry = summary | ||
(tmp_path / f"simulations/realization-{i}/iter-{j}").mkdir(parents=True) | ||
smspec.to_file( | ||
tmp_path / f"simulations/realization-{i}/iter-{j}/CASE.SMSPEC" | ||
) | ||
unsmry.to_file( | ||
tmp_path / f"simulations/realization-{i}/iter-{j}/CASE.UNSMRY" | ||
) | ||
(tmp_path / "observations.txt").write_text(str(observation)) | ||
|
||
if epsilon < 1 / 3600: # less than one second | ||
stderr = StringIO() | ||
with redirect_stderr(stderr): | ||
run_cli( | ||
ES_MDA_MODE, | ||
str(tmp_path / "config.ert"), | ||
"--weights=0,1", | ||
) | ||
assert "Experiment completed" in stderr.getvalue() | ||
else: | ||
with pytest.raises(ErtCliError, match="No active observations"): | ||
run_cli( | ||
ES_MDA_MODE, | ||
str(tmp_path / "config.ert"), | ||
"--weights=0,1", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.