-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store each response key as a column for responses
- Loading branch information
Showing
22 changed files
with
231 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
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,89 @@ | ||
import dataclasses | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import polars | ||
import xarray as xr | ||
|
||
info = "Make responses columnar" | ||
|
||
|
||
@dataclasses.dataclass | ||
class ObservationDatasetInfo: | ||
polars_df: polars.DataFrame | ||
response_type: str | ||
original_ds_path: Path | ||
|
||
@classmethod | ||
def from_path(cls, path: Path) -> "ObservationDatasetInfo": | ||
observation_key = os.path.basename(path) | ||
ds = xr.open_dataset(path, engine="scipy") | ||
response_key = ds.attrs["response"] | ||
response_type = "summary" if response_key == "summary" else "gen_data" | ||
|
||
df = polars.from_pandas(ds.to_dataframe().dropna().reset_index()) | ||
df = df.with_columns(observation_key=polars.lit(observation_key)) | ||
|
||
primary_key = ( | ||
["time"] if response_type == "summary" else ["report_step", "index"] | ||
) | ||
if response_type == "summary": | ||
df = df.rename({"name": "response_key"}) | ||
df = df.with_columns(polars.col("time").dt.cast_time_unit("ms")) | ||
|
||
if response_type == "gen_data": | ||
df = df.with_columns( | ||
polars.col("report_step").cast(polars.UInt16), | ||
polars.col("index").cast(polars.UInt16), | ||
response_key=polars.lit(response_key), | ||
) | ||
|
||
df = df.with_columns( | ||
[ | ||
polars.col("std").cast(polars.Float32), | ||
polars.col("observations").cast(polars.Float32), | ||
] | ||
) | ||
|
||
df = df[ | ||
["response_key", "observation_key", *primary_key, "observations", "std"] | ||
] | ||
|
||
return cls(df, response_type, path) | ||
|
||
|
||
def _migrate_responses_to_one_col_per_response(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"] | ||
|
||
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: | ||
for df_name, columns in [ | ||
("gen_data", ["report_step", "index"]), | ||
("summary", ["time"]), | ||
]: | ||
if (real_dir / f"{df_name}.parquet").exists(): | ||
df = polars.read_parquet(real_dir / f"{df_name}.parquet") | ||
pivoted = df.pivot( | ||
on="response_key", index=["realization", *columns] | ||
) | ||
|
||
os.remove(real_dir / f"{df_name}.parquet") | ||
pivoted.write_parquet(real_dir / f"{df_name}.parquet") | ||
|
||
|
||
def migrate(path: Path) -> None: | ||
_migrate_responses_to_one_col_per_response(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
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
Oops, something went wrong.