-
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.
Migrate finalized keys for response configs
- Loading branch information
Showing
5 changed files
with
88 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import polars | ||
|
||
info = "Migrate finalized response keys into configs" | ||
|
||
|
||
def _migrate_response_configs_wrt_finalized_keys(path: Path) -> None: | ||
for experiment in path.glob("experiments/*"): | ||
ensembles = path.glob("ensembles/*") | ||
|
||
experiment_id = None | ||
with open(experiment / "index.json", encoding="utf-8") as f: | ||
exp_index = json.load(f) | ||
experiment_id = exp_index["id"] | ||
|
||
responses_config = None | ||
with open(experiment / "responses.json", mode="r", encoding="utf-8") as f: | ||
responses_config = json.load(f) | ||
for response_type, config in responses_config.items(): | ||
if not config.get("has_finalized_keys"): | ||
# Read a sample response and write the keys | ||
for ens in ensembles: | ||
with open(ens / "index.json", encoding="utf-8") as f: | ||
ens_file = json.load(f) | ||
if ens_file["experiment_id"] != experiment_id: | ||
continue | ||
|
||
real_dirs = [*ens.glob("realization-*")] | ||
|
||
for real_dir in real_dirs: | ||
if (real_dir / f"{response_type}.parquet").exists(): | ||
df = polars.read_parquet( | ||
real_dir / f"{response_type}.parquet" | ||
) | ||
response_keys = df["response_key"].unique().to_list() | ||
config["has_finalized_keys"] = True | ||
config["keys"] = sorted(response_keys) | ||
break | ||
|
||
if config["has_finalized_keys"]: | ||
break | ||
|
||
if "has_finalized_keys" not in config: | ||
# At this point in "storage history", | ||
# only gendata and summary response types | ||
# exist, and only summary starts without finalized keys | ||
config["has_finalized_keys"] = ( | ||
config["_ert_kind"] != "SummaryConfig" | ||
) | ||
|
||
os.remove(experiment / "responses.json") | ||
with open(experiment / "responses.json", mode="w+", encoding="utf-8") as f: | ||
json.dump( | ||
responses_config, | ||
f, | ||
default=str, | ||
indent=2, | ||
) | ||
|
||
|
||
def migrate(path: Path) -> None: | ||
_migrate_response_configs_wrt_finalized_keys(path) |
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
2 changes: 1 addition & 1 deletion
2
...t/unit_tests/storage/snapshots/test_storage_migration/test_that_storage_matches/responses
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 |
---|---|---|
@@ -1 +1 @@ | ||
{'gen_data': GenDataConfig(name='gen_data', input_files=['gen%d.txt'], keys=['GEN'], has_finalized_keys=True, report_steps_list=[[1]]), 'summary': SummaryConfig(name='summary', input_files=['CASE'], keys=['FOPR', 'RWPR'], has_finalized_keys=False, refcase={})} | ||
{'gen_data': GenDataConfig(name='gen_data', input_files=['gen%d.txt'], keys=['GEN'], has_finalized_keys=True, report_steps_list=[[1]]), 'summary': SummaryConfig(name='summary', input_files=['CASE'], keys=['FOPR'], has_finalized_keys=True, refcase={})} |
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