Skip to content

Commit

Permalink
Apply overrides of data_input_dict in Surface and its superclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
liuly12 committed Mar 18, 2024
1 parent 7195683 commit 325ea01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Binary file added tests/example_data_input_dict.csv.gz
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_land.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,21 @@ def test_surface_overrides(self):
self.assertEqual(surface.capacity, 9.8 * 7.5)
self.assertDictEqual(surface.pollutant_load, {'nitrate': 5.7, 'phosphate': 10.1})
self.assertDictEqual(surface.decays, {"nitrate": {"constant": 0.001, "exponent": 1.005}})

# override the data_input_dict
# test the format of dict
new_data_input_dict = {("temperature", 1): 10,
("temperature", 2): 20,
}
surface.apply_overrides({'data_input_dict': new_data_input_dict})
self.assertDictEqual(surface.data_input_dict, new_data_input_dict)
# test the format of str
new_data_input_dict = "example_data_input_dict.csv.gz"
surface.apply_overrides({'data_input_dict': new_data_input_dict})
from wsimod.orchestration.model import read_csv
new_data_input_dict = read_csv(new_data_input_dict)
self.assertDictEqual(surface.data_input_dict, new_data_input_dict)
print(dict(list(surface.data_input_dict.items())[:5]))

def test_impervioussurface_overrides(self):
constants.set_default_pollutants()
Expand Down
11 changes: 11 additions & 0 deletions wsimod/nodes/land.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ def apply_overrides(self, overrides = Dict[str, Any]):
if 'capacity' in overrides.keys():
overrides.pop('capacity')
print('ERROR: specifying capacity is depreciated in overrides for surface, please specify depth and area instead')

# 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 isinstance(content, dict):
self.data_input_dict = content
else:
print('ERROR: not recognised format for data_input_dict')

super().apply_overrides(overrides)

def run(self):
Expand Down

0 comments on commit 325ea01

Please sign in to comment.