def forecast_hr_begin(site_metadata): return default_forecast(site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1h'), interval_label='beginning')
def test_get_data_start_end_labels_obs_longer_than_1h(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('2h')) forecast = default_forecast(site_metadata, run_length=pd.Timedelta('5min')) run_time = pd.Timestamp('20190422T1945Z') # obs interval cannot be longer than 1 hr with pytest.raises(ValueError) as excinfo: utils.get_data_start_end(observation, forecast, run_time) assert 'observation.interval_length <= 1h' in str(excinfo.value)
def test_run_persistence_fx_too_short(session, site_metadata, obs_5min_begin): forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1min'), run_length=pd.Timedelta('3min'), interval_label='beginning') issue_time = pd.Timestamp('20190423T2300Z') run_time = pd.Timestamp('20190422T1945Z') with pytest.raises(ValueError) as excinfo: main.run_persistence(session, obs_5min_begin, forecast, run_time, issue_time) assert 'requires observation.interval_length' in str(excinfo.value)
def test_get_data_start_end_labels_subhourly_window_limit(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='beginning') forecast = default_forecast( site_metadata, run_length=pd.Timedelta('5min'), # test subhourly limit on window interval_label='beginning') run_time = pd.Timestamp('20190422T1945Z') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp('20190422T1940Z') assert data_end == pd.Timestamp('20190422T1945Z')
def test_get_forecast_start_end_time_instant(site_metadata): # instant forecast = default_forecast( site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('1h'), interval_label='instant') issue_time = pd.Timestamp('20190422T0500') fx_start, fx_end = main.get_forecast_start_end(forecast, issue_time) assert fx_start == pd.Timestamp('20190422T0600') assert fx_end == pd.Timestamp('20190422T065959')
def test_get_data_start_end_labels_obs_avg_fx_instant(site_metadata): run_time = pd.Timestamp('20190422T1945Z') observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='ending') forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('1d'), interval_label='instant') with pytest.raises(ValueError) as excinfo: utils.get_data_start_end(observation, forecast, run_time) assert 'made from interval average obs' in str(excinfo.value)
def test_run_persistence_incompatible_instant_fx(session, site_metadata, obs_5min_begin): forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1h'), interval_label='instantaneous') issue_time = pd.Timestamp('20190423T2300Z') run_time = pd.Timestamp('20190422T1945Z') with pytest.raises(ValueError) as excinfo: main.run_persistence(session, obs_5min_begin, forecast, run_time, issue_time) assert 'instantaneous forecast' in str(excinfo.value).lower()
def test_get_data_start_end_labels_1h_window_limit(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='beginning') forecast = default_forecast( site_metadata, run_length=pd.Timedelta('12h'), # test 1 hr limit on window interval_label='beginning') # ensure data no later than run time run_time = pd.Timestamp('20190422T1945Z') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp('20190422T1845Z') assert data_end == pd.Timestamp('20190422T1945Z')
def test_get_data_start_end_labels_obs_instant_fx_avg_intraday(site_metadata): run_time = pd.Timestamp('20190422T1945Z') observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='instant') forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('15min'), interval_label='ending') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp('20190422T193001Z') assert data_end == pd.Timestamp('20190422T1945Z')
def test_get_data_start_end_labels_obs_fx_instant_mismatch(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='instant') forecast = default_forecast( site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), # interval_length must be equal run_length=pd.Timedelta('1d'), interval_label='instant') # if interval_label also instant run_time = pd.Timestamp('20190422T1945Z') with pytest.raises(ValueError) as excinfo: utils.get_data_start_end(observation, forecast, run_time) assert 'with identical interval length' in str(excinfo.value)
def test_run_persistence_interval_not_midnight_to_midnight( session, site_metadata, obs_5min_begin): # not midnight to midnight forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=22), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('24h'), interval_label='beginning') issue_time = pd.Timestamp('20190423T2200Z') run_time = pd.Timestamp('20190422T1945Z') with pytest.raises(ValueError) as excinfo: main.run_persistence(session, obs_5min_begin, forecast, run_time, issue_time) assert 'midnight to midnight' in str(excinfo.value)
def test_get_data_start_end_labels_obs_instant_fx_avg(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='instant') forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('1d'), interval_label='beginning') run_time = pd.Timestamp('20190422T1945Z') issue_time = pd.Timestamp('20190422T2300Z') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time, issue_time) assert data_start == pd.Timestamp('20190421T0000Z') assert data_end == pd.Timestamp('20190421T235959Z')
def test_get_data_start_end_labels_obs_fx_instant(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='instant') forecast = default_forecast( site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), # interval_length must be equal run_length=pd.Timedelta('1d'), interval_label='instant') # if interval_label also instant run_time = pd.Timestamp('20190422T1945Z') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp('20190421T0000Z') assert data_end == pd.Timestamp('20190421T235959Z')
def test_run_persistence_interval_index(session, site_metadata, obs_5min_begin): # index=True not supported for day ahead forecast = default_forecast( site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('24h'), interval_label='beginning') issue_time = pd.Timestamp('20190423T2300Z') run_time = pd.Timestamp('20190422T1945Z') with pytest.raises(ValueError) as excinfo: main.run_persistence(session, obs_5min_begin, forecast, run_time, issue_time, index=True) assert 'index=True not supported' in str(excinfo.value)
def test_get_forecast_start_end_time_instant(site_metadata, adjust_for_interval_label): # instant forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('1h'), interval_label='instant') issue_time = pd.Timestamp('20190422T0500') fx_start, fx_end = utils.get_forecast_start_end(forecast, issue_time, adjust_for_interval_label) assert fx_start == pd.Timestamp('20190422T0600') fx_end_exp = pd.Timestamp('20190422T0700') if adjust_for_interval_label: fx_end_exp -= pd.Timedelta('1n') assert fx_end == fx_end_exp
def test_get_data_start_end_labels_obs_longer_than_1h_day_ahead(site_metadata): observation = default_observation(site_metadata, interval_length=pd.Timedelta('2h'), interval_label='beginning') forecast = default_forecast( site_metadata, issue_time_of_day=dt.time(hour=5), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1d'), # day ahead interval_label='beginning') run_time = pd.Timestamp('20190422T1945Z') # day ahead doesn't care about obs interval length data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp('20190421T0000Z') assert data_end == pd.Timestamp('20190422T0000Z')
def test_run_persistence_scalar(session, site_metadata, obs_5min_begin, interval_label, mocker): run_time = pd.Timestamp('20190101T1945Z') # intraday, index=False forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1h'), interval_label=interval_label) issue_time = pd.Timestamp('20190101T2300Z') mocker.spy(main.persistence, 'persistence_scalar') out = main.run_persistence(session, obs_5min_begin, forecast, run_time, issue_time) assert isinstance(out, pd.Series) assert len(out) == 1 assert main.persistence.persistence_scalar.call_count == 1
def test_check_persistence_compatibility(obs_kw, fx_kw, index, site_metadata): obs_dict = { 'interval_label': 'ending', 'interval_value_type': 'interval_mean', 'interval_length': pd.Timedelta('30min') } fx_dict = { 'interval_label': 'ending', 'interval_value_type': 'interval_mean', 'interval_length': pd.Timedelta('1h'), 'run_length': pd.Timedelta('12h') } obs_dict.update(obs_kw) fx_dict.update(fx_kw) obs = default_observation(site_metadata, **obs_dict) fx = default_forecast(site_metadata, **fx_dict) with pytest.raises(ValueError): utils.check_persistence_compatibility(obs, fx, index)
def test_get_data_start_end_time_tz(site_metadata, variable, rl, issue, run, expected_start, expected_end): observation = default_observation(site_metadata, variable=variable, interval_length=pd.Timedelta('5min'), interval_label='ending') forecast = default_forecast(site_metadata, variable=variable, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta(rl), interval_label='beginning') data_start, data_end = utils.get_data_start_end(observation, forecast, pd.Timestamp(run), pd.Timestamp(issue)) assert data_start == pd.Timestamp(expected_start) assert data_end == pd.Timestamp(expected_end)
def test_get_data_start_end_time_dayahead(site_metadata, rl, rt, lt, expected_start, expected_end): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='beginning') run_time = pd.Timestamp(rt) issue_time = pd.Timestamp('20190410T2300Z') forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta(lt), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta(rl), interval_label='beginning') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time, issue_time) assert data_start == pd.Timestamp(expected_start) assert data_end == pd.Timestamp(expected_end)
def test_get_data_start_end_labels_obs_fx_instant(site_metadata, lead, issue, it): observation = default_observation(site_metadata, interval_length=pd.Timedelta('5min'), interval_label='instant') # interval length of forecast and obs must be equal if interval label is # instant forecast = default_forecast(site_metadata, issue_time_of_day=dt.time(hour=it), lead_time_to_start=pd.Timedelta(lead), interval_length=pd.Timedelta('5min'), run_length=pd.Timedelta('1d'), interval_label='instant') issue_time = pd.Timestamp(issue) run_time = issue_time - pd.Timedelta('75min') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time, issue_time) assert data_start == pd.Timestamp('20190421T0000Z') assert data_end == pd.Timestamp('20190421T235959Z')
def test_get_forecast_start_end_time_weekahead(site_metadata, variable, expected_start, expected_end): observation = default_observation(site_metadata, variable=variable, interval_length=pd.Timedelta('5min'), interval_label='beginning') run_time = pd.Timestamp('20190410T0630Z') forecast = default_forecast(site_metadata, variable=variable, issue_time_of_day=dt.time(hour=10), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1d'), interval_label='beginning') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time) assert data_start == pd.Timestamp(expected_start) assert data_end == pd.Timestamp(expected_end)
def test_get_data_start_end_time_weekahead_not_midnight(site_metadata): variable = 'net_load' observation = default_observation(site_metadata, variable=variable, interval_length=pd.Timedelta('5min'), interval_label='beginning') run_time = pd.Timestamp('20190410T1030Z') issue_time = pd.Timestamp('20190410T1200Z') # fx from 2019-04-11 12:00 forecast = default_forecast(site_metadata, variable=variable, issue_time_of_day=dt.time(hour=12), lead_time_to_start=pd.Timedelta('1d'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1d'), interval_label='beginning') data_start, data_end = utils.get_data_start_end(observation, forecast, run_time, issue_time) assert data_start == pd.Timestamp('20190404T1200Z') assert data_end == pd.Timestamp('20190405T1200Z')
def test_run_persistence_weekahead(session, site_metadata, mocker): variable = 'net_load' observation = default_observation(site_metadata, variable=variable, interval_length=pd.Timedelta('5min'), interval_label='beginning') run_time = pd.Timestamp('20190110T1945Z') forecast = default_forecast(site_metadata, variable=variable, issue_time_of_day=dt.time(hour=23), lead_time_to_start=pd.Timedelta('1h'), interval_length=pd.Timedelta('1h'), run_length=pd.Timedelta('1d'), interval_label='beginning') issue_time = pd.Timestamp('20190111T2300Z') mocker.spy(main.persistence, 'persistence_interval') out = main.run_persistence(session, observation, forecast, run_time, issue_time) assert isinstance(out, pd.Series) assert len(out) == 24 assert main.persistence.persistence_interval.call_count == 1
def test_find_next_issue_time_from_last_forecast(fxargs, last_time, expected, site_metadata): fx = default_forecast(site_metadata, **fxargs) out = utils.find_next_issue_time_from_last_forecast(fx, last_time) assert out == expected