-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CLI to run WSIMOD #38
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
80a16ac
Add initial version of the CLI
dalonsoa ea2fa79
Add initial arguments for the CLI
dalonsoa 0af6a9f
Add initial arguments for the CLI
dalonsoa de301b8
Add initial arguments for the CLI
dalonsoa 0a1a1e6
Add validation of input paths
dalonsoa a40007d
Implements loading date into the settings
dalonsoa 88f5d51
Add missing docstirngs and fix error in process options
dalonsoa 07e78bd
Scan lists in settings and load items if any
dalonsoa 062cc6f
Add run model function
dalonsoa bc4ebc8
Move run model to __main__.py
dalonsoa b691908
Modify 'run' function to actually run the model
dalonsoa d01d77a
Ignore scratch folder
dalonsoa fcf9790
Improve data reader
dalonsoa 2006ad4
Change order of operations in load data
dalonsoa e879bc2
Separate data loading step
dalonsoa cf81b95
Simplify defining inputs and give meaningful error message
dalonsoa b37b0ea
Add example settings file
dalonsoa 6112bd2
Run examples in YAML as tests
dalonsoa 3c86e14
Fix CI to install wsimod
dalonsoa 6cbcea7
Facilitate wsimod to run with -m
dalonsoa f60b143
Facilitate wsimod to run with -m
dalonsoa 14eb701
Merge branch 'cli' of https://github.com/ImperialCollegeLondon/wsi in…
dalonsoa 3952090
Fix failing CI
dalonsoa 32e8a8d
Fix failing CI, again
dalonsoa 5a7c886
Use command string rather than list in test
dalonsoa 00deb81
Include review comments
dalonsoa 8c4ee87
Add back what I should not have removed...
dalonsoa 619cbd4
Use pip-tools correctly
dalonsoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -152,4 +152,8 @@ cython_debug/ | |
.idea/ | ||
|
||
# VScode configuration | ||
.vscode | ||
.vscode | ||
|
||
# Scratch folder to do testing | ||
scratch | ||
results |
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,87 @@ | ||
inputs: docs/demo/data/processed | ||
outputs: results/quickstart_results | ||
|
||
data: | ||
my_land_data: | ||
filename: timeseries_data.csv | ||
filter: | ||
- where: site | ||
is: oxford_land | ||
scaling: | ||
- where: variable | ||
is: precipitation | ||
variable: value | ||
factor: "MM_TO_M" | ||
format: dict | ||
index: ['variable', 'date'] | ||
output: 'value' | ||
options: parse_dates=['date'] | ||
|
||
dates_data: | ||
filename: timeseries_data.csv | ||
options: usecols=['date'],parse_dates=['date'] | ||
|
||
dates: data:dates_data | ||
|
||
nodes: | ||
- type_: Sewer | ||
name: my_sewer | ||
capacity: 0.04 | ||
|
||
- type_: Land | ||
name: my_land | ||
data_input_dict: data:my_land_data | ||
surfaces: | ||
- type_: ImperviousSurface | ||
surface: urban | ||
area: 10 | ||
pollutant_load: | ||
phosphate: 1.0e-07 | ||
- type_: PerviousSurface | ||
surface: rural | ||
area: 100 | ||
depth: 0.5 | ||
pollutant_load: | ||
phosphate: 1.0e-07 | ||
|
||
- type_: Groundwater | ||
name: my_groundwater | ||
capacity: 100 | ||
area: 100 | ||
|
||
- type_: Node | ||
name: my_river | ||
|
||
- type_: Waste | ||
name: my_outlet | ||
|
||
arcs: | ||
- type_: Arc | ||
name: urban_drainage | ||
in_port: my_land | ||
out_port: my_sewer | ||
|
||
- type_: Arc | ||
name: percolation | ||
in_port: my_land | ||
out_port: my_groundwater | ||
|
||
- type_: Arc | ||
name: runoff | ||
in_port: my_land | ||
out_port: my_river | ||
|
||
- type_: Arc | ||
name: storm_outflow | ||
in_port: my_sewer | ||
out_port: my_river | ||
|
||
- type_: Arc | ||
name: baseflow | ||
in_port: my_groundwater | ||
out_port: my_river | ||
|
||
- type_: Arc | ||
name: catchment_outflow | ||
in_port: my_river | ||
out_port: my_outlet |
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,20 @@ | ||
from pytest import mark | ||
from pathlib import Path | ||
import subprocess | ||
|
||
|
||
def collect_examples() -> list[Path]: | ||
root = Path.cwd() / "docs" / "demo" / "examples" | ||
return list(root.glob("**/*.yaml")) | ||
|
||
|
||
@mark.parametrize("example", collect_examples()) | ||
def test_examples(example: Path, tmp_path: Path) -> None: | ||
result = subprocess.run( | ||
f"wsimod {str(example)} -o {str(tmp_path)}", | ||
shell=True, | ||
check=True, | ||
) | ||
assert (tmp_path / "flows.csv").exists() | ||
assert (tmp_path / "tanks.csv").exists() | ||
assert (tmp_path / "surfaces.csv").exists() |
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,72 @@ | ||
"""The entry point for the myproject program.""" | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
from typing import Any, cast | ||
|
||
import pandas as pd | ||
|
||
from wsimod.orchestration.model import Model | ||
from wsimod.validation import assign_data_to_settings, load_data_files, validate_io_args | ||
|
||
|
||
def create_parser() -> ArgumentParser: | ||
"""Create the CLI argument parser.""" | ||
parser = ArgumentParser(prog="WSIMOD") | ||
parser.add_argument( | ||
"settings", | ||
type=Path, | ||
help="Path to the WSIMOD input file, in YAML format.", | ||
) | ||
parser.add_argument( | ||
"--inputs", | ||
"-i", | ||
type=Path, | ||
help="Base directory for all input files. If present, overwrites value in the" | ||
" settings file.", | ||
) | ||
parser.add_argument( | ||
"--outputs", | ||
"-o", | ||
type=Path, | ||
help="Base directory for all output files. If present, overwrites value in the" | ||
" settings file.", | ||
) | ||
|
||
return parser | ||
|
||
|
||
def run_model(settings: dict[str, Any], outputs: Path) -> None: | ||
"""Runs the mode with the chosen settings and saves the outputs as csv. | ||
|
||
Args: | ||
settings (dict[str, Any]): Settings dictionary with loaded data. | ||
outputs(Path): Directory where to save the outputs. | ||
""" | ||
model = Model() | ||
|
||
model.dates = cast(pd.Series, settings["dates"]).drop_duplicates() | ||
model.add_nodes(settings["nodes"]) | ||
model.add_arcs(settings["arcs"]) | ||
|
||
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) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not currently installing the pinned versions of the main packages here because they are not specified in the
requirements-dev.txt
. You can see that pandas etc is being installed at thepython -m pip install .
step in the CI https://github.com/ImperialCollegeLondon/wsi/actions/runs/6875139142/job/18698156546