Ejemplo n.º 1
0
def generate_hos_actual(data, config):
    # generate an additional data column for actual hospitalized
    # only do this when the mean of the actuals are zero

    delay_rec = get_mean(config['delayREC'])
    delay_hos = get_mean(config['delayHOS'])
    delay_hosrec = get_mean(config['delayHOSREC'])
    delay_hosd = get_mean(config['delayHOSD'])
    delay_icu = get_mean(config['delayICUCAND'])
    delay_icud = get_mean(config['delayICUD'])
    delay_icurec = get_mean(config['delayICUREC'])
    hosfrac = get_mean(config['hosfrac'])
    dfrac = get_mean(config['dfrac'])
    t_max = np.size(data[:, I_HOSCUM])
    time = np.linspace(1, t_max, int(t_max))
    icufrac = read_icufrac_data(config, time, 0)
    # icufrac =config['ICufrac']
    icudfrac = get_mean(config['icudfrac'])

    hos = data[:, I_HOS]

    if np.sum(np.cumsum(hos)) > 0.1:
        return data

    # else include the hospitalised column

    hoscum = data[:, I_HOSCUM]

    fracs = [hosfrac, dfrac, icufrac, icudfrac]
    delays = [
        delay_rec, delay_hos, delay_hosrec, delay_hosd, delay_icu, delay_icud,
        delay_icurec
    ]

    dataprocess = do_hospitalization_process(hoscum, delays, fracs)
    hos = dataprocess[:, OP_HOS]
    hos = hos[:, None]
    icucum = dataprocess[:, OP_ICUCUM]
    # print ('day icufrac icucum')
    # for i, ic in enumerate(icucum):
    #     if (i<t_max):
    #         print (time[i], icufrac[i], ic)
    datanew = np.concatenate((data[:, 0:6], hos), axis=-1)
    return datanew
Ejemplo n.º 2
0
def generate_zero_columns(data, config):
    # generate 3 additional data column (0 is first column index_
    # 4 hospitalized cumulative,
    # 5 ICU
    # 6
    days = data[:, I_TIME]
    tests = data[:, I_INF]
    dead = data[:, I_DEAD]
    #n = np.ndarray.size(days)
    delay_hos = get_mean(config['delayHOS'])
    delay_hos2 = delay_hos + 1 / get_mean(config['gamma'])
    delay_hosd = get_mean(config['delayHOSD'])
    icufrac = read_icufrac_data(config, days, 0)
    icudfrac = get_mean(config['icudfrac'])
    dfrac = get_mean(config['dfrac'])
    delay_icud = get_mean(config['delayICUD'])

    hoscum = days * 0.0
    hoscumcreate = 'none'
    try:
        hoscumcreate = config['hoscumcreate']
    except:
        pass

    if (hoscumcreate == 'dead'):
        hoscum1 = (1 - icufrac) * gauss_smooth_shift(
            dead, delay_hosd, 0, 1.0 / (dfrac - icufrac * icudfrac))
        hoscum2 = (icufrac) * gauss_smooth_shift(dead, delay_icud, 0, 1.0 /
                                                 (icudfrac))
        hoscum = hoscum1 + hoscum2
    elif (hoscumcreate == 'confirmed'):
        try:
            rate_hoscumfromconfirmed = config['rate_hoscumfromconfirmed']
        except:
            pass
        hoscum = gauss_smooth_shift(tests, delay_hos2, 0,
                                    rate_hoscumfromconfirmed)

    icu = days * 0.0
    hosact = days * 0.0
    datanew = np.concatenate(
        (data[:, 0:4], hoscum[:, None], icu[:, None], hosact[:, None]),
        axis=-1)
    datanew = generate_hos_actual(datanew, config)
    return datanew
Ejemplo n.º 3
0
def save_and_plot_prior_and_posterior(results,
                                      fwd_args,
                                      config,
                                      base_filename,
                                      t_obs,
                                      data,
                                      calibration_mode,
                                      output_index,
                                      save_plots,
                                      save_files,
                                      plothammer=False):
    # Save plots, CSV's, and return the organized data

    # Initialize variables
    outpath = os.path.join(
        os.path.split(os.getcwd())[0], 'output', base_filename)
    calmodes = [S_HOS, S_ICU, S_HOSCUM, S_DEAD, S_INF, S_ALPHARUN]
    o_indices = [O_HOS, O_ICU, O_HOSCUM, O_DEAD, O_CUMINF, O_ALPHARUN]
    y_obs_s = [
        data[:, I_HOS], data[:, I_ICU], data[:, I_HOSCUM], data[:, I_DEAD],
        data[:, I_INF], []
    ]
    # useworldfile = config['worldfile']
    # if (useworldfile):
    #    calmodes = [ S_DEAD]
    #    o_indices = [ O_DEAD]
    # else:
    #    # check if I_HOS is present in the data, if not generate it
    #    print('number of columns in data ', data.shape[1])
    #    print('number of rows in data ', data.shape[0])
    #    ncol = data.shape[1]
    #    data = generate_hos_actual(data, config)

    prior = results['fw'][0]
    posterior = results['fw'][-1]
    date_1 = datetime.datetime.strptime(config['startdate'], "%m/%d/%y")
    time = fwd_args['time'] - fwd_args['time_delay']
    times = [date_1 + datetime.timedelta(days=a - 1) for a in time]
    p_values = config['p_values']
    steps = np.arange(1, time.max() + 1)
    t_ind = [np.where(time == a)[0][0] for a in steps]
    h_pvalues = ['P' + str(int(100 * a)) for a in p_values]
    header = 'time,mean,' + ','.join(h_pvalues) + ',observed'

    # Initialize variables for the plots
    if save_plots:
        t_obs = [date_1 + datetime.timedelta(days=a - 1) for a in t_obs]

        titles = [
            'Hospitalized', 'ICU', 'Hospitalized Cum.', 'Mortalities',
            'Infected', '1-$\\alpha$'
        ]
        y_label = [
            'Number of cases', 'Number of cases', 'Number of cases',
            'Number of cases', 'Number of cases', '1-$\\alpha$'
        ]
        symcolors = [['powderblue', 'steelblue'], ['peachpuff', 'sandybrown'],
                     ['lightgreen', 'forestgreen'], ['silver', 'grey'],
                     ['mistyrose', 'lightcoral'], ['violet', 'purple']]
        transparency = max(min(5.0 / len(prior), 1), 0.05)

        y_maxdef = config['YMAX']
        y_maxhos = y_maxdef * get_mean(config['hosfrac'])
        y_maxicu = y_maxhos * get_mean(config['ICufrac'])
        y_maxdead = y_maxhos * get_mean(config['dfrac']) * 4
        y_maxinf = y_maxdef * 10
        y_max = [y_maxhos, y_maxicu, y_maxhos * 4, y_maxdead, y_maxinf, 1.0]

        casename = ''
        try:
            casename = config['plot']['casename']
        except:
            print('No casename in plot parameters')
            pass

    posterior_prior_data = {}
    for i, calmode in enumerate(calmodes):
        output_index = o_indices[i]
        y_obs = y_obs_s[i]
        print(calmode, output_index)

        # Prepare prior data
        prior_curves = np.array([member[output_index, :]
                                 for member in prior]).T
        prior_mean = np.mean(prior_curves, axis=-1)

        # Prepare posterior data
        posterior_curves = np.array(
            [member[output_index, :] for member in posterior]).T
        post_mean = np.mean(posterior_curves, axis=-1)
        post_med = np.median(posterior_curves, axis=-1)

        if save_plots:
            title = titles[i]
            symcolor = symcolors[i]
            ymax = y_max[i]
            ylabel = y_label[i]

            # Save prior plot
            color = 'green'
            if np.size(y_obs) > 0:
                plt.scatter(t_obs, y_obs, marker='o', c='k', label='Data')
            plt.plot(times, prior_curves, alpha=transparency, c=color)
            plt.plot(times, prior_mean, lw=2, c=color, label='Mean of prior')
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval(
                (times[config['XMAX']] - date_1).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.xlim(date_1, times[config['XMAX']])
            plt.ylim(0, ymax)
            plt.xlabel('Date')
            plt.gcf().autofmt_xdate()
            plt.ylabel(ylabel)
            plt.legend(loc='upper left')
            plt.title(title + ' prior ensemble')
            plt.savefig('{}_prior_ensemble_{}.png'.format(outpath, calmode),
                        dpi=300)
            plt.close()

            # Plot posterior
            isicu = (calmode == S_ICU)
            try:
                figure_size = config['plot']['figure_size']
                assert len(figure_size) == 2
                plt.figure(figsize=figure_size)
            except:
                pass
            if (isicu) and plothammer:
                try:
                    # try to read hammer
                    # consider to plot the analyse_hammer results
                    path = '{}_hammer_diagnostics{}.csv'.format(outpath, '' '')
                    data = np.genfromtxt(path, names=True, delimiter=',')
                    th_trig = data['daymon']
                    h_icurate = data['icurate']
                    h_icu = data['icu']
                    th_max = data['daymax']
                    h_icupeak = data['icumax']
                    slope_max = config['hammer_slope']
                    th_plot = [
                        date_1 + datetime.timedelta(days=a - 1)
                        for a in th_trig
                    ]
                    plt.scatter(th_plot,
                                h_icu,
                                c=h_icurate,
                                vmin=0,
                                vmax=slope_max,
                                marker='o',
                                s=8)
                    th_plot = [
                        date_1 + datetime.timedelta(days=a - 1) for a in th_max
                    ]
                    plt.scatter(th_plot,
                                h_icupeak,
                                c=h_icurate,
                                vmin=0,
                                vmax=slope_max,
                                marker='o',
                                s=8)
                    cbar = plt.colorbar()
                    cbar.set_label("daily ICU", labelpad=+1)
                except:
                    print('no hammer entries in ', path)
                    pass

            # Save posterior plots
            color = 'blue'
            if np.size(y_obs) > 0:
                plt.scatter(t_obs, y_obs, marker='o', c='k', label='Data')
            plt.plot(times, posterior_curves, alpha=transparency, c=color)
            plt.plot(times,
                     post_mean,
                     lw=2,
                     ls=':',
                     c=color,
                     label='Mean of posterior')
            plt.plot(times,
                     post_med,
                     lw=2,
                     c=color,
                     label='Median of posterior')
            plt.xlim(date_1, times[config['XMAX']])
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval(
                (times[config['XMAX']] - date_1).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.gcf().autofmt_xdate()
            plt.ylim(0, ymax)
            plt.xlabel('Date')
            plt.ylabel(ylabel)
            plt.legend(loc='upper left')
            plt.title(title + ' posterior ensemble')
            plt.savefig('{}_posterior_ensemble_{}.png'.format(
                outpath, calmode),
                        dpi=300)

            plt.xlim(date_1, times[-1])
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval((times[-1] - date_1).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.gcf().autofmt_xdate()
            plt.savefig('{}_posterior_ensemble_{}_longterm.png'.format(
                outpath, calmode),
                        dpi=300)
            plt.ylim(0, posterior_curves.max())
            plt.savefig('{}_posterior_ensemble_{}_longterm_alt.png'.format(
                outpath, calmode),
                        dpi=300)
            plt.close()

        # Save results to CSV
        posterior_prior_data[calmode] = save_posterior_and_prior(
            posterior_curves, t_ind, p_values, y_obs, steps, post_mean,
            outpath, calmode, config, header, save_files)

    # Save alpha results to CSV
    posterior_prior_data['alpha'] = save_prior_and_posterior_alpha(
        results, config, steps, outpath, save_files)

    return posterior_prior_data
Ejemplo n.º 4
0
def plot_confidence(outpath, config, inputdata, firstdate):

    calmodes = [S_HOS, S_ICU, S_HOSCUM, S_DEAD, S_INF, S_ALPHARUN]
    o_indices = [O_HOS, O_ICU, O_HOSCUM, O_DEAD, O_CUMINF, O_ALPHARUN]

    titles = [
        'Hospitalized', 'ICU', 'Hospitalized Cum.', 'Mortalities', 'Infected',
        '1-$\\alpha$'
    ]
    y_label = [
        'Number of cases', 'Number of cases', 'Number of cases',
        'Number of cases', 'Number of cases', '1-$\\alpha$'
    ]
    symcolors = [['powderblue', 'steelblue'], ['peachpuff', 'sandybrown'],
                 ['lightgreen', 'forestgreen'], ['silver', 'grey'],
                 ['mistyrose', 'lightcoral'], ['plum', 'mediumorchid']]
    y_obs_s = [
        inputdata[:, I_HOS], inputdata[:, I_ICU], inputdata[:, I_HOSCUM],
        inputdata[:, I_DEAD], inputdata[:, I_INF], []
    ]
    y_maxdef = config['YMAX']
    y_maxhos = y_maxdef * get_mean(config['hosfrac'])
    y_maxicu = y_maxhos * get_mean(config['ICufrac'])
    y_maxdead = y_maxhos * get_mean(config['dfrac']) * 4
    y_maxinf = y_maxdef * 5
    y_max = [y_maxhos * 1.5, y_maxicu, y_maxhos * 4, y_maxdead, y_maxinf, 1]

    casename = ''
    try:
        casename = config['plot']['casename']
    except:
        print('No casename in plot parameters')
        pass

    daily = False
    try:
        daily = config['plot']['daily']
    except:
        print(
            'No daily in plot parameters, assuming cumulative display of mortalities and hospitalized cum. Infected, ICU and hospitalized plotted as actual'
        )
        pass
    nolegend = False
    try:
        nolegend = config['plot']['nolegend']
    except:
        print(
            'No daily in plot parameters, assuming cumulative display of mortalities and hospitalized cum. Infected, ICU and hospitalized plotted as actual'
        )
        pass

    x_obs = inputdata[:, 0]
    xmax = config['XMAX']
    time_delay = config['time_delay']
    xmax = xmax + time_delay
    #  make four output plots for hospitalized, cum hospitalized, dead, and hosm
    for i, calmode in enumerate(calmodes):
        output_index = o_indices[i]
        y_obs = None
        title = titles[i]
        symcolor = symcolors[i]
        y_obs = y_obs_s[i]
        ymax = y_max[i]
        ylabel = y_label[i]
        if (daily):
            if ((i == 2) or (i == 3) or (i == 4)):
                y_obs = np.concatenate((np.array([0]), np.diff(y_obs)))
                title = 'Daily ' + titles[i]
                ymax = ymax * 0.1

        # read the
        modelpath = '{}_posterior_prob_{}_calibrated_on_{}.csv'.format(
            outpath, calmode, config['calibration_mode'])
        modeldata = np.genfromtxt(modelpath, names=True, delimiter=',')

        conf_level = [a for a in modeldata.dtype.names if 'P' in a]
        if (daily):
            if ((i == 2) or (i == 3) or (i == 4)):
                for ilevel, cl in enumerate(conf_level):
                    modeldata[cl] = np.concatenate(
                        (np.array([0]), np.diff(modeldata[cl])))
                modeldata['mean'] = np.concatenate(
                    (np.array([0]), np.diff(modeldata['mean'])))

        time = modeldata['time']
        mean = modeldata['mean']
        dolabel = True  # fig, ax = plt.subplots()
        try:
            figure_size = config['plot']['figure_size']
            assert len(figure_size) == 2
            plt.figure(figsize=figure_size)
        except:
            plt.figure()
        date_1 = datetime.datetime.strptime(firstdate, "%m/%d/%y")
        t = [date_1 + datetime.timedelta(days=a - 1) for a in time]

        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((t[xmax] - t[0]).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))

        plt.plot(t, mean, label='Mean (Expectation value)', c='k', lw=0.5)
        symcolor = symcolors[i]
        ls = ['-', '--']
        for ilevel, cl in enumerate(conf_level[2:-2]):
            plt.plot(t, modeldata[cl], label=cl, c='k', ls=ls[ilevel], lw=0.25)
        for iconf in range(0, 2):
            conf_range = float(conf_level[-1 - iconf].strip('P')) - float(
                conf_level[iconf].strip('P'))
            c = symcolor[iconf]
            plt.fill_between(
                t,
                modeldata[conf_level[iconf]],
                modeldata[conf_level[-1 - iconf]],
                label='{}% confidence interval'.format(conf_range),
                color=c)
        if np.size(y_obs) > 0:
            x_days = [date_1 + datetime.timedelta(days=a - 1) for a in x_obs]
            plt.scatter(x_days, y_obs, c='k', label='data', marker='o', s=8)

        plot_hammer = False
        try:
            plot_hammer = config['plot']['hammer']
        except:
            pass

        if (plot_hammer and (i == 1)):
            # consider to plot the analyse_hammer results
            path = '{}_hammer_diagnostics{}.csv'.format(outpath, '' '')
            data = np.genfromtxt(path, names=True, delimiter=',')
            th = data['daymon']
            hicurate = data['icurate']
            hicupeak = data['icumax']
            slope_max = config['hammer_slope']
            try:
                thplot = [date_1 + datetime.timedelta(days=a - 1) for a in th]
                plt.scatter(thplot,
                            hicupeak,
                            c=hicurate,
                            vmin=0,
                            vmax=slope_max,
                            marker='o',
                            s=8)
                if (not nolegend):
                    cbar = plt.colorbar()
                    cbar.set_label("daily ICU", labelpad=+1)
            except:
                print('no hammer entries in ', path)
                pass
        plt.grid(True)

        legendloc = 'upper left'
        try:
            legendloc = config['plot']['legendloc']
        except:
            print('No legendloc plot parameters, taking', legendloc)
            pass

        if (not nolegend):
            plt.legend(loc=legendloc)

        if (dolabel):
            plt.xlabel('Date')
        plt.ylabel(ylabel, fontsize=12)
        title = title + ' ' + casename
        if (not nolegend):
            plt.title(title)
        if (config['plot']['y_axis_log']):
            plt.yscale('log')
            if (ymax > 0):
                plt.ylim(1, ymax)
        else:
            plt.yscale('linear')
            plt.ylim(0, ymax)
        # plt.savefig('Hospital_cases_log.png', dpi=300)

        #plt.xlim([t[0], t[-1]])
        plt.xlim([t[0], t[xmax]])

        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((t[xmax] - t[0]).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.gcf().autofmt_xdate()

        if (not dolabel):
            frame1 = plt.gca()
            for xlabel_i in frame1.axes.get_xticklabels():
                xlabel_i.set_visible(False)
                xlabel_i.set_fontsize(0.0)

        outputpath = '{}_posterior_prob_{}_calibrated_on_{}.png'.format(
            outpath, calmode, config['calibration_mode'])
        plt.savefig(outputpath, dpi=300)

        if np.size(y_obs) > 0:
            # Have to make a new plot to change the size
            plt.figure(figsize=plt.rcParams["figure.figsize"])

            plt.plot(t, mean, label='Mean (Expectation value)', c='k', lw=0.5)
            for ilevel, cl in enumerate(conf_level[2:-2]):
                plt.plot(t,
                         modeldata[cl],
                         label=cl,
                         c='k',
                         ls=ls[ilevel],
                         lw=0.25)
            for iconf in range(0, 2):
                conf_range = float(conf_level[-1 - iconf].strip('P')) - float(
                    conf_level[iconf].strip('P'))
                c = symcolor[iconf]
                plt.fill_between(
                    t,
                    modeldata[conf_level[iconf]],
                    modeldata[conf_level[-1 - iconf]],
                    label='{}% confidence interval'.format(conf_range),
                    color=c)
            if y_obs.any():
                x_days = [
                    date_1 + datetime.timedelta(days=a - 1) for a in x_obs
                ]
                plt.scatter(x_days,
                            y_obs,
                            c='k',
                            label='data',
                            marker='o',
                            s=8)

            plt.grid(True)
            plt.xlabel('Date')
            plt.ylabel('Number of cases')
            plt.title(title)
            plt.yscale('linear')

            legendloc = 'lower right'
            try:
                legendloc = config['plot']['legendloczoom']
            except:
                print('No legendloczoom plot zoom parameters, taking',
                      legendloc)
                pass
            plt.yscale('linear')
            plt.legend(loc=legendloc)
            inow = np.size(y_obs)
            i1 = inow - 15
            i1 = max(0, i1)
            # ax.axvline(x=inow, color='silver')
            i2 = i1 + 25
            plt.xlim([t[i1], t[i2]])
            if (dolabel):
                plt.gca().xaxis.set_major_formatter(
                    mdates.DateFormatter('%d %b'))
                day_interval = calc_axis_interval((t[i2] - t[i1]).days)
                plt.gca().xaxis.set_major_locator(
                    mdates.DayLocator(interval=day_interval))
            plt.ylim(np.min(modeldata[conf_level[0]][i1:i2]),
                     np.max(modeldata[conf_level[-1]][i1:i2]))
            outputpath = '{}_posterior_prob_{}_calibrated_on_{}_zoom.png'.format(
                outpath, calmode, config['calibration_mode'])
            plt.savefig(outputpath, dpi=300)
            plt.close()
Ejemplo n.º 5
0
def plot_prior_and_posterior(results, fwd_args, config, configpath, t_obs,
                             data, calibration_mode, output_index):
    # Plot prior
    base = (os.path.split(configpath)[-1]).split('.')[0]
    outpath = os.path.join(os.path.split(os.getcwd())[0], 'output', base)

    date_1 = datetime.datetime.strptime(config['startdate'], "%m/%d/%y")
    t_obs = [date_1 + datetime.timedelta(days=a - 1) for a in t_obs]

    calmodes = [S_HOS, S_ICU, S_HOSCUM, S_DEAD, S_INF]
    o_indices = [O_HOS, O_ICU, O_HOSCUM, O_DEAD, O_CUMINF]
    # useworldfile = config['worldfile']
    # if (useworldfile):
    #    calmodes = [ S_DEAD]
    #    o_indices = [ O_DEAD]
    # else:
    #    # check if I_HOS is present in the data, if not generate it
    #    print('number of columns in data ', data.shape[1])
    #    print('number of rows in data ', data.shape[0])
    #    ncol = data.shape[1]

    #    data = generate_hos_actual(data, config)

    titles = [
        'Hospitalized', 'ICU', 'Hospitalized cum', 'Mortalities', 'infected'
    ]
    # ['lightcoral', 'brown']
    # ['mistyrose', 'lightcoral'],
    symcolors = [['powderblue', 'steelblue'], ['peachpuff', 'sandybrown'],
                 ['lightgreen', 'forestgreen'], ['gainsboro', 'silver'],
                 ['mistyrose', 'lightcoral']]
    y_obs_s = [
        data[:, I_HOS], data[:, I_ICU], data[:, I_HOSCUM], data[:, I_DEAD],
        data[:, I_INF]
    ]
    y_maxdef = config['YMAX']
    y_maxhos = y_maxdef * get_mean(config['hosfrac'])
    y_maxicu = y_maxhos * get_mean(config['ICufrac'])
    y_maxdead = y_maxhos * get_mean(config['dfrac']) * 4
    y_maxinf = y_maxdef * 10
    y_max = [y_maxhos, y_maxicu, y_maxhos * 4, y_maxdead, y_maxinf]

    casename = ''
    try:
        casename = config['plot']['casename']
    except:
        print('No casename in plot parameters')
        pass

    for i, calmode in enumerate(calmodes):
        output_index = o_indices[i]
        title = titles[i]
        symcolor = symcolors[i]
        y_obs = y_obs_s[i]
        ymax = y_max[i]

        prior = results['fw'][0]
        transparancy = min(5.0 / len(prior), 1)
        print(calmode, output_index)
        prior_curves = np.array([member[output_index, :]
                                 for member in prior]).T
        time = fwd_args['time'] - fwd_args['time_delay']
        times = [date_1 + datetime.timedelta(days=a - 1) for a in time]
        prior_mean = np.mean(prior_curves, axis=-1)
        color = 'green'
        plt.scatter(t_obs, y_obs, marker='o', c='k', label='Data')
        plt.plot(times, prior_curves, alpha=transparancy, c=color)
        plt.plot(times, prior_mean, lw=2, c=color, label='Mean of prior')
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval(
            (times[config['XMAX']] - date_1).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.xlim(date_1, times[config['XMAX']])
        plt.ylim(0, ymax)
        plt.xlabel('Time [days]')
        plt.gcf().autofmt_xdate()
        plt.ylabel('Number of cases')
        plt.legend(loc='upper left')
        plt.title(title + ' prior ensemble')
        plt.savefig('{}_prior_ensemble_{}.png'.format(outpath, calmode),
                    dpi=300)
        plt.close()

        # Plot posterior
        posterior = results['fw'][-1]
        posterior_curves = np.array(
            [member[output_index, :] for member in posterior]).T
        post_mean = np.mean(posterior_curves, axis=-1)
        post_med = np.median(posterior_curves, axis=-1)
        color = 'blue'
        plt.scatter(t_obs, y_obs, marker='o', c='k', label='Data')
        plt.plot(times, posterior_curves, alpha=transparancy, c=color)
        plt.plot(times,
                 post_mean,
                 lw=2,
                 ls=':',
                 c=color,
                 label='Mean of posterior')
        plt.plot(times, post_med, lw=2, c=color, label='Median of posterior')
        plt.xlim(date_1, times[config['XMAX']])
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval(
            (times[config['XMAX']] - date_1).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.gcf().autofmt_xdate()
        plt.ylim(0, ymax)
        plt.xlabel('Time [days]')
        plt.ylabel('Number of cases')
        plt.legend(loc='upper left')
        plt.title(title + ' posterior ensemble')
        plt.savefig('{}_posterior_ensemble_{}.png'.format(outpath, calmode),
                    dpi=300)

        plt.xlim(date_1, times[-1])
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((times[-1] - date_1).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.gcf().autofmt_xdate()
        plt.savefig('{}_posterior_ensemble_{}_longterm.png'.format(
            outpath, calmode),
                    dpi=300)
        plt.ylim(0, posterior_curves.max())
        plt.savefig('{}_posterior_ensemble_{}_longterm_alt.png'.format(
            outpath, calmode),
                    dpi=300)
        plt.close()

        p_values = config['p_values']
        p_array = []
        steps = np.arange(1, time.max() + 1)
        t_ind = [np.where(time == a)[0][0] for a in steps]
        posterior_length = posterior_curves.shape[1]
        for post_day in posterior_curves[t_ind, :]:
            array_sorted = np.sort(post_day)
            p_array.append(
                [array_sorted[int(posterior_length * p)] for p in p_values])

        h_pvalues = ['P' + str(int(100 * a)) for a in p_values]
        header = 'time,mean,' + ','.join(h_pvalues) + ',observed'
        p_array = np.asarray(p_array)
        observed = np.pad(y_obs, (0, len(steps) - len(y_obs)),
                          mode='constant',
                          constant_values=np.nan)[:, None]
        table = np.concatenate(
            (steps[:, None], post_mean[t_ind, None], p_array, observed),
            axis=1)
        np.savetxt('{}_posterior_prob_{}_calibrated_on_{}.csv'.format(
            outpath, calmode, config['calibration_mode']),
                   table,
                   header=header,
                   delimiter=',',
                   comments='',
                   fmt='%.2f')

    # save calibrated alfa values over time
    mnames = results['mnames']
    mvalues = results['mvalues']

    dayalpha1 = config['dayalpha']
    dayalpha = np.array(dayalpha1)
    # posterior
    alpha = np.zeros(np.size(dayalpha))
    alpha_sd = np.zeros(np.size(dayalpha))
    # priors
    alpha0 = np.zeros(np.size(dayalpha))
    alpha0_sd = np.zeros(np.size(dayalpha))
    icount = 0
    for j, val in enumerate(mnames):
        if val == 'a':
            alpha0[icount] = mvalues[j][0]
            alpha0_sd[icount] = mvalues[j][1]
            alpha[icount] = mvalues[j][2]
            alpha_sd[icount] = mvalues[j][3]
            icount += 1

    names = ['posterior', 'prior']
    for j in range(0, 2):
        alpha_t = np.zeros_like(steps)
        alpha_sd_t = np.zeros_like(steps)
        for k, dayr in enumerate(dayalpha):
            if (j == 0):
                alpha_t[steps.tolist().index(dayr):] = alpha[k]
                alpha_sd_t[steps.tolist().index(dayr):] = alpha_sd[k]
            else:
                alpha_t[steps.tolist().index(dayr):] = alpha0[k]
                alpha_sd_t[steps.tolist().index(dayr):] = alpha0_sd[k]

        p_values = [0.05, 0.25, 0.5, 0.75, 0.95]
        h_pvalues = ['P' + str(int(100 * a)) for a in p_values]
        header = 'time,' + ','.join(h_pvalues)
        p_array = np.asarray(p_array)
        observed = np.pad(y_obs, (0, len(steps) - len(y_obs)),
                          mode='constant',
                          constant_values=np.nan)[:, None]
        p5 = alpha_t + alpha_sd_t * 1.96
        p95 = alpha_t - alpha_sd_t * 1.96
        p25 = alpha_t + alpha_sd_t * 0.69
        p75 = alpha_t - alpha_sd_t * 0.69
        table = np.concatenate((steps[:, None], p5[:, None], p25[:, None],
                                alpha_t[:, None], p75[:, None], p95[:, None]),
                               axis=1)
        np.savetxt('{}_{}_prob_{}_calibrated_on_{}.csv'.format(
            outpath, names[j], 'alpha', config['calibration_mode']),
                   table,
                   header=header,
                   delimiter=',',
                   comments='',
                   fmt='%.2f')
Ejemplo n.º 6
0
def plot_confidence(configpath, config, inputdata, firstdate):

    base = (os.path.split(configpath)[-1]).split('.')[0]
    outpath = os.path.join(os.path.split(os.getcwd())[0], 'output', base)
    calmodes = [S_HOS, S_ICU, S_HOSCUM, S_DEAD, S_INF]
    o_indices = [O_HOS, O_ICU, O_HOSCUM, O_DEAD, O_CUMINF]

    titles = [
        'Hospitalized', 'ICU', 'Hospitalized Cum.', 'Mortalities', 'Infected'
    ]
    #['lightcoral', 'brown']
    #['mistyrose', 'lightcoral'],
    symcolors = [['powderblue', 'steelblue'], ['peachpuff', 'sandybrown'],
                 ['lightgreen', 'forestgreen'], ['silver', 'grey'],
                 ['mistyrose', 'lightcoral']]
    y_obs_s = [
        inputdata[:, I_HOS], inputdata[:, I_ICU], inputdata[:, I_HOSCUM],
        inputdata[:, I_DEAD], inputdata[:, I_INF]
    ]
    y_maxdef = config['YMAX']
    y_maxhos = y_maxdef * get_mean(config['hosfrac'])
    y_maxicu = y_maxhos * get_mean(config['ICufrac'])
    y_maxdead = y_maxhos * get_mean(config['dfrac']) * 4
    y_maxinf = y_maxdef * 5
    y_max = [y_maxhos, y_maxicu, y_maxhos * 4, y_maxdead, y_maxinf]

    casename = ''
    try:
        casename = config['plot']['casename']
    except:
        print('No casename in plot parameters')
        pass

    daily = False
    try:
        daily = config['plot']['daily']
    except:
        print(
            'No daily in plot parameters, assuming cumulative display of mortalities and hospitalized cum. Infected, ICU and hospitalized plotted as actual'
        )
        pass

    x_obs = inputdata[:, 0]
    xmax = config['XMAX']
    time_delay = config['time_delay']
    xmax = xmax + time_delay
    #  make four output plots for hospitalized, cum hospitalized, dead, and hosm
    for i, calmode in enumerate(calmodes):
        output_index = o_indices[i]
        y_obs = None
        title = titles[i]
        symcolor = symcolors[i]
        y_obs = y_obs_s[i]
        ymax = y_max[i]

        if (daily):
            if ((i == 2) or (i == 3) or (i == 4)):
                y_obs = np.concatenate((np.array([0]), np.diff(y_obs)))
                title = 'Daily ' + titles[i]
                ymax = ymax * 0.1

        # read the
        modelpath = '{}_posterior_prob_{}_calibrated_on_{}.csv'.format(
            outpath, calmode, config['calibration_mode'])
        modeldata = np.genfromtxt(modelpath, names=True, delimiter=',')

        conf_level = [a for a in modeldata.dtype.names if 'P' in a]
        if (daily):
            if ((i == 2) or (i == 3) or (i == 4)):
                for ilevel, cl in enumerate(conf_level):
                    modeldata[cl] = np.concatenate(
                        (np.array([0]), np.diff(modeldata[cl])))
                modeldata['mean'] = np.concatenate(
                    (np.array([0]), np.diff(modeldata['mean'])))

        time = modeldata['time']
        mean = modeldata['mean']

        # fig, ax = plt.subplots()
        try:
            figure_size = config['plot']['figure_size']
            assert len(figure_size) == 2
            plt.figure(figsize=figure_size)
        except:
            pass
        # plt.figure()
        date_1 = datetime.datetime.strptime(firstdate, "%m/%d/%y")
        t = [date_1 + datetime.timedelta(days=a - 1) for a in time]
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((t[xmax] - t[0]).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))

        plt.plot(t, mean, label='Mean (Expectation value)', c='k', lw=0.5)
        symcolor = symcolors[i]
        ls = ['-', '--']
        for ilevel, cl in enumerate(conf_level[2:-2]):
            plt.plot(t, modeldata[cl], label=cl, c='k', ls=ls[ilevel], lw=0.25)
        for iconf in range(0, 2):
            conf_range = float(conf_level[-1 - iconf].strip('P')) - float(
                conf_level[iconf].strip('P'))
            c = symcolor[iconf]
            plt.fill_between(
                t,
                modeldata[conf_level[iconf]],
                modeldata[conf_level[-1 - iconf]],
                label='{}% confidence interval'.format(conf_range),
                color=c)
        if y_obs.any():
            x_days = [date_1 + datetime.timedelta(days=a - 1) for a in x_obs]
            plt.scatter(x_days, y_obs, c='k', label='data', marker='o', s=8)

        plt.grid(True)

        legendloc = 'upper left'
        try:
            legendloc = config['plot']['legendloc']
        except:
            print('No legendloc plot parameters, taking', legendloc)
            pass

        plt.legend(loc=legendloc)
        plt.xlabel('Date')
        plt.ylabel('Number of cases')
        title = title + ' ' + casename
        plt.title(title)
        # plt.yscale('log')
        # plt.savefig('Hospital_cases_log.png', dpi=300)
        plt.yscale('linear')
        #plt.xlim([t[0], t[-1]])
        plt.xlim([t[0], t[xmax]])
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((t[xmax] - t[0]).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.gcf().autofmt_xdate()
        plt.ylim(0, ymax)
        outputpath = '{}_posterior_prob_{}_calibrated_on_{}.png'.format(
            outpath, calmode, config['calibration_mode'])
        plt.savefig(outputpath, dpi=300)

        if y_obs.any():
            # Have to make a new plot to change the size
            plt.figure(figsize=plt.rcParams["figure.figsize"])

            plt.plot(t, mean, label='Mean (Expectation value)', c='k', lw=0.5)
            for ilevel, cl in enumerate(conf_level[2:-2]):
                plt.plot(t,
                         modeldata[cl],
                         label=cl,
                         c='k',
                         ls=ls[ilevel],
                         lw=0.25)
            for iconf in range(0, 2):
                conf_range = float(conf_level[-1 - iconf].strip('P')) - float(
                    conf_level[iconf].strip('P'))
                c = symcolor[iconf]
                plt.fill_between(
                    t,
                    modeldata[conf_level[iconf]],
                    modeldata[conf_level[-1 - iconf]],
                    label='{}% confidence interval'.format(conf_range),
                    color=c)
            if y_obs.any():
                x_days = [
                    date_1 + datetime.timedelta(days=a - 1) for a in x_obs
                ]
                plt.scatter(x_days,
                            y_obs,
                            c='k',
                            label='data',
                            marker='o',
                            s=8)

            plt.grid(True)
            plt.xlabel('Date')
            plt.ylabel('Number of cases')
            title = title + ' ' + casename
            plt.title(title)
            plt.yscale('linear')

            legendloc = 'lower right'
            try:
                legendloc = config['plot']['legendloczoom']
            except:
                print('No legendloczoom plot zoom parameters, taking',
                      legendloc)
                pass
            plt.legend(loc=legendloc)
            inow = np.size(y_obs)
            i1 = inow - 15
            i1 = max(0, i1)
            # ax.axvline(x=inow, color='silver')
            i2 = i1 + 25
            plt.xlim([t[i1], t[i2]])
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval((t[i2] - t[i1]).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.ylim(np.min(modeldata[conf_level[0]][i1:i2]),
                     np.max(modeldata[conf_level[-1]][i1:i2]))
            outputpath = '{}_posterior_prob_{}_calibrated_on_{}_zoom.png'.format(
                outpath, calmode, config['calibration_mode'])
            plt.savefig(outputpath, dpi=300)
            plt.close()
Ejemplo n.º 7
0
def save_and_plot_IC(config, output_base_filename, save_plots):
    outpath = os.path.join(
        os.path.split(os.getcwd())[0], 'output', output_base_filename)

    firstdate = config['startdate']

    direc = '.'
    # read icufracs from a datafile, these are
    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'
    data = np.genfromtxt(os.path.join(direc, ic_data_file), names=True)

    # Check if database has RIVM hospitalized data. If NOT, then delete that row
    if data[-1][-1] == 0:
        data = data[:-1]

    t = data['day']
    tmin = t[0]
    tmax = t[-1]
    icucum_obs = data['icucum']
    icudead_obs = data['icudead']
    icurec_obs = data['icurec']
    icupres_obs = data['icupres']
    hosprec_obs = data['hosprec']
    hoscum_obs = data['hoscum']

    delay_hosrec = 0

    delay_rec = get_mean(config['delayREC'])
    delay_hos = get_mean(config['delayHOS'])
    #delay_hosrec = get_mean(config['delayHOSREC'])
    delay_hosd = get_mean(config['delayHOSD'])
    delay_icu = get_mean(config['delayICUCAND'])
    delay_icud = get_mean(config['delayICUD'])
    delay_icurec = get_mean(config['delayICUREC'])
    hosfrac = get_mean(config['hosfrac'])
    dfrac = get_mean(config['dfrac'])

    rec_sd = to_gauss_smooth_sd(config['delayREC'])
    hos_sd = to_gauss_smooth_sd(config['delayHOS'])
    hosrec_sd = to_gauss_smooth_sd(config['delayHOSREC'])
    hosd_sd = to_gauss_smooth_sd(config['delayHOSD'])

    icu_sd = to_gauss_smooth_sd(config['delayICUCAND'])
    icud_sd = to_gauss_smooth_sd(config['delayICUD'])
    icurec_sd = to_gauss_smooth_sd(config['delayICUREC'])

    icufrac = 0.35
    # icufrac =config['ICufrac']
    icudfrac = get_mean(config['icudfrac'])

    names = [S_ICUCUM, S_ICUDEAD, S_HOSPREC, S_ICUPRES, S_ICUREC, S_HOSCUM]
    #y_obs = [icucum_obs, icudead_obs, icurec_obs, icupres_obs, hosprec_obs]
    y_obs = [
        icucum_obs, icudead_obs, hosprec_obs + icurec_obs, icupres_obs,
        icurec_obs, hoscum_obs
    ]
    colors = [
        'maroon', 'black', 'darkgreen', 'coral', 'mistyrose', 'lightgreen'
    ]
    colors = [
        'royalblue', 'black', 'darkgreen', 'sandybrown', 'mistyrose', 'coral'
    ]

    nstart = 10
    tstart = np.arange(-10, 1, 1)
    other0 = np.zeros(len(tstart))
    t = np.concatenate((tstart, t), axis=-1)
    for i, y in enumerate(y_obs):
        y_obs[i] = np.concatenate((other0, y), axis=-1)

    icurate = 15  # 15
    hosrate = 80
    nend = 20
    tend = np.arange(tmax + 1, tmax + nend, 1)
    icugrowth = (tend - tend[0] + 1) * icurate
    hosgrowth = (tend - tend[0] + 1) * hosrate
    other0 = np.zeros(len(tend))
    t = np.concatenate((t, tend), axis=-1)
    for i, y in enumerate(y_obs):
        other = other0 + y[-1]
        if (i == I_ICUCUM):
            other = other + icugrowth
        if (i == I_HOSCUM):
            other = other + hosgrowth
        y_obs[i] = np.concatenate((y, other), axis=-1)

    icucum_obs = y_obs[0]
    icudead_obs = y_obs[1]
    icurec_obs = y_obs[2]
    icupres_obs = y_obs[3]
    hosprec_obs = y_obs[4]
    hoscum_obs = y_obs[5]

    gausssmooth_hosp = 1.5
    gausssmooth_icu = 1.5
    germanmax = 0

    hoscum_pred = hoscum_obs
    hoscum_pred = gauss_smooth_shift(hoscum_obs, 0, gausssmooth_hosp)
    # estimate icu frac

    icucum = icucum_obs * 1.0
    icucorrectgerman = icucum * 0.0
    germandaily = 8.0
    indexscale = np.asarray(np.where(t > 30)[0])

    for i, index in enumerate(indexscale):
        ioffset = indexscale[0]
        iact = index
        icucorrectgerman[iact] = min(germanmax, (iact - ioffset) * germandaily)
    icucum = icucum_obs - icucorrectgerman

    #hos = dataprocess[:, OP_HOS]

    date_1 = datetime.datetime.strptime(firstdate, "%m/%d/%y")
    tplot = [date_1 + datetime.timedelta(days=a - 1) for a in t]

    dodirect = False
    if (dodirect):
        totalremoved = hosprec_obs + icurec_obs + icudead_obs
        exp_pres = icucum_obs - totalremoved
        obs_pres = icupres_obs
        dif = exp_pres - obs_pres

        icucum = np.roll(icucum, int(delay_icu))
        icucum[:int(delay_icu)] = 0

        icu_rechos = gauss_smooth_shift(icucum,
                                        delay_icurec,
                                        icurec_sd,
                                        scale=(1 - icudfrac))
        icu_recfull = gauss_smooth_shift(icu_rechos, delay_hosrec, 0)
        # dead from icu
        icu_dead = gauss_smooth_shift(icucum,
                                      delay_icud,
                                      icud_sd,
                                      scale=icudfrac)
        icu = icucum - icu_dead - icu_rechos
    else:
        OP_HOS = 0
        OP_HOSCUM = 1
        OP_ICU = 2
        OP_ICUCUM = 3
        OP_REC = 4
        OP_DEAD = 5
        OP_ICUREC = 6
        OP_ICUDEAD = 7

        hosday2 = np.concatenate((np.array([0]), np.diff(hoscum_pred)))
        hosday = hosday2 * 1.0
        hosday = np.clip(hosday, 1, max(hosday))
        icuday2 = np.concatenate((np.array([0]), np.diff(icucum)))
        icuday = icuday2 * 1.0
        icuday = gauss_smooth_shift(icuday, 0, gausssmooth_icu)
        icufrac = icuday / hosday
        icufrac = np.clip(icufrac, 0, 1)

        # Check if the icufrac file is defined in config, otherwise use the output_base_filename
        try:
            if config['output_base_filename'] == 'netherlands_dashboard':
                try:
                    icufrac_filename = config['icufracfile']
                except KeyError:
                    print(
                        "No icufracfile in config, using base filename {}_icufrac.txt"
                        .format(output_base_filename))
                    # filename = os.path.join(os.path.split(os.getcwd())[0], 'bin', 'output', output_base_filename)
                    filename = os.path.join(os.getcwd(), 'bin', 'output',
                                            output_base_filename)
                    icufrac_filename = '{}_icufrac.txt'.format(filename)
                from pathlib import Path
                Path(os.path.split(icufrac_filename)[0]).mkdir(parents=True,
                                                               exist_ok=True)
            else:
                raise KeyError
        except KeyError:
            try:
                filename = os.path.split(config['icufracfile'])[-1]
                # icufrac_filename = os.path.join(os.path.split(os.getcwd())[0], 'output', filename)
                icufrac_filename = os.path.join(os.getcwd(), 'bin', 'output',
                                                output_base_filename)
            except KeyError:
                print(
                    "No icufracfile in config, using base filename {}_icufrac.txt"
                    .format(output_base_filename))
                # filename = os.path.join(os.path.split(os.getcwd())[0], 'output', output_base_filename)
                filename = os.path.join(os.getcwd(), 'bin', 'output',
                                        output_base_filename)
                icufrac_filename = '{}_icufrac.txt'.format(filename)

        save_input_data(icufrac_filename, t, icufrac, tmin, tmax)

        if save_plots:
            fracs = [hosfrac, dfrac, icufrac, icudfrac]
            delays = [
                delay_rec, delay_hos, delay_hosrec, delay_hosd, delay_icu,
                delay_icud, delay_icurec
            ]
            gauss_sd = [
                rec_sd, hos_sd, hosrec_sd, hosd_sd, icu_sd, icud_sd, icurec_sd
            ]
            #gauss_sd = [0, 0, 0, 0, 0, icud_sd, icurec_sd]

            r = gauss_smooth_shift(hoscum_pred,
                                   -delay_hos,
                                   hos_sd,
                                   scale=1.0 / hosfrac)
            dataprocess = do_hospitalization_process(hoscum_pred,
                                                     delays,
                                                     fracs,
                                                     gauss_stddev=gauss_sd,
                                                     removed=r)

            rec = dataprocess[:, OP_REC, None]
            hos = dataprocess[:, OP_HOS, None]
            icu = dataprocess[:, OP_ICU, None]
            icucum = dataprocess[:, OP_ICUCUM, None]
            icu_dead = dataprocess[:, OP_ICUDEAD, None]
            icu_recfull = dataprocess[:, OP_ICUREC, None]

            fig, ax = plt.subplots()
            plt.figure(figsize=(6.0, 6.0))
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval(
                (tplot[nstart] - tplot[-nend]).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.gcf().autofmt_xdate()

            plt.legend(loc='upper left')
            plt.xlabel('Date', fontsize=12)
            plt.ylabel('number of cases', fontsize=12)
            width = 0.4
            p1 = plt.bar(tplot, icuday, width)
            p2 = plt.bar(tplot, (hosday - icuday), width, bottom=icuday)
            outputpath = '{}_{}_{}.png'.format(outpath, 'ICanalysis', 'hosICU')
            #plt.title('daily hospitalization and ICU')
            plt.legend((p1[0], p2[0]), ('Daily ICU', 'Daily Hospitalized'))
            plt.xlim([date_1, tplot[-nend]])
            # plt.xlim(tmin, tmax)
            plt.grid(True)
            plt.savefig(outputpath, dpi=300)

            fig, ax = plt.subplots()
            plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
            day_interval = calc_axis_interval(
                (tplot[nstart] - tplot[-nend]).days)
            plt.gca().xaxis.set_major_locator(
                mdates.DayLocator(interval=day_interval))
            plt.gcf().autofmt_xdate()
            plt.xlabel('Date')
            plt.title('daily intake ICU')
            plt.ylabel('fraction of daily hospitalization')
            width = 0.4
            rel = np.clip(icuday / hosday, 0, 1)
            one = rel * 0.0 + 1.0
            p1 = plt.bar(tplot, rel, width)
            #p2 = plt.bar(t, (one-rel), width, bottom=rel)
            outputpath = '{}_{}_{}.png'.format(outpath, 'ICanalysis',
                                               'hosICU%')
            plt.xlim([date_1, tplot[-nend]])
            plt.grid(True)

            plt.savefig(outputpath, dpi=300)
            # plt.show()

    if save_plots:
        y_pred = [icucum, icu_dead, icu_recfull, icu, icu_recfull, hoscum_pred]

        fig, ax = plt.subplots()
        plt.figure(figsize=(6.0, 6.0))
        plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
        day_interval = calc_axis_interval((tplot[nstart] - tplot[-nend]).days)
        plt.gca().xaxis.set_major_locator(
            mdates.DayLocator(interval=day_interval))
        plt.gcf().autofmt_xdate()

        for i, name in enumerate(names):
            name2 = name + ' - data'
            if (i != 4):
                #plt.scatter(tplot, y_obs[i], c=colors[i], label=name2, marker='o', s=8)
                plt.scatter(tplot,
                            y_obs[i],
                            c=colors[i],
                            label=name,
                            marker='o',
                            s=8)
            name2 = name + ' - model'
            if (i != 4):
                #plt.plot(tplot, y_pred[i],  c=colors[i], label=name2,  lw=2)
                plt.plot(tplot, y_pred[i], c=colors[i], lw=2)
        #plt.scatter(t, exp_pres, c='b', label='icu pres (expected)', marker='o', s=8)

        plt.legend(loc='upper left')
        plt.xlabel('Date', fontsize=12)
        plt.ylabel('number of cases', fontsize=12)
        title = 'ICU analysis'
        #plt.title(title)
        # plt.yscale('log')
        # plt.savefig('Hospital_cases_log.png', dpi=300)
        plt.yscale('linear')
        plt.xlim([date_1, tplot[-nend]])
        plt.ylim(0, 3000)
        plt.grid(True)

        #plt.show()

        outputpath = '{}_{}_{}.png'.format(outpath, 'ICanalysis', 'data')
        plt.savefig(outputpath, dpi=300)