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'])
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'])
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)
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)
def run_dashboard_wrapper(config_input): # Input is a json with all the input data # Extra keys that are NOT in the standard config files are: # - output_base_filename: the base name for the icu frac file to be saved, e.g. 'netherlands_dashboard' # - single_run: a boolean if it should only perform a single run # Relative imports from src.io_func import load_data from bin.corona_esmda import run_esmda_model, single_run_mean from bin.analyseIC import save_and_plot_IC from src.api_nl_data import get_and_save_nl_data # Load the model configuration and the data (observed cases) config = json.loads(config_input) data = load_data(config) updated_config = config if config['single_run']: # Run the single run model # try: # num_single_runs = config['num_single_runs'] - 1 # except KeyError: # num_single_runs = 4 # Run the single run with the mean provided results_mean = single_run_mean(config, data) # # Run the single run for the remaining times with values sampled from the config distributions # results_sampled = np.zeros((num_single_runs, results_mean.shape[0], results_mean.shape[1])) # # Get a list of configs with the mean changed to be a sampled value of the original config mean and standard # # deviation. # sampled_configs = sample_config_distributions(config, num_single_runs) # for i in range(0, num_single_runs): # results_sampled[i] = single_run_mean(sampled_configs[i], data) # Rearrange the output data starting from day 1 [start_index] = np.where(results_mean[0] == 1)[0] # Set the results dashboard_data = { 'susceptible': np.concatenate( (results_mean[0, start_index:, None], results_mean[1, start_index:, None]), axis=1), # results_sampled[:, 1, start_index:].T), axis=1), 'exposed': np.concatenate( (results_mean[0, start_index:, None], results_mean[2, start_index:, None]), axis=1), # results_sampled[:, 2, start_index:].T), axis=1), 'infected': np.concatenate( (results_mean[0, start_index:, None], results_mean[3, start_index:, None]), axis=1), # results_sampled[:, 3, start_index:].T), axis=1), 'removed': np.concatenate( (results_mean[0, start_index:, None], results_mean[4, start_index:, None]), axis=1), # results_sampled[:, 4, start_index:].T), axis=1), 'hospitalized': np.concatenate( (results_mean[0, start_index:, None], results_mean[5, start_index:, None]), axis=1), # results_sampled[:, 5, start_index:].T), axis=1), 'hospitalizedcum': np.concatenate( (results_mean[0, start_index:, None], results_mean[6, start_index:, None]), axis=1), # results_sampled[:, 6, start_index:].T), axis=1), 'ICU': np.concatenate( (results_mean[0, start_index:, None], results_mean[7, start_index:, None]), axis=1), # results_sampled[:, 7, start_index:].T), axis=1), 'icu_cum': np.concatenate( (results_mean[0, start_index:, None], results_mean[8, start_index:, None]), axis=1), # results_sampled[:, 8, start_index:].T), axis=1), 'recovered': np.concatenate( (results_mean[0, start_index:, None], results_mean[9, start_index:, None]), axis=1), # results_sampled[:, 9, start_index:].T), axis=1), 'dead': np.concatenate( (results_mean[0, start_index:, None], results_mean[10, start_index:, None]), axis=1), # results_sampled[:, 10, start_index:].T), axis=1), 'infected_cum': np.concatenate( (results_mean[0, start_index:, None], results_mean[11, start_index:, None]), axis=1), # results_sampled[:, 11, start_index:].T), axis=1), 'alpha': np.concatenate( (results_mean[0, start_index:, None], results_mean[12, start_index:, None]), axis=1) } # results_sampled[:, 12, start_index:].T), axis=1)} else: # Make the input data and IC files get_and_save_nl_data(config['startdate'], config['country'], config['icdatafile']) # Make the IC fraction file save_and_plot_IC(config, config['output_base_filename'], 0) # Run the model dashboard_data, results = run_esmda_model( config['output_base_filename'], config, data, save_plots=0, save_files=0) # Update the config with the new posteriors to return updated_config = generate_updated_config(updated_config, results) # Return the result data (for both multiple or single run) as well as the updated config file. The dashboard_data # contains the following columns for the respective variables: # ~~ Full run ~~ # - alpha: # - time | P5 | P25 | P50 | P75 | P95 # - alpharun, dead, hospitalized, hospitalized cumulative, icu, infected: # - time | mean | P5 | P30 | P50 | P70 | P95 | observed # ~~ Single run ~~ # - susceptible, exposed, infected, removed, hospitalized, hospitalizedcum, # ICU, icu_cum, recovered, dead, infected_cum, alpha: # - time | single run with original updated config | remaining number of single runs with sampled config return dashboard_data, updated_config
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!
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