Exemplo n.º 1
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
    def gen_rdmnb_from_string(self, method_rdm, dim=1):
        """
        #TODO: change name gen_rdm_XXX // use genrdmdunction // 
        #Old name = GenRdmNumbersFromStr
        Purpose:
            Generate Random Sequences based on a string or list of string
            
            {X0,   XN-1} is represented as a N row 
                                             NxD matrix if each RandomVar has dim D
            More generally dim = [dim_pop, dim_RV]
        """
        functmp = self.gen_rdmfunc_from_string(method_rdm, dim)
        if (ut.is_list(functmp)):
            res = [f() for f in functmp]
        else:
            res = functmp()

        return res
    def gen_rdmfunc_from_string(self, method_rdm, dim=1):
        """return a function that when called return random variables according
        to the distribution described by the string, with specific dim
        convention: dim[-1] correspond to the dim of the RV while dim[:-1] to 
        the size of the population
        TODO: works only for 1D dim
        """
        if ut.is_list(method_rdm):
            # return a lst of function
            # should it be a function returning a list??
            func = [
                self.gen_rdmfunc_from_string(meth, dim) for meth in method_rdm
            ]
        else:

            args = ut.splitString(method_rdm)
            if (ut.is_int(dim)):
                dimRV = dim
            else:
                dimRV = dim[-1]
            if (args[0] in ['uniform', 'normal']):
                if (len(args) == 1):
                    first_args = np.repeat(0, dimRV)
                    second_args = np.repeat(1, dimRV)

                elif (len(args) == 3):
                    first_args = np.repeat(float(args[1]), dimRV)
                    second_args = np.repeat(float(args[2]), dimRV)

                elif (len(args) == (1 + 2 * dimRV)):
                    first_args = np.array(
                        [float(args[1 + 2 * d]) for d in range(dimRV)])
                    second_args = np.array(
                        [float(args[2 + 2 * d]) for d in range(dimRV)])
                else:
                    raise NotImplementedError()

                if (dim == 1):
                    # such that function return a value instead of an array
                    # may change
                    first_args, second_args = first_args[0], second_args[0]
                    dim = None
                if args[0] == 'normal':

                    def func():
                        return self.normal(first_args, second_args, size=dim)
                else:

                    def func():
                        return self.uniform(first_args, second_args, size=dim)

            elif (args[0] == 'determ'):
                if (len(args) == 1):
                    constant = np.repeat(0, dim)
                elif (len(args) == 2):
                    constant = np.repeat(float(args[1]), dim)
                elif (len(args) == (1 + dimRV)):
                    constant = np.array(args[1:])
                    if (ut.is_list(dim)):
                        dim_pop = np.product(dim[:-1])
                        constant = np.tile(constant, dim_pop).reshape(dim)
                else:
                    raise NotImplementedError()

                def func():
                    return constant
            else:
                raise NotImplementedError()
        return func