Skip to content

Commit 44ca2e7

Browse files
committed
Natsort summary obs csv rows
Natsort is a transitive dependency of resdata, and should be an explicit dependency of ert when used in Ert code.
1 parent 3fb9cab commit 44ca2e7

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ dependencies = [
7979
"xarray",
8080
"xtgeo >= 3.3.0",
8181
"resfo-utilities>=0.5.0",
82+
"natsort>=8.4.0",
8283
]
8384

8485
[project.scripts]

src/ert/gather_summary_observations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import anyio
1717
import httpx
1818
from httpx import AsyncClient
19+
from natsort import natsorted
1920
from resfo_utilities import SummaryKeyType
2021

2122
from ert.cli.main import ErtCliError
@@ -203,9 +204,9 @@ def convert_summary_observations(
203204
with Path(csv_file_name).open(mode="w", encoding="utf-8") as fout:
204205
fout.write(", ".join([*header_fields, "value", "error", "date"]))
205206
fout.write("\n")
206-
for key, observations in summary_observations.items():
207+
for key in natsorted(summary_observations.keys()):
207208
skd = make_summary_key_data(key)
208-
for observation in observations:
209+
for observation in summary_observations[key]:
209210
for f in header_fields:
210211
v = getattr(skd, f)
211212
if v is None:

tests/ert/unit_tests/cli/test_gather_summary_observations.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
from textwrap import dedent
23

34
import pytest
@@ -72,3 +73,34 @@ def test_that_convert_summary_observations_extracts_localization_information(cap
7273
};""")
7374

7475
assert expected_print_with_localization in capsys.readouterr().out
76+
77+
78+
@pytest.mark.usefixtures("use_tmpdir")
79+
def test_that_convert_summary_observations_produces_natsorted_csv_rows(capsys):
80+
def make_summary_obs(number: int):
81+
return {
82+
f"WOPR:OP{number}": [
83+
{
84+
"name": f"WOPR_OP{number}",
85+
"errors": [0.02],
86+
"values": [0.5],
87+
"x_axis": ["2010-01-27T00:00:00"],
88+
},
89+
]
90+
}
91+
92+
summary_obs = (
93+
make_summary_obs(30)
94+
| make_summary_obs(10)
95+
| make_summary_obs(4)
96+
| make_summary_obs(2)
97+
)
98+
convert_summary_observations(
99+
summary_observations=summary_obs,
100+
breakthrough_observations={},
101+
csv_file_name="foo.csv",
102+
)
103+
with pathlib.Path("foo.csv").open(encoding="utf-8") as f:
104+
csv_content = f.readlines()
105+
well_names = [row.split(",")[1].strip() for row in csv_content[1:]]
106+
assert well_names == ["OP2", "OP4", "OP10", "OP30"]

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)