Skip to content

Commit d1fce12

Browse files
committed
wip
1 parent 44cd2ff commit d1fce12

2 files changed

Lines changed: 58 additions & 32 deletions

File tree

src/ert/observation_converters/summary_to_yaml.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import warnings
21
from pathlib import Path
32
from typing import Literal, TypedDict
43

54
from ruamel.yaml import YAML
65

76
from ert.cli.main import ErtCliError
8-
from ert.config import ErtConfig, Observation
7+
from ert.config import ErtConfig, ObservationType
8+
from ert.config.parsing import ObservationDict, read_file
99

1010

1111
class YamlObservation(TypedDict):
@@ -25,25 +25,28 @@ class SummaryDict(TypedDict):
2525
class YamlConverter:
2626
target_file = "summary_observations.yaml"
2727

28-
def __init__(self, observations: list[Observation]) -> None:
28+
def __init__(self, observations: list[ObservationDict]) -> None:
2929
summary_observations = [
30-
o for o in observations if o.type == "summary_observation"
30+
o for o in observations if o["type"] == ObservationType.SUMMARY
3131
]
3232
if not summary_observations:
33-
raise ErtCliError("No summary observations found.\nExiting ...")
33+
raise ErtCliError("No summary observations in configuration.\nExiting ...")
3434

3535
self.summary_observations = summary_observations
3636

3737
def _summary_to_yaml_dict(self) -> YamlDict:
38-
summary_observations = [
39-
o for o in self.summary_observations if o.type == "summary_observation"
40-
]
41-
summary_keys: set[str] = {o.key for o in summary_observations}
38+
summary_keys: set[str] = {str(o["KEY"]) for o in self.summary_observations}
4239
summary_list: list[SummaryDict] = []
4340
for key in summary_keys:
44-
observations_with_key = [o for o in summary_observations if o.key == key]
41+
observations_with_key = [
42+
o for o in self.summary_observations if o["KEY"] == key
43+
]
4544
obs_dicts: list[YamlObservation] = [
46-
{"date": o.date, "value": o.value, "error": o.error}
45+
{
46+
"date": str(o["DATE"]),
47+
"value": float(o["VALUE"]),
48+
"error": float(o["ERROR"]),
49+
}
4750
for o in observations_with_key
4851
]
4952
summary_dict: SummaryDict = {"key": key, "observations": obs_dicts}
@@ -64,10 +67,17 @@ def export_yaml(self) -> None:
6467

6568

6669
def convert_summary_to_yaml(config: str) -> None:
67-
with warnings.catch_warnings(action="ignore"):
68-
ert_config = ErtConfig.from_file(config)
70+
user_config_contents = read_file(config)
71+
config_dict = ErtConfig._config_dict_from_contents(
72+
user_config_contents,
73+
config,
74+
)
75+
file, obs_config = config_dict.get("OBS_CONFIG", (None, None))
76+
77+
if file is None or config_dict is None:
78+
raise ErtCliError("No observation configuration found.\nExiting ...")
6979

7080
yaml_exporter = YamlConverter(
71-
observations=ert_config.observation_declarations,
81+
observations=obs_config,
7282
)
7383
yaml_exporter.export_yaml()

tests/ert/unit_tests/cli/test_summary_to_yaml.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,28 @@
77
import pytest
88

99
from ert.cli.main import ErtCliError
10-
from ert.config._observations import GeneralObservation
10+
from ert.config import ObservationType
11+
from ert.config.parsing import ObservationDict
1112
from ert.observation_converters import convert_observations
1213
from ert.observation_converters.summary_to_yaml import YamlConverter
13-
from tests.ert.unit_tests.cli.test_summary_to_bulk import _make_summary_obs
14+
15+
16+
def _make_obs_dict(
17+
obs_type: ObservationType = ObservationType.SUMMARY,
18+
key: str = "WOPR",
19+
well: str = "OP1",
20+
) -> ObservationDict:
21+
key = f"{key}:{well}" if well else key
22+
return ObservationDict(
23+
{
24+
"type": obs_type,
25+
"KEY": key,
26+
"ERROR": 5,
27+
"VALUE": 10,
28+
"DATE": "2010-10-10",
29+
},
30+
context=MagicMock(),
31+
)
1432

1533

1634
@pytest.mark.usefixtures("snake_oil_case")
@@ -22,22 +40,22 @@ def test_that_happy_path_on_snake_oil_produces_yaml_and_stdout(capsys):
2240
smry:
2341
- key: WOPR:OP1
2442
observations:
25-
- date: '2010-03-31T00:00:00'
43+
- date: '2010-03-31'
2644
value: 0.1
2745
error: 0.05
28-
- date: '2010-12-26T00:00:00'
46+
- date: '2010-12-26'
2947
value: 0.7
3048
error: 0.07
31-
- date: '2011-12-21T00:00:00'
49+
- date: '2011-12-21'
3250
value: 0.5
3351
error: 0.05
34-
- date: '2012-12-15T00:00:00'
52+
- date: '2012-12-15'
3553
value: 0.3
3654
error: 0.075
37-
- date: '2013-12-10T00:00:00'
55+
- date: '2013-12-10'
3856
value: 0.2
3957
error: 0.035
40-
- date: '2015-03-15T00:00:00'
58+
- date: '2015-03-15'
4159
value: 0.015
4260
error: 0.01
4361
""")
@@ -52,23 +70,21 @@ def test_that_happy_path_on_snake_oil_produces_yaml_and_stdout(capsys):
5270

5371

5472
def test_that_empty_observations_raises_ert_cli_error():
55-
with pytest.raises(ErtCliError, match="No summary observations found"):
73+
with pytest.raises(ErtCliError, match="No summary observations in configuration"):
5674
YamlConverter([])
5775

5876

5977
def test_that_no_summary_observations_raises_ert_cli_error():
60-
gen_obs = GeneralObservation(
61-
name="foo", data="foo", value=1.0, error=1.0, restart=5, index=5
62-
)
63-
with pytest.raises(ErtCliError, match="No summary observations found"):
78+
gen_obs = _make_obs_dict(obs_type=ObservationType.GENERAL)
79+
with pytest.raises(ErtCliError, match="No summary observations in configuration"):
6480
YamlConverter([gen_obs])
6581

6682

6783
def test_that_observations_with_same_summary_key_are_gathered_in_yaml_dict(use_tmpdir):
6884
k1, k2 = "foo", "bar"
6985
observations = 2 * [
70-
_make_summary_obs(key=k1, well=None),
71-
_make_summary_obs(key=k2, well=None),
86+
_make_obs_dict(key=k1, well=""),
87+
_make_obs_dict(key=k2, well=""),
7288
]
7389

7490
converter = YamlConverter(observations=observations)
@@ -85,8 +101,8 @@ def test_that_observations_with_same_summary_key_are_gathered_in_yaml_dict(use_t
85101
def test_that_observations_with_different_summary_keys_are_separated_in_yaml_dict():
86102
k1, k2 = "foo", "bar"
87103
observations = [
88-
_make_summary_obs(key=k1, well=None),
89-
_make_summary_obs(key=k2, well=None),
104+
_make_obs_dict(key=k1, well=""),
105+
_make_obs_dict(key=k2, well=""),
90106
]
91107

92108
converter = YamlConverter(observations=observations)
@@ -98,7 +114,7 @@ def test_that_observations_with_different_summary_keys_are_separated_in_yaml_dic
98114

99115

100116
def test_that_dumping_to_yaml_is_skipped_when_file_already_exists(use_tmpdir):
101-
observations = [_make_summary_obs()]
117+
observations = [_make_obs_dict()]
102118

103119
Path("summary_observations.yaml").write_text("existing", encoding="utf-8")
104120
assert Path("summary_observations.yaml").is_file()

0 commit comments

Comments
 (0)