-
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.
Add workaround for problems with microseconds
Workaround for storage not handling datetimes with microseconds due to index overflow in netcdf3. #6952
- Loading branch information
1 parent
cdd505b
commit 7cdce3d
Showing
7 changed files
with
264 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
""" | ||
Tests behavior of matching response times to observation times | ||
""" | ||
from contextlib import redirect_stderr | ||
from datetime import datetime | ||
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_time = datetime(2024, 1, 1) | ||
epsilon = 0.1 | ||
|
||
|
||
@settings(max_examples=3) | ||
@given( | ||
responses=st.lists( | ||
summaries( | ||
start_date=st.just(start), | ||
time_deltas=st.just( | ||
[((observation_time - start).total_seconds() + epsilon) / 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), | ||
) | ||
def test_small_time_mismatches_are_ignored( | ||
responses, observation, tmp_path_factory, std_cutoff, enkf_alpha | ||
): | ||
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} | ||
""" | ||
) | ||
) | ||
(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)) | ||
|
||
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() | ||
|
||
|
||
@settings(max_examples=3) | ||
@given( | ||
responses=st.lists( | ||
summaries( | ||
start_date=st.just(start), | ||
time_deltas=st.just( | ||
[((observation_time - start).total_seconds() + 360000) / 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), | ||
) | ||
def test_big_time_mismatches_results_in_failure( | ||
responses, observation, tmp_path_factory, std_cutoff, enkf_alpha | ||
): | ||
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} | ||
""" | ||
) | ||
) | ||
(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)) | ||
|
||
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.