def plot_mcmc_bestfit(): """ Load a mcmc bestfit and plot it """ from pyRSD.rsdfit import FittingDriver import plotify as pfy parser = argparse.ArgumentParser() parser.formatter_class = argparse.RawTextHelpFormatter # arguments h = 'the name of directory holding the results' parser.add_argument('results_dir', type=str, help=h) h = 'the name of the results file to load' parser.add_argument('results_file', type=str, help=h) h = 'the name of the model to load' parser.add_argument('model', type=str, help=h) h = 'the output file name' parser.add_argument('-o', '--output', type=str, help=h) h = 'the number of burnin steps to set' parser.add_argument('-b', '--burnin', type=int, help=h) args = parser.parse_args() # load the driver kwargs = {'results_file':args.results_file, 'model_file':args.model} driver = FittingDriver.from_directory(args.results_dir, **kwargs) if args.burnin is not None: driver.results.burnin = args.burnin driver.set_fit_results() driver.plot() if args.output is not None: pfy.savefig(args.output) pfy.show()
def compare_bestfits(mode, **kwargs): """ Compare the best-fit parameters to the data. Parameters ---------- mode : str, {`function`, `params`, `gp`, `spline`} either compare to bestfit function or bestfit params kwargs : key/value pairs data : subclass of lsskit.speckmod.plugins.ModelInput plugin instance specifying the data model : subclass of lsskit.speckmod.plugins.ModelInput plugin instance specifying the model, only needed for `mode == params` bestfit_file : str the name of the file holding the pickled dataframe select : list of str a list holding strings with the format should `index_col`:value'. This specifies which bin to be compared """ import plotify as pfy import pandas as pd if mode == 'gp': from pyRSD.rsd.mu0_modeling import GPModelParams elif mode == 'spline': from pyRSD.rsd.mu0_modeling import SplineTableModelParams if mode not in ['function', 'params', 'gp', 'spline']: raise ValueError("``mode`` in compare_bestfits must be `function`, `params`, `gp`, or `spline`") # make the index cols try: index_cols = [x.split(':')[0] for x in kwargs['select']] select = [int(x.split(':')[1]) for x in kwargs['select']] except: raise ValueError("``select`` should have format: `index_col`:value") # read the bestfits file and select df = pd.read_pickle(kwargs['bestfit_file']) valid = index_cols if mode == 'function': valid += ['k'] if not all(x in df.columns for x in valid): raise ValueError("please specify a bestfit file with columns: %s" %(", ".join(valid))) df = df.set_index(valid) # get the key dictionary and print out what we are selecting key = dict((df.index.names[i], df.index.levels[i][v]) for i, v in enumerate(select)) msg = ", ".join("%s = %s" %(k,v) for k,v in key.items()) print("selecting " + msg) # select the bestfit select = tuple(df.index.levels[i][v] for i, v in enumerate(select)) # load the GP if mode == 'gp': gp = GPModelParams(kwargs['gp_file']) args = tuple(df.loc[select, col] for col in kwargs['interp_cols']) bestfits = gp.to_dict(*args) print("gp bestfit values:\n-------------") print("\n".join("%s = %s" %(k,str(v)) for k,v in bestfits.items())) actual = {k:df.loc[select, k] for k in kwargs['model'].param_names} print("actual bestfit values:\n-------------") print("\n".join("%s = %s" %(k,str(v)) for k,v in actual.items())) # load the spline table elif mode == 'spline': table = SplineTableModelParams(kwargs['spline_file']) s8_z = df.loc[select, 's8_z'] b1 = df.loc[select, 'b1'] bestfits = table(s8_z, b1) print("spline table bestfit values:\n-------------") print("\n".join("%s = %s" %(k,str(v)) for k,v in bestfits.items())) actual = {k:df.loc[select, k] for k in kwargs['model'].param_names} print("actual bestfit values:\n-------------") print("\n".join("%s = %s" %(k,str(v)) for k,v in actual.items())) elif mode == 'params': bestfits = {k:df.loc[select, k] for k in kwargs['model'].param_names} if mode == 'params' or mode == 'gp' or mode == 'spline': kwargs['data'].select = key # this should hopefully only loop over one thing for key, extra, data_df in kwargs['data']: # plot the data pfy.errorbar(data_df.index.values, data_df['y'], data_df['error']) # plot the bestfit parameters x = data_df.index.values y = kwargs['model'](x, **dict(bestfits, **extra)) lines = pfy.plot(x, y) else: # mode is `function` df = df.xs(select) # select the data data_df = kwargs['data'].to_dataframe(key) # plot the data pfy.errorbar(data_df.index.values, data_df['y'], data_df['error']) # plot the bestfit function mean x = df.index.values y = df['mean'] errs = df['error'] lines = pfy.plot(x, y) pfy.plt.fill(np.concatenate([x, x[::-1]]), np.concatenate([y - errs, (y + errs)[::-1]]), alpha=.5, fc=lines[0].get_color(), ec='None') ax = pfy.gca() ax.title.update('Bestfit (%s) comparison for %s' %(mode,msg), fontsize=16) ax.xlabel.update(r"$k$ ($h$/Mpc)", fontsize=16) ax.ylabel.update(kwargs['data'].variable_str, fontsize=16) pfy.show()