def __init__(self, source='grid4', source2='grid4', version=1, runs=(9, 10, 11), batches=(5, 5, 5), n_walkers=960, n_steps=1000): self.grid = grid_analyser.Kgrid(source2) self.bfit = burstfit.BurstFit(source, version, re_interp=False) self.runs = runs self.batches = batches self.best_params = mcmc_tools.get_max_lhood_params(source, version=version, n_walkers=n_walkers, n_steps=n_steps) red_idx = self.bfit.param_idxs['redshift'] fb_idx = self.bfit.param_idxs['f_b'] self.redshift = self.best_params[red_idx] self.f_b = self.best_params[fb_idx] self.n_epochs = len(runs) self.shifted_lc = {} self.interp_lc = {} self.t_shifts = None self.extract_lc() self.get_all_tshifts()
def resave_obs(source, obs_data=None): ref_source = {'gs1826': 'grid5', '4u1820': '4u1820'} epochs = {'gs1826': (1998, 2000, 2007), '4u1820': (1997, 2009)} col_order = [ 'epoch', 'dt', 'fluence', 'fper', 'cbol', 'peak', 'rate', 'u_dt', 'u_fluence', 'u_fper', 'u_cbol', 'u_peak', 'u_rate' ] if obs_data is None: bfit = burstfit.BurstFit(ref_source[source], version=0) obs_data = bfit.obs_data table = pd.DataFrame(obs_data) table['epoch'] = epochs[source] table = table[col_order] table_str = table.to_string(index=False, justify='left', formatters=formatters) path = os.path.join( f'/home/zacpetej/projects/codes/pyburst/files/obs_data/{source}') filename = f'{source}.dat' filepath = os.path.join(path, filename) with open(filepath, 'w') as f: f.write(table_str)
def __init__(self, source, mcmc_source, mcmc_version, batches, runs=None, verbose=True, n_bursts=1, fit_tail_only=False, n_points=None): self.source = source self.n_epochs = len(batches) self.obs_source = obs_sources[source] self.grid = grid_analyser.Kgrid(self.source) self.bfit = burstfit.BurstFit(mcmc_source, version=mcmc_version, re_interp=False) self.obs = ctools.load_obs(self.obs_source) self.batches = batches self.params = load_param_sample(self.source, self.batches) self.verbose = verbose self.n_bursts = n_bursts # no. bursts to get from each model self.xlims = { 'gs1826': (-10, 170), '4u1820': (-2, 27), }.get(self.obs_source) self.epochs = { 'gs1826': (1998, 2000, 2007), '4u1820': (1997, 2009), }.get(self.obs_source) if runs is None: # assume all batches have corresponding runs sub_batch = self.grid.get_params(self.batches[0]) self.runs = np.array(sub_batch['run']) else: self.runs = runs self.n_runs = len(self.runs) self.n_bursts_batch = self.n_runs * self.n_bursts self.n_points = n_points if self.n_points is None: self.n_points = {'gs1826': 200, '4u1820': 500}.get(self.obs_source) self.peak_i = np.zeros(self.n_epochs, dtype=int) if fit_tail_only: self.get_peak_indexes() self.loaded_lc = {} self.shifted_lc = {} self.interp_lc = {} self.t_shifts = None self.load_model_lc() self.extract_lc() self.interp_obs_lc() self.get_all_tshifts()
def compare_bprops(source, version, n_walkers, n_steps, epoch): """Plot model/obs comparison of single epoch, as one plot""" bfit = burstfit.BurstFit(source, version) max_p = mcmc_tools.get_max_lhood_params(source, version=version, n_walkers=n_walkers, n_steps=n_steps) interp_params = np.zeros(4) interp_params[0] = max_p[epoch-1] interp_params[1:] = max_p[epoch:epoch:3] interp = bfit.interpolate(interp_params)
def plot_interp_residuals(synth_source, batches, mc_source, mc_version, fontsize=16): """Plot synthetic burst properties against interpolated predictions to test accuracy of interpolator """ n_sigma = 1.96 bfit = burstfit.BurstFit(source=mc_source, version=mc_version) bprops = bfit.mcmc_version.bprops kgrid = grid_analyser.Kgrid(source=synth_source) param_table = kgrid.get_combined_params(batches) interp_table = extract_interp_table(param_table, bfit=bfit) summ_table = kgrid.get_combined_summ(batches) fig, ax = plt.subplots(len(bprops), figsize=(6, 8)) for i, bprop in enumerate(bprops): u_bprop = f'u_{bprop}' yscale = plot_tools.unit_scale(bprop) yunits = plot_tools.unit_label(bprop) model = np.array(summ_table[bprop]) / yscale interp = np.array(interp_table[bprop]) / yscale u_model = np.array(summ_table[u_bprop]) / yscale u_interp = np.array(interp_table[u_bprop]) / yscale residuals = interp - model u_residuals = n_sigma * np.sqrt(u_model**2 + u_interp**2) ax[i].errorbar(model, residuals, yerr=u_residuals, marker='o', ls='none', capsize=3) x_max = np.max(model) x_min = np.min(model) ax[i].plot([0.9 * x_min, 1.1 * x_max], [0, 0], ls='--', color='black') ax[i].set_xlabel(f'{bprop} ({yunits})', fontsize=fontsize) ax[1].set_ylabel(f'Interpolated - model', fontsize=fontsize) plt.tight_layout() plt.show(block=False)
def shift_model(model, params=duncan_params, source='grid5', version=1): """Returns model lightcurve shifted into observer frame (s, 1e-9 erg/cm^2/s) """ lc = np.zeros_like(model[:, :2]) bfit = burstfit.BurstFit(source, version=version) bfit_params = get_burstfit_params(params) lc[:, 0] = bfit.shift_to_observer(model[:, 0], bprop='dt', params=bfit_params) lc[:, 0] *= 3600 # bfit outputs in hrs lc[:, 0] -= params['t_offset'] * params['redshift'] lc[:, 1] = bfit.shift_to_observer(model[:, 1], bprop='peak', params=bfit_params) lc[:, 1] *= 1e9 # flux units return lc
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 __init__(self, source, mcmc_source, mcmc_version, batches, runs=None, verbose=True): self.source = source self.grid = grid_analyser.Kgrid(self.source) self.bfit = burstfit.BurstFit(mcmc_source, version=mcmc_version, re_interp=False) self.obs = ctools.load_obs('gs1826') self.batches = batches self.params = load_param_sample(self.source, self.batches) self.verbose = verbose if runs is None: sub_batch = self.grid.get_params(self.batches[0]) self.runs = np.array(sub_batch['run']) else: self.runs = runs self.n_epochs = len(batches) self.shifted_lc = {} self.interp_lc = {} self.t_shifts = None self.extract_lc() self.interp_obs_lc() self.get_all_tshifts()
def __init__(self, source, version, priors_only=True): self.bfit = burstfit.BurstFit(source, version, re_interp=True, priors_only=priors_only)