Ejemplo n.º 1
0
def test_forecast_truncate(config=CONFIG):
    """make sure truncate functionality works"""
    if platform.system() == 'Darwin':
        pytest.xfail('Unable to run fbprophet on mac')

    test_data = pd.read_csv(TEST_DATA_PATH)
    test_data['date'] = pd.to_datetime(test_data['date'])
    max_date = test_data['date'].max()

    truncate_range = int(config.get('TEST', 'truncate_range'))
    predict_data = forecast_utils.build_forecast(
        test_data, int(config.get('TEST', 'forecast_range')), truncate_range)

    expected_min_date = max_date - timedelta(days=truncate_range - 1)
    actual_min_date = predict_data['date'].min()

    assert expected_min_date == actual_min_date
Ejemplo n.º 2
0
def test_forecast_truncate(config=CONFIG):
    """make sure truncate functionality works"""
    test_data = pd.read_csv(TEST_DATA_PATH)
    test_data['date'] = pd.to_datetime(test_data['date'])
    max_date = test_data['date'].max()

    truncate_range = int(config.get('TEST', 'truncate_range'))
    predict_data = forecast_utils.build_forecast(
        test_data,
        int(config.get('TEST', 'forecast_range')),
        truncate_range
    )

    expected_min_date = max_date - timedelta(days=truncate_range-1)
    actual_min_date = predict_data['date'].min()

    assert expected_min_date == actual_min_date
Ejemplo n.º 3
0
def test_build_forecast(config=CONFIG):
    """try to build a forecast"""
    test_data = pd.read_csv(TEST_DATA_PATH)
    test_data['date'] = pd.to_datetime(test_data['date'])
    max_date = test_data['date'].max()

    expected_rows = [
        'date',
        'avgPrice',
        'yhat',
        'yhat_low',
        'yhat_high',
        'prediction'
    ]
    predict_data = forecast_utils.build_forecast(
        test_data,
        int(config.get('TEST', 'forecast_range'))
    )

    headers = list(predict_data.columns.values)
    assert set(expected_rows) == set(headers)

    assert predict_data['date'].max() == \
        max_date + timedelta(days=int(config.get('TEST', 'forecast_range')))

    expected_prediction = pd.read_csv(TEST_PREDICT_PATH)
    expected_prediction['date'] = pd.to_datetime(expected_prediction['date'])
    float_limit = float(config.get('TEST', 'float_limit'))

    for key in expected_rows:
        print(key)
        print(predict_data[key].dtype)

        if predict_data[key].dtype == np.float64:
            pass
            #TODO: ubuntu systems have >0.1 spread on values
            #unique_vals = predict_data[key] - expected_prediction[key]
            #for val in unique_vals.values:
            #    assert (abs(val) < float_limit) or (np.isnan(val)) #f*****g floats
        else:
            assert predict_data[key].equals(expected_prediction[key])
Ejemplo n.º 4
0
def test_build_forecast(config=CONFIG):
    """try to build a forecast"""
    if platform.system() == 'Darwin':
        pytest.xfail('Unable to run fbprophet on mac')

    test_data = pd.read_csv(TEST_DATA_PATH)
    test_data['date'] = pd.to_datetime(test_data['date'])
    max_date = test_data['date'].max()

    expected_rows = [
        'date', 'avgPrice', 'yhat', 'yhat_low', 'yhat_high', 'prediction'
    ]
    predict_data = forecast_utils.build_forecast(
        test_data, int(config.get('TEST', 'forecast_range')))

    headers = list(predict_data.columns.values)
    assert set(expected_rows) == set(headers)

    assert predict_data['date'].max() == \
        max_date + timedelta(days=int(config.get('TEST', 'forecast_range')))

    expected_prediction = pd.read_csv(TEST_PREDICT_PATH)
    expected_prediction['date'] = pd.to_datetime(expected_prediction['date'])
    float_limit = float(config.get('TEST', 'float_limit'))

    for key in expected_rows:
        print(key)
        print(predict_data[key].dtype)

        if predict_data[key].dtype == np.float64:
            pass
            #TODO: ubuntu systems have >0.1 spread on values
            #unique_vals = predict_data[key] - expected_prediction[key]
            #for val in unique_vals.values:
            #    assert (abs(val) < float_limit) or (np.isnan(val)) #f*****g floats
        else:
            assert predict_data[key].equals(expected_prediction[key])
Ejemplo n.º 5
0
    def get(self, return_type):
        args = self.reqparse.parse_args()
        LOGGER.info('Prophet {0} Request: {1}'.format(return_type, args))

        if return_type not in return_supported_types():
            return 'INVALID RETURN FORMAT', 405

        mode = api_config.SwitchCCPSource(
            api_config.CONFIG.get('GLOBAL', 'crest_or_esi'))
        forecast_range = api_config.DEFAULT_RANGE
        if 'range' in args:
            forecast_range = args.get('range')
        ## Validate inputs ##
        try:
            api_utils.check_key(args.get('api'),
                                throw_on_fail=True,
                                logger=LOGGER)
            crest_utils.validate_id('map_regions',
                                    args.get('regionID'),
                                    config=api_config.CONFIG,
                                    mode=mode,
                                    logger=LOGGER)
            crest_utils.validate_id('inventory_types',
                                    args.get('typeID'),
                                    config=api_config.CONFIG,
                                    mode=mode,
                                    logger=LOGGER)
            forecast_range = forecast_utils.check_requested_range(
                forecast_range,
                max_range=api_config.MAX_RANGE,
                raise_for_status=True)
        except exceptions.ValidatorException as err:
            LOGGER.warning('ERROR: unable to validate type/region ids' +
                           '\n\targs={0}'.format(args),
                           exc_info=True)
            return err.message, err.status
        except Exception:  #pragma: no cover
            LOGGER.error('ERROR: unable to validate type/region ids' +
                         'args={0}'.format(args),
                         exc_info=True)
            return 'UNHANDLED EXCEPTION', 500

        ## check cache ##
        cache_data = forecast_utils.check_prediction_cache(
            args.get('regionID'), args.get('typeID'))
        LOGGER.debug(cache_data)
        if cache_data is not None:
            LOGGER.info('returning cached forecast')
            message = forecast_reporter(cache_data, forecast_range,
                                        return_type, LOGGER)

            return message

        ## No cache, get data ##
        try:
            if args.get('typeID') in api_config.SPLIT_INFO:
                LOGGER.info('FORK: using split utility')
                data = split_utils.fetch_split_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    mode,
                    data_range=api_config.MAX_RANGE,
                    config=api_config.CONFIG,
                    logger=LOGGER)
                data.sort_values(by='date', ascending=True, inplace=True)
            else:
                data = forecast_utils.fetch_extended_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    mode=mode,
                    data_range=api_config.MAX_RANGE,
                    config=api_config.CONFIG,
                    logger=LOGGER)
            data = forecast_utils.build_forecast(data, api_config.MAX_RANGE)
        except exceptions.ValidatorException as err:
            #FIX ME: testing?
            LOGGER.warning('ERROR: unable to generate forecast' +
                           '\n\targs={0}'.format(args),
                           exc_info=True)
            return err.message, err.status
        except Exception:  #pragma: no cover
            LOGGER.error('ERROR: unable to generate forecast' +
                         '\n\targs={0}'.format(args),
                         exc_info=True)
            return 'UNHANDLED EXCEPTION', 500

        ## Update cache ##
        forecast_utils.write_prediction_cache(args.get('regionID'),
                                              args.get('typeID'),
                                              data,
                                              logger=LOGGER)
        try:
            message = forecast_reporter(data, forecast_range, return_type,
                                        LOGGER)
        except Exception as err_msg:  #pragma: no cover
            LOGGER.error('invalid format requested' +
                         '\n\targs={0}'.format(args) +
                         '\n\treturn_type={0}'.format(return_type),
                         exc_info=True)
            return 'UNABLE TO GENERATE REPORT', 500
        return message
Ejemplo n.º 6
0
    def get(self, return_type):
        args = self.reqparse.parse_args()
        self.logger.info('Prophet %s Request: %s', return_type, args)

        if return_type not in return_supported_types():
            return 'INVALID RETURN FORMAT', 405

        forecast_range = api_config.DEFAULT_RANGE
        if 'range' in args:
            forecast_range = args.get('range')
        ## Validate inputs ##
        try:
            api_utils.check_key(
                args.get('api'),
                throw_on_fail=True,
                logger=self.logger,
            )
            crest_utils.validate_id(
                'map_regions',
                args.get('regionID'),
                config=api_config.CONFIG,
                logger=self.logger,
            )
            crest_utils.validate_id(
                'inventory_types',
                args.get('typeID'),
                config=api_config.CONFIG,
                logger=self.logger,
            )
            forecast_range = forecast_utils.check_requested_range(
                forecast_range,
                max_range=api_config.MAX_RANGE,
                raise_for_status=True
            )
        except exceptions.ValidatorException as err:
            self.logger.warning(
                'ERROR: unable to validate type/region ids\n\targs=%s',
                args,
                exc_info=True
            )
            return err.message, err.status
        except Exception: #pragma: no cover
            self.logger.error(
                'ERROR: unable to validate type/region ids\n\targs=%s',
                args,
                exc_info=True
            )
            return 'UNHANDLED EXCEPTION', 500

        ## check cache ##
        cache_data = forecast_utils.check_prediction_cache(
            args.get('regionID'),
            args.get('typeID')
        )
        self.logger.debug(cache_data)
        if cache_data is not None:
            self.logger.info('returning cached forecast')
            message = forecast_reporter(
                cache_data,
                forecast_range,
                return_type,
                self.logger,
            )

            return message

        ## No cache, get data ##
        try:
            if args.get('typeID') in api_config.SPLIT_INFO:
                LOGGER.info('FORK: using split utility')
                data = split_utils.fetch_split_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    data_range=api_config.MAX_RANGE,
                    config=api_config.CONFIG,
                    logger=self.logger,
                )
                data.sort_values(
                    by='date',
                    ascending=True,
                    inplace=True
                )
            else:
                data = forecast_utils.fetch_extended_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    data_range=api_config.MAX_RANGE,
                    config=api_config.CONFIG,
                    logger=self.logger,
                )
            data = forecast_utils.build_forecast(
                data,
                api_config.MAX_RANGE
            )
        except exceptions.ValidatorException as err:
            #FIX ME: testing?
            self.logger.warning(
                'ERROR: unable to generate forecast\n\targs=%s',
                args,
                exc_info=True
            )
            return err.message, err.status
        except Exception: #pragma: no cover
            LOGGER.error(
                'ERROR: unable to generate forecast\n\targs=%s',
                args,
                exc_info=True
            )
            return 'UNHANDLED EXCEPTION', 500

        ## Update cache ##
        forecast_utils.write_prediction_cache(
            args.get('regionID'),
            args.get('typeID'),
            data,
            logger=self.logger,
        )
        try:
            message = forecast_reporter(
                data,
                forecast_range,
                return_type,
                self.logger,
            )
        except Exception as err_msg:    #pragma: no cover
            LOGGER.error(
                'invalid format requested'
                '\n\targs=%s'
                '\n\treturn_type=%s',
                args, return_type,
                exc_info=True
            )
            return 'UNABLE TO GENERATE REPORT', 500
        return message