def _apply_metaparams(cls, list_configs, dico_meta): """ Deal with genearting random runs, names of the simulation METAPARAMETERS: RANDOM RUNS --------------------------- '_RDM_RUNS': default = 1 type = 'int': Number of random runs for each config '_RDM_FIXSEED': default = False type = 'bool' If True all the configurations for the same random run will have the same random seed (new key, value added in config '_RDM_SEED' != None) To keep track of which configurations map to the same initial configuration (i.e. without taking care of rdm runs) a flag 'ID_DET' is added METAPARAMETERS: OUTPUT ---------------------- '_OUT_PREFIX': ('res_','str'), '_OUT_FOLDER': (None, 'str'), '_OUT_COUNTER': (None, 'int'), '_OUT_NAME': (None, 'list'), '_OUT_STORE_CONFIG': (True, 'bool'), METAPARAMETERS: MISC ------------------------ '_MP_FLAG':(False, 'bool')} """ dico_meta_filled = cls._parse_metaparams(dico_meta) #Management of the random runs nb_rdm_run = dico_meta_filled['_RDM_RUNS'] fix_seed = dico_meta_filled['_RDM_FIXSEED'] runs = range(nb_rdm_run) if(fix_seed): seeds = RandomGenerator.RandomGenerator.gen_seed(nb = nb_rdm_run) if(nb_rdm_run == 1): seeds = [seeds] else: seeds = [None for _ in runs] random_bits = [{'_RDM_RUN':n, '_RDM_SEED': seeds[n]} for n in runs] for conf in list_configs: conf['_ID_DET'] = RandomGenerator.RandomGenerator.gen_seed() list_configs = ut.cartesianProduct(list_configs, random_bits, ut.add_dico) #Management of the output name_res_list = [] for conf in list_configs: name_res_tmp, dico_meta_filled = cls._gen_name_res(conf, dico_meta_filled) conf['_RES_NAME'] = name_res_tmp assert (name_res_tmp not in name_res_list), "Duplicated name: {0}".format(name_res_tmp) name_res_list.append(name_res_tmp) conf['_OUT_FOLDER'] = dico_meta_filled['_OUT_FOLDER'] conf['_OUT_STORE_CONFIG'] = dico_meta_filled['_OUT_STORE_CONFIG'] conf['_MP_FLAG'] = dico_meta_filled['_MP_FLAG'] return list_configs
def parse_meta_config(cls, input_file = 'inputfile.txt', update_rules = False, debug = False): """ Parsing an input file containing a meta-config into a list of configs. It relies on generating the cartesian product of the elements Example (txt file containing a meta config) ------- ### _CONTEXT {'_T':5} key1 Val1 Val2 key2 Val3 ### --> [{key1: eval(Val1), key2: eval(Val3)}, {key1: eval(Val2), key2: eval(Val3)}] Syntax of the meta-config file: --------------------------------- Keys starting with a _ and in self.METAPARAMS_INFO.keys() are metaparameters of the parser. o.w. they will be keys of the final config dico. If the first line key = _CONTEXT the Val associated should be of the form {'_contextvar1':val,'_contextvar2':val1} i.e. a dico with keys starting with a '_' Arguments --------- input_file: str path of the meta-config file update_rules : When building the configs should the first value seen (e.g. Val1) be used as a reference. Only works if Vals are <dic>. If True exampel's output becomes: [{key1: eval(Val1), key2: eval(Val3)}, {key1: eval(Val2).update(eval(Val2)), key2: eval(Val3)}] debug : bool debug mode Output ------ list_configs_processing_meta_configs: <list<dic>> """ if(debug): pdb.set_trace() use_context = False with open(input_file, 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ' ') list_values = list([]) list_keys = list([]) nbline = 0 dico_METAPARAMS = {} for line in reader: nbline += 1 if(line in cls.EMPTY_LINE) or (line[0] in cls.LEX_NA): pass elif(line[0] == '_CONTEXT'): context = literal_eval(line[1]) if(ut.is_dico(context) and np.product([c[0] == '_' for c in context])): use_context = True elif(line[0] in cls.METAPARAMS_NAME): assert(len(line) == 2), 'batch input file: 1 arg expected in l.' + str(nbline)+ ' (' +str(line[0]) + ')' dico_METAPARAMS[line[0]] = literal_eval(line[1]) else: assert (len(line)>=2), 'batch input file: not enough args in l.' + str(nbline) list_keys.append(line[0]) if(use_context): line_with_context = [cls._apply_context(line[i], context) for i in range(1,len(line))] ev_tmp = [eval(lwc) for lwc in line_with_context] else: ev_tmp = [literal_eval(line[i]) for i in range(1,len(line))] if(update_rules): ref_value = copy.copy(ev_tmp[0]) ev_tmp_new = [] for ev_i in ev_tmp: in_progress = ut.merge_dico(ref_value, ev_i, update_type = 0, copy = True) ev_tmp_new.append(copy.copy(in_progress)) ev_tmp = ev_tmp_new list_values = ut.cartesianProduct(list_values, ev_tmp, ut.appendList) list_configs = [ut.lists2dico(list_keys, l) for l in list_values] list_configs = cls._apply_metaparams(list_configs, dico_METAPARAMS) list_configs = [cls._processing_meta_configs(c) for c in list_configs] return list_configs