Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_directory(self, simple_linear_model, tmpdir):
""" Test TablesRecorder to create a new directory """
model = simple_linear_model
otpt = model.nodes['Output']
inpt = model.nodes['Input']
agg_node = AggregatedNode(model, 'Sum', [otpt, inpt])
inpt.max_flow = 10.0
otpt.cost = -2.0
# Make a path with a new directory
folder = tmpdir.join('outputs')
h5file = folder.join('output.h5')
assert(not folder.exists())
rec = TablesRecorder(model, str(h5file), create_directories=True)
model.run()
assert(folder.exists())
assert(h5file.exists())
from pywr.parameters import ConstantScenarioParameter
model = simple_linear_model
scA = Scenario(model, name='A', size=4)
scB = Scenario(model, name='B', size=2)
otpt = model.nodes['Output']
inpt = model.nodes['Input']
inpt.max_flow = ConstantScenarioParameter(model, scA, [10, 20, 30, 40])
otpt.max_flow = ConstantScenarioParameter(model, scB, [20, 40])
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
rec = TablesRecorder(model, h5f, routes_flows='flows')
model.run()
flows = h5f.get_node('/flows')
assert flows.shape == (365, 1, 4, 2)
np.testing.assert_allclose(flows[0, 0], [[10, 10], [20, 20], [20, 30], [20, 40]])
model = simple_linear_model
otpt = model.nodes['Output']
inpt = model.nodes['Input']
agg_node = AggregatedNode(model, 'Sum', [otpt, inpt])
p = ConstantParameter(model, 10.0, name='max_flow')
inpt.max_flow = p
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
nodes = ['Output', 'Input', 'Sum']
where = "/agroup"
rec = TablesRecorder(model, h5f, nodes=nodes,
parameters=[p, ], where=where)
model.run()
for node_name in ['Output', 'Input', 'Sum', 'max_flow']:
ca = h5f.get_node("/agroup/" + node_name)
assert ca.shape == (365, 1)
if node_name == 'Sum':
np.testing.assert_allclose(ca, 20.0)
else:
np.testing.assert_allclose(ca, 10.0)
inpt.max_flow = p
# ensure TablesRecorder can handle parameters with a / in the name
p_slash = ConstantParameter(model, 0.0, name='name with a / in it')
inpt.min_flow = p_slash
agg_node = AggregatedNode(model, 'Sum', [otpt, inpt])
inpt.max_flow = 10.0
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
with pytest.warns(ParameterNameWarning):
rec = TablesRecorder(model, h5f, parameters=[p, p_slash])
# check parameters have been added to the component tree
# this is particularly important for parameters which update their
# values in `after`, e.g. DeficitParameter (see #465)
assert(not model.find_orphaned_parameters())
assert(p in rec.children)
assert(p_slash in rec.children)
with pytest.warns(tables.NaturalNameWarning):
model.run()
for node_name in model.nodes.keys():
ca = h5f.get_node('/', node_name)
assert ca.shape == (365, 1)
if node_name == 'Sum':
np.testing.assert_allclose(ca, 20.0)
def test_generate_dataframes(self, simple_linear_model, tmpdir):
"""Test TablesRecorder.generate_dataframes """
from pywr.parameters import ConstantScenarioParameter
model = simple_linear_model
scA = Scenario(model, name='A', size=4)
scB = Scenario(model, name='B', size=2)
otpt = model.nodes['Output']
inpt = model.nodes['Input']
inpt.max_flow = ConstantScenarioParameter(model, scA, [10, 20, 30, 40])
otpt.max_flow = ConstantScenarioParameter(model, scB, [20, 40])
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
TablesRecorder(model, h5file)
model.run()
dfs = {}
for node, df in TablesRecorder.generate_dataframes(h5file):
dfs[node] = df
for node_name in model.nodes.keys():
df = dfs[node_name]
assert df.shape == (365, 8)
np.testing.assert_allclose(df.iloc[0, :], [10, 10, 20, 20, 20, 30, 20, 40])
scB = Scenario(model, name='B', size=2)
# Use first and last combinations
model.scenarios.user_combinations = [[0, 0], [3, 1]]
otpt = model.nodes['Output']
inpt = model.nodes['Input']
inpt.max_flow = ConstantScenarioParameter(model, scA, [10, 20, 30, 40])
otpt.max_flow = ConstantScenarioParameter(model, scB, [20, 40])
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
rec = TablesRecorder(model, h5f)
model.run()
for node_name in model.nodes.keys():
ca = h5f.get_node('/', node_name)
assert ca.shape == (365, 2)
np.testing.assert_allclose(ca[0, ...], [10, 40])
# check combinations table exists
combinations = h5f.get_node('/scenario_combinations')
for i, comb in enumerate(model.scenarios.user_combinations):
row = combinations[i]
assert row['A'] == comb[0]
assert row['B'] == comb[1]
scB = Scenario(model, name='B', size=2)
# Use first and last combinations
model.scenarios.user_combinations = [[0, 0], [3, 1]]
otpt = model.nodes['Output']
inpt = model.nodes['Input']
inpt.max_flow = ConstantScenarioParameter(model, scA, [10, 20, 30, 40])
otpt.max_flow = ConstantScenarioParameter(model, scB, [20, 40])
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
rec = TablesRecorder(model, h5f, routes_flows='flows')
model.run()
flows = h5f.get_node('/flows')
assert flows.shape == (365, 1, 2)
np.testing.assert_allclose(flows[0, 0], [10, 40])
# check combinations table exists
combinations = h5f.get_node('/scenario_combinations')
for i, comb in enumerate(model.scenarios.user_combinations):
row = combinations[i]
assert row['A'] == comb[0]
assert row['B'] == comb[1]
# This part of the test requires IPython (see `pywr.notebook`)
pytest.importorskip("IPython") # triggers a skip of the test if IPython not found.
model.check()
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
nodes = [
('/outputs/demand', 'Demand'),
('/storage/reservoir', 'Reservoir'),
]
parameters = [
('/parameters/demand_saving_level', 'demand_saving_level'),
]
rec = TablesRecorder(model, h5f, nodes=nodes, parameters=parameters)
model.run()
max_volume = model.nodes["Reservoir"].max_volume
rec_demand = h5f.get_node('/outputs/demand', 'Demand').read()
rec_storage = h5f.get_node('/storage/reservoir', 'Reservoir').read()
# model starts with no demand saving
demand_baseline = 50.0
demand_factor = 0.9 # jan-apr
demand_saving = 1.0
assert_allclose(rec_demand[0, 0], demand_baseline * demand_factor * demand_saving)
# first control curve breached
demand_saving = 0.95
assert (rec_storage[4, 0] < (0.8 * max_volume))
from pywr.parameters import ConstantScenarioParameter
model = simple_linear_model
scA = Scenario(model, name='A', size=4)
scB = Scenario(model, name='B', size=2)
otpt = model.nodes['Output']
inpt = model.nodes['Input']
inpt.max_flow = ConstantScenarioParameter(model, scA, [10, 20, 30, 40])
otpt.max_flow = ConstantScenarioParameter(model, scB, [20, 40])
otpt.cost = -2.0
h5file = tmpdir.join('output.h5')
import tables
with tables.open_file(str(h5file), 'w') as h5f:
rec = TablesRecorder(model, h5f)
model.run()
for node_name in model.nodes.keys():
ca = h5f.get_node('/', node_name)
assert ca.shape == (365, 4, 2)
np.testing.assert_allclose(ca[0, ...], [[10, 10], [20, 20], [20, 30], [20, 40]])
scenarios = h5f.get_node('/scenarios')
for i, s in enumerate(model.scenarios.scenarios):
row = scenarios[i]
assert row['name'] == s.name.encode('utf-8')
assert row['size'] == s.size