diff --git a/docs/demo/data/processed/example_override_data.csv.gz b/docs/demo/data/processed/example_override_data.csv.gz new file mode 100644 index 00000000..a88da4fa Binary files /dev/null and b/docs/demo/data/processed/example_override_data.csv.gz differ diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 576ef7b9..a8a211b2 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -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): @@ -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() diff --git a/wsimod/nodes/nodes.py b/wsimod/nodes/nodes.py index ba7ade47..a405917d 100644 --- a/wsimod/nodes/nodes.py +++ b/wsimod/nodes/nodes.py @@ -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()}")