Exemple #1
0
    # Forecast date is equal to the date of the last known datapoint, unless manually supplied
    forecast_date = region.index[-1]
    region = region[region.index <= forecast_date]

    # Early exit: If there are less than DATAPOINT_COUNT_MIN datapoints
    if len(region) < DATAPOINT_COUNT_MIN: continue

    # Define the subfolder that will hold the output assets
    forecast_chart = ROOT / 'output' / 'charts' / ('%s_US_%s.svg' %
                                                   (forecast_date, key))

    # Perform forecast
    forecast_data = forecast(region['Confirmed'], predict_window)

    # Output charts as SVG files
    plot_forecast(forecast_chart, region['Confirmed'], forecast_data)

    # Aggregate forecast data
    for idx in forecast_data.index:
        forecast_df.loc[(key, idx), 'CountryCode'] = country_code
        forecast_df.loc[(key, idx), 'CountryName'] = country_name
        forecast_df.loc[(key, idx), 'ForecastDate'] = forecast_date
        forecast_df.loc[(key, idx), 'Estimated'] = '%.03f' % forecast_data[idx]
        forecast_df.loc[(key, idx),
                        'ForecastChart'] = forecast_chart.relative_to(ROOT /
                                                                      'output')
    for idx in region['Confirmed'].index:
        forecast_df.loc[(key, idx), 'Confirmed'] = int(region.loc[idx,
                                                                  'Confirmed'])

# Save output to CSV and JSON
Exemple #2
0

if __name__ == "__main__":
    df = pd.read_csv("../data/India_OWID_with_mobility_data.csv")
    dataset = Data(df, 1, 1)
    dataset.smoothen_df()
    X, y = dataset.sliding_window(config["features"], dataset.previous_days,
                                  dataset.forecast_days - 1)
    print(len(X))
    print(X.shape)
    print(y.shape)
    model = CoronaVirusPredictor(1, 1, 1)
    tscv = TimeSeriesSplit()
    r2_scores, test_error_mae, test_error_mse, train, test = train_model_with_crossval(
        model,
        X,
        y,
        tscv,
    )
    print(r2_scores)
    for i in range(1, 6):
        plot_forecast(train["pred"][i], train["true"][i], test["pred"][i],
                      test["true"][i])

    exit(0)
    gg, train_hist, test_hist = train_model(model, config, dataset)
    plt.plot(train_hist, label="train")
    plt.plot(test_hist, label="test")
    plt.legend(loc="best")
    plt.show()
Exemple #3
0
    # Store the charts in a helper JSON file
    chart_outputs[key] = {}

    confirmed = subset['Confirmed'].dropna()
    if len(confirmed) > 0:
        plot_column(charts_root / fname_confirmed, confirmed)
        chart_outputs[key]['Confirmed'] = fname_confirmed

    deaths = subset['Deaths'].dropna()
    if len(deaths) > 0:
        plot_column(charts_root / fname_deaths, deaths)
        chart_outputs[key]['Deaths'] = fname_deaths

    try:
        # Get the estimated cases from the forecast
        estimated = df_forecast[df_forecast['Key'] == key].set_index(
            ['Date'])['Estimated']
        if len(estimated) == 0: continue

        # Line up the indices for both the estimated and confirmed cases
        estimated = estimated[estimated.index >= confirmed.index[0]].dropna()
        confirmed = confirmed[confirmed.index >= estimated.index[0]].dropna()
        if len(estimated) > len(confirmed):
            plot_forecast(charts_root / fname_forecast, confirmed, estimated)
            chart_outputs[key]['Forecast'] = fname_forecast
    except Exception as exc:
        print('Unexpected error: %r' % sys.exc_info()[0], file=sys.stderr)
        print(subset, file=sys.stderr)

# Output the chart map to stdout
json.dump(chart_outputs, sys.stdout)
Exemple #4
0
                                             ]).query('~index.duplicated()')
    subset = subset.reset_index().set_index('Date').sort_index().iloc[-14:]

    fname_forecast = ('%s_%s_forecast.svg' % (prefix, suffix))
    fname_confirmed = ('%s_%s_confirmed.svg' % (prefix, suffix))
    fname_deaths = ('%s_%s_deaths.svg' % (prefix, suffix))

    # Store the charts in a helper JSON file
    chart_outputs[suffix] = {}

    confirmed = subset['Confirmed'].dropna()
    if len(confirmed) > 0:
        plot_column(charts_root / fname_confirmed, confirmed)
        chart_outputs[suffix]['Confirmed'] = fname_confirmed

    deaths = subset['Deaths'].dropna()
    if len(deaths) > 0:
        plot_column(charts_root / fname_deaths, deaths)
        chart_outputs[suffix]['Deaths'] = fname_deaths

    try:
        if len(subset['Estimated'].dropna()) > len(confirmed):
            plot_forecast(charts_root / fname_forecast, subset['Confirmed'],
                          subset['Estimated'])
            chart_outputs[suffix]['Forecast'] = fname_forecast
    except Exception as exc:
        print('Unexpected error:', sys.exc_info()[0])
        print(subset)

with open(charts_root / 'map.json', 'w') as f:
    json.dump(chart_outputs, f)