示例#1
0
def single_run_mean(config, data):
    ndata = np.size(data[:, 0])
    m_prior, fwd_args = parse_config(config, ndata, mode='mean')
    m_prior = reshape_prior(m_prior)
    param = m_prior[0]
    res = base_seir_model(param, fwd_args)

    try:
        posterior_param = [param]
        accc_timeinterval = config['ACCC_timeinterval']
        accc_timestart = config['ACCC_timestart']
        accc_step = config['ACCC_step']
        accc_maxstep = config['ACCC_maxstep']
        accc_step_sd = config['ACCC_step_sd']
        accc_low = config['ACCC_low']
        accc_slope = config['ACCC_slope']
        accc_scale = config['ACCC_scale']
        accc_cliplow = config['ACCC_cliplow']
        accc_cliphigh = config['ACCC_cliphigh']
        time_delay = config['time_delay']
        hammer_icu = config['hammer_ICU']
        hammer_slope = config['hammer_slope']
        hammer_release = config['hammer_release']
        hammer_alpha = config['hammer_alpha']
        accc_results = apply_accc(config['output_base_filename'],
                                  posterior_param,
                                  fwd_args,
                                  accc_timeinterval,
                                  accc_timestart,
                                  accc_maxstep,
                                  accc_step,
                                  accc_step_sd,
                                  accc_low,
                                  accc_slope,
                                  accc_scale,
                                  hammer_icu,
                                  hammer_slope,
                                  hammer_alpha,
                                  hammer_release,
                                  accc_cliplow,
                                  accc_cliphigh,
                                  time_delay,
                                  data_end=data[-1, 0])
        res = accc_results[0]

    except KeyError:
        pass

    return res
示例#2
0
def find_N(config, data, imatch=I_DEAD, itarget=10):
    """
    Create a prior ensemble of model runs.
    :param config: The entire model configuration in dict-form
    :param  imatch:  column in data to match N
    :param  itarget: number of target value to match N
    :return: N to use
    """

    np.random.seed(1)
    # Parse parameters
    #  get just 1 base case sample corresponding to average

    # results = np.zeros((len(m_prior), 13, len(fwd_args['time'])))

    i_mod = o_calmodes[imatch]
    i_obs = i_calmodes[imatch]
    obsdead = data[:, i_obs]
    time_delay = config['time_delay']
    obsdead_index = np.where(obsdead > itarget)[0][0] + time_delay
    found = False
    icount = 0
    ncountmax = 50
    nnew = 1000

    ndata = np.size(data[:, 0])
    m_prior, fwd_args = parse_config(config, ndata, mode='mean')
    m_prior = reshape_prior(m_prior)
    param = m_prior[0]

    while not found and icount < ncountmax:
        fwd_args['locked']['n'] = nnew
        res = base_seir_model(param, fwd_args)
        moddead = res[i_mod, :]
        moddead_index = np.where(moddead > itarget)

        print('moddead index, obsdead index ', moddead_index[0][0],
              obsdead_index)
        found = moddead_index[0][0] >= obsdead_index
        if not found:
            icount += 1
            nnew = fwd_args['locked']['n'] * 2
            fwd_args['locked']['n'] = nnew

    return nnew
示例#3
0
def run_prior(config):
    """
    Create a prior ensemble of model runs.
    :param config: The entire model configuration in dict-form
    :return: Forward model results, the parameter sets used to create them, and an array of timesteps
    """
    np.random.seed(1)
    # Parse parameters
    m_prior, fwd_args = parse_config(config)
    m_prior = reshape_prior(m_prior)

    # Run simulation
    results = np.zeros((len(m_prior), 12, len(fwd_args['time'])))
    for i in tqdm(range(len(m_prior)), desc='Running prior ensemble'):
        param = m_prior[i]
        results[i] = coronaSEIR.base_seir_model(param, fwd_args)

    tnew = fwd_args['time'] - fwd_args['time_delay']

    return results, tnew, m_prior, fwd_args
示例#4
0
def run_esmda_model(base_filename, config, data, save_plots=1, save_files=1):
    # 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(base_filename, 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]
            if calmode == S_HOS or calmode == S_ICU:
                error = y_obs * (observation_errors[0] / 100)
                error[error <= 0] = 1
            else:
                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]
                    # error2[min(0,len(error2)-20):] *= 1
                    if calmode == S_HOS or calmode == S_ICU:
                        error2 = y_obs2 * (observation_errors[ical] / 100)
                        error2[error2 <= 0] = 1
                    else:
                        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)

    # Run the find_N function only if specified in the config file
    try:
        if config['find_N']:
            nnew = find_N(config, data)
            config['N'] = {
                'type': 'uniform',
                'min': 0.25 * nnew,
                'max': 4 * nnew
            }
    except KeyError:
        pass

    # Parse the configuration json
    ndata = np.size(data[:, 0])
    m_prior, fwd_args = parse_config(config, ndata)
    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)

    prior_and_posterior_results = save_and_plot_prior_and_posterior(
        results, fwd_args, config, base_filename, t_obs, data,
        calibration_mode, output_index, save_plots, save_files)

    if save_files:
        # if True:
        # Add this to have a output folder in main directory
        outpath = os.path.join(os.getcwd(), 'bin', 'output', base_filename)
        # outpath = os.path.join(os.path.split(os.getcwd())[0], 'output', base_filename + '_output.h5')
        save_results(results, fwd_args, config, outpath, data, mode='esmda')

        try:
            accc_timeinterval = config['ACCC_timeinterval']
            accc_timestart = config['ACCC_timestart']
            accc_step = config['ACCC_step']
            accc_maxstep = config['ACCC_maxstep']
            accc_step_sd = config['ACCC_step_sd']
            accc_low = config['ACCC_low']
            accc_slope = config['ACCC_slope']
            accc_scale = config['ACCC_scale']
            accc_cliplow = config['ACCC_cliplow']
            accc_cliphigh = config['ACCC_cliphigh']
            time_delay = config['time_delay']
            hammer_icu = config['hammer_ICU']
            hammer_slope = config['hammer_slope']
            hammer_release = config['hammer_release']
            hammer_alpha = config['hammer_alpha']
            accc_results = apply_accc(base_filename,
                                      results['M'][-1],
                                      fwd_args,
                                      accc_timeinterval,
                                      accc_timestart,
                                      accc_maxstep,
                                      accc_step,
                                      accc_step_sd,
                                      accc_low,
                                      accc_slope,
                                      accc_scale,
                                      hammer_icu,
                                      hammer_slope,
                                      hammer_alpha,
                                      hammer_release,
                                      accc_cliplow,
                                      accc_cliphigh,
                                      time_delay,
                                      data_end=data[-1, 0])
            # TODO: Add hammer results to h5 file and plotting
            results['fw'][-1] = accc_results
            save_and_plot_prior_and_posterior(results,
                                              fwd_args,
                                              config,
                                              base_filename,
                                              t_obs,
                                              data,
                                              calibration_mode,
                                              output_index,
                                              save_plots,
                                              save_files,
                                              plothammer=True)
            add_hammer_to_results(accc_results, outpath, mode='esmda')

        except KeyError:
            pass

    return prior_and_posterior_results, results
示例#5
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!