def test_months_load(self, months): """Pass a monthly time-interval definition into the register """ register = NDimensionalRegister() register.register(IntervalSet('months', months)) actual = register.get_entry('months') expected = [ Interval('jan', ('P0M', 'P1M')), Interval('feb', ('P1M', 'P2M')), Interval('mar', ('P2M', 'P3M')), Interval('apr', ('P3M', 'P4M')), Interval('may', ('P4M', 'P5M')), Interval('jun', ('P5M', 'P6M')), Interval('jul', ('P6M', 'P7M')), Interval('aug', ('P7M', 'P8M')), Interval('sep', ('P8M', 'P9M')), Interval('oct', ('P9M', 'P10M')), Interval('nov', ('P10M', 'P11M')), Interval('dec', ('P11M', 'P12M')) ] for idx, interval in enumerate(expected): assert actual.data[idx] == interval
def test_interval_load_duplicate_name_raises(self, months): """Tests that error is raised if a duplicate name is used for an interval set """ register = NDimensionalRegister() register.register(IntervalSet('months', months)) with raises(ValueError): register.register(IntervalSet('months', months))
def test_coeff(self, months, seasons, month_to_season_coefficients): register = NDimensionalRegister() register.register(IntervalSet('months', months)) register.register(IntervalSet('seasons', seasons)) actual = register.get_coefficients('months', 'seasons') expected = month_to_season_coefficients assert np.allclose(actual, expected, rtol=1e-05, atol=1e-08)
def test_remap_interval_load(self, remap_months): register = NDimensionalRegister() intervals = IntervalSet('remap_months', remap_months) register.register(intervals) actual = register.get_entry('remap_months') assert actual == intervals
def test_remap_months_to_mapped_months(self, months, monthly_data, remap_months, remap_month_data): register = NDimensionalRegister() register.register(IntervalSet('months', months)) register.register(IntervalSet('remap_months', remap_months)) actual = register.convert(monthly_data, 'months', 'remap_months') expected = remap_month_data np.testing.assert_allclose(actual, expected, rtol=1e-05, atol=1e-08)
def register(regions_half_triangles, regions_half_squares, regions_single_half_square, regions_rect): """Region register with regions pre-registered """ register = NDimensionalRegister() register.register(RegionSet('half_triangles', regions_half_triangles)) register.register(RegionSet('half_squares', regions_half_squares)) register.register( RegionSet('single_half_square', regions_single_half_square)) register.register(RegionSet('rect', regions_rect)) alt = copy(regions_rect) register.register(RegionSet('rect_alt', alt)) return register
def test_conversion_with_different_coverage_fails(self, one_year, one_day, caplog): register = NDimensionalRegister() register.register(IntervalSet("one_year", one_year)) register.register(IntervalSet("one_day", one_day)) data = np.array([[1]]) register.convert(data, 'one_year', 'one_day') expected = "Coverage for 'one_year' is 8760 and does not match " \ "coverage for 'one_day' which is 24" assert expected in caplog.text
def test_resample_mapped_months_to_months(self, months, remap_months): """Converts from remapped month data (where one average month is used for each season) back to months """ register = NDimensionalRegister() register.register(IntervalSet('remap_months', remap_months)) register.register(IntervalSet('months', months)) data = np.array([[1, 1, 1, 1]]) actual = register.convert(data, 'remap_months', 'months') expected = np.array([[ 1.033333, 0.933333, 1.01087, 0.978261, 1.01087, 0.978261, 1.01087, 1.01087, 0.989011, 1.021978, 0.989011, 1.033333 ]]) np.testing.assert_allclose(actual, expected, rtol=1e-3)
def test_interval_loads(self): """Pass a time-interval definition into the register """ data = [{'name': '1_1', 'interval': [('PT0H', 'PT1H')]}] register = NDimensionalRegister() register.register(IntervalSet('energy_supply_hourly', data)) assert register.names == ['energy_supply_hourly'] actual = register.get_entry('energy_supply_hourly') element = Interval('1_1', ('PT0H', 'PT1H'), base_year=2010) expected = [] expected.append(element) assert actual.data == expected
def generate_coefficients(self, from_spec, to_spec) -> np.ndarray: """Generate conversion coefficients for interval dimensions Assumes that the Coordinates elements contain an 'interval' key whose value corresponds to :class:`Interval` data, that is a `{'name': interval_id, 'interval': list of interval extents}`. For example, intervals covering each hour of a period :: { 'name': 'first_hour', 'interval': [('PT0H', 'PT1H')] } { 'name': ''second_hour', 'interval': [('PT1H', 'PT2H')] } ... Or intervals corresponding to repeating hours for each day of a period :: { 'name': midnight', 'interval': [ ('PT0H', 'PT1H'), ('PT24H', 'PT25H'), ('PT48H', 'PT49H'), ... ] }, { 'name': ''one_am', 'interval': [ ('PT1H', 'PT2H'), ('PT25H', 'PT26H'), ('PT49H', 'PT50H'), ... ] } ... """ # find dimensions to convert from_dim, to_dim = self.get_convert_dims(from_spec, to_spec) # get dimension coordinates from_coords = from_spec.dim_coords(from_dim) to_coords = to_spec.dim_coords(to_dim) # create IntervalSets from Coordinates from_set = IntervalSet(from_dim, from_coords.elements) to_set = IntervalSet(to_dim, to_coords.elements) # register IntervalSets register = NDimensionalRegister() register.register(from_set) register.register(to_set) # use NDimensionalRegister to get coefficients coefficients = register.get_coefficients(from_dim, to_dim) return coefficients
def generate_coefficients(self, from_spec, to_spec): """Generate conversion coefficients for spatial dimensions Assumes that the Coordinates elements contain a 'feature' key whose value corresponds to a GDAL vector feature represented as a dict, for example as returned by a `fiona` reader. """ # find dimensions to convert from_dim, to_dim = self.get_convert_dims(from_spec, to_spec) # get dimension coordinates from_coords = from_spec.dim_coords(from_dim) to_coords = to_spec.dim_coords(to_dim) # create RegionSets from Coordinates from_set = RegionSet(from_dim, from_coords.elements) to_set = RegionSet(to_dim, to_coords.elements) # register RegionSets register = NDimensionalRegister() register.register(from_set) register.register(to_set) # use NDimensionalRegister to get coefficients coefficients = register.get_coefficients(from_dim, to_dim) return coefficients