def test_bilby_to_lalinference(self): mass_1 = [1, 20] mass_2 = [1, 20] chirp_mass = [1, 5] mass_ratio = [0.125, 1] bilby_prior = BBHPriorDict(dictionary=dict( chirp_mass=Uniform(name='chirp_mass', minimum=chirp_mass[0], maximum=chirp_mass[1]), mass_ratio=Uniform(name='mass_ratio', minimum=mass_ratio[0], maximum=mass_ratio[1]), mass_2=Constraint(name='mass_2', minimum=mass_1[0], maximum=mass_1[1]), mass_1=Constraint(name='mass_1', minimum=mass_2[0], maximum=mass_2[1]))) lalinf_prior = BBHPriorDict(dictionary=dict( mass_ratio=Constraint(name='mass_ratio', minimum=mass_ratio[0], maximum=mass_ratio[1]), chirp_mass=Constraint(name='chirp_mass', minimum=chirp_mass[0], maximum=chirp_mass[1]), mass_2=Uniform(name='mass_2', minimum=mass_1[0], maximum=mass_1[1]), mass_1=Uniform(name='mass_1', minimum=mass_2[0], maximum=mass_2[1]))) nsamples = 5000 bilby_samples = bilby_prior.sample(nsamples) bilby_samples, _ = conversion.convert_to_lal_binary_black_hole_parameters( bilby_samples) # Quicker way to generate LA prior samples (rather than specifying Constraint) lalinf_samples = [] while len(lalinf_samples) < nsamples: s = lalinf_prior.sample() if s["mass_1"] < s["mass_2"]: s["mass_1"], s["mass_2"] = s["mass_2"], s["mass_1"] if s["mass_2"] / s["mass_1"] > 0.125: lalinf_samples.append(s) lalinf_samples = pd.DataFrame(lalinf_samples) lalinf_samples["mass_ratio"] = lalinf_samples["mass_2"] / lalinf_samples["mass_1"] # Construct fake result object result = bilby.core.result.Result() result.search_parameter_keys = ["mass_ratio", "chirp_mass"] result.meta_data = dict() result.priors = bilby_prior result.posterior = pd.DataFrame(bilby_samples) result_converted = bilby.gw.prior.convert_to_flat_in_component_mass_prior(result) if "plot" in sys.argv: # Useful for debugging plt.hist(bilby_samples["mass_ratio"], bins=50, density=True, alpha=0.5) plt.hist(result_converted.posterior["mass_ratio"], bins=50, density=True, alpha=0.5) plt.hist(lalinf_samples["mass_ratio"], bins=50, alpha=0.5, density=True) plt.show() # Check that the non-reweighted posteriors fail a KS test ks = ks_2samp(bilby_samples["mass_ratio"], lalinf_samples["mass_ratio"]) print("Non-reweighted KS test = ", ks) self.assertFalse(ks.pvalue > 0.05) # Check that the non-reweighted posteriors pass a KS test ks = ks_2samp(result_converted.posterior["mass_ratio"], lalinf_samples["mass_ratio"]) print("Reweighted KS test = ", ks) self.assertTrue(ks.pvalue > 0.001)
def test_absolute_overlap(self): priors = BBHPriorDict(aligned_spin=True) del priors["mass_1"], priors["mass_2"] priors["total_mass"] = Uniform(5, 50) priors["mass_ratio"] = Uniform(0.5, 1) priors["geocent_time"] = Uniform(-10, 10) n_samples = 100 all_parameters = pd.DataFrame(priors.sample(n_samples)) overlaps = list() for ii in range(n_samples): parameters = dict(all_parameters.iloc[ii]) bilby_pols = self.bilby_wfg.frequency_domain_strain(parameters) gpu_pols = self.gpu_wfg.frequency_domain_strain(parameters) bilby_strain = self.ifo.get_detector_response( waveform_polarizations=bilby_pols, parameters=parameters) gpu_strain = self.ifo.get_detector_response( waveform_polarizations=gpu_pols, parameters=parameters) inner_product = noise_weighted_inner_product( aa=bilby_strain, bb=gpu_strain, power_spectral_density=self.ifo.power_spectral_density_array, duration=self.duration) overlap = (inner_product / self.ifo.optimal_snr_squared(signal=bilby_strain)**0.5 / self.ifo.optimal_snr_squared(signal=gpu_strain)**0.5) overlaps.append(overlap) self.assertTrue(min(np.abs(overlaps)) > 0.995)
class Samples: def __init__(self, prior_file, num_samples=1000): self.prior_filename = prior_file self.prior = BBHPriorDict(filename=prior_file) self.num_samples = num_samples self.samples = self.__generate_samples() def __generate_samples(self): samples = pd.DataFrame(self.prior.sample(size=self.num_samples)) samples = conversion.generate_all_bbh_parameters(samples) samples = conversion.generate_component_spins(samples) if 'chi_1' not in samples: samples['chi_1'] = samples['spin_1z'] samples['chi_2'] = samples['spin_2z'] samples[REMNANT_KICK] = None samples[REMNANT_MASS] = None samples[REMNANT_SPIN] = None return samples def add_remnant_data_to_samples(self): for idx, s in tqdm.tqdm(self.samples.iterrows(), total=self.num_samples, desc="Merging BH"): remnant = merge_bbh_pair( bh_1=BlackHole(mass=s.mass_1, spin=[s.spin_1x, s.spin_1y, s.spin_1z]), bh_2=BlackHole(mass=s.mass_2, spin=[s.spin_2x, s.spin_2y, s.spin_2z])) self.samples.at[idx, REMNANT_MASS] = remnant.mass self.samples.at[idx, REMNANT_SPIN] = remnant.spin_mag self.samples.at[idx, REMNANT_KICK] = remnant.kick_mag def plot_corner(self, filename): param_of_interest = [ "chi_1", "chi_2", "mass_ratio", "remnant_kick", "remnant_spin" ] if "chi_1_in_plane" in self.samples: param_of_interest += ["chi_1_in_plane", "chi_2_in_plane"] corner.corner(self.samples[param_of_interest], **CORNER_KWARGS) font = {'color': 'darkblue', 'weight': 'bold'} plt.suptitle(filename, fontdict=font, fontsize=40) plt.savefig(f"{filename}.png") plt.close() def save_samples(self): self.samples.to_csv(self.prior_filename.replace(".prior", ".csv"), index=False)