def test_serialise(self, regions): rset = RegionSet('test', regions) rset.description = 'my description' actual = rset.as_dict() expected = {'name': 'test', 'description': 'my description'} assert actual == expected
def setup_registers(setup_region_data): """One-time setup: load all the fixture region and interval sets into the module-level registers. """ regions = get_region_register() lsoa = RegionSet('LSOA', setup_region_data['features']) regions.register(lsoa) regions.register(regions_half_squares()) regions.register(regions_single_half_square()) regions.register(regions_half_triangles()) regions.register(regions_rect()) # register alt rect (same area) regions_rect_alt = copy(regions_rect()) regions_rect_alt.name = 'rect_alt' regions.register(regions_rect_alt) intervals = get_interval_register() annual_data = [{'id': 1, 'start': 'P0Y', 'end': 'P1Y'}] intervals.register(IntervalSet('annual', annual_data)) intervals.register(IntervalSet('months', months())) intervals.register(IntervalSet('seasons', seasons())) intervals.register(IntervalSet('hourly_day', twenty_four_hours())) intervals.register(IntervalSet('one_day', one_day())) intervals.register(IntervalSet('remap_months', remap_months()))
def regions_half_squares(): """Return two adjacent square regions:: |```|```| | A | B | |...|...| """ return RegionSet('half_squares', [ { 'type': 'Feature', 'properties': { 'name': 'a' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0]]] } }, { 'type': 'Feature', 'properties': { 'name': 'b' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 1], [0, 2], [1, 2], [1, 1]]] } }, ])
def test_must_have_unique_names(self): with raises(AssertionError) as ex: RegionSet('test', [ { 'type': 'Feature', 'properties': { 'name': 'a' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0]]] } }, { 'type': 'Feature', 'properties': { 'name': 'a' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 1], [0, 2], [1, 2], [1, 1]]] } }, ]) assert 'Region set must have uniquely named regions' in str(ex)
def test_create(self, regions): rset = RegionSet('test', regions) assert rset.name == 'test' assert len(rset) == 3 assert rset[0].name == 'unit' assert rset[1].name == 'half' assert rset[2].name == 'two'
def regions_half_triangles(): """Return regions split diagonally:: |``````/| | 0 / 1 | |/......| """ return RegionSet('half_triangles', [ { 'type': 'Feature', 'properties': { 'name': 'zero' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 2], [1, 0]]] } }, { 'type': 'Feature', 'properties': { 'name': 'one' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 2], [1, 2], [1, 0]]] } }, ])
def region_set(): """Return a region set of two square regions:: |```|```| | a | b | |...|...| """ rset = RegionSet('half_squares', [ { 'type': 'Feature', 'properties': {'name': 'a'}, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0]]] } }, { 'type': 'Feature', 'properties': {'name': 'b'}, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 1], [0, 2], [1, 2], [1, 1]]] } }, ]) return rset
def load_region_sets(handler): """Loads the region sets into the project registries Parameters ---------- handler: :class:`smif.data_layer.DataInterface` """ region_definitions = handler.read_region_definitions() for region_def in region_definitions: region_name = region_def['name'] LOGGER.info("Reading in region definition %s", region_name) region_data = handler.read_region_definition_data(region_name) region_set = RegionSet(region_name, region_data) REGIONS.register(region_set)
def regions_rect(): """Return single region covering 2x1 area:: |```````| | 0 | |.......| """ return RegionSet('rect', [{ 'type': 'Feature', 'properties': { 'name': 'zero' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 2], [1, 2], [1, 0]]] } }])
def regions_single_half_square(): """Return single half-size square region:: |```| | A | |...| """ return RegionSet('single_half_square', [{ 'type': 'Feature', 'properties': { 'name': 'a' }, 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0]]] } }])
def load_region_sets(self, region_sets): """Loads the region sets into the system-of-system model Parameters ---------- region_sets: list A dict, where key is the name of the region set, and the value the data """ assert isinstance(region_sets, dict) region_set_definitions = region_sets.items() if len(region_set_definitions) == 0: msg = "No region sets have been defined" self.logger.warning(msg) for name, data in region_set_definitions: msg = "Region set data is not a list" assert isinstance(data, list), msg self.model_run.regions.register(RegionSet(name, data))
def test_scenario_dependency(self, get_sos_model_config, setup_region_data): """Expect successful build with dependency on scenario data Should raise error if no spatial or temporal sets are defined """ config = get_sos_model_config config["sector_model_data"][0]["inputs"] = [{ 'name': 'raininess', 'spatial_resolution': 'blobby', 'temporal_resolution': 'annual', 'units': 'ml' }] builder = SosModelBuilder() with raises(ValueError): builder.construct(config, [2010, 2011, 2012]) builder.region_register.register( RegionSet('blobby', setup_region_data['features'])) interval_data = [{'id': 'ultra', 'start': 'P0Y', 'end': 'P1Y'}] builder.interval_register.register(IntervalSet('mega', interval_data)) builder.construct(config, [2010, 2011, 2012])
def test_get_names(self, regions): rset = RegionSet('test', regions) actual = rset.get_entry_names() expected = ['unit', 'half', 'two'] assert actual == expected
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)