Пример #1
0
 def maxLikeRunner(self, iniFile=None, **kwargs):
     if self.analyzername is None: self.analyzername = 'maxlike'
     self.outputpath = '{}_{}_optimization'.format(self.outputpath,
                                                   self.analyzername)
     self.outputChecker()
     if iniFile:
         withErrors = self.config.getboolean('maxlike',
                                             'withErrors',
                                             fallback=False)
         plot_par1 = self.config.get('maxlike', 'plot_par1', fallback=False)
         plot_par2 = self.config.get('maxlike', 'plot_par2', fallback=False)
     else:
         withErrors = kwargs.pop('withErrors', False)
         plot_par1 = kwargs.pop('plot_par1', False)
         plot_par2 = kwargs.pop('plot_par2', False)
         if kwargs:
             logger.critical(
                 'Unexpected **kwargs for MaxLike: {}'.format(kwargs))
             logger.info(
                 'You can skip writing any option and SimpleMC will use the default value.\n'
                 'MaxLikeAnalyzer executer options are:'
                 '\n\twithErrors (boolean) Default: False')
             sys.exit(1)
     ti = time.time()
     A = MaxLikeAnalyzer(self.L,
                         self.model,
                         withErrors=withErrors,
                         param1=plot_par1,
                         param2=plot_par2)
     params = self.T.printParameters(A.params)
     self.ttime = time.time() - ti
     self.result = ['maxlike', A, params]
     return True
Пример #2
0
 def __init__(self, name, values_filename, cov_filename, fn='generic'):
     BaseLikelihood.__init__(self, name)
     # print("Loading ", values_filename)
     logger.info("Loading ", values_filename)
     data = sp.loadtxt(values_filename)
     self.xx = data[:, 0]
     self.yy = data[:, 1]
     self.cov = sp.loadtxt(cov_filename, skiprows=0)
     assert (len(self.cov) == len(self.xx))
     self.icov = la.inv(self.cov)
     self.fn = fn
Пример #3
0
    def geneticRunner(self, iniFile=None, **kwargs):
        if self.analyzername is None: self.analyzername = 'genetic'
        self.outputpath = '{}_{}_optimization'.format(self.outputpath,
                                                      self.analyzername)
        self.outputChecker()

        if iniFile:
            n_individuals = self.config.getint('genetic',
                                               'n_individuals',
                                               fallback=400)
            n_generations = self.config.getint('genetic',
                                               'n_generations',
                                               fallback=1000)
            selection_method = self.config.get('genetic',
                                               'selection_method',
                                               fallback='tournament')
            mut_prob = self.config.getfloat('genetic',
                                            'mut_prob',
                                            fallback=0.6)
        else:
            n_individuals = kwargs.pop('n_individuals', 400)
            n_generations = kwargs.pop('n_generations', 1000)
            selection_method = kwargs.pop('selection_method', 'tournament')
            mut_prob = kwargs.pop('mut_prob', 0.6)
            if kwargs:
                logger.critical(
                    'Unexpected **kwargs for genetic optimizer: {}'.format(
                        kwargs))
                logger.info(
                    'You can skip writing any option and SimpleMC will use the default value.\n'
                    'genetic executer options are:'
                    '\n\tn_individuals (int) Default: 400\n\t'
                    'n_generations (int) Default: 1000\n\t'
                    'selection_method {"tournament","rank","roulette"} Default: "tournament"'
                    '\n\tmut_prob (float) Default: 0.4')
                sys.exit(1)
        logger.info("\n\tn_individuals: {}\n\tn_generations: {}"
                    "\n\tselection method: {}\n\t"
                    "mut prob: {}".format(n_individuals, n_generations,
                                          selection_method, mut_prob))
        ti = time.time()

        M = SimpleGenetic(self.logLikeGenetic,
                          self.dims,
                          self.bounds,
                          n_individuals=n_individuals,
                          n_generations=n_generations,
                          prob_mut=mut_prob,
                          method_selection=selection_method,
                          outputname=self.outputpath)

        self.ttime = time.time() - ti
        self.result = ['genetic', M]
        return True
Пример #4
0
    def writeSummary(self, time, *args):
        if self.analyzername == 'genetic':
            sys.exit(1)
        file = open(self.filename + "_Summary" + ".txt", 'w')
        file.write('SUMMARY\n-------\n')

        for item in self.args:
            if type(item) is list:
                for element in item:
                    if element is not None:
                        file.write(str(element) + '\n')
            else:
                if item is not None:
                    file.write(str(item) + '\n')

        for item in args:
            if type(item) is list:
                for element in item:
                    if element is not None:
                        file.write(str(element) + '\n')
            else:
                if item is not None:
                    file.write(str(item) + '\n')

        if self.engine =='dynesty':
            pars = self.loglike.freeParameters()
            dims = len(pars)
            for i, p in enumerate(pars):
                mean = np.mean(self.result.samples[:, i])
                std = np.std(self.result.samples[:, i])
                print("{}: {:.4f} +/- {:.4f}".format(p.name, mean, std))
                file.write("{}: {:.4f} +/- {:.4f}\n".format(p.name, mean, std))

            file.write("nlive: {:d}\nniter: {:d}\nncall: {:d}\n"
                       "eff(%): {:6.3f}\nlogz: "
                       "{:6.3f} +/- {:6.3f}".format(self.result.nlive, self.result.niter,
                                               sum(self.result.ncall), self.result.eff,
                                               self.result.logz[-1], self.result.logzerr[-1]))

        logger.info("\nElapsed time: {:.3f} minutes = {:.3f} seconds".format(time / 60, time))
        file.write('\nElapsed time: {:.3f} minutes = {:.3f} seconds \n'.format(time / 60, time))
        file.close()
Пример #5
0
    def outputChecker(self):
        while True:
            if os.path.isfile(self.outputpath + ".txt"):
                logger.info(
                    "{0} file already exists, {0}_new was created".format(
                        self.outputpath))
                self.outputpath = "{}_new".format(self.outputpath)
            else:
                break
        while True:
            flag = False
            for i in range(1, 10):
                if os.path.isfile("{}_{}.txt".format(self.outputpath, i)):
                    flag = True
            if flag:
                logger.info(
                    "{0}_{1} file already exists, {0}_new was created".format(
                        self.outputpath, i))
                self.outputpath = "{}_new".format(self.outputpath)
            else:
                break
        self.paramFiles()

        return True
Пример #6
0
    def saveEmceeSamples(self):
        dims = len(self.paramList)
        # postsamples = self.result.chain[:, self.skip:, :].reshape((-1, dims))
        tau = self.result.get_autocorr_time()
        logger.info("Autocorrelation time: {}".format(tau))
        tau = np.mean(tau)
        burnin = int(0.5 * np.max(tau))
        thin = int(0.5 * np.min(tau))
        f = open(self.filename + '.txt', 'w+')
        logprobs = self.result.get_log_prob(discard=burnin, flat=True, thin=thin)
        flat_log_prior_samps = self.result.get_blobs(flat=True)
        postsamples = self.result.get_chain(discard=burnin, flat=True, thin=thin)

        for i, row in enumerate(postsamples):
            strsamples = str(row).lstrip('[').rstrip(']')
            # strsamples = "{} {} {}\n".format(1, -2 * (logprobs[i] - flat_log_prior_samps[i]), strsamples)
            strsamples = "{} {} {}\n".format(1, -2 * (logprobs[i]), strsamples)
            strsamples = re.sub(' +', ' ', strsamples)
            strsamples = re.sub('\n ', ' ', strsamples)
            if self.derived:
                for pd in self.AD.listDerived(self.loglike):
                    strsamples = "{} {}".format(strsamples, pd.value)
            f.write(strsamples)
        f.close()
Пример #7
0
    def getdistAnalyzer(self, cov=False):
        from getdist import mcsamples

        mcsamplefile = mcsamples.loadMCSamples(self.filename, settings={'ignore_rows': self.skip})

        if cov:
            cov = mcsamplefile.cov(pars=self.paramList)
            np.savetxt(self.filename + '_' + 'cov.txt', cov)
            logger.info("Covariance matrix:\n")
            logger.info(cov)
            logger.info("\n")

        means  = mcsamplefile.getMeans()
        stddev = mcsamplefile.std(self.paramList)
        summaryResults = []

        for i, param in enumerate(self.paramList):
            logger.info(self.paramList[i] + " : " + str(round(means[i], 4)) + \
                        "+/-" + str(round(stddev[i], 4)))
            summaryResults.append(self.paramList[i] + " : " + str(round(means[i], 4)) + \
                                  "+/-" + str(round(stddev[i], 4)))
        return summaryResults
Пример #8
0
    def mppool(self, nproc):
        if nproc <= 0:
            ncores = mp.cpu_count()
            nprocess = ncores // 2
            logger.info("Using  {} processors of {}.".format(nprocess, ncores))
            pool = mp.Pool(processes=nprocess)
        elif nproc == 1:
            logger.info("Using 1 processor")
            nprocess = None
            pool = None
        else:
            nprocess = nproc
            logger.info("Using {} processors.".format(nprocess))
            pool = mp.Pool(processes=nprocess)

        return pool, nprocess
Пример #9
0
 def emceeRunner(self, iniFile=None, **kwargs):
     if iniFile:
         walkers = self.config.getint('emcee', 'walkers', fallback=500)
         nsamp = self.config.getint('emcee', 'nsamp', fallback=20000)
         burnin = self.config.getint('emcee', 'burnin', fallback=0)
         nproc = self.config.getint('emcee', 'nproc', fallback=1)
     else:
         walkers = kwargs.pop('walkers', 30)
         nsamp = kwargs.pop('nsamp', 20000)
         burnin = kwargs.pop('burnin', 0)
         nproc = kwargs.pop('nproc', 1)
         if kwargs:
             logger.critical(
                 'Unexpected **kwargs for emcee sampler: {}'.format(kwargs))
             logger.info(
                 'You can skip writing any option and SimpleMC will use the default value.\n'
                 'Emcee executer options are:'
                 '\n\twalkers (int) Default: 30\n\t'
                 'nsamp (int) Default: 20000\n\t'
                 'burnin (int) Default: 0\n\t'
                 'nproc (int) Default: 1')
             sys.exit(1)
     logger.info("\n\twalkers: {}\n\tnsamp: {}\n"
                 "\tburnin: {}\n\t"
                 "nproc: {}".format(walkers, nsamp, burnin, nproc))
     if self.analyzername is None: self.analyzername = 'emcee'
     self.outputpath = "{}_{}_{}_walkers".format(self.outputpath,
                                                 self.analyzername, walkers)
     self.outputChecker()
     pool, _ = self.mppool(nproc)
     # initial_state = None
     ini = []
     for bound in self.bounds:
         ini.append(np.random.uniform(bound[0], bound[1], walkers))
     inisamples = np.array(ini).T  # initial samples
     try:
         import emcee
         ti = time.time()
         sampler = emcee.EnsembleSampler(walkers,
                                         self.dims,
                                         self.logPosterior,
                                         pool=pool)
         # pass the initial samples and total number of samples required
         sampler.run_mcmc(inisamples, nsamp + burnin, progress=True)
         # extract the samples (removing the burn-in)
         # postsamples = sampler.chain[:, burnin:, :].reshape((-1, self.dims))
         self.ttime = time.time() - ti
     except ImportError as error:
         sys.exit("{}: Please install this module"
                  "or try using other sampler".format(
                      error.__class__.__name__))
     # set up the sampler
     self.burnin = burnin
     try:
         pool.close()
     except:
         pass
     # fig = corner.corner(postsamples)
     # fig.savefig('emcee.png')
     self.result = [
         'emcee', sampler, 'walkers : {}'.format(walkers),
         'samples: {}'.format(nsamp)
     ]
     return True
Пример #10
0
    def nestedRunner(self, iniFile=None, **kwargs):
        """
            This method calls Dynesty samplers:
            --
            -- MULTINEST
            bound : {'none', 'single', 'multi', 'balls', 'cubes'},
        """
        if iniFile:
            self.engine = self.config.get('nested',
                                          'engine',
                                          fallback='dynesty')
            dynamic = self.config.getboolean('nested',
                                             'dynamic',
                                             fallback=False)
            neuralNetwork = self.config.getboolean('nested',
                                                   'neuralNetwork',
                                                   fallback=False)
            nestedType = self.config.get('nested',
                                         'nestedType',
                                         fallback='multi')
            nlivepoints = self.config.getint('nested',
                                             'nlivepoints',
                                             fallback=1024)
            accuracy = self.config.getfloat('nested',
                                            'accuracy',
                                            fallback=0.01)
            nproc = self.config.getint('nested', 'nproc', fallback=1)

            self.priortype = self.config.get('nested',
                                             'priortype',
                                             fallback='u')
            #nsigma is the default value for sigma in gaussian priors
            self.nsigma = self.config.get('nested', 'sigma', fallback=2)

            if neuralNetwork:
                split = self.config.getfloat('neural', 'split', fallback=0.8)
                numNeurons = self.config.getint('neural',
                                                'numNeurons',
                                                fallback=100)
        else:
            self.engine = kwargs.pop('engine', 'dynesty')
            dynamic = kwargs.pop('dynamic', False)
            neuralNetwork = kwargs.pop('neuralNetwork', False)
            nestedType = kwargs.pop('nestedType', 'multi')
            nlivepoints = kwargs.pop('nlivepoints', 1024)
            accuracy = kwargs.pop('accuracy', 0.01)
            nproc = kwargs.pop('nproc', 1)

            self.priortype = kwargs.pop('priortype', 'u')
            self.nsigma = kwargs.pop('sigma', 2)

            # For neural networks
            split = kwargs.pop('split', 0.8)
            numNeurons = kwargs.pop('numNeurons', 100)

            if kwargs:
                logger.critical(
                    'Unexpected **kwargs for nested sampler: {}'.format(
                        kwargs))
                logger.info(
                    'You can skip writing any option and SimpleMC will use the default value.\n'
                    'Nested executer options are:\n\tnlivepoints (int) Default: 1024\n\t'
                    'accuracy (float) Default: 0.01\n\tpriortype ({"u", "g"}) Default: "u"\n\t'
                    'nestedType {"multi", "single", "balls", "cubes"} Default: "multi"\n\t'
                    'neuralNetwork (boolean) Default: True\n\t'
                    'dynamic (boolean) Default: False\n\t'
                    'addDerived (boolean) Default: True\n\t'
                    'engine {"dynesty", "nestle"} Default: "dynesty"')
                sys.exit(1)

        #stored output files
        if self.analyzername is None: self.analyzername = 'nested'
        self.outputpath = '{}_{}_{}_{}'.format(self.outputpath,
                                               self.analyzername, self.engine,
                                               nestedType)
        if neuralNetwork: self.outputpath = "{}_ANN".format(self.outputpath)
        self.outputChecker()

        #paralel run
        pool, nprocess = self.mppool(nproc)
        logger.info("\n\tnlivepoints: {}\n"
                    "\taccuracy: {}\n"
                    "\tnested type: {}\n"
                    "\tengine: {}".format(nlivepoints, accuracy, nestedType,
                                          self.engine))

        ti = time.time()
        if neuralNetwork:
            logger.info("\tUsing neural network.")
            #from bambi import run_pyBAMBI
            from simplemc.analyzers.pybambi.bambi import loglike_thumper
            learner = 'keras'
            # kerasmodel = None
            self.logLike = loglike_thumper(self.logLike, learner=learner, \
                                             ntrain=nlivepoints, nDims=self.dims)

        if dynamic:
            logger.info("\nUsing dynamic nested sampling...")
            sampler = dynesty.DynamicNestedSampler(self.logLike,
                                                   self.priorTransform,
                                                   self.dims,
                                                   bound=nestedType,
                                                   pool=pool,
                                                   queue_size=nprocess)

            sampler.run_nested(nlive_init=nlivepoints,
                               dlogz_init=0.05,
                               nlive_batch=100,
                               maxiter_init=10000,
                               maxiter_batch=1000,
                               maxbatch=10,
                               outputname=self.outputpath,
                               addDerived=self.addDerived,
                               simpleLike=self.L)
            M = sampler.results

        elif self.engine == 'dynesty':
            sampler = dynesty.NestedSampler(self.logLike,
                                            self.priorTransform,
                                            self.dims,
                                            bound=nestedType,
                                            sample='unif',
                                            nlive=nlivepoints,
                                            pool=pool,
                                            queue_size=nprocess)
            sampler.run_nested(dlogz=accuracy,
                               outputname=self.outputpath,
                               addDerived=self.addDerived,
                               simpleLike=self.L)
            M = sampler.results

        elif self.engine == 'nestle':
            try:
                import nestle
                M = nestle.sample(self.logLike,
                                  self.priorTransform,
                                  ndim=self.dims,
                                  method=nestedType,
                                  npoints=nlivepoints,
                                  dlogz=accuracy,
                                  callback=nestle.print_progress,
                                  pool=pool,
                                  queue_size=nprocess)
            except ImportError as error:
                sys.exit("{}: Please install nestle module"
                         "or use dynesty engine".format(
                             error.__class__.__name__))
        else:
            sys.exit('wrong selection')
        try:
            pool.close()
        except:
            pass
        self.ttime = time.time() - ti
        self.result = [
            'nested', M,
            M.summary(), 'nested :{}'.format(nestedType),
            'dynamic : {}'.format(dynamic), 'ANN :{}'.format(neuralNetwork),
            'engine: {}'.format(self.engine)
        ]
        return True
Пример #11
0
    def __init__(self, iniFile=None, **kwargs):
        """
        Read the input parameters or ini file
        Parameters
        ----------
        iniFile
        kwargs

        Returns
        -------

        """
        self.iniFile = iniFile
        if self.iniFile: self.iniReader(iniFile)
        else:
            self.chainsdir = kwargs.pop('chainsdir', 'simplemc/chains')
            self.model = kwargs.pop('model', None)
            self.prefact = kwargs.pop('prefact', 'phy')
            self.varys8 = kwargs.pop('varys8', False)
            self.datasets = kwargs.pop('datasets', 'HD')
            self.analyzername = kwargs.pop('analyzername', None)
            self.addDerived = kwargs.pop('addDerived', False)

            ## Next two are for custom model
            self.custom_parameters = kwargs.pop('custom_parameters', None)
            self.custom_function = kwargs.pop('custom_function', None)
            ## Following two are for custom data
            self.path_to_data = kwargs.pop('path_to_data', None)
            self.path_to_cov = kwargs.pop('path_to_cov', None)
            self.fn = kwargs.pop("fn", "generic")

            if os.path.exists(os.path.join(self.chainsdir)):
                self.chainsdir = os.path.join(self.chainsdir)
            else:
                logger.info(
                    "Your chains directory does not exist. Create a new one and try again."
                )
                sys.exit(1)
            if kwargs:
                logger.critical(
                    'Unexpected **kwargs for DriverMC: {}'.format(kwargs))
                logger.info(
                    'You can skip writing any option and SimpleMC will use the default value.\n'
                    'DriverMC **kwargs are:\n\tmodel\n\t'
                    'datasets\n\t'
                    'analyzername {"nested", "mcmc", "maxlike", "emcee" , "genetic"} Default: mcmc'
                    '\n\tchainsdir Default: SimpleMC_chains\n\t')
                sys.exit(1)

        #Initialize the Theory & Datasets
        T = ParseModel(self.model,
                       custom_parameters=self.custom_parameters,
                       custom_function=self.custom_function)
        L = ParseDataset(self.datasets,
                         path_to_data=self.path_to_data,
                         path_to_cov=self.path_to_cov,
                         fn=self.fn)

        if self.prefact == "pre": T.setVaryPrefactor()
        if self.varys8 == "True": T.setVarys8()
        T.printFreeParameters()

        #set the likelihood for a model
        L.setTheory(T)

        self.T, self.L = T, L

        self.pars_info = self.L.freeParameters()
        self.bounds = [p.bounds for p in self.pars_info]
        self.means = [p.value for p in self.pars_info]
        self.paramsList = [p.name for p in self.pars_info]
        self.dims = len(self.paramsList)
        self.result = None

        self.root = "{}_{}_{}".format(self.model, self.prefact, self.datasets)
        self.outputpath = "{}/{}".format(self.chainsdir, self.root)
Пример #12
0
    def mcmcRunner(self, iniFile=None, **kwargs):
        """
            This method calls MCMCAnalyzer.
            Returns [MCMCAnalyzer object, time, evidence via MCEvidence (if it is possible)]

        """
        if iniFile:
            nsamp = self.config.getint('mcmc', 'nsamp', fallback=50000)
            skip = self.config.getint('mcmc', 'skip', fallback=300)
            ## temperature at which to sample, weights get readjusted on the fly
            temp = self.config.getfloat('mcmc', 'temp', fallback=2)
            chainno = self.config.getint('mcmc', 'chainno', fallback=1)
            GRstop = self.config.getfloat('mcmc', 'GRstop', fallback=0.01)
            evidence = self.config.getboolean('mcmc',
                                              'evidence',
                                              fallback=False)
        else:
            nsamp = kwargs.pop('nsamp', 50000)
            skip = kwargs.pop('skip', 300)
            temp = kwargs.pop('temp', 2)
            chainno = kwargs.pop('chainno', 1)
            GRstop = kwargs.pop('GRstop', 0.01)
            evidence = kwargs.pop('evidence', False)
            if kwargs:
                logger.critical(
                    'Unexpected **kwargs for MCMC: {}'.format(kwargs))
                logger.info(
                    'You can skip writing any option and SimpleMC will use default values.\n'
                    'MCMC executer kwargs are:\n\tnsamp (int) Default: 50000\n\t'
                    'skip (int) Default 300\n\ttemp (float) Default: 2.0'
                    '\n\tchainno (int) Default: 1\n\t'
                    'evidence (boolean) Default: False')
                sys.exit(1)
                #raise TypeError('Unexpected **kwargs: {}'.format(kwargs))
        logger.info("\n\tnsamp: {}\n\tskip: {}\n\t"
                    "temp: {}\n\tchain num: {}\n\tevidence: {}".format(
                        nsamp, skip, temp, chainno, evidence))
        if self.analyzername is None: self.analyzername = 'mcmc'
        self.outputpath = "{}_{}".format(self.outputpath, self.analyzername)
        #Check whether the file already exists
        self.outputChecker()
        ti = time.time()

        #Main process
        M = MCMCAnalyzer(self.L,
                         self.outputpath,
                         skip=skip,
                         nsamp=nsamp,
                         temp=temp,
                         chain_num=chainno,
                         addDerived=self.addDerived,
                         GRstop=GRstop)

        self.ttime = time.time() - ti

        #Compute Bayesian Evidence
        if evidence:
            try:
                from MCEvidence import MCEvidence
                logger.info(
                    "Aproximating bayesian evidence with MCEvidence (arXiv:1704.03472)\n"
                )
                MLE = MCEvidence(self.outputpath + ".txt").evidence()
                self.result = [
                    'mcmc', M, "Evidence with MCEvidence : {}\n".format(MLE),
                    strresult
                ]
            except:
                #writeSummary(self.chainsdir, outputname, ttime)
                # print("Warning!")
                # print("MCEvidence could not calculate the Bayesian evidence [very small weights]\n")
                logger.error(
                    "MCEvidence could not calculate the Bayesian evidence [very small weights]"
                )
        else:
            self.result = ['mcmc', M, "Maxlike: {}".format(M.maxloglike)]

        return True
Пример #13
0
 def printParameters(self, params):
     for p in params:
         logger.info(p.name, '=', p.value, '+/-', p.error)
Пример #14
0
 def printFreeParameters(self):
     logger.info("Free parameters and its bounds:")
     self.printParameters(self.freeParameters())