Exemplo n.º 1
0
def test_run_license1(solver):
    model = load_model('simple1.xml', solver=solver)

    model.timestamp = datetime.datetime(2015, 1, 1)

    # add licenses to supply node
    supply1 = model.nodes['supply1']
    daily_lic = pywr.licenses.DailyLicense(5)
    annual_lic = pywr.licenses.AnnualLicense(7)
    collection = pywr.licenses.LicenseCollection([daily_lic, annual_lic])
    supply1.licenses = collection

    model.check()

    # daily license is limit
    result = model.step()
    d1 = model.nodes['demand1']
    assert_allclose(d1.flow, 5.0, atol=1e-7)

    # resource state is getting worse
    assert(annual_lic.resource_state(model.timestep) < 1.0)

    # annual license is limit
    result = model.step()
    d1 = model.nodes['demand1']
    assert_allclose(d1.flow, 2.0, atol=1e-7)

    # annual license is exhausted
    result = model.step()
    d1 = model.nodes['demand1']
    assert_allclose(d1.flow, 0.0, atol=1e-7)
    assert_allclose(annual_lic.resource_state(model.timestep), 0.0, atol=1e-7)
Exemplo n.º 2
0
def test_annual_license_json():
    """
    This test demonstrates how an annual licence can be forceably distributed
    evenly across a year. The licence must build up a surplus before it can
    use more than the average.
    """
    model = load_model("annual_license.json")

    model.timestepper.start = "2001-01-01"
    model.timestepper.end = "2001-01-31"
    model.timestepper.delta = 5

    rec = NumpyArrayNodeRecorder(model, model.nodes["supply1"])

    model.run()

    initial_amount = 200.0
    # first day evenly apportions initial amount for each day of year
    first_day = initial_amount / 365
    assert_allclose(rec.data[0], first_day)
    # second day does the same, minus yesterday and with less days remaining
    remaining_days = 365 - 5
    second_day = (initial_amount - first_day * 5) / remaining_days
    assert_allclose(rec.data[1], second_day)
    # actual amount is the same as maximum was taken
    assert_allclose(first_day, second_day)
    # third day nothing is taken (no demand), so licence is saved
    assert_allclose(rec.data[2], 0.0)
    # fourth day more can be supplied as we've built up a surplus
    remaining_days = 365 - 5 * 3
    fourth_day = (initial_amount -
                  (first_day + second_day) * 5) / remaining_days
    assert_allclose(rec.data[3], fourth_day)
    assert fourth_day > first_day
Exemplo n.º 3
0
def test_run_river2():
    '''Test a river abstraction with two catchments, a confluence and a split'''
    model = load_model('river2.xml')

    result = model.step()
    assert (result[0:3] == ('optimal', 12.0, 9.25))
    assert (model.failure)
Exemplo n.º 4
0
def test_simple1():
    '''Test parsing a simple XML document'''
    # parse the XML into a model
    model = load_model('simple1.xml')

    # metadata
    assert (model.metadata['title'] == 'Simple 1')
    assert (model.metadata['description'] == 'A very simple example.')

    # node names
    nodes = model.nodes()
    assert (len(nodes) == 3)
    supply1 = model.node['supply1']
    link1 = model.node['link1']
    demand1 = model.node['demand1']

    # node types
    assert (type(supply1) is pywr.core.Supply)
    assert (type(link1) is pywr.core.Link)
    assert (type(demand1) is pywr.core.Demand)

    # node positions
    assert (supply1.position == (1, 1))
    assert (link1.position == (2, 1))
    assert (demand1.position == (3, 1))

    # edges
    edges = model.graph.edges()
    assert (len(edges) == 2)
    assert ((supply1, link1) in edges)
    assert ((link1, demand1) in edges)

    model.check()
Exemplo n.º 5
0
def test_run_river1():
    '''Test a river abstraction with a simple catchment'''
    model = load_model('river1.xml')

    result = model.step()
    assert (result[0:3] == ('optimal', 10.0, 5.0))
    assert (model.failure)
Exemplo n.º 6
0
def test_run_until_date():
    model = load_model('simple1.xml')

    # run until date
    model.reset()
    timesteps = model.run(until_date=pandas.to_datetime('2015-01-20'))
    assert (timesteps == 20)
Exemplo n.º 7
0
def test_run_license1():
    model = load_model('simple1.xml')

    model.timestamp = datetime.datetime(2015, 1, 1)

    # add licenses to supply node
    supply1 = model.node['supply1']
    daily_lic = pywr.licenses.DailyLicense(5)
    annual_lic = pywr.licenses.AnnualLicense(7)
    collection = pywr.licenses.LicenseCollection([daily_lic, annual_lic])
    supply1.licenses = collection

    model.check()

    # daily license is limit
    result = model.step()
    assert (result[0:3] == ('optimal', 10.0, 5.0))

    # resource state is getting worse
    assert (annual_lic.resource_state(model.timestamp) < 1.0)

    # annual license is limit
    result = model.step()
    assert (result[0:3] == ('optimal', 10.0, 2.0))

    # annual license is exhausted
    result = model.step()
    assert (result[0:3] == ('optimal', 10.0, 0.0))
    assert (annual_lic.resource_state(model.timestamp) == 0.0)
Exemplo n.º 8
0
def test_from_json(from_json):
    json_path = os.path.join(
        os.path.dirname(__file__), "models", "demand_saving2_with_variables.json"
    )

    if from_json:
        json_dict = pywr_json_to_d3_json(json_path, attributes=True)
    else:
        model = load_model("demand_saving2_with_variables.json")
        json_dict = pywr_model_to_d3_json(model, attributes=True)

    assert "nodes" in json_dict.keys()
    assert "links" in json_dict.keys()

    node_names = ["Inflow", "Reservoir", "Demand", "Spill"]
    for node in json_dict["nodes"]:
        assert node["name"] in node_names

        if node["name"] == "Reservoir":
            assert_array_equal(node["position"], [1, 1])

    demand = get_node(json_dict["nodes"], "Demand")
    demand_max_flow = get_node_attribute(demand, "max_flow")

    assert demand_max_flow["value"] == "demand_max_flow - AggregatedParameter"
Exemplo n.º 9
0
    def test_target_json(self):
        """ Test loading a HydropowerTargetParameter from JSON. """
        model = load_model("hydropower_target_example.json")
        si = ScenarioIndex(0, np.array([0], dtype=np.int32))

        # 30 time-steps are run such that the head gets so flow to hit the max_flow
        # constraint. The first few time-steps are also bound by the min_flow constraint.
        for i in range(30):
            model.step()

            rec = model.recorders["turbine1_energy"]
            param = model.parameters["turbine1_discharge"]

            turbine1 = model.nodes["turbine1"]
            assert turbine1.flow[0] > 0

            if np.allclose(turbine1.flow[0], 500.0):
                # If flow is bounded by min_flow then more HP is produced.
                assert rec.data[i, 0] > param.target.get_value(si)
            elif np.allclose(turbine1.flow[0], 1000.0):
                # If flow is bounded by max_flow then less HP is produced.
                assert rec.data[i, 0] < param.target.get_value(si)
            else:
                # If flow is within the bounds target is met exactly.
                assert_allclose(rec.data[i, 0], param.target.get_value(si))
Exemplo n.º 10
0
    def test_fdc_recorder(self, agg_func):
        """
        Test the FlowDurationCurveRecorder
        """
        model = load_model("timeseries2.json")
        input = model.nodes['catchment1']

        percentiles = np.linspace(20., 100., 5)
        rec = FlowDurationCurveRecorder(model, input, percentiles, fdc_agg_func=agg_func, agg_func="min")

        # test retrieval of recorder
        assert model.recorders['flowdurationcurverecorder.catchment1'] == rec
        # test changing name of recorder
        rec.name = 'timeseries.Input'
        assert model.recorders['timeseries.Input'] == rec
        with pytest.raises(KeyError):
            model.recorders['flowdurationcurverecorder.catchment1']

        model.run()

        func = TestAggregatedRecorder.funcs[agg_func]

        assert_allclose(rec.fdc[:, 0], [20.42,  21.78,  23.22,  26.47,  29.31])
        assert_allclose(func(rec.fdc, axis=0), rec.values())
        assert_allclose(np.min(func(rec.fdc, axis=0)), rec.aggregated_value())

        assert rec.fdc.shape == (len(percentiles), len(model.scenarios.combinations))
        df = rec.to_dataframe()
        assert df.shape == (len(percentiles), len(model.scenarios.combinations))
Exemplo n.º 11
0
def test_run_license1(solver):
    model = load_model("simple1.xml", solver=solver)

    model.timestamp = datetime.datetime(2015, 1, 1)

    # add licenses to supply node
    supply1 = model.node["supply1"]
    daily_lic = pywr.licenses.DailyLicense(5)
    annual_lic = pywr.licenses.AnnualLicense(7)
    collection = pywr.licenses.LicenseCollection([daily_lic, annual_lic])
    supply1.licenses = collection

    model.check()

    # daily license is limit
    result = model.step()
    d1 = model.node["demand1"]
    assert_allclose(d1.flow, 5.0, atol=1e-7)

    # resource state is getting worse
    assert annual_lic.resource_state(model.timestep) < 1.0

    # annual license is limit
    result = model.step()
    d1 = model.node["demand1"]
    assert_allclose(d1.flow, 2.0, atol=1e-7)

    # annual license is exhausted
    result = model.step()
    d1 = model.node["demand1"]
    assert_allclose(d1.flow, 0.0, atol=1e-7)
    assert_allclose(annual_lic.resource_state(model.timestep), 0.0, atol=1e-7)
Exemplo n.º 12
0
def test_sdc_recorder():
    """
    Test the StorageDurationCurveRecorder
    """
    model = load_model("timeseries3.json")
    inpt = model.nodes['catchment1']
    strg = model.nodes['reservoir1']

    percentiles = np.linspace(20., 100., 5)
    flow_rec = NumpyArrayNodeRecorder(model, inpt)
    rec = StorageDurationCurveRecorder(model, strg, percentiles, sdc_agg_func="max", agg_func="min")

    # test retrieval of recorder
    assert model.recorders['storagedurationcurverecorder.reservoir1'] == rec

    model.run()

    # Manually calculate expected storage and percentiles
    strg_volume = strg.initial_volume + np.cumsum(flow_rec.data - 23.0, axis=0)
    strg_pciles = np.percentile(strg_volume, percentiles, axis=0)

    assert_allclose(rec.sdc, strg_pciles)
    assert_allclose(np.max(rec.sdc, axis=0), rec.values())
    assert_allclose(np.min(np.max(rec.sdc, axis=0)), rec.aggregated_value())

    assert rec.sdc.shape == (len(percentiles), len(model.scenarios.combinations))
    df = rec.to_dataframe()
    assert df.shape == (len(percentiles), len(model.scenarios.combinations))
Exemplo n.º 13
0
def test_timestamps():
    '''Test datetime related model parameters'''
    model = load_model('timeseries1.xml')
    
    assert(model.parameters['timestamp_start'] == pandas.to_datetime('1970-01-01'))
    assert(model.parameters['timestamp_finish'] == pandas.to_datetime('3027-08-22'))
    assert(model.parameters['timestep'] == datetime.timedelta(1))
Exemplo n.º 14
0
def test_run_cost1(solver):
    model = load_model("cost1.xml", solver=solver)

    supply1 = model.node["supply1"]
    supply2 = model.node["supply2"]
    demand1 = model.node["demand1"]

    assert_allclose(supply1.get_cost(None), 1)
    assert_allclose(supply2.get_cost(None), 2)  # more expensive

    result = model.step()
    # check entire demand was supplied by supply1
    assert_allclose(supply1.flow, 10.0, atol=1e-7)
    assert_allclose(supply2.flow, 0.0, atol=1e-7)
    assert_allclose(demand1.flow, 10.0, atol=1e-7)

    # increase demand to more than supply1 can provide on it's own
    # and check that supply2 is used to pick up the slack
    demand1.max_flow = 20.0
    result = model.step()
    assert_allclose(supply1.flow, 15.0, atol=1e-7)
    assert_allclose(supply2.flow, 5.0, atol=1e-7)
    assert_allclose(demand1.flow, 20.0, atol=1e-7)

    # supply as much as possible, even if it isn't enough
    demand1.max_flow = 40.0
    result = model.step()
    assert_allclose(supply1.flow, 15.0, atol=1e-7)
    assert_allclose(supply2.flow, 15.0, atol=1e-7)
    assert_allclose(demand1.flow, 30.0, atol=1e-7)
Exemplo n.º 15
0
def test_run_bottleneck():
    """Test max flow constraint on intermediate nodes is upheld"""
    model = load_model("bottleneck.json")
    result = model.step()
    d1 = model.nodes["demand1"]
    d2 = model.nodes["demand2"]
    assert_allclose(d1.flow + d2.flow, 15.0, atol=1e-7)
Exemplo n.º 16
0
def test_simple1():
    '''Test parsing a simple XML document'''
    # parse the XML into a model
    model = load_model('simple1.xml')

    # metadata
    assert(model.metadata['title'] == 'Simple 1')
    assert(model.metadata['description'] == 'A very simple example.')

    # node names
    nodes = model.nodes()
    assert(len(nodes) == 3)
    supply1 = model.node['supply1']
    link1 = model.node['link1']
    demand1 = model.node['demand1']

    # node types
    assert(type(supply1) is pywr.core.Supply)
    assert(type(link1) is pywr.core.Link)
    assert(type(demand1) is pywr.core.Demand)

    # node positions
    assert(supply1.position == (1,1))
    assert(link1.position == (2,1))
    assert(demand1.position == (3,1))

    # edges
    edges = model.graph.edges()
    assert(len(edges) == 2)
    assert((supply1, link1) in edges)
    assert((link1, demand1) in edges)

    model.check()
Exemplo n.º 17
0
def test_run_river1():
    """Test a river abstraction with a simple catchment"""
    model = load_model("river1.json")

    result = model.step()
    demand1 = model.nodes["demand1"]
    assert_allclose(demand1.flow, 5.0, atol=1e-7)
Exemplo n.º 18
0
def test_run_river1(solver):
    '''Test a river abstraction with a simple catchment'''
    model = load_model('river1.xml', solver=solver)

    result = model.step()
    demand1 = model.node['demand1']
    assert_allclose(demand1.flow, 5.0, atol=1e-7)
Exemplo n.º 19
0
def test_parameter_recorder_json(solver):
    model = load_model("parameter_recorder.json", solver=solver)
    rec_demand = model.recorders["demand_max_recorder"]
    rec_supply = model.recorders["supply_max_recorder"]
    model.run()
    assert_allclose(rec_demand.data, 10)
    assert_allclose(rec_supply.data, 15)
Exemplo n.º 20
0
def test_run_bottleneck(solver):
    """Test max flow constraint on intermediate nodes is upheld"""
    model = load_model("bottleneck.xml", solver=solver)
    result = model.step()
    d1 = model.node["demand1"]
    d2 = model.node["demand2"]
    assert_allclose(d1.flow + d2.flow, 15.0, atol=1e-7)
Exemplo n.º 21
0
def test_loss_link_node(loss_factor):
    """Test LossLink node"""
    model = load_model('loss_link.json')

    supply1 = model.nodes["supply1"]
    link1 = model.nodes["link1"]
    demand1 = model.nodes["demand1"]

    if loss_factor is not None:
        link1.loss_factor = loss_factor

    model.check()
    model.run()

    if loss_factor is None:
        expected_supply = 12
        expected_demand = 10
    elif loss_factor == 1.0:
        # 100% loss means no flow can be provided.
        expected_supply = 0.0
        expected_demand = 0.0
    else:
        expected_supply = 10 * (1 + loss_factor)
        expected_demand = 10

    # Supply must provide 20% more flow because of the loss in link1
    assert_allclose(supply1.flow, expected_supply)
    # link1 records the net flow after losses
    assert_allclose(link1.flow, expected_demand)
    assert_allclose(demand1.flow, expected_demand)
Exemplo n.º 22
0
    def test_json_load(self, solver):

        model = load_model("demand_saving.json", solver=solver)

        storage = model.nodes["supply1"]
        demand = model.nodes["demand1"]
        assert (isinstance(demand.max_flow, MonthlyProfileControlCurveParameter))

        model.setup()

        profile = np.array([1.0, 1.0, 1.0, 1.0, 1.2, 1.2, 1.2, 1.2, 1.0, 1.0, 1.0, 1.0]) * 10.0
        saving = np.array([
                    [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                    [0.8, 0.8, 0.8, 0.8, 0.7, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8]
                ])

        scenario_index = ScenarioIndex(0, np.array([], dtype=np.int32))

        for i in range(12):
            model.step()
            # First two timesteps should result in storage above 50% control curve
            # Therefore no demand saving
            if i < 2:
                expected_max_flow = profile[i] * saving[0, i]
            else:
                expected_max_flow = profile[i] * saving[1, i]

            value = demand.max_flow.value(model.timestepper.current, scenario_index)
            assert_allclose(value, expected_max_flow)
Exemplo n.º 23
0
def test_run_bottleneck():
    '''Test max flow constraint on intermediate nodes is upheld'''
    model = load_model('bottleneck.json')
    result = model.step()
    d1 = model.nodes['demand1']
    d2 = model.nodes['demand2']
    assert_allclose(d1.flow + d2.flow, 15.0, atol=1e-7)
Exemplo n.º 24
0
def test_reservoir_surface_area_without_area_property():
    """ Temporary test while the above test is not working. """
    model = load_model('reservoir_evaporation_without_area_property.json')
    model.timestepper.start = "1920-01-01"
    model.timestepper.end = "1920-01-02"
    res = model.run()
    assert_allclose(model.nodes["evaporation"].flow, 2.46875)
Exemplo n.º 25
0
def test_run_bottleneck(solver):
    '''Test max flow constraint on intermediate nodes is upheld'''
    model = load_model('bottleneck.xml', solver=solver)
    result = model.step()
    d1 = model.node['demand1']
    d2 = model.node['demand2']
    assert_allclose(d1.flow+d2.flow, 15.0, atol=1e-7)
Exemplo n.º 26
0
def test_timeseries_with_scenarios():

    model = load_model("timeseries2.json")

    model.setup()

    assert len(model.scenarios) == 1

    model.step()
    catchment1 = model.nodes["catchment1"]

    step1 = np.array(
        [21.64, 21.72, 23.97, 23.35, 21.79, 21.52, 21.21, 22.58, 26.19, 25.71],
        dtype=np.float64,
    )
    assert_allclose(catchment1.flow, step1)

    model.step()
    step2 = np.array(
        [20.03, 20.10, 22.18, 21.62, 20.17, 19.92, 19.63, 20.90, 24.24, 23.80],
        dtype=np.float64,
    )
    # Low tolerance because test values were truncated to 2 decimal places.
    assert_allclose(catchment1.flow, step2)

    model.finish()
Exemplo n.º 27
0
def test_timeseries_with_scenarios_hdf():
    # this test uses TablesArrayParameter
    model = load_model('timeseries2_hdf.json')

    model.setup()

    assert len(model.scenarios) == 1

    catchment1 = model.nodes['catchment1']

    model.step()
    step1 = np.array(
        [21.64, 21.72, 23.97, 23.35, 21.79, 21.52, 21.21, 22.58, 26.19, 25.71],
        dtype=np.float64)
    # Low tolerance because test values were truncated to 2 decimal places.
    assert_allclose(catchment1.flow, step1, atol=1e-1)

    model.step()
    step2 = np.array(
        [20.03, 20.10, 22.18, 21.62, 20.17, 19.92, 19.63, 20.90, 24.24, 23.80],
        dtype=np.float64)
    # Low tolerance because test values were truncated to 2 decimal places.
    assert_allclose(catchment1.flow, step2, atol=1e-1)

    model.finish()
Exemplo n.º 28
0
def test_run_license1():
    model = load_model('simple1.xml')
    
    model.timestamp = datetime.datetime(2015, 1, 1)
    
    # add licenses to supply node
    supply1 = model.node['supply1']
    daily_lic = pywr.licenses.DailyLicense(5)
    annual_lic = pywr.licenses.AnnualLicense(7)
    collection = pywr.licenses.LicenseCollection([daily_lic, annual_lic])
    supply1.licenses = collection
    
    model.check()
    
    # daily license is limit
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 5.0))
    
    # resource state is getting worse
    assert(annual_lic.resource_state(model.timestamp) < 1.0)
    
    # annual license is limit
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 2.0))
    
    # annual license is exhausted
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 0.0))
    assert(annual_lic.resource_state(model.timestamp) == 0.0)
Exemplo n.º 29
0
    def test_year_overlap(self):
        """test the the node works: end date < reset date < model start date. This
        means that the node's active period extends from one year to the next
        """

        model = load_model('seasonal_virtual_storage.json')

        vs = model.nodes["licence1"]
        vs.reset_day = 1
        vs.reset_month = 12
        vs.end_day = 31
        vs.end_month = 3
        model.timestepper.start = "2015-12-15"
        model.timestepper.end = "2016-12-02"

        model.run()
        supply_df = model.recorders["supply1"].to_dataframe()
        licence_df = model.recorders["licence1"].to_dataframe()

        # Start date is after reset data so there should be flow and volume should be reduced
        assert_allclose(supply_df.loc["2015-12-15", :], 10)
        assert_allclose(licence_df.loc["2015-12-15", :], 90)

        # Licence is depleted but remains active so flow is 0
        assert_allclose(supply_df.loc["2015-12-31", :], 0)
        assert_allclose(licence_df.loc["2015-12-31", :], 0)

        # License is turned off so flow is not constrained
        assert_allclose(supply_df.loc["2016-03-31", :], 10)

        # License is reset and made active
        assert_allclose(supply_df.loc["2016-12-01", :], 10)
        assert_allclose(licence_df.loc["2016-12-01", :], 90)
Exemplo n.º 30
0
    def test_start_deactivated(self):
        """test the the node is not active when:  model start date < reset date < end date
        """

        model = load_model('seasonal_virtual_storage.json')

        vs = model.nodes["licence1"]
        vs.reset_day = 1
        vs.reset_month = 2
        vs.end_day = 1
        vs.end_month = 3
        model.timestepper.start = "2015-01-01"
        model.timestepper.end = "2015-04-01"

        model.run()
        supply_df = model.recorders["supply1"].to_dataframe()
        licence_df = model.recorders["licence1"].to_dataframe()

        # Start date before reset date and end date so there should be flow but no reduction in node storage
        assert_allclose(supply_df.loc["2015-01-15", :], 10)
        assert_allclose(licence_df.loc["2015-01-15", :], 100)

        # Licence active but depleted so flow is 0
        assert_allclose(supply_df.loc["2015-2-15", :], 0)
        assert_allclose(licence_df.loc["2015-2-15", :], 0)

        # License is turned off so flow is not constrained
        assert_allclose(supply_df.loc["2015-03-01", :], 10)
Exemplo n.º 31
0
def test_run_cost1():
    model = load_model('cost1.xml')

    supply1 = model.node['supply1']
    supply2 = model.node['supply2']
    demand1 = model.node['demand1']

    assert (supply1.properties['cost'].value(None) == 1)
    assert (supply2.properties['cost'].value(None) == 2)  # more expensive

    result = model.step()
    # check entire demand was supplied by supply1
    assert (result[0:3] == ('optimal', 10.0, 10.0))
    assert (list(result[3].items()) == [((supply1, demand1), 10.0)])

    # increase demand to more than supply1 can provide on it's own
    # and check that supply2 is used to pick up the slack
    demand1.properties['demand'] = pywr.core.ParameterConstant(20.0)
    result = model.step()
    assert (result[0:3] == ('optimal', 20.0, 20.0))
    assert (result[3][(supply1, demand1)] == 15.0)
    assert (result[3][(supply2, demand1)] == 5.0)
    assert (not model.failure)

    # supply as much as possible, even if it isn't enough
    demand1.properties['demand'] = pywr.core.ParameterConstant(40.0)
    result = model.step()
    assert (result[0:3] == ('optimal', 40.0, 30.0))
    assert (model.failure)
Exemplo n.º 32
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-fit", nargs='?', default=False)
    parser.add_argument("-model", nargs='?', default="simple_model")
    parser.add_argument("-visualize", nargs='?', default=False)
    parser.add_argument("-evaluate", nargs='?')
    parser.add_argument("-dataset")
    parser.add_argument("-model_path", nargs='?')
    parser.add_argument("-visualize_heatmap", nargs='?')

    args = parser.parse_args()
    if args.dataset:
        if args.dataset == 'fashion_mnist':
            train_loader, test_loader = fashion_mnist_dataset.get_data_loaders(
            )
            visualize = fashion_mnist_dataset.visualize_dataset
        elif args.dataset == "dogs_cats":
            train_loader, test_loader = dogs_cats_dataset.get_data_loaders()
            visualize = dogs_cats_dataset.visualize_dataset
    if args.model:
        if args.model == 'simple_model':
            model = SimpleModel()
        if args.model == 'explain_model':
            model = ExplainModel()
    if args.fit:
        fit_classifier(model, train_loader, test_loader, args.model)
    elif args.visualize:
        visualize(train_loader)
    elif args.evaluate and args.model_path:
        model = load_model(model_path)
        evaluate(model, test_loader)
    elif args.visualize_heatmap and args.model_path:
        load_visualize_heatmap(args.model_path, test_loader)
Exemplo n.º 33
0
def test_run_river2(solver):
    """Test a river abstraction with two catchments, a confluence and a split"""
    model = load_model("river2.xml", solver=solver)

    result = model.step()
    assert result[0:3] == ("optimal", 12.0, 9.25)
    assert model.failure
Exemplo n.º 34
0
def test_run_river2():
    '''Test a river abstraction with two catchments, a confluence and a split'''
    model = load_model('river2.xml')
    
    result = model.step()
    assert(result[0:3] == ('optimal', 12.0, 9.25))
    assert(model.failure)
Exemplo n.º 35
0
def test_select_solver():
    """Test specifying the solver in JSON"""
    solver_names = [solver.name for solver in pywr.solvers.solver_registry]
    for solver_name in solver_names:
        data = '''{"metadata": {"minimum_version": "0.1"}, "nodes": {}, "edges": {}, "timestepper": {"start": "1990-01-01","end": "1999-12-31","timestep": 1}, "solver": {"name": "%s"}}''' % solver_name
        model = load_model(data=data)
        assert (model.solver.name.lower() == solver_name)
Exemplo n.º 36
0
def test_run_river1():
    '''Test a river abstraction with a simple catchment'''
    model = load_model('river1.xml')
    
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 5.0))
    assert(model.failure)
Exemplo n.º 37
0
def test_demand_saving_with_indexed_array_from_hdf():
    """Test demand saving based on a predefined demand saving level in a HDF file."""
    model = load_model("demand_saving_hdf.json")

    model.timestepper.end = pd.Timestamp("2016-01-31")

    rec_demand = NumpyArrayNodeRecorder(model, model.nodes["Demand"])
    rec_storage = NumpyArrayStorageRecorder(model, model.nodes["Reservoir"])

    model.check()
    model.run()

    max_volume = model.nodes["Reservoir"].max_volume

    # model starts with no demand saving
    demand_baseline = 50.0
    demand_saving = 1.0
    assert_allclose(rec_demand.data[0, 0], demand_baseline * demand_saving)

    # first control curve breached
    demand_saving = 0.8
    assert_allclose(rec_demand.data[11, 0], demand_baseline * demand_saving)

    # second control curve breached
    demand_saving = 0.5
    assert_allclose(rec_demand.data[12, 0], demand_baseline * demand_saving)

    # second control curve breached
    demand_saving = 0.25
    assert_allclose(rec_demand.data[13, 0], demand_baseline * demand_saving)
Exemplo n.º 38
0
def test_run_river1():
    '''Test a river abstraction with a simple catchment'''
    model = load_model('river1.json')

    result = model.step()
    demand1 = model.nodes['demand1']
    assert_allclose(demand1.flow, 5.0, atol=1e-7)
Exemplo n.º 39
0
def test_run_until_date():
    model = load_model('simple1.xml')
    
    # run until date
    model.reset()
    timesteps = model.run(until_date=pandas.to_datetime('2015-01-20'))
    assert(timesteps == 20)
Exemplo n.º 40
0
def test_run_cost1():
    model = load_model('cost1.xml')
    
    supply1 = model.node['supply1']
    supply2 = model.node['supply2']
    demand1 = model.node['demand1']
    
    assert(supply1.properties['cost'].value(None) == 1)
    assert(supply2.properties['cost'].value(None) == 2) # more expensive
    
    result = model.step()
    # check entire demand was supplied by supply1
    assert(result[0:3] == ('optimal', 10.0, 10.0))
    assert(list(result[3].items()) == [((supply1, demand1), 10.0)])
    
    # increase demand to more than supply1 can provide on it's own
    # and check that supply2 is used to pick up the slack
    demand1.properties['demand'] = pywr.core.ParameterConstant(20.0)
    result = model.step()
    assert(result[0:3] == ('optimal', 20.0, 20.0))
    assert(result[3][(supply1, demand1)] == 15.0)
    assert(result[3][(supply2, demand1)] == 5.0)
    assert(not model.failure)
    
    # supply as much as possible, even if it isn't enough
    demand1.properties['demand'] = pywr.core.ParameterConstant(40.0)
    result = model.step()
    assert(result[0:3] == ('optimal', 40.0, 30.0))
    assert(model.failure)
Exemplo n.º 41
0
def test_scenarios_from_json(json_file):
    """
    Test a simple model with two scenarios.

    The model varies in the inflow by "scenario A" and the demand
    by "scenario B". The test ensures the correct size of model is
    created, and uses a `NumpyArrayNodeRecorder` to check the output
    in multiple dimensions is correct. The latter is done using
    the `MultiIndex` on the `DataFrame` from the recorder.
    """

    model = load_model(json_file)
    assert len(model.scenarios) == 2

    model.setup()
    assert len(model.scenarios.combinations) == 20
    model.run()

    # Test the recorder data is correct
    df = model.recorders['demand1'].to_dataframe()

    assert df.shape[1] == 20
    assert df.columns.names[0] == 'scenario A'
    assert_equal(df.columns.levels[0], np.arange(10))
    assert df.columns.names[1] == 'scenario B'
    assert_equal(df.columns.levels[1], np.array(['First', 'Second']))
    # Data for first demand (B) ensemble
    d1 = df.xs('First', level='scenario B', axis=1).iloc[0, :].values
    assert_allclose(d1, [10]*10)
    # Data for second demand (B) ensemble
    d2 = df.xs('Second', level='scenario B', axis=1).iloc[0, :]
    assert_allclose(d2, [10, 11, 12, 13, 14]+[15]*5)
Exemplo n.º 42
0
def test_run_cost1():
    model = load_model('cost1.json')

    supply1 = model.nodes['supply1']
    supply2 = model.nodes['supply2']
    demand1 = model.nodes['demand1']

    assert_allclose(supply1.get_cost(None), 1)
    assert_allclose(supply2.get_cost(None), 2)  # more expensive

    result = model.step()
    # check entire demand was supplied by supply1
    assert_allclose(supply1.flow, 10.0, atol=1e-7)
    assert_allclose(supply2.flow, 0.0, atol=1e-7)
    assert_allclose(demand1.flow, 10.0, atol=1e-7)

    # increase demand to more than supply1 can provide on it's own
    # and check that supply2 is used to pick up the slack
    demand1.max_flow = 20.0
    result = model.step()
    assert_allclose(supply1.flow, 15.0, atol=1e-7)
    assert_allclose(supply2.flow, 5.0, atol=1e-7)
    assert_allclose(demand1.flow, 20.0, atol=1e-7)

    # supply as much as possible, even if it isn't enough
    demand1.max_flow = 40.0
    result = model.step()
    assert_allclose(supply1.flow, 15.0, atol=1e-7)
    assert_allclose(supply2.flow, 15.0, atol=1e-7)
    assert_allclose(demand1.flow, 30.0, atol=1e-7)
Exemplo n.º 43
0
def test_run_river1(solver):
    """Test a river abstraction with a simple catchment"""
    model = load_model("river1.xml", solver=solver)

    result = model.step()
    demand1 = model.node["demand1"]
    assert_allclose(demand1.flow, 5.0, atol=1e-7)
Exemplo n.º 44
0
def main(workdir, identifier, numtopics): 
    print("\n== evaluation ==")
    listcorpus = helpers.load_pickle(workdir, identifier, "allprepared.pickle")
    vectorcorpus = helpers.load_pickle(workdir, identifier, "vectorcorpus.pickle")
    model = helpers.load_model(workdir, identifier)
    resultsfolder = join(workdir, "results", identifier)
    model_coherence(listcorpus, vectorcorpus, model, numtopics, resultsfolder)
    topic_coherence(listcorpus, vectorcorpus, model, numtopics, resultsfolder)
Exemplo n.º 45
0
def test_run_discharge_upstream():
    '''Test river with inline discharge (upstream)
    
    In this instance the discharge is upstream of the abstraction, and so can
    be abstracted in the same way as the water from the catchment
    '''
    model = load_model('river_discharge1.xml')
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 8.0))
Exemplo n.º 46
0
def test_run_discharge_downstream():
    '''Test river with inline discharge (downstream)
    
    In this instance the discharge is downstream of the abstraction, so the
    water shouldn't be available.
    '''
    model = load_model('river_discharge2.xml')
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 5.0))
Exemplo n.º 47
0
def test_reset():
    """Test model reset"""
    model = load_model('license1.xml')
    supply1 = model.node['supply1']
    license_collection = supply1.licenses
    license = [lic for lic in license_collection._licenses if isinstance(lic, pywr.licenses.AnnualLicense)][0]
    assert(license.available(None) == 7.0)
    model.step()
    assert(license.available(None) == 2.0)
    model.reset()
    assert(license.available(None) == 7.0)
Exemplo n.º 48
0
def test_reset(solver):
    """Test model reset"""
    model = load_model("license1.xml", solver=solver)
    supply1 = model.node["supply1"]
    license_collection = supply1.licenses
    license = [lic for lic in license_collection._licenses if isinstance(lic, pywr.licenses.AnnualLicense)][0]
    assert_allclose(license.available(None), 7.0, atol=1e-7)
    model.step()
    assert_allclose(license.available(None), 2.0, atol=1e-7)
    model.reset()
    assert_allclose(license.available(None), 7.0, atol=1e-7)
Exemplo n.º 49
0
def test_run_reservoir1():
    '''Test a reservoir with no refill
    
    Without an additional supply the reservoir should empty and cause a failure.
    '''
    model = load_model('reservoir1.xml')

    for delivered in [10.0, 10.0, 10.0, 5.0, 0.0]:
        result = model.step()
        assert(result[0:3] == ('optimal', 10.0, delivered))
    assert(model.failure)
Exemplo n.º 50
0
def test_run_license_group():
    '''Test license groups'''
    model = load_model('groups1.xml')
    
    supply1 = model.node['supply1']
    supply2 = model.node['supply2']
    
    assert(len(model.group) == 2)
    
    result = model.step()
    assert(result[0:3] == ('optimal', 10.0, 6.0))
Exemplo n.º 51
0
def test_run_license_group(solver):
    '''Test license groups'''
    model = load_model('groups1.xml', solver=solver)

    supply1 = model.node['supply1']
    supply2 = model.node['supply2']

    assert(len(model.group) == 2)

    result = model.step()
    d1 = model.node['demand1']
    assert_allclose(d1.flow, 6.0, atol=1e-7)
Exemplo n.º 52
0
def test_run_license_group(solver):
    """Test license groups"""
    model = load_model("groups1.xml", solver=solver)

    supply1 = model.node["supply1"]
    supply2 = model.node["supply2"]

    assert len(model.group) == 2

    result = model.step()
    d1 = model.node["demand1"]
    assert_allclose(d1.flow, 6.0, atol=1e-7)
Exemplo n.º 53
0
def test_run_blender2(solver):
    '''Test blender constraint/component'''
    model = load_model('blender2.xml', solver=solver)

    blender = model.node['blender1']
    supply1 = model.node['supply1']
    supply2 = model.node['supply2']

    # test model results
    result = model.step()
    assert_allclose(result[3][(supply1, blender)], 3.0, atol=1e-7)
    assert_allclose(result[3][(supply2, blender)], 7.0, atol=1e-7)
Exemplo n.º 54
0
def test_run_discharge_downstream(solver):
    """Test river with inline discharge (downstream)

    In this instance the discharge is downstream of the abstraction, so the
    water shouldn't be available.
    """
    model = load_model("river_discharge2.xml", solver=solver)
    model.step()
    demand = model.node["demand1"]
    term = model.node["term1"]
    assert_allclose(demand.flow, 5.0, atol=1e-7)
    assert_allclose(term.flow, 3.0, atol=1e-7)
Exemplo n.º 55
0
def test_run_discharge_upstream(solver):
    """Test river with inline discharge (upstream)

    In this instance the discharge is upstream of the abstraction, and so can
    be abstracted in the same way as the water from the catchment
    """
    model = load_model("river_discharge1.xml", solver=solver)
    model.step()
    demand = model.node["demand1"]
    term = model.node["term1"]
    assert_allclose(demand.flow, 8.0, atol=1e-7)
    assert_allclose(term.flow, 0.0, atol=1e-7)
Exemplo n.º 56
0
def test_run_blender2(solver):
    """Test blender constraint/component"""
    model = load_model("blender2.xml", solver=solver)

    blender = model.node["blender1"]
    supply1 = model.node["supply1"]
    supply2 = model.node["supply2"]

    # test model results
    result = model.step()
    assert_allclose(result[3][(supply1, blender)], 3.0, atol=1e-7)
    assert_allclose(result[3][(supply2, blender)], 7.0, atol=1e-7)
Exemplo n.º 57
0
def test_run_reservoir1(solver):
    """Test a reservoir with no refill

    Without an additional supply the reservoir should empty and cause a failure.
    """
    model = load_model("reservoir1.xml", solver=solver)
    demand1 = model.node["demand1"]
    supply1 = model.node["supply1"]
    for demand, stored in [(10.0, 25.0), (10.0, 15.0), (10.0, 5.0), (5.0, 0.0), (0.0, 0.0)]:
        result = model.step()
        assert_allclose(demand1.flow, demand, atol=1e-7)
        assert_allclose(supply1.volume, stored, atol=1e-7)
Exemplo n.º 58
0
def test_run_blender2():
    '''Test blender constraint/component'''
    model = load_model('blender2.xml')

    blender = model.node['blender1']
    supply1 = model.node['supply1']
    supply2 = model.node['supply2']
    
    # test model results
    result = model.step()
    assert(result[3][(supply1, blender)] == 3.0)
    assert(result[3][(supply2, blender)] == 7.0)
Exemplo n.º 59
0
def test_run_until_failure():
    model = load_model('simple1.xml')
    
    # run until failure
    model.timestamp = pandas.to_datetime('2015-12-01')
    demand1 = model.node['demand1']
    def demand_func(node, timestamp):
        return timestamp.day
    demand1.properties['demand'] = pywr.core.ParameterFunction(demand1, demand_func)
    timesteps = model.run(until_failure=True)
    assert(model.failure)
    assert(timesteps == 16)
Exemplo n.º 60
0
def test_run_reservoir2():
    '''Test a reservoir fed by a river abstraction
    
    The river abstraction should refill the reservoir, but not quickly enough
    to keep up with the demand.
    '''
    model = load_model('reservoir2.xml')
    
    for demand, supply in [(10.0, 10.0), (20.0, 14.0), (26.0, 14.0), (32.0, 14.0), (38.0, 11.0), (41.0, 8.0), (41.0, 8.0)]:
        result = model.step()
        assert(result[0:3] == ('optimal', demand, supply))
    assert(model.failure)