def generate_params(source, batches, run, mc_source, mc_version, free_params=('m_gr', 'd_b', 'xi_ratio')): """""" synth_grid = grid_analyser.Kgrid(source, use_sub_cols=True) mv = mcmc_versions.McmcVersion(mc_source, version=mc_version) n_epochs = len(batches) pkeys = mv.param_keys params = dict.fromkeys(pkeys) # ===== Pull model params from kepler grid ===== for key in mv.interp_keys: key = mv.param_aliases.get(key, key) if key in mv.epoch_unique: for i in range(n_epochs): grid_params = synth_grid.get_params(batches[i], run) e_key = f'{key}{i+1}' grid_key = {'mdot': 'accrate'}.get(key, key) params[e_key] = float(grid_params[grid_key]) else: grid_key = {'m_nw': 'mass'}.get(key, key) grid_params = synth_grid.get_params(batches[0], run) params[key] = float(grid_params[grid_key]) # ===== Randomly generate free params ===== for key in free_params: params[key] = mcmc_tools.get_random_params(key, n_models=1, mv=mv)[0] return params
def setup_table(kgrid, batches, synth_source, mc_source, mc_version, synth_version, params=('x', 'z', 'accrate', 'qb', 'mass'), summ_list=('rate', 'dt', 'fluence', 'peak'), free_params=('redshift', 'd_b', 'xi_ratio'), observables=('rate', 'peak', 'fluence', 'fper'), update=False): """Sets up table of synthetic data, including input/output values parameters ---------- kgrid : Kgrid object batches : list list of batches, each corresponding to an epoch. Assumes the runs in each batch correspond to each other. synth_source : str mc_source : str mc_version : int synth_version : int params : sequence(str) parameters to extract from kgrid and add to the table summ_list : sequence(str) burst properties to extract from kgrid and add to the table free_params : sequence(str) free parameters to randomly choose observables : sequence(str) names of observables to calculate from burst properties update : bool whether to simply update an existing synth table, e.g. if the models just need updating. Keeps the previously randomised params """ mcv = mcmc_versions.McmcVersion(source=mc_source, version=mc_version) sub = grid_tools.reduce_table(kgrid.params, params={'batch': batches[0]}) groups = np.array(sub['run']) table = pd.DataFrame() # ===== For each group of epochs, setup sub-table of inputs/outputs ===== for group in groups: group_table = initialise_group_table(group, batches) set_param_cols(group_table, batches=batches, kgrid=kgrid, params=params) set_summ_cols(group_table, batches=batches, kgrid=kgrid, summ_list=summ_list) if update: inherit_free_params(group_table, free_params=free_params, parent_group=group, parent_source=synth_source, parent_version=synth_version) else: set_rand_free_params(group_table, mcv=mcv, free_params=free_params) set_observables(group_table, observables=observables) table = pd.concat([table, group_table], ignore_index=True) save_table(table, source=synth_source, version=synth_version) return table
def setup_mcmc_sample(batch0, source, chain, n_models, n_epochs, ref_source, ref_mcmc_version, kgrid, constant=None, epoch_independent=('x', 'z', 'mass'), epoch_dependent=('accrate', 'qb'), discard=200, cap=None): """Creates batches of models, with random sample of params drawn from MCMC chain """ ref_mass = 1.4 aliases = {'mass': 'g', 'accrate': 'mdot'} if constant is None: constant = {'tshift': 0.0, 'acc_mult': 1.0, 'qnuc': 5.0, 'qb_delay': 0.0, 'accmass': 1e16, 'accdepth': 1e19} mv = mcmc_versions.McmcVersion(source=ref_source, version=ref_mcmc_version) params_full = {} param_sample, idxs = mcmc_tools.get_random_sample(chain, n=n_models, discard=discard, cap=cap) batch1 = batch0 + n_epochs - 1 save_sample_array(param_sample, source=source, batch0=batch0, batch1=batch1) idx_string = get_index_str(idxs, discard=discard, cap=cap) # ===== fill model params ===== for key in epoch_independent: mv_key = aliases.get(key, key) params_full[key] = get_mcmc_params(mv_key, param_sample=param_sample, mv=mv) # ===== fill constant params ===== for key, val in constant.items(): params_full[key] = np.full(n_models, val) params_full['mass'] *= ref_mass for i in range(n_epochs): for key in epoch_dependent: mv_key = aliases.get(key, key) mv_key = f'{mv_key}{i+1}' params_full[key] = get_mcmc_params(mv_key, param_sample=param_sample, mv=mv) create_batch(batch0+i, dv={}, params={}, source=source, nbursts=30, kgrid=kgrid, qos='normal', walltime=96, setup_test=False, nsdump=500, nuc_heat=True, predict_qnuc=False, grid_version=0, substrate_off=True, ibdatov=1, params_full=params_full, notes=idx_string)
def random_models(batch0, source, n_models, n_epochs, ref_source, kgrid, ref_mcmc_version, constant=None, epoch_independent=('x', 'z', 'mass'), epoch_dependent=('accrate', 'qb'), epoch_chosen=None): """Creates random sample of model parameters """ ref_mass = 1.4 aliases = {'mass': 'g', 'accrate': 'mdot'} if constant is None: constant = {'tshift': 0.0, 'acc_mult': 1.0, 'qnuc': 5.0, 'qb_delay': 0.0, 'accmass': 1e16, 'accdepth': 1e19} if epoch_chosen is None: epoch_chosen = {} mv = mcmc_versions.McmcVersion(source=ref_source, version=ref_mcmc_version) params_full = {} # ===== fill model params ===== for key in epoch_independent: mv_key = aliases.get(key, key) params_full[key] = mcmc_tools.get_random_params(mv_key, n_models=n_models, mv=mv) # ===== fill constant params ===== for key, val in constant.items(): params_full[key] = np.full(n_models, val) params_full['mass'] *= ref_mass for i in range(n_epochs): for key in epoch_dependent: if key in epoch_chosen: val = epoch_chosen[key][i] params_full[key] = np.full(n_models, val) else: mv_key = aliases.get(key, key) mv_key = f'{mv_key}{i+1}' params_full[key] = mcmc_tools.get_random_params(mv_key, n_models=n_models, mv=mv) create_batch(batch0+i, dv={}, params={}, source=source, nbursts=30, kgrid=kgrid, qos='normal', walltime=96, setup_test=False, nsdump=500, nuc_heat=True, predict_qnuc=False, grid_version=0, substrate_off=True, ibdatov=1, params_full=params_full)
def plot_truth(plot_type, source, mc_version, discard, chain=None, n_walkers=None, n_steps=None, save=False, display=True): """Plots results of MCMC against true values of synthetic data """ mcv = mcmc_versions.McmcVersion(source, mc_version) chain = check_chain(chain, n_walkers=n_walkers, n_steps=n_steps, source=source, version=mc_version) truth = synth.get_true_values(source, version=mcv.synth_version, group=mcv.synth_group) if plot_type == 'posteriors': mcmc_plot.plot_posteriors(chain, source=source, version=mc_version, discard=discard, truth_values=truth, save=save, display=display) elif plot_type == 'contours': mcmc_plot.plot_contours(chain, discard=discard, source=source, truth=True, version=mc_version, truth_values=truth, save=save, display=display) else: raise ValueError('plot_type must be one of: (posteriors, corner)')
def setup_synth_table(source, batches, run, mc_source, mc_version, free_params=('m_gr', 'd_b', 'xi_ratio'), params=None, u_fedd_frac=0.08, u_fper_frac=0.01): """""" if params is None: params = generate_params(source, batches=batches, run=run, mc_source=mc_source, mc_version=mc_version, free_params=free_params) bfit = burstfit.BurstFit(mc_source, version=mc_version, debug=False, u_fper_frac=u_fper_frac, u_fedd_frac=u_fedd_frac) mv = mcmc_versions.McmcVersion(mc_source, version=mc_version) bprops = bfit.bprop_sample(x=None, params=params) table = pd.DataFrame() pd.set_option("display.precision", 5) for i, key in enumerate(mv.bprops): bp_i = 2 * i u_i = bp_i + 1 u_key = f'u_{key}' table[key] = bprops[:, bp_i] table[u_key] = bprops[:, u_i] return table
def setup_mcmc_sample(batch0, sample_source, chain, discard, n_models_epoch, n_epochs, ref_source, ref_mcmc_version, kgrid, nbursts, constant=None, epoch_independent=('x', 'z', 'mass'), walltime=96, epoch_dependent=('accrate', 'qb'), cap=None, scratch_file_sys=False): """Creates batches of models, with random sample of params drawn from MCMC chain """ aliases = {'mass': 'm_nw', 'accrate': 'mdot'} # TODO: - use config defaults for constants # - account for different variable params (he2, grid5 with x,z) if constant is None: # TODO: hack fix for he2/grid5 different params if ref_source == 'he2': constant = { 'tshift': 0.0, 'acc_mult': 1.0, 'qnuc': 5.0, 'qb_delay': 0.0, 'accmass': 1e16, 'accdepth': 1e20, 'x': 0.0, 'z': 0.015, 'mass': 1.4 } elif ref_source == 'grid5': constant = { 'tshift': 0.0, 'acc_mult': 1.0, 'qnuc': 5.0, 'qb_delay': 0.0, 'accmass': 1e16, 'accdepth': 1e20 } mv = mcmc_versions.McmcVersion(source=ref_source, version=ref_mcmc_version) params_full = {} param_sample, idxs = mcmc_tools.get_random_sample(chain, n=n_models_epoch, discard=discard, cap=cap) batch1 = batch0 + n_epochs - 1 save_sample_array(param_sample, source=sample_source, batch0=batch0, batch1=batch1) idx_string = get_index_str(idxs, discard=discard, cap=cap) # ===== fill model params ===== for key in epoch_independent: mv_key = aliases.get(key, key) params_full[key] = get_mcmc_params(mv_key, param_sample=param_sample, mv=mv) # ===== fill constant params ===== for key, val in constant.items(): params_full[key] = np.full(n_models_epoch, val) for i in range(n_epochs): for key in epoch_dependent: mv_key = aliases.get(key, key) mv_key = f'{mv_key}{i+1}' params_full[key] = get_mcmc_params(mv_key, param_sample=param_sample, mv=mv) create_batch(batch0 + i, dv={}, params={}, source=sample_source, nbursts=nbursts, kgrid=kgrid, walltime=walltime, setup_test=False, nuc_heat=True, auto_qnuc=False, substrate_off=True, params_full=params_full, notes=idx_string, scratch_file_sys=scratch_file_sys)