Skip to content

Commit

Permalink
Merge pull request #48 from ImperialCollegeLondon/input_file
Browse files Browse the repository at this point in the history
Add running WSIMOD saved files
dalonsoa authored Dec 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 9a18ef7 + 8316ee5 commit 7871ad2
Showing 2 changed files with 66 additions and 10 deletions.
48 changes: 39 additions & 9 deletions wsimod/__main__.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,14 @@
import pandas as pd

from wsimod.orchestration.model import Model
from wsimod.validation import assign_data_to_settings, load_data_files, validate_io_args
from wsimod.validation import (
_validate_input_dir,
_validate_output_dir,
assign_data_to_settings,
evaluate_input_file,
load_data_files,
validate_io_args,
)


def create_parser() -> ArgumentParser:
@@ -55,17 +62,40 @@ def run_model(settings: dict[str, Any], outputs: Path) -> None:
pd.DataFrame(surfaces).to_csv(outputs / "surfaces.csv")


def run_saved_model(settings: str, inputs: Path, outputs: Path) -> None:
"""Runs a previously saved model.
Args:
settings (str): The name of the settings file to load.
inputs (Path): The location of that file, as well as any other input file.
outputs (Path): Directory where to save the outputs.
"""
model = Model()
model.load(inputs, config_name=settings)
flows, tanks, _, surfaces = model.run()

pd.DataFrame(flows).to_csv(outputs / "flows.csv")
pd.DataFrame(tanks).to_csv(outputs / "tanks.csv")
pd.DataFrame(surfaces).to_csv(outputs / "surfaces.csv")


def run() -> None:
"""Main entry point of the application."""
args = vars(create_parser().parse_args())
settings = validate_io_args(**args)

inputs = settings.pop("inputs")
outputs = settings.pop("outputs")
loaded_data = load_data_files(settings.pop("data", {}), inputs)
loaded_settings = assign_data_to_settings(settings, loaded_data)

run_model(loaded_settings, outputs)
settings_type = evaluate_input_file(args["settings"])

if settings_type == "custom":
settings = validate_io_args(**args)
inputs = settings.pop("inputs")
outputs = settings.pop("outputs")
loaded_data = load_data_files(settings.pop("data", {}), inputs)
loaded_settings = assign_data_to_settings(settings, loaded_data)
run_model(loaded_settings, outputs)
else:
settings_file: Path = args["settings"]
inputs = _validate_input_dir(args["inputs"], settings_file.parent)
outputs = _validate_output_dir(args["outputs"], settings_file.parent)
run_saved_model(settings_file.name, inputs, outputs)


if __name__ == "__main__":
28 changes: 27 additions & 1 deletion wsimod/validation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import ast
import os
from pathlib import Path
from typing import Any, Optional, Union
from typing import Any, Literal, Optional, Union

import pandas as pd
import yaml

from wsimod.core import constants


def evaluate_input_file(settings: Path) -> Literal["saved", "custom"]:
"""Decides what type of input file we are dealing with.
"save" correspond to fully constructed models which have been saved, alongside
any necessary data files. "custom" are input files constructed manually.
Raises:
ValueError: If the settings file do not exist.
Return:
If the input file is a saved model file or a custom input.
"""
if settings.is_dir() or not settings.exists():
raise ValueError(
f"The settings file at {settings.absolute()} could not be found."
)

with settings.open("rb") as f:
settings_ = yaml.safe_load(f)

if set(["data", "inputs", "outputs"]).isdisjoint(settings_.keys()):
return "saved"

return "custom"


def validate_io_args(
settings: Path, inputs: Optional[Path], outputs: Optional[Path]
) -> dict[str, Any]:

0 comments on commit 7871ad2

Please sign in to comment.