Example #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
Example #2
0
def load_results(filename):
    """
    Load a result from file
    """
    try:
        result = EmceeResults.from_npz(filename)
    except:
        result = LBFGSResults.from_npz(filename)
    return result
Example #3
0
def load_fits():
    """
    Load a set of fit results.

    Returns a structued numpy array holding best-fit values for all free
    parameters all mocks.
    """
    # find matches
    pattern = os.path.join(os.environ['RSDFIT_FITS'], 'cutsky',
                           'ChallengeMockN', 'fkp_1e4', 'box*')
    pattern = os.path.join(pattern, 'poles',
                           'nlopt_gausscov_base_high_nbar_unscaled_kmax04')
    matches = glob(pattern)
    assert len(matches) > 0

    driver = None
    data = defaultdict(list)
    for f in matches:
        r = sorted(glob(os.path.join(f, '*.npz')),
                   key=os.path.getmtime,
                   reverse=True)
        assert len(
            r
        ) > 0, "no npz results found in directory '%s'" % os.path.normpath(f)

        th = ParameterSet.from_file(os.path.join(f, 'params.dat'),
                                    tags='theory')
        r = LBFGSResults.from_npz(r[0])
        for param in r.free_names:
            data[param].append(r[param])
            th[param].value = r[param]

        if driver is None:
            driver = FittingDriver.from_directory(f, init_model=False)

        # add fsigma8
        if 'f' in r.free_names and 'sigma8_z' in r.free_names:
            data['fsigma8'].append(r['f'] * r['sigma8_z'])

        # the prior to add back
        lnprior = sum(par.lnprior for par in th.free)

        # add the reduced chi2
        red_chi2 = (2 * (r.min_chi2 + lnprior)) / driver.dof
        data['red_chi2'].append(red_chi2)

    params = list(data.keys())
    dtype = list(zip(params, ['f8'] * len(params)))
    out = numpy.empty(len(matches), dtype=dtype)
    for param in out.dtype.names:
        out[param] = numpy.array(data[param])

    return out
Example #4
0
def load_fits(sample, params):
    """
    Load a set of ezmock fit results.

    Returns a structued numpy array holding best-fit values for all free
    parameters all mocks.
    """
    # find matches
    pattern = os.path.join(os.environ['EBOSS_FITS'], 'mocks', 'ezmock', 'v1.8e-fph', '0.0001-0.3', params)
    assert os.path.isdir(pattern)
    assert sample in 'NS'
    pattern = os.path.join(pattern, '0.8-2.2', f'QSO-{sample}-*-P0+P2-mock-cov-*')

    matches = glob(pattern)
    assert len(matches) > 0

    driver = None
    data = defaultdict(list)
    for f in matches:
        r = sorted(glob(os.path.join(f, '*.npz')), key=os.path.getmtime, reverse=True)
        assert len(r) > 0, "no npz results found in directory '%s'" %os.path.normpath(f)

        th = ParameterSet.from_file(os.path.join(f, 'params.dat'), tags='theory')
        r = LBFGSResults.from_npz(r[0])
        for param in r.free_names:
            data[param].append(r[param])
            th[param].value = r[param]

        if driver is None:
            driver = FittingDriver.from_directory(f, init_model=False)

        # add fsigma8
        if 'f' in r.free_names and 'sigma8_z' in r.free_names:
            data['fsigma8'].append(r['f'] * r['sigma8_z'])

        # the prior to add back
        lnprior = sum(par.lnprior for par in th.free)

        # add the reduced chi2
        red_chi2 = (2*(r.min_chi2 + lnprior)) / driver.dof
        data['red_chi2'].append(red_chi2)

    params = list(data.keys())
    dtype = list(zip(params, ['f8']*len(params)))
    out = numpy.empty(len(matches), dtype=dtype)
    for param in out.dtype.names:
        out[param] = numpy.array(data[param])

    return out
Example #5
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
Example #6
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
Example #7
0
def load_ezmock_results(version, sample, krange, params, z_weighted, p=None):
    """
    Load a set of ezmock fit results.

    Returns a structued numpy array holding best-fit values for all free
    parameters all mocks.
    """
    from pyRSD.rsdfit.results import LBFGSResults
    from pyRSD.rsdfit import FittingDriver
    from pyRSD.rsdfit.parameters import ParameterSet
    from collections import defaultdict

    assert sample in ['N', 'S']
    assert version in ['v1.8e-no', 'v1.8e-fph', 'v1.8e-reg']

    d = os.path.join(os.environ['EBOSS_DIR'], 'fits', 'results', 'mocks',
                     'ezmock', version)
    d = os.path.join(d, krange, params, '0.8-2.2')
    assert os.path.isdir(d), "'%s' directory not found" % d

    if p is None or p == 1.6:
        p = [None, 1.6]
    else:
        p = [p]

    matches = glob(os.path.join(d, f'QSO-{sample}-0001-*'))
    match = None
    for f in matches:
        hashkeys = get_hashkeys(f, None)
        if hashkeys['p'] in p and hashkeys['z-weighted'] == z_weighted:
            match = f

    if match is None:
        raise ValueError((
            f"no matches found: version={version}, sample={sample}, "
            f"krange={krange}, params={params}, z_weighted={z_weighted}, p={p}"
        ))

    # load the driver
    driver = FittingDriver.from_directory(match)
    dirname, basename = os.path.split(match)
    pattern = os.path.join(dirname, basename.replace('0001', '*'))

    data = defaultdict(list)
    matches = glob(pattern)

    for f in matches:
        r = sorted(glob(os.path.join(f, '*.npz')),
                   key=os.path.getmtime,
                   reverse=True)
        if len(r) == 0:
            raise ValueError(
                "warning: no npz results found in directory '%s'" %
                os.path.normpath(f))

        th = ParameterSet.from_file(os.path.join(f, 'params.dat'),
                                    tags='theory')
        r = LBFGSResults.from_npz(r[0])
        for param in r.free_names:
            data[param].append(r[param])
            th[param].value = r[param]

        # add fsigma8
        if 'f' in r.free_names and 'sigma8_z' in r.free_names:
            data['fsigma8'].append(r['f'] * r['sigma8_z'])

        # the prior to add back
        lnprior = sum(par.lnprior for par in th.free)

        # add the reduced chi2
        red_chi2 = (2 * (r.min_chi2 + lnprior)) / driver.dof
        data['red_chi2'].append(red_chi2)

    params = list(data.keys())
    dtype = list(zip(params, ['f8'] * len(params)))
    out = numpy.empty(len(matches), dtype=dtype)
    for param in out.dtype.names:
        out[param] = numpy.array(data[param])

    return out