Beispiel #1
0
    def test_attributes(self, months):
        interval_set = IntervalSet('months', months)
        interval_set.description = 'a descriptions'

        actual = interval_set.as_dict()
        expected = {'name': 'months', 'description': 'a descriptions'}
        assert actual == expected
Beispiel #2
0
    def test_intersection(self, months, seasons):
        month_set = IntervalSet('months', months)
        season_set = IntervalSet('seasons', seasons)

        actual = month_set.intersection(season_set.data[0])
        expected = [0, 1, 11]
        assert actual == expected
Beispiel #3
0
    def test_get_names(self, months):

        expected_names = \
            ['1_0', '1_1', '1_2', '1_3', '1_4', '1_5',
             '1_6', '1_7', '1_8', '1_9', '1_10', '1_11']
        interval_set = IntervalSet('months', months)
        actual_names = interval_set.get_entry_names()
        assert expected_names == actual_names
Beispiel #4
0
    def test_get_names(self, months):

        expected_names = \
            ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
             'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
        interval_set = IntervalSet('months', months)
        actual_names = interval_set.get_entry_names()
        assert expected_names == actual_names
Beispiel #5
0
 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 = TimeIntervalRegister()
     register.register(IntervalSet('months', months))
     with raises(ValueError):
         register.register(IntervalSet('months', months))
Beispiel #6
0
    def test_bounds_checker_fails(self, months):
        bounds = [(0, 8759), (8759, 8760)]

        expected = False

        time_set = IntervalSet('months', months)
        actual = time_set.check_interval_bounds_equal(bounds)

        assert actual == expected
Beispiel #7
0
    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)
Beispiel #8
0
    def test_get_proportion_month_in_season(self, months, seasons):
        month_set = IntervalSet('months', months)
        season_set = IntervalSet('seasons', seasons)

        winter = season_set.data[0]

        actual = month_set.get_proportion(0, winter)
        expected = 1
        assert actual == expected
Beispiel #9
0
    def test_get_proportion_season_in_month(self, months, seasons):
        month_set = IntervalSet('months', months)
        season_set = IntervalSet('seasons', seasons)

        january = month_set.data[0]

        actual = season_set.get_proportion(0, january)
        expected = 31 * 1 / (31 + 31 + 28)
        np.testing.assert_allclose(actual, expected)
Beispiel #10
0
    def test_remap_agg_proportions(self, months, remap_months):
        month_set = IntervalSet('months', months)
        remap_set = IntervalSet('remap_months', remap_months)

        to_interval = month_set.data[0]  # january

        actual = remap_set.get_proportion(0, to_interval)
        expected = 1.0333333333333
        np.testing.assert_allclose(actual, expected, rtol=1e-3)
Beispiel #11
0
    def test_convert_from_month_to_seasons(self, months, seasons, monthly_data,
                                           monthly_data_as_seasons):
        register = TimeIntervalRegister()
        register.register(IntervalSet('months', months))
        register.register(IntervalSet('seasons', seasons))

        actual = register.convert(monthly_data, 'months', 'seasons')
        expected = monthly_data_as_seasons
        assert np.allclose(actual, expected, rtol=1e-05, atol=1e-08)
Beispiel #12
0
    def test_bounds_checker(self, months):
        bounds = [(0, 1), (1, 2)]

        expected = True

        time_set = IntervalSet('months', months)
        actual = time_set.check_interval_bounds_equal(bounds)

        assert actual == expected
Beispiel #13
0
    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)
Beispiel #14
0
    def test_remap_months_to_timeslices(self, months, monthly_data,
                                        remap_months, remap_month_data):
        register = TimeIntervalRegister()
        register.register(IntervalSet('months', months))
        register.register(IntervalSet('remap_months', remap_months))

        actual = register.convert(monthly_data, 'months', 'remap_months')
        expected = remap_month_data

        assert np.allclose(actual, expected, rtol=1e-05, atol=1e-08)
Beispiel #15
0
    def test_remap_disagg_proportions(self, months, remap_months):
        """Find proportion of January in cold_month, split over year
        """
        month_set = IntervalSet('months', months)
        remap_set = IntervalSet('remap_months', remap_months)

        to_interval = remap_set.data[0]  # cold month

        actual = month_set.get_proportion(0, to_interval)
        expected = 0.33333
        np.testing.assert_allclose(actual, expected, rtol=1e-3)
Beispiel #16
0
    def test_convert_from_hour_to_day(self, twenty_four_hours, one_day):

        data = np.ones(24)

        register = TimeIntervalRegister()
        register.register(IntervalSet('hourly_day', twenty_four_hours))
        register.register(IntervalSet('one_day', one_day))

        actual = register.convert(data, 'hourly_day', 'one_day')
        expected = np.array([24])

        assert np.allclose(actual, expected, rtol=1e-05, atol=1e-08)
Beispiel #17
0
    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
Beispiel #18
0
 def test_validate_intervals_fails(self, remap_months):
     data = remap_months
     data.append({'id': '5', 'start': 'PT0H', 'end': 'PT1H'})
     with raises(ValueError) as excinfo:
         IntervalSet('remap_months', data)
     assert "Duplicate entry for hour 0 in interval set remap_months." in str(
         excinfo.value)
Beispiel #19
0
    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)
Beispiel #20
0
    def test_months_load(self, months):
        """Pass a monthly time-interval definition into the register

        """
        register = TimeIntervalRegister()
        register.register(IntervalSet('months', months))

        actual = register.get_entry('months')

        expected_names = \
            ['1_0', '1_1', '1_2', '1_3', '1_4', '1_5',
             '1_6', '1_7', '1_8', '1_9', '1_10', '1_11']

        expected = [Interval('1_0', ('P0M', 'P1M')),
                    Interval('1_1', ('P1M', 'P2M')),
                    Interval('1_2', ('P2M', 'P3M')),
                    Interval('1_3', ('P3M', 'P4M')),
                    Interval('1_4', ('P4M', 'P5M')),
                    Interval('1_5', ('P5M', 'P6M')),
                    Interval('1_6', ('P6M', 'P7M')),
                    Interval('1_7', ('P7M', 'P8M')),
                    Interval('1_8', ('P8M', 'P9M')),
                    Interval('1_9', ('P9M', 'P10M')),
                    Interval('1_10', ('P10M', 'P11M')),
                    Interval('1_11', ('P11M', 'P12M'))]

        for name, interval in zip(expected_names, expected):
            assert actual.data[name] == interval
Beispiel #21
0
    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
Beispiel #22
0
 def test_validate_intervals_fails(self, remap_months):
     data = remap_months
     data.append({'name': '5', 'interval': [('PT0H', 'PT1H')]})
     with raises(ValueError) as ex:
         IntervalSet('remap_months', data)
     assert "Duplicate entry for hour 0 in interval set remap_months." in str(
         ex.value)
Beispiel #23
0
def interval_set():
    """Return an interval set of the four seasons
    """
    seasons = [{
        'id': 'winter',
        'start': 'P0M',
        'end': 'P2M'
    }, {
        'id': 'spring',
        'start': 'P2M',
        'end': 'P5M'
    }, {
        'id': 'summer',
        'start': 'P5M',
        'end': 'P8M'
    }, {
        'id': 'autumn',
        'start': 'P8M',
        'end': 'P11M'
    }, {
        'id': 'winter',
        'start': 'P11M',
        'end': 'P12M'
    }]

    return IntervalSet('seasons', seasons)
Beispiel #24
0
    def test_remap_interval_load(self, remap_months):
        register = TimeIntervalRegister()

        intervals = IntervalSet('remap_months', remap_months)

        register.register(intervals)

        actual = register.get_entry('remap_months')

        assert actual == intervals
Beispiel #25
0
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()))
Beispiel #26
0
def load_interval_sets(handler):
    """Loads the time-interval sets into the project registries

    Parameters
    ----------
    handler: :class:`smif.data_layer.DataInterface`

    """
    interval_definitions = handler.read_interval_definitions()
    for interval_def in interval_definitions:
        interval_name = interval_def['name']
        LOGGER.info("Reading in interval definition %s", interval_name)
        interval_data = handler.read_interval_definition_data(interval_name)
        interval_set = IntervalSet(interval_name, interval_data)
        INTERVALS.register(interval_set)
Beispiel #27
0
    def load_interval_sets(self, interval_sets):
        """Loads the time-interval sets into the system-of-system model

        Parameters
        ----------
        interval_sets: list
            A dict, where key is the name of the interval set, and the value
            the data
        """
        interval_set_definitions = interval_sets.items()
        if len(interval_set_definitions) == 0:
            msg = "No interval sets have been defined"
            self.logger.warning(msg)

        for name, data in interval_set_definitions:
            interval_set = IntervalSet(name, data)
            self.model_run.intervals.register(interval_set)
Beispiel #28
0
    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
Beispiel #29
0
    def test_interval_loads(self):
        """Pass a time-interval definition into the register

        """
        data = [{'id': '1_1', 'start': 'PT0H', 'end': 'PT1H'}]

        register = TimeIntervalRegister()
        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 = OrderedDict()
        expected['1_1'] = element

        assert actual.data == expected
Beispiel #30
0
    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])