def get_one_sigma_errs(indep_vars, params, model, **kwargs): """ Return the function mean and 1-sigma error at each `k` value """ data = {name : params[name] for name in params.columns} data.update(kwargs) def function(data, **indep_vars): data.update(indep_vars) values = model(**data) errs = one_sigma(values) return 0.5*(errs[0] + errs[1]) names = indep_vars.keys() indep_vars = np.vstack([v for v in indep_vars.values()]).T return np.array([function(data, **dict(zip(names, row))) for row in indep_vars])
def load_data(data, space, **meta): """ Load the data, saved in columns to an ASCII file, into either a ``PkmuResult`` or ``PkResult`` object """ # make the edges k = data[:, 0] kedges = make_kedges(data) factor = 1.0 / 12 ** 0.5 if space == "redshift": Nmu = 5 muedges = np.linspace(0, 1, Nmu + 1) mu = 0.5 * (muedges[1:] + muedges[:-1]) k, mu = np.broadcast_arrays(k[..., None], mu[None, ...]) power = np.vstack([data[:, 2 * i + 1] for i in range(Nmu)]).T error = np.vstack([data[:, 2 * (i + 1)] for i in range(Nmu)]).T data_dict = {"power": power, "error": error * factor, "k": k, "mu": mu} return Power2dDataSet([kedges, muedges], data_dict, **meta) elif space == "real": data_dict = {"power": data[:, -2], "error": data[:, -1] * factor, "k": k} return Power1dDataSet(kedges, data_dict, **meta) else: raise ValueError
def write(self, key, result, model, eval_kws, **extra): if 'k' not in key: raise ValueError("to write the best-fit function must have `k` in the key") index_cols = key.keys() with self.open(index_cols) as output: Nk = len(key['k']) # the function mean kws = eval_kws.copy() params = dict(zip(result.free_names, result.values())) kws.update(params) mu = model.eval(**kws) # get the 1sigma errors traces = [result[k].flat_trace for k in result.free_names] other_names = [name for name in model.param_names if name not in result.free_names] traces += [np.repeat(result[name].median, len(traces[0])) for name in other_names] params = np.vstack(traces).T params = pd.DataFrame(params, columns=result.free_names+other_names) indep_vars = {k:eval_kws[k] for k in model.independent_vars} mean_errs = get_one_sigma_errs(indep_vars, params, model.eval, **eval_kws) # append to the output frame d = {'mean':mu, 'error':mean_errs} for k in key: if np.isscalar(key[k]): d[k] = np.repeat(key[k], Nk) else: d[k] = key[k] for k in extra: d[k] = np.repeat(extra[k], Nk) self._output = output.append(pd.DataFrame(d))