示例#1
0
    def _save_simulation_results(self, save_only_ID_0=False, time_elapsed=None) :

        if save_only_ID_0 and self.cfg.network.ID != 0 :
            return None

        filename_hdf5 = self._get_filename(name="network", filetype="hdf5")
        utils.make_sure_folder_exist(filename_hdf5)

        with h5py.File(filename_hdf5, "w", **hdf5_kwargs) as f :  #
            f.create_dataset("my_state", data=self.my_state)
            f.create_dataset("my_corona_type", data=self.my.corona_type)
            f.create_dataset("my_number_of_contacts", data=self.my.number_of_contacts)
            f.create_dataset("day_found_infected", data=self.intervention.day_found_infected)
            f.create_dataset("coordinates", data=self.my.coordinates)
            # import ast; ast.literal_eval(str(cfg))
            f.create_dataset("cfg_str", data=str(self.cfg))
            f.create_dataset("R_true", data=self.intervention.R_true_list)
            f.create_dataset("freedom_impact", data=self.intervention.freedom_impact_list)
            f.create_dataset("R_true_brit", data=self.intervention.R_true_list_brit)
            f.create_dataset("df", data=utils.dataframe_to_hdf5_format(self.df))
            # f.create_dataset(
            #     "df_coordinates",
            #     data=utils.dataframe_to_hdf5_format(self.df_coordinates, cols_to_str="kommune"),
            # )

            if time_elapsed :
                f.create_dataset("time_elapsed", data=time_elapsed)

            self._add_cfg_to_hdf5_file(f)

        return None
示例#2
0
    def _save_initialized_network(self, filename) :
        if self.verbose :
            print(f"Saving initialized network to {filename}", flush=True)
        utils.make_sure_folder_exist(filename)
        my_hdf5ready = nb_load_jitclass.jitclass_to_hdf5_ready_dict(self.my)

        with h5py.File(filename, "w", **hdf5_kwargs) as f :
            group_my = f.create_group("my")
            nb_load_jitclass.save_jitclass_hdf5ready(group_my, my_hdf5ready)
            utils.NestedArray(self.agents_in_age_group).add_to_hdf5_file(f, "agents_in_age_group")
            f.create_dataset("N_ages", data=self.N_ages)
            self._add_cfg_to_hdf5_file(f)
示例#3
0
def get_fit_results(abm_files, force_rerun=False, num_cores=1, y_max=0.01):

    all_fits_file = f"Data/fits_ymax_{y_max}.joblib"

    if Path(all_fits_file).exists() and not force_rerun:
        print("Loading all Imax fits", flush=True)
        return joblib.load(all_fits_file)

    else:

        all_fits = {}
        print(
            f"Fitting {len(abm_files.all_filenames)} files with {len(abm_files.cfgs)} different simulation parameters, please wait.",
            flush=True,
        )

        reject_counter = Counter()

        desc = "Fitting ABM simulations"
        for cfg, filenames in tqdm(abm_files.iter_folders(),
                                   total=len(abm_files.cfgs),
                                   desc=desc):
            # break
            output_filename = Path(
                "Data/fits") / f"fits_{cfg.hash}_ymax_{y_max}.joblib"
            utils.make_sure_folder_exist(output_filename)

            if output_filename.exists():
                all_fits[cfg.hash] = joblib.load(output_filename)

            else:
                with warnings.catch_warnings():
                    warnings.filterwarnings(
                        "ignore",
                        message="covariance is not positive-semidefinite.")
                    fit_results, reject_counter_tmp = fit_multiple_files(
                        cfg,
                        filenames,
                        num_cores=num_cores,
                        y_max=y_max,
                    )

                joblib.dump(fit_results, output_filename)
                all_fits[cfg.hash] = fit_results
                reject_counter += reject_counter_tmp

        print(reject_counter)

        joblib.dump(all_fits, all_fits_file)
        return all_fits
示例#4
0
    def _save_dataframe(self, save_csv=False, save_hdf5=True) :

        # Save CSV
        if save_csv :
            filename_csv = self._get_filename(name="ABM", filetype="csv")
            utils.make_sure_folder_exist(filename_csv)
            self.df.to_csv(filename_csv, index=False)

        if save_hdf5 :
            filename_hdf5 = self._get_filename(name="ABM", filetype="hdf5")
            utils.make_sure_folder_exist(filename_hdf5)
            with h5py.File(filename_hdf5, "w", **hdf5_kwargs) as f :  #
                f.create_dataset("df", data=utils.dataframe_to_hdf5_format(self.df))
                self._add_cfg_to_hdf5_file(f)

        return None
    def plot_simulation(I_tot_scaled, f, start_date, axes) :

        # Create the plots
        tmp_handles_0 = axes[0].plot(pd.date_range(start=start_date, periods = len(I_tot_scaled), freq="D"),     I_tot_scaled, lw = 4, c = "k")[0]
        tmp_handles_2 = axes[1].plot(pd.date_range(start=start_date, periods = len(f),            freq="W-SUN"), f,            lw = 4, c = "k")[0]

        return [tmp_handles_0, tmp_handles_2]

    rc_params.set_rc_params()

    reload(plot)
    reload(file_loaders)

    # Prepare output file
    utils.make_sure_folder_exist(fig_name)


    logK, logK_sigma, beta, covid_index_offset, t_index = load_covid_index(start_date.date())
    fraction, fraction_sigma, fraction_offset, t_fraction = load_b117_fraction()



    # Load the ABM simulations
    abm_files = file_loaders.ABM_simulations(base_dir="Output/ABM", subset=subset, verbose=True)

    if len(abm_files.all_filenames) == 0 :
        raise ValueError(f"No files loaded with subset: {subset}")

    plot_handles = []
    lls     = []
    

    return fig, axes, fi_list, pc_list,name_list


rc_params.set_rc_params()

#%%

reload(plot)
reload(file_loaders)

abm_files = file_loaders.ABM_simulations(verbose=True)
network_files = file_loaders.ABM_simulations(base_dir="Data/network", filetype="hdf5")
pdf_name = Path(f"Figures/data_anal.pdf")
utils.make_sure_folder_exist(pdf_name)
with PdfPages(pdf_name) as pdf:
        fi_list = []
        pc_list = []
        name_list = []
        # for ABM_parameter in tqdm(abm_files.keys, desc="Plotting individual ABM parameters"):
        for cfg in tqdm(
            abm_files.iter_cfgs(),
            desc="Plotting individual ABM parameters",
            total=len(abm_files.cfgs),
        ):

            # break


            fig, _, fi_list, pc_list, name_list = analyse_single_ABM_simulation(cfg, abm_files, network_files, fi_list, pc_list, name_list)