Ejemplo n.º 1
0
 def result(self):
     """
     The fitting result, either a LBFGSResult or EmceeResult
     """
     try:
         return self._result
     except AttributeError:
         
         # check if combined mcmc result is there
         path = os.path.join(self.fitting_dir, 'info', 'combined_result.npz')
         if os.path.isfile(path):
             r = EmceeResults.from_npz(path)
         else:
             files = glob(os.path.join(self.fitting_dir, '*npz'))
             if not len(files):
                 raise ValueError("no suitable results files found in directory '%s'" %d)
             
             # grab the file modified last
             times = [os.stat(f).st_mtime for f in files]
             try:
                 r = LBFGSResults.from_npz(files[np.argmax(times)])
             except:
                 raise ValueError("if directory is from mcmc fit, define the `info` directory")
                     
         self._result = r
         return self._result
Ejemplo n.º 2
0
def visualizing_results(*args):

    from pyRSD.rsdfit.results import EmceeResults

    r = EmceeResults.from_npz('data/mcmc_result.npz')

    # 2D kernel density plot
    r.kdeplot_2d('b1_cA', 'fsigma8', thin=10)
    savefig('kdeplot.png', size=(8, 6))

    # 2D joint plot
    r.jointplot_2d('b1_cA', 'fsigma8', thin=10)
    savefig('jointplot.png', size=(8, 6))

    # timeline plot
    r.plot_timeline('fsigma8', 'b1_cA', thin=10)
    savefig('timeline.png', size=(8, 6))

    # correlation between free parameters
    r.plot_correlation(params='free')
    savefig('correlation.png', size=(8, 6))

    # make a triangle plot
    r.plot_triangle('fsigma8', 'alpha_perp', 'alpha_par', thin=10)
    savefig('triangle.png', size=(8, 6))
Ejemplo n.º 3
0
def load_results(filename):
    """
    Load a result from file
    """
    try:
        result = EmceeResults.from_npz(filename)
    except:
        result = LBFGSResults.from_npz(filename)
    return result
Ejemplo n.º 4
0
def run_global_mcmc(args, theory_model, data_loader):
    """
    A generator that runs the mcmc solver over a specified 
    coordinate grid of bins
    
    Parameters
    ----------
    args : argparse.Namespace
        the namespace of arguments returned by `parse_global_mcmc`
    theory_model : lmfit.Model
        the model class that will be called to return the theoretical model
    data_loader : callable
        a function that should take a dictionary specifying the coordinate
        values as the only argument
        
    Returns
    -------
    result : EmceeResults
        the result of the mcmc run, stored as an `EmceeResults` instance
    """
    # setup the mcmc run
    params, theory_params, init_values = setup_mcmc(args.param_file)
    
    # check for init_from == chain
    if params['init_from'] == 'chain':
        from pyRSD.rsdfit.results import EmceeResults
        r = EmceeResults.from_npz(params['start_chain'].value)
        best = dict(zip(r.free_names, r.max_lnprob_values()))
        init_values = np.array([best[k] for k in theory_params.free_names])
    
    # load the data
    data_kws = data_loader()

    # make the objective function
    objective = functools.partial(lnprob, model=theory_model, theory=theory_params, **data_kws)

    # run emcee
    result = emcee_fitter.solve(params, theory_params, objective, init_values=init_values)
    print(result)
    
    return result
Ejemplo n.º 5
0
def load_joint_data_results(kmin, z_weighted, p, ells=[0, 2]):
    """
    Load a set of data joint NGC + SGC fit results.
    """
    from eboss_qso.measurements.utils import make_hash
    from pyRSD.rsdfit.results import EmceeResults

    # the data to load
    kws = {}
    kws['version'] = 'v1.9f'
    kws['krange'] = '%s-0.3' % kmin
    kws['params'] = 'basemodel-N-fnl'
    kws['zrange'] = '0.8-2.2'
    kws['z_weighted'] = z_weighted
    kws['p'] = p

    if ells == [0]:
        kws['ells'] = ells

    hashstr = make_hash(kws)

    d = os.path.join(os.environ['EBOSS_DIR'], 'fits', 'results', 'data',
                     'v1.9f')
    d = os.path.join(d, kws['krange'], kws['params'], kws['zrange'])
    assert os.path.isdir(d), "'%s' directory not found" % d

    matches = glob(os.path.join(d, f'QSO-N+S-{hashstr}'))
    assert len(matches) == 1
    match = matches[0]

    if match is None:
        raise ValueError("no matches found for joint NGC + SGC data fits!")

    r = sorted(glob(os.path.join(match, '*.npz')),
               key=os.path.getmtime,
               reverse=True)
    assert len(
        r) > 0, "no npz results found in directory '%s'" % os.path.normpath(f)

    return EmceeResults.from_npz(r[0])
Ejemplo n.º 6
0
    def start_from(self, val):
        if val is None:
            self._start_from = val
            return
            
        if '$' in val:
            from pyRSD.rsdfit.parameters.tools import replace_vars
            val = replace_vars(val, {})
        
        import os
        if not os.path.exists(val):
            raise RuntimeError("cannot set `start_from` to `%s`: no such file" %val)
        
        if os.path.isdir(val):
            from glob import glob
            from pyRSD.rsdfit.results import EmceeResults, LBFGSResults
            import operator
            
            pattern = os.path.join(val, "*.npz")
            result_files = glob(pattern)
            if not len(result_files):
                raise RuntimeError("did not find any chain (`.npz`) files matching pattern `%s`" %pattern)
            
            # find the chain file which has the maximum log prob in it and use that
            max_lnprobs = []
            for f in result_files:
                
                try:
                    r = EmceeResults.from_npz(f)
                    max_lnprobs.append(r.max_lnprob)
                except:
                    r = LBFGSResults.from_npz(f)
                    max_lnprobs.append(-r.min_chi2)

            index, value = max(enumerate(max_lnprobs), key=operator.itemgetter(1))
            self._start_from = result_files[index]
        else:
            self._start_from = val
Ejemplo n.º 7
0
def find_init_result(val):
    """
    Return the name of the file holding the maximum probability
    from a directory
    """
    import os
    if not os.path.exists(val):
        raise RuntimeError("cannot set `start_from` to `%s`: no such file" %
                           val)

    if os.path.isdir(val):
        from glob import glob
        from pyRSD.rsdfit.results import EmceeResults, LBFGSResults
        import operator

        pattern = os.path.join(val, "*.npz")
        result_files = glob(pattern)
        if not len(result_files):
            raise RuntimeError(
                "did not find any chain (`.npz`) files matching pattern `%s`" %
                pattern)

        # find the chain file which has the maximum log prob in it and use that
        max_lnprobs = []
        for f in result_files:

            try:
                r = EmceeResults.from_npz(f)
                max_lnprobs.append(r.max_lnprob)
            except:
                r = LBFGSResults.from_npz(f)
                max_lnprobs.append(-r.min_chi2)

        index, value = max(enumerate(max_lnprobs), key=operator.itemgetter(1))
        return result_files[index]
    else:
        return val