Skip to content

Commit

Permalink
Merge pull request #91 from ImperialCollegeLondon/data_input_dict-ove…
Browse files Browse the repository at this point in the history
…rrides

Apply overrides for data_input_dict in Node
  • Loading branch information
liuly12 authored Oct 10, 2024
2 parents f067f4f + 91f198d commit d3e8dd3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from wsimod.nodes.nodes import Node
from wsimod.nodes.storage import Storage
from wsimod.nodes.waste import Waste
import os
import pandas as pd


class MyTestClass(TestCase):
Expand Down Expand Up @@ -417,6 +419,29 @@ def test_data_read(self):

self.assertEqual(15, node.get_data_input("temperature"))

def test_data_overrides(self):
data_path = os.path.join(
os.getcwd(),
"docs",
"demo",
"data",
"processed",
"example_override_data.csv.gz",
)
input_data = pd.read_csv(data_path)

overrides = {"data_input_dict": data_path}
node = Node(name="")
node.apply_overrides(overrides)
node.t = list(node.data_input_dict.keys())[0][1]

self.assertEqual(
input_data.groupby("variable").get_group("temperature")["value"].iloc[0],
node.get_data_input("temperature"),
)
# test runtime error
self.assertRaises(RuntimeError, lambda: node.apply_overrides({}))


if __name__ == "__main__":
unittest.main()
11 changes: 11 additions & 0 deletions wsimod/nodes/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ def apply_overrides(self, overrides: Dict[str, Any] = {}) -> None:
Args:
overrides (dict, optional): Dictionary of overrides. Defaults to {}.
"""
# overrides data_input_dict
from wsimod.orchestration.model import read_csv

content = overrides.pop("data_input_dict", self.data_input_dict)
if isinstance(content, str):
self.data_input_dict = read_csv(content)
elif not content:
pass
else:
raise RuntimeError("Not recognised format for data_input_dict")

if len(overrides) > 0:
print(f"No override behaviour defined for: {overrides.keys()}")

Expand Down

0 comments on commit d3e8dd3

Please sign in to comment.