Esempio n. 1
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)

    useworldfile = config['worldfile']
    if (not useworldfile):
        data = generate_hos_actual(data, config)
    else:
        data = generate_zero_columns(data, config)

    plot_confidence(configpath, config, data, config['startdate'])
    plot_confidence_alpha(configpath, config, data, config['startdate'])
Esempio n. 2
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)

    useworldfile = config['worldfile']
    if (not useworldfile):
        data = generate_hos_actual(data, config)
    else:
        data = generate_zero_columns(data, config)

    base = (os.path.split(configpath)[-1]).split('.')[0]
    outpath = os.path.join(os.path.split(os.getcwd())[0], 'output', base)

    plot_confidence(outpath, config, data, config['startdate'])
Esempio n. 3
0
def main(action, configpath):
    if action == 1:
        scrape_and_post_nl_data()
    elif action == 2:
        config = load_config(configpath)

        # Check if the IC data file is defined in config, otherwise use '../res/icdata_main.txt'
        try:
            ic_data_file = config['icdatafile']
        except KeyError:
            print("No icdatafile in config, using '../res/icdata_main.txt'.")
            ic_data_file = '../res/icdata_main.txt'

        # Input argument is the path to the config file
        get_and_save_nl_data(config['startdate'], config['country'], ic_data_file)
Esempio n. 4
0
def preprocess(configpath, save_plots=True):
    # This function will
    #   1) scrape the data from the RIVM and NICE sites and post it to the database,
    #   2) pull from the database and generate the dataNL file and the icdata file (in the res directory), and
    #   3) generate the icufrac file and ICanalysis plots (in the output directory)

    config = load_config(configpath)
    output_base_filename = (os.path.split(configpath)[-1]).split('.')[0]

    # Check if the IC data file is defined in config, otherwise use '../res/icdata_main.txt'
    try:
        ic_data_file = config['icdatafile']
    except KeyError:
        print("No icdatafile in config, using '../res/icdata_main.txt'.")
        ic_data_file = '../res/icdata_main.txt'

    scrape_and_post_nl_data()
    get_and_save_nl_data(config['startdate'], config['country'], ic_data_file)
    save_and_plot_IC(config, output_base_filename, int(save_plots))
    print("Preprocessing complete.")
Esempio n. 5
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)
    base_filename = (os.path.split(configpath)[-1]).split('.')[0]
    run_esmda_model(base_filename, config, data)
Esempio n. 6
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)
    outpath = config['outpath']

    useworldfile = config['worldfile']
    if (not useworldfile):
        data = generate_hos_actual(data, config)
    else:
        data = generate_zero_columns(data, config)
    # Run the forward model to obtain a prior ensemble of models
    save_input_data(configpath, outpath, data)
    prior, time, prior_param, fwd_args = run_prior(config)

    # Calibrate the model (as calibration data, we either use 'hospitalized' or 'dead')
    calibration_mode = get_calibration_modes(config['calibration_mode'])[0]
    if calibration_mode == 'dead':
        data_index = I_DEAD
        output_index = O_DEAD
    elif calibration_mode == 'hospitalized':
        data_index = I_HOS
        output_index = O_HOS
    elif calibration_mode == 'hospitalizedcum':
        data_index = I_HOSCUM
        output_index = O_HOSCUM
    elif calibration_mode == 'ICU':
        data_index = I_ICU
        output_index = O_ICU
    else:
        raise NotImplementedError

    plotplume = config['plot']['hindcast_plume']

    p = calibrate_with_data(
        prior, time, data, data_index,
        output_index)  # Posterior probability for each ensemble member
    integrated_parameter_values = integrate_parameters(prior_param, p)
    np.savetxt(outpath + "/integrated_parameter_values.csv",
               integrated_parameter_values,
               delimiter=";")

    integrated_results = integrate_calibration(prior,
                                               p)  # Mean posterior model

    # Create output of the calibration phase
    plot_results(prior,
                 time,
                 configpath,
                 outpath,
                 config,
                 data,
                 integrated_results,
                 calibration_mode,
                 prior=True,
                 plot_plume=plotplume)

    # Based on the model performance in the past, run models again, forecasting the future

    forecast = rerun_model_with_alpha_uncertainty(prior_param, p, config,
                                                  fwd_args)

    plot_results(forecast,
                 time,
                 configpath,
                 outpath,
                 config,
                 data,
                 cal_mode=config['calibration_mode'],
                 prior=False,
                 plot_plume=True)

    plot_posterior(forecast, config, configpath, outpath, data, ['time'])

    hospital_forecast_to_txt(forecast, time, configpath, outpath, config)
Esempio n. 7
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)
    # concatenate additional column for actual observed hospitalized based
    t_obs = data[:, I_TIME]

    useworldfile = config['worldfile']
    if (not useworldfile):
        data = generate_hos_actual(data, config)
    else:
        data = generate_zero_columns(data, config)
        # Run the forward model to obtain a prior ensemble of models
    save_input_data(configpath, data)

    calibration_modes = get_calibration_modes(config['calibration_mode'])
    observation_errors = get_calibration_errors(config['observation_error'])
    calibration_mode = calibration_modes[0]

    found = False
    for i, calmode in enumerate(s_calmodes):
        if (calmode == calibration_mode):
            print('ensemble fit on : ', calibration_modes[0], ' with error ',
                  observation_errors[0])
            y_obs = data[:, i_calmodes[i]]
            output_index = [o_calmodes[i]]
            error = np.ones_like(y_obs) * observation_errors[0]
            found = True
    if not found:
        raise NotImplementedError

    for ical, calibration_mode in enumerate(calibration_modes):
        if (ical > 0):
            print('additional ensemble fit on : ', calibration_modes[ical],
                  ' with error ', observation_errors[ical])
            for i, calmode in enumerate(s_calmodes):
                if (calmode == calibration_mode):
                    y_obs2 = data[:, i_calmodes[i]]
                    output_index2 = [o_calmodes[i]]
                    error2 = np.ones_like(y_obs2) * observation_errors[ical]
                    y_obs = np.append(y_obs, y_obs2)
                    # output_index = [output_index[0], O_ICU]
                    output_index = np.append(output_index, output_index2)
                    error = np.append(error, error2)
                    found = True
            if not found:
                raise NotImplementedError

    # Load inversion method and define forward model
    try:
        ni = config['esmda_iterations']
    except KeyError:
        ni = 8
    im = InversionMethods(ni=ni)
    forward = base_seir_model
    np.random.seed(12345)

    # Parse the configuration json
    m_prior, fwd_args = parse_config(config)
    m_prior = reshape_prior(m_prior)

    # Estimated uncertainty on the data
    # sigma = np.sqrt(y_obs).clip(min=10)

    # error = config['observation_error']
    # if calibration_mode == S_HOS or calibration_mode == S_HOSCUM:
    #    error = 2 * error
    # sigma = error * np.ones_like(y_obs)

    sigma = error

    add_arg = [fwd_args]
    kwargs = {
        't_obs': t_obs,
        'output_index': output_index,
        'G': map_model_to_obs,
        'print_results': 1
    }

    results = im.es_mda(forward, m_prior, y_obs, sigma, *add_arg, **kwargs)

    plot_prior_and_posterior(results, fwd_args, config, configpath, t_obs,
                             data, calibration_mode, output_index)
    base = (os.path.split(configpath)[-1]).split('.')[0]
    outpath = os.path.join(
        os.path.split(os.getcwd())[0], 'output', base + '_output.h5')
    save_results(results, fwd_args, config, outpath, data, mode='esmda')

    # Check if we want to apply a 'hammer'
    try:
        hammer_icu = config['hammer_ICU']
        hammer_alpha = config['hammer_alpha']
        assert len(hammer_alpha) == 2
        hammered_results = apply_hammer(results['fw'][-1],
                                        results['M'][-1],
                                        fwd_args,
                                        hammer_icu,
                                        hammer_alpha,
                                        data_end=data[-1, 0])
        # TODO: Add hammer results to h5 file and plotting
        add_hammer_to_results(hammered_results, outpath, mode='esmda')

    except KeyError as e:
        pass  # Don't apply hammer, you're done early!
Esempio n. 8
0
def main(configpath):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    data = load_data(config)
    base_filename = (os.path.split(configpath)[-1]).split('.')[0]

    useworldfile = config['worldfile']
    if (not useworldfile):
        data = generate_hos_actual(data, config)
    else:
        data = generate_zero_columns(data, config)
    # Run the forward model to obtain a prior ensemble of models
    save_input_data(base_filename, data)
    ndata = np.size(data[:, 0])
    prior, time, prior_param, fwd_args = run_prior(config, ndata)

    # Calibrate the model (as calibration data, we either use 'hospitalized' or 'dead')
    calibration_mode = get_calibration_modes(config['calibration_mode'])[0]
    if calibration_mode == 'dead':
        data_index = I_DEAD
        output_index = O_DEAD
    elif calibration_mode == 'hospitalized':
        data_index = I_HOS
        output_index = O_HOS
    elif calibration_mode == 'hospitalizedcum':
        data_index = I_HOSCUM
        output_index = O_HOSCUM
    elif calibration_mode == 'ICU':
        data_index = I_ICU
        output_index = O_ICU
    else:
        raise NotImplementedError

    plotplume = config['plot']['hindcast_plume']

    p = calibrate_with_data(
        prior, time, data, data_index,
        output_index)  # Posterior probability for each ensemble member
    integrated_results = integrate_calibration(prior,
                                               p)  # Mean posterior model

    # Create output of the calibration phase
    plot_results(prior,
                 time,
                 configpath,
                 config,
                 data,
                 integrated_results,
                 calibration_mode,
                 prior=True,
                 plot_plume=plotplume)

    # Based on the model performance in the past, run models again, forecasting the future

    forecast = rerun_model_with_alpha_uncertainty(prior_param, p, config,
                                                  fwd_args)
    base = (os.path.split(configpath)[-1]).split('.')[0]
    outbase = os.path.join(os.path.split(os.getcwd())[0], 'output', base)

    plot_results(forecast,
                 time,
                 configpath,
                 config,
                 data,
                 cal_mode=config['calibration_mode'],
                 prior=False,
                 plot_plume=True)

    plot_posterior(forecast, config, outbase, data, ['time'])

    try:
        hospital_forecast_to_txt(forecast, time, configpath, config, data)
    except:
        pass
Esempio n. 9
0
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import os
import locale
import matplotlib.patches as mpatches
from src.io_func import load_config
country = 'OPT'
save_dirs = ['output/OPT/final-plots']
configpath = 'configs/OPT.json'

config = load_config(configpath)
pop = config['population']
sigma = config['sigma']

locale.setlocale(locale.LC_ALL, "nl_NL")
today = datetime.today().strftime("%d-%B-%Y").lower()
span = datetime.today() - datetime.strptime('1-maart-2020', "%d-%B-%Y")
short = span.days + 7
long = span.days + 100

labels = ['S', 'E', 'REM', 'I', 'REC', 'HOS', 'D']
dfs = []
for label in labels:
    data_phl = pd.read_csv('output/{}/{}_{}_prob.csv'.format(
        country, country, label),
                           header=None,
                           names=[
                               'time', label, label + '5', label + '30',
                               label + '50', label + '70', label + '95'
                           ],
Esempio n. 10
0
def main(configpath, save_plots=True):
    # Load the model configuration file and the data (observed cases)
    config = load_config(configpath)
    output_base_filename = (os.path.split(configpath)[-1]).split('.')[0]
    save_and_plot_IC(config, output_base_filename, save_plots)