Пример #1
0
    def fit(self,
            numLevels,
            directory="",
            numPerSave=1000,
            numParticles=5,
            new_level_interval=10000):

        sampler = dnest4.DNest4Sampler(self.model,
                                       backend=dnest4.backends.CSVBackend(
                                           basedir="./" + directory, sep=" "))

        # Set up the sampler. The first argument is max_num_levels
        gen = sampler.sample(max_num_levels=numLevels,
                             num_steps=200000,
                             new_level_interval=new_level_interval,
                             num_per_step=numPerSave,
                             thread_steps=100,
                             num_particles=numParticles,
                             lam=10,
                             beta=100,
                             seed=1234)

        # Do the sampling (one iteration here = one particle save)
        for i, sample in enumerate(gen):
            print("# Saved {k} particles.".format(k=(i + 1)))
Пример #2
0
    def fit_particle(self, manager_comm,  numLevels, directory="", numPerSave=1000, numParticles=5, new_level_interval=10000):
      """This is a different method to be used in place of fit() for the case where you are fitting with MPI, with a 
        thread for each particle. 
      """
      mpi_sampler = dnest4.MPISampler(comm=manager_comm, debug=False)

      if manager_comm.rank == 0:
          # Set up the sampler. The first argument is max_num_levels
          sampler = dnest4.DNest4Sampler(self.model, backend=dnest4.backends.CSVBackend(basedir =directory,#basedir ="./" + directory,
                                                                          sep=" "), MPISampler=mpi_sampler)

          gen = sampler.sample(max_num_levels=numLevels, num_steps=200000, new_level_interval=new_level_interval,
                                num_per_step=numParticles, thread_steps=100,
                                lam=10, beta=100, seed=1234)

          # Do the sampling (one iteration here = one particle save)
          for i, sample in enumerate(gen):
            #   print("# Saved {k} particles.".format(k=(i+1)))

              if self.debug:
                  meminfo = "Particle {0} memory: {1}\n".format(MPI.COMM_WORLD.Get_rank(),  memory_usage_psutil())
                  with open(self.debug_mem_file, "a") as f:
                      f.write(meminfo)

      else:
          mpi_sampler.wait(self.model, max_num_levels=numLevels, num_steps=200000, new_level_interval=new_level_interval,
                                num_per_step=numPerSave, thread_steps=100,
                                lam=10, beta=100, seed=1234)
          return
Пример #3
0
    def fit(self, numLevels, directory=None, numPerSave=1000,numParticles=5,new_level_interval=10000,debug=False ):

      if directory is None:
        directory = "./"  

      if not os.access(directory, os.W_OK):
        raise OSError("Directory {} either does not exist or is not writeable to the fitter...".format(directory))

    
    #   mpi = dnest4.MPISampler()

      sampler = dnest4.DNest4Sampler(self.model,
                                     backend=dnest4.backends.CSVBackend(basedir = directory,
                                                                        sep=" "))
                                    # MPISampler=mpi)

      # Set up the sampler. The first argument is max_num_levels
      gen = sampler.sample(max_num_levels=numLevels, num_steps=200000, new_level_interval=new_level_interval,
                            num_per_step=numPerSave, thread_steps=100,
                            num_particles=numParticles, lam=10, beta=100, seed=1234)

      # Set up the sampler. The first argument is max_num_levels
    #   gen = sampler.sample(model=self.model,
    #                         max_num_levels=numLevels, 
    #                         num_steps=-1, 
    #                         num_per_step=numPerSave,
    #                         new_level_interval=new_level_interval,
    #                         thread_steps=1,
    #                         lam=10, beta=100, seed=1234)


      # Do the sampling (one iteration here = one particle save)
      for i, sample in enumerate(gen):
          print("# Saved {k} particles.".format(k=(i+1)))
Пример #4
0
    def fit(self, numLevels, directory="",numPerSave=1000,numParticles=5 ):
        """Called by rank 0 of the fit, directly from the top script where the 
        fit manager is instantiated. 
        In general, this is where the master rank will spend all its time, and 
        it will not leave this method until it is killed. 
        """
        mpi_sampler = dnest4.MPISampler(debug=self.debug)#comm=manager_comm, debug=False)

        print(F"Initializing DNest4Sampler from fm fit in {directory}...")
        sampler = dnest4.DNest4Sampler(
            self.model,
            backend=dnest4.backends.CSVBackend(basedir =directory,sep=" "),
            #MPISampler=mpi_sampler
            )
        
        print("Setting up DNest4Sampler generator...")
        # Set up the sampler. The first argument is max_num_levels
        gen = sampler.sample(max_num_levels=numLevels, num_steps=200000, new_level_interval=10000,
                        num_per_step=numParticles, thread_steps=100,
                        lam=10, beta=100, seed=1234)
        # gen = sampler.sample(max_num_levels=numLevels, num_steps=200000, new_level_interval=10000,
        #                     num_per_step=numPerSave, thread_steps=100,
        #                     num_particles=numParticles, lam=10, beta=100, seed=1234)

        print("Doing the sampling from generator...")
        # Do the sampling (one iteration here = one particle save)
        for i, sample in enumerate(gen):
            print("# Saved {k} particles.".format(k=(i+1)))
Пример #5
0
def fit(directory):
    if reinitializeDetector:
        initializeDetectorAndWaveforms(det.__getstate__(),
                                       wfs,
                                       reinit=reinitializeDetector,
                                       doInterp=doMaxInterp)
    else:
        initializeDetectorAndWaveforms(det,
                                       wfs,
                                       reinit=reinitializeDetector,
                                       doInterp=doMaxInterp)
    initMultiThreading(numThreads)

    # Create a model object and a sampler
    model = Model()
    sampler = dnest4.DNest4Sampler(model,
                                   backend=dnest4.backends.CSVBackend(
                                       basedir="./" + directory, sep=" "))

    # Set up the sampler. The first argument is max_num_levels
    gen = sampler.sample(max_num_levels=numLevels,
                         num_steps=100000,
                         new_level_interval=25000,
                         num_per_step=1000,
                         thread_steps=100,
                         num_particles=5,
                         lam=10,
                         beta=100,
                         seed=1234)

    # Do the sampling (one iteration here = one particle save)
    for i, sample in enumerate(gen):
        print("# Saved {k} particles.".format(k=(i + 1)))
Пример #6
0
def fit(argv):

    initializeDetector(det, )
    initializeWaveform(wf, results[wf_idx]['x'])

    # Create a model object and a sampler
    model = Model()
    sampler = dnest4.DNest4Sampler(model,
                                   backend=dnest4.backends.CSVBackend(".",
                                                                      sep=" "))

    seed = 1234
    np.random.seed(seed)
    # Set up the sampler. The first argument is max_num_levels
    gen = sampler.sample(max_num_levels=100,
                         num_steps=1000000,
                         new_level_interval=100000,
                         num_per_step=10000,
                         thread_steps=100,
                         num_particles=5,
                         lam=10,
                         beta=100,
                         seed=seed)

    # Do the sampling (one iteration here = one particle save)
    for i, sample in enumerate(gen):
        print("# Saved {k} particles.".format(k=(i + 1)))

    # Run the postprocessing
    dnest4.postprocess()
Пример #7
0
    def run_sampler(self):
        import dnest4

        self._set_dnest4_kwargs()
        backend = self._set_backend()

        self._verify_kwargs_against_default_kwargs()
        self._setup_run_directory()
        self._check_and_load_sampling_time_file()
        self.start_time = time.time()

        self.sampler = dnest4.DNest4Sampler(self._dnest4_model,
                                            backend=backend)
        out = self.sampler.sample(self.max_num_levels,
                                  num_particles=self.num_particles,
                                  **self.dnest4_kwargs)

        for i, sample in enumerate(out):
            if self._verbose and ((i + 1) % 100 == 0):
                stats = self.sampler.postprocess()
                logger.info("Iteration: {0} log(Z): {1}".format(
                    i + 1, stats['log_Z']))

        self._calculate_and_save_sampling_time()
        self._clean_up_run_directory()

        stats = self.sampler.postprocess(resample=1)
        self.result.log_evidence = stats['log_Z']
        self._information = stats['H']
        self.result.log_evidence_err = np.sqrt(self._information /
                                               self.num_particles)

        if self._backend == 'memory':
            self._last_live_sample_info = pd.DataFrame(
                self.sampler.backend.sample_info[-1])
            self.result.log_likelihood_evaluations = self._last_live_sample_info[
                'log_likelihood']
            self.result.samples = np.array(
                self.sampler.backend.posterior_samples)
        else:
            sample_info_path = './' + self.kwargs[
                "outputfiles_basename"] + '/sample_info.txt'
            sample_info = np.genfromtxt(sample_info_path,
                                        comments='#',
                                        names=True)
            self.result.log_likelihood_evaluations = sample_info[
                'log_likelihood']
            self.result.samples = np.array(
                self.sampler.backend.posterior_samples)

        self.result.sampler_output = out
        self.result.outputfiles_basename = self.outputfiles_basename
        self.result.sampling_time = datetime.timedelta(
            seconds=self.total_sampling_time)

        self.calc_likelihood_count()

        return self.result
Пример #8
0
def fit(directory):
    # Create a model object and a sampler
    model = Model()
    sampler = dnest4.DNest4Sampler(model,
                                 backend=dnest4.backends.CSVBackend(basedir ="./" + directory,
                                                                    sep=" "))

    # Set up the sampler. The first argument is max_num_levels
    gen = sampler.sample(max_num_levels=100, num_steps=10000, new_level_interval=10000,
                        num_per_step=1000, thread_steps=100,
                        num_particles=5, lam=10, beta=100, seed=1234)

    # Do the sampling (one iteration here = one particle save)
    for i, sample in enumerate(gen):
      print("# Saved {k} particles.".format(k=(i+1)))
Пример #9
0
    def run(self, verbose=False):
        """Initiate the DNest4 Nested Sampling run."""

        if self.dnest4_backend == 'csv':
            # for CSVBackend, which is output data to disk
            backend = dnest4.backends.CSVBackend("./dnest4_run_", sep=" ")
        else:
            # for the MemoryBackend, which is output data to memory
            backend = dnest4.backends.MemoryBackend()
        sampler = dnest4.DNest4Sampler(self._dnest4_model, backend=backend)
        output = sampler.sample(self.n_diffusive_levels,
                                num_particles=self.population_size,
                                **self.dnest4_kwargs)
        self._output = output
        for i, sample in enumerate(output):
            if verbose and ((i + 1) % 100 == 0):
                stats = sampler.postprocess()
                print("Iteration: {0} log(Z): {1}".format(
                    i + 1, stats['log_Z']))
        stats = sampler.postprocess(resample=1)
        self._log_evidence = stats['log_Z']
        self._information = stats['H']
        logZ_err = np.sqrt(self._information / self.population_size)
        self._logZ_err = logZ_err
        ev_err = np.exp(logZ_err)
        self._evidence_error = ev_err
        self._evidence = np.exp(self._log_evidence)
        # To compute posterior distributions
        self._samples = np.array(sampler.backend.posterior_samples)
        # To compute AIC estimate
        # print(sampler.backend.sample_info)
        # print(len(sampler.backend.sample_info))
        # print(sampler.backend.sample_info[-1])
        # print(len(sampler.backend.sample_info[-1]))
        # print(pd.DataFrame(sampler.backend.sample_info[-1]))
        print(len(sampler.backend.samples[-1]))
        print(len(sampler.backend.weights[-1]))
        # quit()
        self._last_live_sample = sampler.backend.samples[-1]
        self._last_live_sample_weights = sampler.backend.weights[-1]
        self._last_live_sample_info = pd.DataFrame(
            sampler.backend.sample_info[-1])
        return self.log_evidence, self.log_evidence_error
Пример #10
0
 def run(self):
     """
     Executes the inference
     """
     if self.prepare():
         # Set up the inference
         backend = dnest4.backends.CSVBackend(basedir=self.outputdir,
                                              sep=" ")
         dns = dnest4.DNest4Sampler(DNest4_Model(loglike=self.loglike,
                                                 perturb=self.perturb,
                                                 prior=self.prior),
                                    backend=backend)
         # Run it
         out = dns.sample(max_num_levels=self.nlevel,
                          num_steps=self.niter,
                          new_level_interval=self.nlevelint,
                          num_per_step=self.nperstep,
                          lam=self.lam,
                          beta=self.beta)
         for i, samp in enumerate(out):
             if self.verb:
                 print(''.join(
                     ['Iteration: ',
                      str(i + 1), '/',
                      str(self.niter)]),
                       end='\r')
         print('')
         # Best-fit parameters
         ibest = np.argmax(backend.sample_info["log_likelihood"])
         self.bestp = backend.samples[ibest]
         # Resample for posterior
         stats = dns.postprocess(resample=self.resample)
         self.outp = backend.posterior_samples.T
         """
         stats = dns.postprocess()
         # Load the weights to properly estimate the posterior
         #weights = np.loadtxt(os.path.join(self.outputdir, 'weights.txt'))
         weights = np.squeeze(backend.weights)
         wsh     = weights.shape[0]
         # Remove cases w/ 0 weight
         samps   = backend.samples
         samps   = samps  [weights!=0]
         weights = weights[weights!=0]
         # As in MultiNest, resample to equal weights by considering 
         # int(nsamples*weight) repetitions
         self.outp = []
         for i in range(len(weights)):
             self.outp.extend([samps[i] 
                               for j in range(int(wsh * weights[i]))])
         self.outp = np.asarray(self.outp).T
         """
         # Quantiles
         if self.kll is not None:
             for i in range(self.outp.shape[-1]):
                 self.kll.update(self.model(self.outp[:, i], fullout=True))
         # Save posterior and bestfit params
         if self.fsavefile is not None:
             np.save(self.fsavefile, self.outp)
         if self.fbestp is not None:
             np.save(self.fbestp, self.bestp)
     else:
         if self.verb:
             print("Sampler is not fully prepared to run. " + \
                   "Correct the above errors and try again.")
Пример #11
0
        Generate from the heavy-tailed distribution.
        """
        a = np.random.randn()
        b = np.random.rand()
        t = a/np.sqrt(-np.log(b))
        n = np.random.randn()
        return 10.0**(1.5 - 3*np.abs(t))*n

    def wrap(self, x, a, b):
        assert b > a
        return (x - a)%(b - a) + a

# Create a model object and a sampler
model = Model()
sampler = dnest4.DNest4Sampler(model,
                               backend=dnest4.backends.CSVBackend(".",
                                                                  sep=" "))

# Set up the sampler. The first argument is max_num_levels
gen = sampler.sample(max_num_levels=30, num_steps=1000, new_level_interval=10000,
                      num_per_step=10000, thread_steps=100,
                      num_particles=5, lam=10, beta=100, seed=1234)

# Do the sampling (one iteration here = one particle save)
for i, sample in enumerate(gen):
    print("# Saved {k} particles.".format(k=(i+1)))

# Run the postprocessing
dnest4.postprocess()