-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_omegas.py
executable file
·283 lines (242 loc) · 8.26 KB
/
training_omegas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) Arianna I. Renzini 2024
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# This file is part of the stochmon package
import os
import sys
sys.path.append(os.getcwd())
from popstock.PopulationOmegaGW import PopulationOmegaGW
from gwpopulation.models.mass import SinglePeakSmoothedMassDistribution
from gwpopulation.models.redshift import MadauDickinsonRedshift
from gwpopulation.utils import xp
import argparse
import numpy as np
import bilby
import tqdm
import json
import os
from pathlib import Path
from bilby.core.prior import Interped
from bilby.core.utils import infer_args_from_function_except_n_args
"""
***
Use popstock to generate a training set of Omegas from a set of
hyper-parameters describing the redshift and mass distribution
of a population of black hole binaries.
popstock will calculate omega_GW once, and reweight the samples from
the fiducial distribution to the provided distributions.
***
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-ps",
"--parameter_set",
help="Parameter sets to generate training data for.",
action="store",
type=str,
default=10000,
)
parser.add_argument(
"-rd",
"--run_directory",
help="Run directory.",
action="store",
type=str,
default="./",
)
parser.add_argument(
"-sm", "--samples", help="Samples to use.", action="store", type=str, default=None
)
parser.add_argument(
"-hsm", "--hyper_samples", help="Values of the hyper-parameters to use.", action="store", type=str, default=None
)
parser.add_argument(
"-ns",
"--number_samples",
help="number of samples.",
action="store",
type=int,
default=None,
)
parser.add_argument(
"-t",
"--tag",
help="Tag to label the run.",
action="store",
type=str,
default="test",
)
parser.add_argument(
"-wf",
"--waveform_approximant",
help="Wavefrom approximant. Default is IMRPhenomD.",
action="store",
type=str,
default="IMRPhenomD",
)
args = parser.parse_args()
"""
Unpack arguments
---
Loading in parameters from parser; frequencies are fixed here
(these can be easily customised later on by passing in a range and spacing).
"""
N_proposal_samples = args.number_samples
wave_approx = args.waveform_approximant
rundir = Path(args.run_directory)
tag = args.tag
freqs = np.arange(10, 2000, 5)
"""
Create mass and redshift distributions
---
These are *fixed* in this script. To use custom distributions,
more development is required. Current distributions are imported
from the gwpopulation package (https://github.com/ColmTalbot/gwpopulation)
Mass distribution: Power-Law Plus Peak (PLPP)
(see App. B.2 of https://iopscience.iop.org/article/10.3847/2041-8213/abe949)
Redshift distribution: Madau-Dickinson Star Formation Rate (SFR)
(see https://arxiv.org/abs/1805.10270,
and https://arxiv.org/abs/2003.12152 for the normalisation)
"""
mass_obj = SinglePeakSmoothedMassDistribution()
redshift_obj = MadauDickinsonRedshift(z_max=10)
models = {
"mass_model": mass_obj,
"redshift_model": redshift_obj,
}
"""
Create the popstock object
---
To create the PopulationOmegaGW object, you just need the mass and
redshift models, and frequencies to use in the spectra calculations.
We can then either sample over the models, or use user-defined samples.
As sampling takes time (and is currently not optimized), it is recommended
to do it once and use a (healthy) set of samples for future calculations.
"""
newpop = PopulationOmegaGW(models=models, frequency_array=freqs)
if args.samples is not None:
with open(args.samples) as samples_file:
samples_dict = json.load(samples_file)
Lambda_0 = samples_dict["Lambda_0"]
samples_dict.pop("Lambda_0")
if args.number_samples is None:
args.number_samples = len(samples_dict["redshift"])
else:
for key in samples_dict.keys():
samples_dict[key] = samples_dict[key][: args.number_samples]
newpop.set_proposal_samples(proposal_samples=samples_dict)
print(f"Using {args.number_samples} samples...")
else:
# fiducial distribution Lambda_0, should be good to generate
# healthy sample sets.
Lambda_0 = {
"alpha": 2.5,
"beta": 1,
"delta_m": 3,
"lam": 0.04,
"mmax": 100,
"mmin": 4,
"mpp": 33,
"sigpp": 5,
"gamma": 2.7,
"kappa": 3,
"z_peak": 1.9,
"rate": 15,
}
newpop.draw_and_set_proposal_samples(
Lambda_0, N_proposal_samples=N_proposal_samples
)
"""
Calculate omega_GW once
---
Omega_GW is calculated once from the sample set provided. This
can take a while depending on the number of samples, but only needs
to be done once. using 10^6 samples takes about 40 minutes.
This first omega_GW is saved to file, together with the samples used
in case these weren't loaded from file.
"""
newpop.calculate_omega_gw(waveform_approximant=wave_approx, Lambda=Lambda_0)
if args.samples is not None:
np.savez(
f"{os.path.join(rundir, f'omegagw_0_{tag}.npz')}",
omega_gw=newpop.omega_gw,
freqs=newpop.frequency_array,
Lambda_0=Lambda_0,
)
else:
np.savez(
f"{os.path.join(rundir, f'omegagw_0_{tag}.npz')}",
omega_gw=newpop.omega_gw,
freqs=newpop.frequency_array,
fiducial_samples=newpop.proposal_samples,
Lambda_0=Lambda_0,
draw_dict=newpop.pdraws,
)
"""
Calculate omega_GW for a set of hyper-parameters (Lambda)
---
This is the main step to create the omega_GW training set.
The desired set of Lambdas should be passed in as a json file.
In case none is passed, this script will look for a posterior file
from the LVK O3 Populations paper (arXiv 2010.14533) lying around
on CIT. All samples provided will be used.
Result omega_GWs with their respective Lambdas are saved to a json file.
"""
new_omegas = {}
new_omegas["Lambdas"] = []
new_omegas["Neff"] = []
new_omegas["omega_gw"] = []
if args.hyper_samples is not None:
with open(args.hyper_samples) as samples_file:
lambda_samples = json.load(samples_file)
else:
try:
result = bilby.core.result.read_in_result(filename='/home/jacob.golomb/o3b-population-data/analyses/PowerLawPeak/o1o2o3_mass_c_iid_mag_iid_tilt_powerlaw_redshift_result.json')
lambda_samples = result.posterior.sample(10000).to_dict('list')
except IOError:
raise ValueError("No samples for Lambda hyper-parameters have been provided, nor a default found.")
N_trials = len(lambda_samples["alpha"])
print("Running trials...")
for idx in tqdm.tqdm(range(N_trials)):
Lambda_new = {
"alpha": lambda_samples["alpha"][idx],
"beta": lambda_samples["beta"][idx],
"delta_m": lambda_samples["delta_m"][idx],
"lam": lambda_samples["lam"][idx],
"mmax": lambda_samples["mmax"][idx],
"mmin": lambda_samples["mmin"][idx],
"mpp": lambda_samples["mpp"][idx],
"sigpp": lambda_samples["sigpp"][idx],
"rate": lambda_samples["rate"][idx],
"gamma": lambda_samples['lamb'][idx],
"kappa": 3.83, #make this madau-dickinson; right now these two fixed parameters are fixed but we can pass in other ones!
"z_peak": 2.0,
}
new_omegas["Lambdas"].append(Lambda_new)
newpop.calculate_omega_gw(sampling_frequency=2048, Lambda=Lambda_new)
new_omegas["Neff"].append(
float((xp.sum(newpop.weights) ** 2) / (xp.sum(newpop.weights ** 2)))
)
new_omegas["omega_gw"].append(newpop.omega_gw.tolist())
new_omegas["freqs"] = newpop.frequency_array.tolist()
omegas_dict = json.dumps(new_omegas)
f = open(f"{os.path.join(rundir, f'new_omegas_{tag}.json')}", "w")
f.write(omegas_dict)
f.close()
print("Done!")
exit()