-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_nltm_v1.py
317 lines (284 loc) · 9.58 KB
/
example_nltm_v1.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import numpy as np
import glob, os, sys, pickle, json, inspect
from collections import OrderedDict
current_path = os.getcwd()
splt_path = current_path.split("/")
top_path_idx = splt_path.index("nanograv")
# top_path_idx = splt_path.index("akaiser")
# top_path_idx = splt_path.index("ark0015")
top_dir = "/".join(splt_path[0 : top_path_idx + 1])
e_e_path = top_dir + "/enterprise_extensions/"
e_path = top_dir + "/enterprise/"
# ptmcmc_path = top_dir + "/PTMCMCSampler"
sys.path.insert(0, e_e_path)
# sys.path.insert(0, ptmcmc_path)
sys.path.insert(0, e_path)
from PTMCMCSampler.PTMCMCSampler import PTSampler as ptmcmc
import enterprise
from enterprise.pulsar import Pulsar
from enterprise.signals import utils
from enterprise.signals import parameter
from enterprise.signals import white_signals
from enterprise.signals import signal_base
from enterprise.signals import selections
from enterprise.signals import gp_signals
import enterprise_extensions as e_e
from enterprise_extensions import sampler
from enterprise_extensions import models
from enterprise_extensions.sampler import JumpProposal
from enterprise_extensions.timing import timing_block
from enterprise_extensions.blocks import channelized_backends
import argparse
def add_bool_arg(parser, name, help, default):
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument("--" + name, dest=name, action="store_true", help=help)
group.add_argument("--no-" + name, dest=name, action="store_false", help=help)
parser.set_defaults(**{name: default})
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"--psr_name", required=True, type=str, help="name of pulsar used for search"
)
parser.add_argument("--datarelease", required=True, help="What dataset to use")
parser.add_argument("--run_num", required=True, help="Label at end of output file.")
parser.add_argument(
"--tm_prior",
choices=["uniform", "bounded"],
default="uniform",
help="Use either uniform or bounded for ephemeris modeling? (DEFAULT: uniform)",
)
parser.add_argument(
"--ephem", default="DE436", help="Ephemeris option (DEFAULT: DE436)"
)
add_bool_arg(parser, "white_var", "Vary the white noise? (DEFAULT: TRUE)", True)
add_bool_arg(parser, "red_var", "Vary the red noise? (DEFAULT: TRUE)", True)
add_bool_arg(parser, "resume", "Whether to resume the chains. (DEFAULT: FALSE", False)
add_bool_arg(
parser,
"fit_remaining_pars",
"Whether to use non-linear plus linear timing model variations. (DEFAULT: True)",
True,
)
add_bool_arg(
parser,
"fixed_remaining_pars",
"Whether to use non-linear plus fixed timing model parameters. (DEFAULT: FALSE)",
False,
)
add_bool_arg(
parser,
"writeHotChains",
"Whether to write out the parallel tempering chains. (DEFAULT: TRUE)",
True,
)
add_bool_arg(
parser,
"reallyHotChain",
"Whether to include a really hot chain in the parallel tempering runs. (DEFAULT: FALSE)",
False,
)
parser.add_argument("--N", default=int(1e6), help="Number of samples (DEFAULT: 1e6)")
add_bool_arg(
parser,
"lin_dmx_jump_fd",
"Whether to use linear timing for DMX, JUMP, and FD parameters. (DEFAULT: FALSE)",
False,
)
add_bool_arg(
parser,
"sample_cos",
"Whether to sample inclination in COSI, if false, samples in SINI. (DEFAULT: True)",
True,
)
add_bool_arg(
parser,
"zero_start",
"Whether to start the timing parameters at the parfile value. (DEFAULT: TRUE",
True,
)
parser.add_argument(
"--parfile", default="", help="Location of parfile </PATH/TO/FILE/PARFILE.par>"
)
parser.add_argument(
"--timfile", default="", help="Location of timfile </PATH/TO/FILE/TIMFILE.tim>"
)
parser.add_argument(
"--timing_package",
default="tempo2",
help="Whether to use PINT or Tempo2 (DEFAULT: tempo2)",
)
args = parser.parse_args()
if not isinstance(args.N, int):
N = int(float(args.N))
else:
N = args.N
if len(args.parfile):
parfile = args.parfile
if not os.path.isfile(parfile):
raise ValueError(f"{parfile} does not exist. Please pick a real parfile.")
if len(args.timfile):
timfile = args.timfile
if not os.path.isfile(timfile):
raise ValueError(f"{timfile} does not exist. Please pick a real timfile.")
# Outdir name assignment
if args.fit_remaining_pars:
outdir = (
current_path
+ "/{}/chains/{}/".format(args.psr_name, args.datarelease)
+ args.psr_name
+ "_{}_{}_nltm_ltm_{}".format(
"_".join(args.tm_prior.split("-")), args.ephem, args.run_num
)
)
else:
outdir = (
current_path
+ "/{}/chains/{}/".format(args.psr_name, args.datarelease)
+ args.psr_name
+ "_{}_{}_tm_{}".format(
"_".join(args.tm_prior.split("-")), args.ephem, args.run_num
)
)
if not os.path.isdir(outdir):
os.makedirs(outdir, exist_ok=True)
else:
if not args.resume:
print("nothing!")
# raise ValueError("{} already exists!".format(outdir))
# filter
is_psr = False
if args.psr_name in parfile:
if args.timing_package.lower() == "tempo2":
psr = Pulsar(
parfile,
timfile,
ephem=args.ephem,
clk=None,
drop_t2pulsar=False,
timing_package="tempo2",
)
elif args.timing_package.lower() == "pint":
psr = Pulsar(
parfile,
timfile,
ephem=args.ephem,
clk=None,
drop_pintpsr=False,
timing_package="pint",
)
is_psr = True
if not is_psr:
raise ValueError(
"{} does not exist in {} datarelease.".format(args.psr_name, args.datarelease)
)
nltm_params = []
ltm_list = []
fixed_list = []
tm_param_dict = {}
for par in psr.fitpars:
if par == "Offset":
ltm_list.append(par)
elif "DMX" in par and any([args.lin_dmx_jump_fd, args.fixed_remaining_pars]):
if args.fixed_remaining_pars:
fixed_list.append(par)
else:
ltm_list.append(par)
elif "JUMP" in par and any([args.lin_dmx_jump_fd, args.fixed_remaining_pars]):
if args.fixed_remaining_pars:
fixed_list.append(par)
else:
ltm_list.append(par)
elif "FD" in par and any([args.lin_dmx_jump_fd, args.fixed_remaining_pars]):
if args.fixed_remaining_pars:
fixed_list.append(par)
else:
ltm_list.append(par)
elif par == "SINI" and args.sample_cos:
nltm_params.append("COSI")
else:
nltm_params.append(par)
# Need to convert for correct units in tempo2
if par in ["PBDOT", "XDOT"] and hasattr(psr, "t2pulsar"):
par_val = np.double(psr.t2pulsar.vals()[psr.t2pulsar.pars().index(par)])
par_sigma = np.double(psr.t2pulsar.errs()[psr.t2pulsar.pars().index(par)])
if np.log10(par_sigma) > -10.0:
print(f"USING PHYSICAL {par}. Val: ", par_val, "Err: ", par_sigma * 1e-12)
lower = par_val - 50 * par_sigma * 1e-12
upper = par_val + 50 * par_sigma * 1e-12
# lower = pbdot - 5 * pbdot_sigma * 1e-12
# upper = pbdot + 5 * pbdot_sigma * 1e-12
tm_param_dict[par] = {
"prior_mu": par_val,
"prior_sigma": par_sigma * 1e-12,
"prior_lower_bound": lower,
"prior_upper_bound": upper,
}
# Helpful print statements
print(
"Non-linearly varying these values: ",
nltm_params,
"\n in pulsar ",
args.psr_name,
)
if args.fit_remaining_pars:
print("Linearly varying these values: ", ltm_list)
if args.fixed_remaining_pars:
print("Fixing these parameters: ", fixed_list)
print("Using ", args.tm_prior, " prior.")
"""full nltm"""
model_args = inspect.getfullargspec(models.model_singlepsr_noise)
model_keys = model_args[0][1:]
model_vals = model_args[3]
model_kwargs = dict(zip(model_keys, model_vals))
model_kwargs.update(
{
"tm_var": args.tm_var,
"tm_linear": False,
"tm_param_list": nltm_params,
"ltm_list": ltm_list,
"tm_param_dict": tm_param_dict,
"tm_prior": args.tm_prior,
"normalize_prior_bound": 50.0,
"fit_remaining_pars": args.fit_remaining_pars,
"red_var": args.red_var,
"noisedict": None,
"white_vary": args.white_var,
}
)
pta = models.model_singlepsr_noise(psr, **model_kwargs)
psampler = sampler.setup_sampler(
pta, outdir=outdir, resume=args.resume, timing=True, psr=psr
)
with open(outdir + "/orig_timing_pars.pkl", "wb") as fout:
pickle.dump(psr.tm_params_orig, fout)
# I highly recommend starting the timing parameters at the parfile values (i.e. zero_start = True, the default)
if args.zero_start:
x0_list = []
for p in pta.params:
if "timing" in p.name:
if "DMX" in p.name:
p_name = ("_").join(p.name.split("_")[-2:])
else:
p_name = p.name.split("_")[-1]
if psr.tm_params_orig[p_name][-1] == "normalized":
x0_list.append(np.double(0.0))
else:
if p_name in tm_param_dict.keys():
x0_list.append(np.double(tm_param_dict[p_name]["prior_mu"]))
else:
x0_list.append(np.double(psr.tm_params_orig[p_name][0]))
else:
x0_list.append(p.sample())
x0 = np.asarray(x0_list)
else:
x0 = np.hstack([p.sample() for p in pta.params])
# In the nltm branch I have I add the different SCAM, AM, and DE jumps in sampler.py
# It should work either way
psampler.sample(
x0,
N,
SCAMweight=0,
AMweight=0,
DEweight=0,
writeHotChains=args.writeHotChains,
hotChain=args.reallyHotChain,
)