def run_experiment(path: Path, custom_settings: dict): # 1. Initialize the experiment exp = Experiment() exp.load_config(config_file=path / 'config.json', custom_settings=custom_settings) if exp.config['model']['n_areas'] == "TBD": exp.config['model']['n_areas'] = 2 # 2. Simulate contact areas sim = Simulation(experiment=exp) sim.run_simulation() # 3. Define MCMC mc = MCMC(data=sim, experiment=exp) # 4. Warm-up sampler and sample from posterior mc.warm_up() mc.sample() # 5. Evaluate ground truth mc.eval_ground_truth() # 6. Log sampling statistics and save samples to file mc.save_samples(run=0)
def main(config=None, experiment_name=None): if config is None: parser = argparse.ArgumentParser( description="An MCMC algorithm to identify contact zones") parser.add_argument("config", nargs="?", type=Path, help="The JSON configuration file") parser.add_argument( "name", nargs="?", type=str, help= "The experiment name used for logging and as the name of the results directory." ) args = parser.parse_args() config = args.config experiment_name = args.name # 0. Ask for config file via files-dialog, if not provided as argument. if config is None: tk.Tk().withdraw() config = filedialog.askopenfilename( title='Select a config file in JSON format.', initialdir='..', filetypes=(('json files', '*.json'), ('all files', '*.*'))) # Initialize the experiment experiment = Experiment(experiment_name=experiment_name, config_file=config, log=True) if experiment.is_simulation(): # The data is defined by a ´Simulation´ object data = Simulation(experiment=experiment) data.run_simulation() data.log_simulation() else: # Experiment based on a specified (in config) data-set data = Data(experiment=experiment) data.load_features() # Counts for priors data.load_universal_counts() data.load_inheritance_counts() data.load_geo_cost_matrix() # Log data.log_loading() # Rerun experiment to check for consistency for run in range(experiment.config['mcmc']['n_runs']): n_areas = experiment.config['model']['n_areas'] iterate_or_run( x=n_areas, config_setter=lambda x: experiment.config['model'].__setitem__( 'n_areas', x), function=lambda x: run_experiment(experiment, data, run))
def load_and_run(): # Create Experiment object from config file experiment = Experiment('prior_simulation', config_file='config.json', logging=True) # Simulate data data = Simulation(experiment=experiment) data.run_simulation() data.log_simulation() # Run experiments for run in range(experiment.config['mcmc']['N_RUNS']): run_experiment(experiment, data, run)
from sbayes.experiment_setup import Experiment from sbayes.simulation import Simulation from sbayes.mcmc_setup import MCMC if __name__ == '__main__': # When performing the MCMC iterate over different setups (inheritance) INHERITANCE = [False, True] for IN in INHERITANCE: # 1. Initialize the experiment exp = Experiment() exp.load_config( config_file='experiments/simulation/sim_exp2/config.json', custom_settings={'model': { 'INHERITANCE': IN }}) exp.log_experiment() # 2. Simulate contact areas sim = Simulation(experiment=exp) sim.run_simulation() sim.log_simulation() # 3. Define MCMC mc = MCMC(data=sim, experiment=exp) mc.log_setup() # Rerun experiment to check for consistency for run in range(exp.config['mcmc']['N_RUNS']):
SETUP = list(itertools.product(STRENGTH, AREA)) for S in SETUP: # Update config information according to the current setup custom_settings = { 'simulation': { 'i_contact': I_CONTACT[S[0]], 'e_contact': E_CONTACT[S[0]], 'STRENGTH': S[0], 'area': S[1] } } # 1. Initialize the experiment exp = Experiment() exp.load_config( config_file='experiments/simulation/sim_exp1/config.json', custom_settings=custom_settings) exp.log_experiment() # 2. Simulate contact areas sim = Simulation(experiment=exp) sim.run_simulation() sim.log_simulation() # 3. Define MCMC mc = MCMC(data=sim, experiment=exp) mc.log_setup() # Rerun experiment to check for consistency
from sbayes.experiment_setup import Experiment from sbayes.simulation import Simulation from sbayes.mcmc_setup import MCMC if __name__ == '__main__': import os # 1. Initialize the experiment exp = Experiment() exp.load_config(config_file='experiments/simulation/sim_exp3/config.json') exp.log_experiment() # 2. Simulate contact areas sim = Simulation(experiment=exp) sim.run_simulation() sim.log_simulation() # Iterate over different setups (different number of areas) NUMBER_AREAS = range(1, 8) for N in NUMBER_AREAS: # Update config information according to the current setup exp.config['model']['n_areas'] = N # 3. Define MCMC mc = MCMC(data=sim, experiment=exp) mc.log_setup() # Rerun experiment to check for consistency for run in range(exp.config['mcmc']['n_runs']):
from sbayes.experiment_setup import Experiment from sbayes.load_data import Data from sbayes.mcmc_setup import MCMC if __name__ == '__main__': # Initialize the experiment exp = Experiment() exp.load_config(config_file='experiments/south_america/config.json') exp.log_experiment() # Load Data dat = Data(experiment=exp) # Features dat.load_features() # Counts for priors dat.load_universal_counts() dat.load_inheritance_counts() # Log dat.log_loading() NUMBER_AREAS = range(1, 8) # Rerun experiment to check for consistency for run in range(exp.config['mcmc']['N_RUNS']): for N in NUMBER_AREAS: # Update config information according to the current setup exp.config['model']['N_AREAS'] = N
from sbayes.experiment_setup import Experiment from sbayes.load_data import Data from sbayes.mcmc_setup import MCMC if __name__ == '__main__': # Initialize the experiment exp = Experiment() exp.load_config(config_file='experiments/balkan/config.json') exp.log_experiment() # Load Data dat = Data(experiment=exp) # Features dat.load_features() # Counts for priors dat.load_universal_counts() dat.load_inheritance_counts() # Log dat.log_loading() NUMBER_AREAS = range(1, 8) # Rerun experiment to check for consistency for run in range(exp.config['mcmc']['N_RUNS']): for N in NUMBER_AREAS: # Update config information according to the current setup exp.config['model']['N_AREAS'] = N
# Number of samples N_BACKGROUND = 20 N_AREA = 10 # Spatial distribution BBOX = Rectangle(left=0, bottom=0, right=100, top=100) AREA_MEAN = np.array([35, 60]) AREA_STD = 10 # Generate spatial locations background_locations = generate_background_locations(N_BACKGROUND, BBOX) area_locations = generate_area_locations(N_AREA, AREA_MEAN, AREA_STD) plot_locations(background_locations, area_locations) # 1. Initialize the experiment exp = Experiment() exp.load_config(config_file='../config.json') exp.log_experiment() # When simulating iterate over different setups (different areas and strengths of contact) I_CONTACT = [1.5, 2, 2.5] E_CONTACT = [1.25, 0.75, 0.25] STRENGTH = range(len(E_CONTACT)) AREA = [4, 6, 3, 8] SETUP = list(itertools.product(STRENGTH, AREA)) for S in SETUP: # Update config information according to the current setup exp.config['simulation']['I_CONTACT'] = I_CONTACT[S[0]] exp.config['simulation']['E_CONTACT'] = E_CONTACT[S[0]]
def main(args=None): if args is None: parser = argparse.ArgumentParser( description="An MCMC algorithm to identify contact zones") parser.add_argument("config", nargs="?", type=Path, help="The JSON configuration file") args = parser.parse_args() # 0. Ask for config file via files-dialog, if not provided as argument. config = args.config if config is None: tk.Tk().withdraw() config = filedialog.askopenfilename( title='Select a config file in JSON format.', initialdir='..', filetypes=(('json files', '*.json'), ('all files', '*.*'))) # Initialize the experiment experiment = Experiment(config_file=config, log=True) if experiment.is_simulation(): # The data is defined by a ´Simulation´ object data = Simulation(experiment=experiment) data.run_simulation() data.log_simulation() else: # Experiment based on a specified (in config) data-set data = Data(experiment=experiment) data.load_features() # Counts for priors data.load_universal_counts() data.load_inheritance_counts() # Log data.log_loading() initial_sample = None # Rerun experiment to check for consistency for run in range(experiment.config['mcmc']['N_RUNS']): if isinstance(experiment.config['mcmc']['N_AREAS'], str): assert experiment.config['mcmc']['N_AREAS'].lower() == 'tbd' # Run the experiment multiple times to determine the number of areas. for N in NUMBER_AREAS_GRID: # Update config information according to the current setup experiment.config['mcmc']['N_AREAS'] = N # Run the experiment with the specified number of areas initial_sample = run_experiment(experiment, data, run, initial_sample=initial_sample) else: # Run the experiment once, with the specified settings run_experiment(experiment, data, run) initial_sample = None