Example #1
0
def forecast_gru(station: str, index: str, start: datetime, end: datetime):
    """
    Apply LSTM Net Forecast.
    """
    if not os.getenv("LOGS_HOME"):
        log_home = forecast_params['LOGS_HOME']
    else:
        log_home = os.getenv("LOGS_HOME")

    gru_logger = init_logger(log_path=os.path.join(
        log_home, 'gru_{0}.log'.format(start.strftime("%Y%m%d%H"))))
    session = orm.create_session()

    preds = dict()
    while start <= end:
        this_start = start - timedelta(
            days=forecast_params['gru_params']['last_days'])
        this_end = start
        #         this_train = query_obs_4h(
        #             session=session, station_name=station, start=this_start, end=this_end
        #         )
        this_train = query_qc_4h(session=session,
                                 station_name=station,
                                 start=this_start,
                                 end=this_end)

        this_predict_start = start - timedelta(
            hours=(forecast_params['gru_params']['IN_STEPS'] - 1) *
            forecast_params['FREQ'])
        this_predict_end = start
        #         this_predict = query_obs_4h(
        #             session=session, station_name=station, start=this_predict_start, end=this_predict_end
        #         )
        this_predict = query_qc_4h(session=session,
                                   station_name=station,
                                   start=this_predict_start,
                                   end=this_predict_end)
        print("This Train Shape: {0}".format(this_train.shape))
        print("This Predict Shape: {0}".format(this_predict.shape))
        gru_logger.info("Query [{0}] data before [{1}] done.".format(
            station, start.strftime("%Y/%m/%d %H")))

        this_forecast = deepl_model(this_train=this_train,
                                    this_predict=this_predict,
                                    kernel="gru")
        gru_logger.info("Forecast [{0}] [{1}] [{2}] done.".format(
            station, start.strftime("%Y/%m/%d %H"), index))

        insert_gru(session,
                   data=this_forecast[[index]],
                   this_time=start,
                   station_name=station)
        gru_logger.info("ADD NEW [{0}] [{1}] [{2}] forecast data done.".format(
            station, start.strftime("%Y/%m/%d %H"), index))
        start += timedelta(hours=forecast_params['FREQ'])

    session.close()
    gru_logger.info("[{0}] close session done.".format(station))
Example #2
0
def qc_entrance(station: str, index: str, start: datetime, end: datetime):
    session = orm.create_session()

    qc_input = query_qc_input(session=session,
                              station_name=station,
                              start=start,
                              end=end)

    print(qc_input)
    qc_output = quality_control(qc_input)
    print(qc_output)

    insert_qc_output(session=session, data=qc_output, station_name=station)

    session.close()
Example #3
0
def forecast_fbprophet(station: str, index: str, start: datetime,
                       end: datetime):
    """
    Apply FbPropHet Forecast.
    """
    if not os.getenv("LOGS_HOME"):
        log_home = forecast_params['LOGS_HOME']
    else:
        log_home = os.getenv("LOGS_HOME")

    fbprophet_logger = init_logger(log_path=os.path.join(
        log_home, 'fbprophet_{0}.log'.format(start.strftime("%Y%m%d%H"))))
    session = orm.create_session()

    preds = dict()
    while start <= end:
        this_start = start - timedelta(
            days=forecast_params['fbprophet_params']['last_days'])
        this_end = start
        #this_resp = query_obs_4h(session=session, station_name=station, start=this_start, end=this_end)
        this_resp = query_qc_4h(session=session,
                                station_name=station,
                                start=this_start,
                                end=this_end)
        this_data = this_resp[['time', index]].rename(columns={
            'time': 'ds',
            index: 'y'
        })
        fbprophet_logger.info(
            "Query [{0}] history data before [{1}] done.".format(
                station, start.strftime("%Y/%m/%d %H")))

        this_forecast = fbprophet_model(this_data, start, index)
        fbprophet_logger.info("Forecast [{0}] [{1}] [{2}]done.".format(
            station, start.strftime("%Y/%m/%d %H"), index))
        insert_fbprophet(session,
                         data=this_forecast,
                         this_time=start,
                         station_name=station,
                         index=index)
        fbprophet_logger.info(
            "ADD NEW [{0}] [{1}] [{2}] forecast data done.".format(
                station, start.strftime("%Y/%m/%d %H"), index))
        start += timedelta(hours=forecast_params['FREQ'])

    session.close()
    fbprophet_logger.info("[{0}] close session done.".format(station))
Example #4
0
def forecast_arima(station: str, index: str, start: datetime, end: datetime):
    """
    Apply ARIMA Forecast.
    """
    if not os.getenv("LOGS_HOME"):
        log_home = forecast_params['LOGS_HOME']
    else:
        log_home = os.getenv("LOGS_HOME")

    arima_logger = init_logger(log_path=os.path.join(
        log_home, 'arima_{0}.log'.format(start.strftime("%Y%m%d%H"))))

    session = orm.create_session()

    preds = dict()
    while start <= end:
        this_start = start - timedelta(
            days=forecast_params['arima_params']['last_days'])
        this_end = start
        #this_resp = query_obs_4h(session=session, station_name=station, start=this_start, end=this_end)
        this_resp = query_qc_4h(session=session,
                                station_name=station,
                                start=this_start,
                                end=this_end)
        this_data = this_resp[['time', index]].set_index('time')

        arima_logger.info("Query [{0}] history data before [{1}] done.".format(
            station, start.strftime("%Y/%m/%d %H")))
        this_forecast = arima_model(this_data)
        print("*** {0} Forecast ***\n{1}".format(
            start.strftime("%Y-%m-%d %H:00:00"), this_forecast))
        arima_logger.info("Forecast [{0}] [{1}] [{2}] done.".format(
            station, start.strftime("%Y/%m/%d %H"), index))
        insert_arima(session,
                     data=this_forecast,
                     this_time=start,
                     station_name=station,
                     index=index)
        arima_logger.info(
            "ADD NEW [{0}] [{1}] [{2}] forecast data done.".format(
                station, start.strftime("%Y/%m/%d %H"), index))
        start += timedelta(hours=forecast_params['FREQ'])

    session.close()
    arima_logger.info("[{0}] close session done.".format(station))