示例#1
0
def forecast_settings(message):

    # Initial forecast settings - the first time the user sends forecast settings through the app - will use this value in forecastr method
    build_settings = 'initial'

    # store message['data'] into a df called data
    data = message['data']

    # Keep Original Data in Exisiting Structure
    original_dataset = data[1]['data'][1]['data']

    #print("******************** ORIGINAL DATASET *****************************")
    #print(original_dataset)
    #print("******************** ORIGINAL DATASET *****************************")

    # Extract info from forecast_settings message
    time_series_data = pd.DataFrame(data[1]['data'][1]['data'])
    forecast_settings = data[0]
    freq = data[2]
    column_headers = data[1]['data'][0]

    # Format the date and metric unit
    time_unit = column_headers[0]
    print(time_unit)
    time_series_data[time_unit] = time_series_data[time_unit].apply(
        lambda x: pd.to_datetime(str(x)))
    metric = column_headers[1]

    # y (aka as "the original data for the metric being forecasted") will be used in the chartjs line graph
    y = time_series_data[metric].tolist()

    # Use Facebook Prophet through forecastr method
    forecast = forecastr(time_series_data, forecast_settings, column_headers,
                         freq, build_settings)

    # Need to convert forecast back into a list / array for y, y_hat and date so it can be properly graphed with chartjs
    y_hat = forecast[0]
    dates = forecast[1]
    model = forecast[2]
    csv_export = forecast[3]
    forecasted_vals = forecast[4]
    forecasted_vals_mean = forecast[5]

    # Send data back to the client
    data_back_to_client = [
        dates, y_hat, y, forecast_settings, column_headers, freq,
        original_dataset, csv_export, forecasted_vals, forecasted_vals_mean
    ]
    #print(data_back_to_client)

    emit('render_forecast_chart', {'data': data_back_to_client})
示例#2
0
def update_chart(message):

    # This is an update to the initial forecast settings. The user has changed their settings on Step 3, so we set build_settings to update.
    build_settings = 'update'

    data = message['data']

    ### Setup variables for use in the forecastr method
    time_series_data = data[4]
    original_dataset = time_series_data
    time_series_data = pd.DataFrame(time_series_data)

    #print("********* TIME SERIES DF ****************")
    #print(time_series_data.head())
    #print("********* TIME SERIES DF ****************")

    forecast_settings = data[1]
    column_headers = data[2]
    freq = data[3]

    # Dimension and Metric
    time_unit = column_headers[0]
    metric = column_headers[1]

    # Make sure time_unit is converted to datetime in order to join in helper_v3
    time_series_data[time_unit] = time_series_data[time_unit].apply(
        lambda x: pd.to_datetime(str(x)))

    #print([time_unit,metric])

    # Original Data
    y = time_series_data[metric].tolist()

    # Use Facebook Prophet through forecastr method
    forecast = forecastr(time_series_data, forecast_settings, column_headers,
                         freq, build_settings)

    # Need to convert forecast back into a list / array for y, y_hat and date so it can be properly graphed with chartjs
    y_hat = forecast[0]
    dates = forecast[1]
    model = forecast[2]
    csv_export = forecast[3]
    forecasted_vals = forecast[4]
    forecasted_vals_mean = forecast[5]

    # Send data back to the client - took out original dataset
    data_back_to_client = [
        dates, y_hat, y, forecast_settings, column_headers, freq,
        original_dataset, csv_export, forecasted_vals, forecasted_vals_mean
    ]
    emit('render_forecast_chart', {'data': data_back_to_client})