def test_scenario_data_missing_param_interval( self, setup_region_data, ): data = { "length": [{ 'value': 3.14, 'region': 'oxford', 'interval': 1, 'year': 2015 }, { 'value': 3.14, 'region': 'oxford', 'interval': 'extra', 'year': 2015 }] } builder = SosModelBuilder() msg = "Interval 'extra' not defined in set 'annual' for parameter 'length'" with raises(ValueError) as ex: builder.load_scenario_models([{ 'name': 'length', 'spatial_resolution': 'LSOA', 'temporal_resolution': 'annual', 'units': 'm' }], data, [2015]) assert msg in str(ex)
def test_scenario_data_missing_param_mapping(self): data = {"length": [{'value': 3.14, 'year': 2015}]} builder = SosModelBuilder() msg = "Parameter 'bla_length' in scenario definitions not registered in scenario data" with raises(ValueError) as ex: builder.load_scenario_models([{ 'name': 'bla_length', 'spatial_resolution': 'LSOA', 'temporal_resolution': 'annual', 'units': 'm' }], data, [2015]) assert msg in str(ex.value)
def get_sos_model_with_summed_dependency(setup_region_data): builder = SosModelBuilder() builder.load_scenario_models( [{ 'name': 'raininess', 'temporal_resolution': 'annual', 'spatial_resolution': 'LSOA', 'units': 'ml' }], { "raininess": [{ 'year': 2010, 'value': 3, 'region': 'oxford', 'interval': 1 }] }, [2010, 2011, 2012]) sos_model = builder.finish() region_register = sos_model.regions interval_register = sos_model.intervals raininess_model = sos_model.models['raininess'] ws = WaterSupplySectorModel('water_supply') ws.add_input('raininess', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'ml') ws.add_output('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') ws.add_dependency(raininess_model, 'raininess', 'raininess') sos_model.add_model(ws) ws2 = WaterSupplySectorModel('water_supply_2') ws2.add_input('raininess', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'ml') ws2.add_output('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') ws2.add_dependency(raininess_model, 'raininess', 'raininess') sos_model.add_model(ws2) ws3 = WaterSupplySectorModel('water_supply_3') ws3.add_input('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') # TODO implement summed dependency # ws3.add_dependency(ws, 'water', 'water') ws3.add_dependency(ws2, 'water', 'water') sos_model.add_model(ws3) return sos_model
def test_scenario_data_missing_year( self, setup_region_data, ): data = {"length": [{'value': 3.14}]} builder = SosModelBuilder() msg = "Scenario data item missing year" with raises(ValueError) as ex: builder.load_scenario_models([{ 'name': 'length', 'spatial_resolution': 'LSOA', 'temporal_resolution': 'annual', 'units': 'm' }], data, [2015]) assert msg in str(ex.value)
def test_scenario_data_defaults(self, setup_region_data): data = { "length": [{ 'year': 2015, 'interval': 1, 'value': 3.14, 'region': 'oxford' }] } expected = np.array([[[3.14]]]) builder = SosModelBuilder() builder.load_scenario_models([{ 'name': 'length', 'spatial_resolution': 'LSOA', 'temporal_resolution': 'annual', 'units': 'm' }], data, [2015]) assert builder.sos_model.models['length']._data == expected
def get_sos_model_with_summed_dependency(oxford_region): scenario_model = get_scenario_model_object builder = SosModelBuilder() builder.load_scenario_models([scenario_model]) sos_model = builder.finish() region_register = sos_model.regions interval_register = sos_model.intervals raininess_model = sos_model.models['raininess'] ws = WaterSupplySectorModel('water_supply') ws.add_input('raininess', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'ml') ws.add_output('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') ws.add_dependency(raininess_model, 'raininess', 'raininess') sos_model.add_model(ws) ws2 = WaterSupplySectorModel('water_supply_2') ws2.add_input('raininess', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'ml') ws2.add_output('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') ws2.add_dependency(raininess_model, 'raininess', 'raininess') sos_model.add_model(ws2) ws3 = WaterSupplySectorModel('water_supply_3') ws3.add_input('water', region_register.get_entry('LSOA'), interval_register.get_entry('annual'), 'Ml') # TODO implement summed dependency # ws3.add_dependency(ws, 'water', 'water') ws3.add_dependency(ws2, 'water', 'water') sos_model.add_model(ws3) return sos_model
def test_nest_scenario_data(self, setup_country_data): data = { "mass": [{ 'year': 2015, 'region': 'GB', 'interval': 'wet_season', 'value': 3 }, { 'year': 2015, 'region': 'GB', 'interval': 'dry_season', 'value': 5 }, { 'year': 2015, 'region': 'NI', 'interval': 'wet_season', 'value': 1 }, { 'year': 2015, 'region': 'NI', 'interval': 'dry_season', 'value': 2 }, { 'year': 2016, 'region': 'GB', 'interval': 'wet_season', 'value': 4 }, { 'year': 2016, 'region': 'GB', 'interval': 'dry_season', 'value': 6 }, { 'year': 2016, 'region': 'NI', 'interval': 'wet_season', 'value': 1 }, { 'year': 2016, 'region': 'NI', 'interval': 'dry_season', 'value': 2.5 }] } expected = np.array( [ # 2015 [ # GB [3, 5], # NI [1, 2] ], # 2016 [ # GB [4, 6], # NI [1, 2.5] ] ], dtype=float) builder = SosModelBuilder() interval_data = [ { 'id': 'wet_season', 'start': 'P0M', 'end': 'P5M' }, { 'id': 'dry_season', 'start': 'P5M', 'end': 'P10M' }, { 'id': 'wet_season', 'start': 'P10M', 'end': 'P1Y' }, ] builder.interval_register.register( IntervalSet('seasonal', interval_data)) builder.region_register.register( RegionSet('country', setup_country_data['features'])) builder.load_scenario_models([{ 'name': 'mass', 'spatial_resolution': 'country', 'temporal_resolution': 'seasonal', 'units': 'kg' }], data, [2015, 2016]) actual = builder.sos_model.models['mass']._data print(actual) print(expected) assert np.allclose(actual, expected)