Exemplo n.º 1
0
 def context(self, context):
     if (context is None):
         self._context = {}
     elif (ut.is_dico(context)):
         self._context = context
     else:
         SystemError('context should be a dictionnary')
Exemplo n.º 2
0
    def _read_configs(cls, config_object, list_output = True):
        """ Allow for different type of configs to be passed (<dic>, <list<dic>>, 
        <str>, <list<str>>)
        """
        if(ut.is_dico(config_object)):
            configs = [config_object] if(list_output) else config_object
        elif(ut.is_list(config_object)):
            configs = [cls._read_configs(conf, list_output = False) for conf in config_object]
        elif(ut.is_str(config_object)):
            configs = ut.file_to_dico(config_object)
            if(list_output):
                configs = [configs]
        else:
            raise NotImplementedError()

        return configs
Exemplo n.º 3
0
    def _gen_name_res(cls, config, metadico = {}, type_output = '.txt'):
        """ Generate a name associated to a config

        Rules
        -----
        if _OUT_NAME is not None and has a <dic> type:
            {'k':v} --> 'k_XXX' where XXX has been found in the config structure
            following path given by v


        elif _OUT_COUNTER is not None increment this counter for each new config

        
        add _OUT_PREFIX

        """
        res_name = ''
        if(metadico.get('_OUT_NAME') is not None):
            name_rules = metadico['_OUT_NAME']
            if(ut.is_dico(name_rules)):
                for k, v in name_rules.items():
                    res_name += (k + "_" + str(ut.extract_from_nested(config, v)))
            else:
                raise NotImplementedError()
                
            if((config.get("_RDM_RUN") is not None)):
                res_name += "_" 
                res_name += str(config["_RDM_RUN"])
            
        elif(metadico.get('_OUT_COUNTER') is not None):
            res_name += str(metadico['_OUT_COUNTER'])
            metadico['_OUT_COUNTER'] +=1
        
        if(res_name == ''):
            res_name = str(RandomGenerator.RandomGenerator.gen_seed()) 
        
        prefix = metadico.get('_OUT_PREFIX', '')
        res_name = prefix + res_name + type_output

        return res_name, metadico
Exemplo n.º 4
0
 def update_context(self, more_context):
     if (ut.is_dico(more_context)):
         self._context.update(more_context)
     else:
         SystemError('context should be a dictionnary')
Exemplo n.º 5
0
    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