def test_numpy_parameter_recorder(simple_linear_model): """ Test the NumpyArrayParameterRecorder """ from pywr.parameters import DailyProfileParameter model = simple_linear_model # using leap year simplifies tests model.timestepper.start = pandas.to_datetime("2016-01-01") model.timestepper.end = pandas.to_datetime("2016-12-31") otpt = model.nodes['Output'] p = DailyProfileParameter(model, np.arange(366, dtype=np.float64), ) p.name = 'daily profile' model.nodes['Input'].max_flow = p otpt.cost = -2.0 rec = NumpyArrayParameterRecorder(model, model.nodes['Input'].max_flow) # test retrieval of recorder assert model.recorders['numpyarrayparameterrecorder.daily profile'] == rec model.run() assert rec.data.shape == (366, 1) assert_allclose(rec.data, np.arange(366, dtype=np.float64)[:, np.newaxis]) df = rec.to_dataframe() assert df.shape == (366, 1) assert_allclose(df.values, np.arange(366, dtype=np.float64)[:, np.newaxis])
def test_dynamic_factors(model): model.timestepper.end = Timestamp("2016-01-03") A = Input(model, "A", max_flow=10.0) B = Input(model, "B", max_flow=10.0) C = Input(model, "C", max_flow=10.0) Z = Output(model, "Z", cost=-10) agg = AggregatedNode(model, "agg", [A, B, C]) agg.max_flow = 10.0 factor1 = DailyProfileParameter( model, np.append(np.array([0.8, 0.3]), np.ones(364))) factor2 = DailyProfileParameter( model, np.append(np.array([0.1, 0.3]), np.ones(364))) factor3 = DailyProfileParameter( model, np.append(np.array([0.1, 0.4]), np.ones(364))) agg.factors = [factor1, factor2, factor3] A.connect(Z) B.connect(Z) C.connect(Z) model.step() assert_allclose(A.flow, 8) assert_allclose(B.flow, 1) assert_allclose(C.flow, 1) model.step() assert_allclose(A.flow, 3) assert_allclose(B.flow, 3) assert_allclose(C.flow, 4)
def test_dynamic_factors_load(model): model.timestepper.end = Timestamp("2016-01-03") A = Input(model, "A", max_flow=10.0) B = Input(model, "B", max_flow=10.0) Z = Output(model, "Z", cost=-10, max_flow=10.0) A.connect(Z) B.connect(Z) DailyProfileParameter(model, np.append(np.array([3, 4]), np.ones(364)), name="factor1") data = {"name": "agg", "factors": [1, "factor1"], "nodes": ["A", "B"]} AggregatedNode.load(data, model) model.step() assert_allclose(A.flow, 2.5) assert_allclose(B.flow, 7.5) model.step() assert_allclose(A.flow, 2) assert_allclose(B.flow, 8)
def test_parameter_daily_profile(simple_linear_model): """ Test DailyProfileParameter """ model = simple_linear_model values = np.arange(366, dtype=np.float64) p = DailyProfileParameter(model, values) model.setup() # Iterate in time for ts in model.timestepper: month = ts.datetime.month day = ts.datetime.day iday = int((datetime.datetime(2016, month, day) - datetime.datetime(2016, 1, 1)).days) si = ScenarioIndex(0, np.array([0], dtype=np.int32)) np.testing.assert_allclose(p.value(ts, si), values[iday])
def test_parameter_mean_recorder_json(simple_linear_model): model = simple_linear_model node = model.nodes["Input"] values = np.arange(0, 366, dtype=np.float64) parameter = DailyProfileParameter(values, name="input_max_flow") model.parameters.append(parameter) # HACK node.max_flow = parameter data = { "type": "meanparameter", "parameter": "input_max_flow", "timesteps": 3, } rec = load_recorder(model, data)
def test_parameter_mean_recorder_json(simple_linear_model): model = simple_linear_model node = model.nodes["Input"] values = np.arange(0, 366, dtype=np.float64) parameter = DailyProfileParameter(model, values, name="input_max_flow") node.max_flow = parameter data = { "type": "rollingwindowparameter", "parameter": "input_max_flow", "window": 3, "agg_func": "mean", } rec = load_recorder(model, data)
def test_parameter_mean_recorder(simple_linear_model): model = simple_linear_model # using leap year simplifies test model.timestepper.start = pandas.to_datetime("2016-01-01") model.timestepper.end = pandas.to_datetime("2016-12-31") node = model.nodes["Input"] values = np.arange(0, 366, dtype=np.float64) node.max_flow = DailyProfileParameter(values) scenario = Scenario(model, "dummy", size=3) timesteps = 3 rec = MeanParameterRecorder(model, node.max_flow, timesteps) model.run() assert_allclose(rec.data[[0, 1, 2, 3, 364], 0], [0, 0.5, 1, 2, 363])
def test_daily_profile_leap_day(model): """Test behaviour of daily profile parameter for leap years """ inpt = Input(model, "input") otpt = Output(model, "otpt", max_flow=None, cost=-999) inpt.connect(otpt) inpt.max_flow = DailyProfileParameter(model, np.arange(0, 366, dtype=np.float64)) # non-leap year model.timestepper.start = pd.to_datetime("2015-01-01") model.timestepper.end = pd.to_datetime("2015-12-31") model.run() assert_allclose(inpt.flow, 365) # NOT 364 # leap year model.timestepper.start = pd.to_datetime("2016-01-01") model.timestepper.end = pd.to_datetime("2016-12-31") model.run() assert_allclose(inpt.flow, 365)
def test_agg(self, simple_linear_model, agg_func): model = simple_linear_model model.timestepper.delta = 15 scenarioA = Scenario(model, "Scenario A", size=2) scenarioB = Scenario(model, "Scenario B", size=5) values = np.arange(366, dtype=np.float64) p1 = DailyProfileParameter(model, values) p2 = ConstantScenarioParameter(model, scenarioB, np.arange(scenarioB.size, dtype=np.float64)) p = AggregatedParameter(model, [p1, p2], agg_func=agg_func) func = TestAggregatedParameter.funcs[agg_func] @assert_rec(model, p) def expected_func(timestep, scenario_index): x = p1.get_value(scenario_index) y = p2.get_value(scenario_index) return func(np.array([x,y])) model.run()
def test_max(self, model): # Add two scenarios scA = Scenario(model, 'Scenario A', size=2) scB = Scenario(model, 'Scenario B', size=5) values = np.arange(366, dtype=np.float64) p1 = DailyProfileParameter(values) p2 = ConstantScenarioParameter(scB, np.arange(scB.size, dtype=np.float64)) p = AggregatedParameter([p1, p2], agg_func='max') p.setup(model) for ts in model.timestepper: month = ts.datetime.month day = ts.datetime.day iday = int((datetime.datetime(2016, month, day) - datetime.datetime(2016, 1, 1)).days) for i in range(scB.size): si = ScenarioIndex(i, np.array([0, i], dtype=np.int32)) np.testing.assert_allclose(p.value(ts, si), max(values[iday], i))
def test_min(self, model): # Add two scenarios scA = Scenario(model, 'Scenario A', size=2) scB = Scenario(model, 'Scenario B', size=5) values = np.arange(366, dtype=np.float64) p1 = DailyProfileParameter(values) p2 = ConstantScenarioParameter(scB, np.arange(scB.size, dtype=np.float64)) p = AggregatedParameter([ p1, ], agg_func='min') p.add(p2) p.setup(model) for ts in model.timestepper: iday = ts.datetime.dayofyear - 1 for i in range(scB.size): si = ScenarioIndex(i, np.array([0, i], dtype=np.int32)) np.testing.assert_allclose(p.value(ts, si), min(values[iday], i))
def test_parameter_mean_recorder(simple_linear_model): model = simple_linear_model # using leap year simplifies test model.timestepper.start = pandas.to_datetime("2016-01-01") model.timestepper.end = pandas.to_datetime("2016-12-31") node = model.nodes["Input"] values = np.arange(0, 366, dtype=np.float64) node.max_flow = DailyProfileParameter(model, values) scenario = Scenario(model, "dummy", size=3) timesteps = 3 rec_mean = RollingWindowParameterRecorder(model, node.max_flow, timesteps, "mean", name="rec_mean") rec_sum = RollingWindowParameterRecorder(model, node.max_flow, timesteps, "sum", name="rec_sum") rec_min = RollingWindowParameterRecorder(model, node.max_flow, timesteps, "min", name="rec_min") rec_max = RollingWindowParameterRecorder(model, node.max_flow, timesteps, "max", name="rec_max") model.run() assert_allclose(rec_mean.data[[0, 1, 2, 3, 364], 0], [0, 0.5, 1, 2, 363]) assert_allclose(rec_max.data[[0, 1, 2, 3, 364], 0], [0, 1, 2, 3, 364]) assert_allclose(rec_min.data[[0, 1, 2, 3, 364], 0], [0, 0, 0, 1, 362]) assert_allclose(rec_sum.data[[0, 1, 2, 3, 364], 0], [0, 1, 3, 6, 1089])