コード例 #1
0
def _pers_loop(session, fx, obs, index, data_start, data_end, issue_times):
    load_data = _preload_load_data(session, obs, data_start, data_end)
    out = defaultdict(list)
    logger.info('Making persistence forecast for %s:%s from %s to %s', fx.name,
                fx.forecast_id, issue_times[0], issue_times[-1])
    for issue_time in issue_times:
        run_time = issue_time
        try:
            fx_out = run_persistence(session,
                                     obs,
                                     fx,
                                     run_time,
                                     issue_time,
                                     index=index,
                                     load_data=load_data)
        except ValueError as e:
            logger.error('Unable to generate persistence forecast: %s', e)
        else:
            if hasattr(fx, 'constant_values'):
                cv_ids = [f.forecast_id for f in fx.constant_values]
                for id_, fx_ser in zip(cv_ids, fx_out):
                    out[id_].append(fx_ser)
            else:
                out[fx.forecast_id].append(fx_out)
    for id_, serlist in out.items():
        if len(serlist) > 0:
            ser = pd.concat(serlist)
            for cser in generate_continuous_chunks(ser, fx.interval_length):
                if type(fx) == datamodel.Forecast:
                    session.post_forecast_values(id_, cser)
                else:
                    session.post_probabilistic_forecast_constant_value_values(
                        id_, cser)
コード例 #2
0
def test_generate_continuous_chunks(data, freq, expected):
    lg = list(utils.generate_continuous_chunks(data, freq))
    assert len(lg) == len(expected)
    for i, g in enumerate(lg):
        if isinstance(expected[i], pd.DataFrame):
            pdt.assert_frame_equal(expected[i], g, check_column_type=False)
        else:
            pdt.assert_series_equal(expected[i], g)
コード例 #3
0
def make_latest_probabilistic_persistence_forecasts(
        token, max_run_time, base_url=None):
    """Make all reference probabilistic persistence forecasts that need to
    be made up to *max_run_time*.

    Parameters
    ----------
    token : str
        Access token for the API
    max_run_time : pandas.Timestamp
        Last possible run time of the forecast generation
    base_url : str or None, default None
        Alternate base_url of the API
    """
    session = api.APISession(token, base_url=base_url)
    forecasts = session.list_probabilistic_forecasts()
    observations = session.list_observations()
    params = generate_reference_persistence_forecast_parameters(
        session, forecasts, observations, max_run_time)
    for fx, obs, index, data_start, issue_times in params:
        load_data = _preload_load_data(session, obs, data_start, max_run_time)
        out = defaultdict(list)
        logger.info('Making persistence forecast for %s:%s from %s to %s',
                    fx.name, fx.forecast_id, issue_times[0], issue_times[-1])
        for issue_time in issue_times:
            run_time = issue_time
            try:
                fx_list = run_persistence(
                    session, obs, fx, run_time, issue_time,
                    index=index, load_data=load_data)
            except ValueError as e:
                logger.error('Unable to generate persistence forecast: %s', e)
            else:
                # api requires a post per constant value
                cv_ids = [f.forecast_id for f in fx.constant_values]
                for id_, fx_ser in zip(cv_ids, fx_list):
                    out[id_].append(fx_ser)

        for id_, serlist in out.items():
            if len(serlist) > 0:
                ser = pd.concat(serlist)
                for cser in generate_continuous_chunks(
                        ser, fx.interval_length):
                    session.post_probabilistic_forecast_constant_value_values(
                        id_, cser)
コード例 #4
0
def test_generate_continuous_chunks_errs(data, freq, err):
    with pytest.raises(err):
        list(utils.generate_continuous_chunks(data, freq))