def test_run_ex_1_caching(): # given global this_dir, logging_conf_fpath sim_conf_fpath = "test_sets/ex_1_full/run_realistic.ini" exp_log_fpath = "test_sets/ex_1_full/exp_log_realistic.txt" floader = fl.FileLoader() # when os.chdir(this_dir) sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath, floader) # then assert sf.compare_files_by_line("log.txt", exp_log_fpath, False) # tear down shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/res_realistic"))) os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_1.tsv"))) # when sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath, floader) # then assert sf.compare_files_by_line("log.txt", exp_log_fpath, False) # tear down shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/res_realistic"))) os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_1.tsv")))
def test_run_ex_3_realistic(): # given global this_dir, logging_conf_fpath sim_conf_fpath = 'test_sets/ex_3_full/run_realistic.ini' exp_log_fpath = 'test_sets/ex_3_full/exp_log_realistic.txt' exp_end_stats_fpath = os.path.normpath( 'test_sets/ex_3_full/exp_end_stats_realistic.tsv') res_end_stats_fpath = os.path.normpath('test_sets/useless/useless_3.tsv') floader = fl.FileLoader() # when os.chdir(this_dir) sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath, floader) # then assert sf.compare_files_by_line('log.txt', exp_log_fpath, False) assert sf.compare_files_by_line(res_end_stats_fpath, exp_end_stats_fpath, False) # tear down shutil.rmtree( os.path.join(this_dir, os.path.normpath('test_sets/ex_3_full/res_realistic'))) os.remove( os.path.join(this_dir, os.path.normpath('test_sets/useless/useless_3.tsv')))
def test_run_ex_unstable_2(): # given global this_dir, logging_conf_fpath sim_conf_fpath = "test_sets/ex_unstable_2/run_uniform.ini" exp_log_fpath = "test_sets/ex_unstable_2/exp_log_uniform.txt" floader = fl.FileLoader() # when os.chdir(this_dir) sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath, floader) # then assert sf.compare_files_by_line("log.txt", exp_log_fpath, False) # tear down shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_unstable_2/res_uniform"))) os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_4.tsv")))
def test_run_ex_unstable_2(): # given global this_dir, logging_conf_fpath sim_conf_fpath = 'test_sets/ex_unstable_2/run_uniform.ini' exp_log_fpath = 'test_sets/ex_unstable_2/exp_log_uniform.txt' floader = fl.FileLoader() # when os.chdir(this_dir) sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath, floader) # then assert sf.compare_files_by_line('log.txt', exp_log_fpath, False) # tear down shutil.rmtree( os.path.join(this_dir, os.path.normpath('test_sets/ex_unstable_2/res_uniform'))) os.remove( os.path.join(this_dir, os.path.normpath('test_sets/useless/useless_4.tsv')))
__author__ = 'Agostino Sturaro' import os import filecmp import shutil import shared_functions as sf import cascades_sim as cs # given this_dir = os.path.normpath(os.path.dirname(__file__)) logging_conf_fpath = os.path.join(this_dir, 'logging_base_conf.json') sim_conf_fpath = '../Simulations/test_2/example_sim_2.ini' # exp_log_fpath = 'test_sets/ex_1_full/exp_log_realistic.txt' # when os.chdir(this_dir) sf.setup_logging(logging_conf_fpath) cs.run(sim_conf_fpath) # then #assert filecmp.cmp('log.txt', exp_log_fpath, False) # assuming UNIX EOLs are used # tear down # shutil.rmtree(os.path.join(this_dir, os.path.normpath('test_sets/ex_1_full/res_realistic')))
# inner cycle ranging over different network instances for instance_num in range(first_instance, last_instance, 1): # if it's the first simulation we run for this instance, then take note if instance_num not in run_num_by_inst: run_num_by_inst[instance_num] = 0 # inner cycle ranging over different seeds for seed in seeds: # pick up the simulation number (n-th time we run a simulation on this instance) run_num = run_num_by_inst[instance_num] misc['instance'] = instance_num # mark in the configuration file the number of this instance misc['run'] = run_num paths['netw_dir'] = os.path.join(instances_dir, 'instance_{}'.format(instance_num)) # input run_options['seed'] = seed paths['results_dir'] = os.path.join(group_results_dir, 'instance_' + str(instance_num), 'run_' + str(run_num)) paths['run_stats_fname'] = 'run_{}_stats.tsv'.format(run_num) conf_fpath = os.path.join(group_results_dir, 'instance_' + str(instance_num), 'run_' + str(run_num) + '.ini') write_conf(conf_fpath, paths, run_options, misc, safe_nodes_opts) with open(group_index_fpath, 'ab') as group_index_file: group_index = csv.writer(group_index_file, delimiter='\t', quoting=csv.QUOTE_MINIMAL) group_index.writerow([instance_num, conf_fpath]) logger.warning('Batch {}) Running simulation {} of {}\nsim group {}, value {}, instance {}, seed {}' .format(batch_no, cur_sim_num, sim_cnt, sim_group, var_value, instance_num, seed)) sim.run(conf_fpath, floader) # run the simulation run_num_by_inst[instance_num] += 1 # next simulation for this instance will be number + 1 cur_sim_num += 1
# inner cycle ranging over different network instances for instance_num in range(first_instance, last_instance, 1): # if it's the first simulation we run for this instance, then take note if instance_num not in run_num_by_inst: run_num_by_inst[instance_num] = 0 # inner cycle ranging over different seeds for seed in seeds: print("Batch {}) {} value, seed {}".format(batch_no, var_value, seed)) # debug # pick up the simulation number (n-th time we run a simulation on this instance) run_num = run_num_by_inst[instance_num] misc["instance"] = instance_num # mark in the configuration file the number of this instance paths["netw_dir"] = os.path.join(instances_dir, "instance_{}".format(instance_num)) # input run_options["seed"] = seed paths["results_dir"] = os.path.join( group_results_dir, "instance_" + str(instance_num), "run_" + str(run_num) ) paths["run_stats_fname"] = "run_{}_stats.tsv".format(run_num) conf_fpath = os.path.join( group_results_dir, "instance_" + str(instance_num), "run_" + str(run_num) + ".ini" ) write_conf(conf_fpath, paths, run_options, misc) with open(group_index_fpath, "ab") as group_index_file: group_index = csv.writer(group_index_file, delimiter="\t", quoting=csv.QUOTE_MINIMAL) group_index.writerow([instance_num, conf_fpath]) sim.run(conf_fpath, floader) # run the simulation run_num_by_inst[instance_num] += 1 # next simulation for this instance will be number + 1
paths['end_stats_fpath'] = os.path.join( group_results_dir, 'group_{}_stats_{}.tsv'.format(instance_type, point_num)) for seed in seeds: run_options['seed'] = seed paths['run_stats_fname'] = 'run_{}_stats.tsv'.format(run_num) paths['results_dir'] = os.path.join( group_results_dir, 'instance_' + str(instance_num), 'run_' + str(run_num)) conf_fpath = os.path.join(group_results_dir, 'instance_' + str(instance_num), 'run_' + str(run_num) + '.ini') write_conf(conf_fpath, paths, run_options) # TODO: build system using this to aggregate stats group_index_fpath = os.path.join( group_results_dir, 'group_{}_index_{}.tsv'.format(instance_type, point_num)) with open(group_index_fpath, 'ab') as group_index_file: group_index = csv.writer(group_index_file, delimiter='\t', quoting=csv.QUOTE_MINIMAL) group_index.writerow([ os.path.join(paths['results_dir'], paths['run_stats_fname']) ]) sim.run(conf_fpath, floader) run_num += 1