-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit_orbit.py
336 lines (285 loc) · 9.29 KB
/
fit_orbit.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import patches
import matplotlib.transforms as transforms
from astropy.time import Time
from orbitize import system, read_input, priors, sampler
from orbitize import results
"""
Fits to run:
1. lit astrometry only (True, False, False, False, False)
2. lit astrometry + first GRAVITY epoch (True, True, False, False, False)
2.5. lit astrometry + second GRAVITY epoch (True, False, True, False, False)
3. lit astrometry + first 2 GRAVITY (True, True, True, False, False)
4. first 2 GRAVITY only (False, True, True, False, False)
5. first 2 GRAVITY with e fixed to 0 (True, True, True, True, False)
6. first 2 GRAVITY with linearly decreasing prior on e (True, True, True, False, True)
7. all astrometry
8. all astrometry with e fixed to 0
9. all astrometry with linearly decreasing e prior
10. all astrometry except NACO, linearly decreasing e prior
"""
np.random.seed(10)
"""
Begin keywords <<
"""
run_fit = False
make_corner = True
lit_astrom = True
first_grav = True
second_grav = True
third_grav = True
exclude_naco = False
fix_ecc = False
lin_ecc_prior = False
savedir = "results/"
if not os.path.exists(savedir):
os.mkdir(savedir)
if lit_astrom:
savedir += "with_literature_astrom"
if first_grav:
savedir += "with_first_vlti_point"
if second_grav:
savedir += "with_second_vlti_point"
if third_grav:
savedir += "with_third_vlti_point"
if fix_ecc:
savedir += "_fixed_ecc"
if lin_ecc_prior:
savedir += "_linear_ecc"
if exclude_naco:
savedir += "_noNACO"
"""
>> End keywords
"""
if not os.path.exists(savedir):
os.mkdir(savedir)
input_file = "HIP65426.csv"
plx = 9.303088053872793 # [mas] (Gaia eDR3)
plx_err = 0.034559656
m_st = 1.96 # [M_sol] (Bowler+ 2020)
mass_err = 0.04
num_secondary_bodies = 1
data_table = read_input.read_file(input_file)
insts = data_table["instrument"]
if not lit_astrom:
data_table = data_table[data_table["instrument"] == "GRAVITY"]
if not first_grav:
data_table = data_table[0 : -2 & -2 :]
if not second_grav:
data_table = data_table[0 : -1 & -1]
if not third_grav:
data_table = data_table[0:-1]
if exclude_naco:
data_table = data_table[data_table["instrument"] != "NACO"]
print(data_table)
HIP654_system = system.System(
num_secondary_bodies,
data_table,
m_st,
plx,
fit_secondary_mass=False,
mass_err=mass_err,
plx_err=plx_err,
)
# fix eccentricity to 0
if fix_ecc:
HIP654_system.sys_priors[HIP654_system.param_idx["ecc1"]] = 0
# set a linearly decreasing prior on ecc
if lin_ecc_prior:
HIP654_system.sys_priors[HIP654_system.param_idx["ecc1"]] = priors.LinearPrior(
-2.18, 2.01
)
# Check that orbitizie! initialized everything correctly.
# (I wrote the code, therefore I do not trust the code.)
assert not HIP654_system.fit_secondary_mass
assert not HIP654_system.track_planet_perturbs
# run MCMC
num_threads = 50
num_temps = 20
num_walkers = 1000
num_steps = 50_000_000 # 200_000_000 # n_walkers x n_steps_per_walker
burn_steps = 10_000 # 100_000
thin = 100
if run_fit:
HIP654_sampler = sampler.MCMC(
HIP654_system,
num_threads=num_threads,
num_temps=num_temps,
num_walkers=num_walkers,
)
HIP654_sampler.run_sampler(num_steps, burn_steps=burn_steps, thin=thin)
# save chains
HIP654_sampler.results.save_results("{}/chains.hdf5".format(savedir))
HIP654_results = results.Results() # create blank results object for loading
HIP654_results.load_results("{}/chains.hdf5".format(savedir))
# chop chains
# num_chop = 1000
# reshaped_post = HIP654_results.post.reshape(
# (num_walkers, num_steps // num_walkers // thin, HIP654_results.post.shape[1])
# )
# HIP654_results.post = reshaped_post[:, -num_chop:, :].reshape(
# (-1, HIP654_results.post.shape[1])
# )
# make corner plot
if make_corner:
if fix_ecc:
param_list = ["sma1", "inc1", "aop1", "pan1", "tau1", "mtot", "plx"]
else:
param_list = None
corner_kwargs = {"show_titles": True} # , "quantiles": [0.16, 0.84]}
# median_values = np.median(HIP654_results.post, axis=0)
# range_values = np.ones_like(median_values)*0.997 # Plot only 3-sigma range for each parameter
fig = HIP654_results.plot_corner(
param_list=param_list, range=[(40, 120), 1, 1, 1, 1, 1, 1, 1], **corner_kwargs
)
for ax in fig.get_axes():
ax.tick_params(axis="both", labelsize=14)
ax.set_xlabel(ax.get_xlabel(), fontsize=14)
ax.set_ylabel(ax.get_ylabel(), fontsize=14)
ax.set_title(ax.get_title(), fontsize=14)
plt.savefig("{}/corner.png".format(savedir), dpi=250)
# make orbit plot
fig = HIP654_results.plot_orbits(
num_epochs_to_plot=500, start_mjd=56000, plot_astrometry=False
)
radec_ax, sep_ax, pa_ax, cbar_ax = fig.axes
sep, serr, pa, paerr = (
data_table["quant1"],
data_table["quant1_err"],
data_table["quant2"],
data_table["quant2_err"],
)
epoch = Time(data_table["epoch"], format="mjd").decimalyear
sphere_mask = np.where(insts == "SPHERE")[0]
naco_mask = np.where(insts == "NACO")[0]
grav_mask = np.where(insts == "GRAVITY")[0]
gravity_ra, gravity_dec = sep[grav_mask], pa[grav_mask]
sphere_ra, sphere_dec = system.seppa2radec(sep[sphere_mask], pa[sphere_mask])
naco_ra, naco_dec = system.seppa2radec(sep[naco_mask], pa[naco_mask])
radec_ax.scatter(sphere_ra, sphere_dec, marker="o", color="hotpink", zorder=20, s=3)
radec_ax.scatter(naco_ra, naco_dec, marker="o", color="hotpink", zorder=20, s=3)
radec_ax.scatter(gravity_ra, gravity_dec, marker="o", color="hotpink", zorder=20, s=3)
gravity_sep, gravity_pa = system.radec2seppa(sep[grav_mask], pa[grav_mask])
gravity_raerr, gravity_decerr, gravity_corr = (
data_table["quant1_err"][grav_mask],
data_table["quant2_err"][grav_mask],
data_table["quant12_corr"][grav_mask],
)
sep_ax.errorbar(
epoch[sphere_mask],
sep[sphere_mask],
serr[sphere_mask],
marker="^",
color="purple",
markeredgecolor="purple",
markerfacecolor="white",
ls="",
label="SPHERE",
)
sep_ax.errorbar(
epoch[naco_mask],
sep[naco_mask],
serr[naco_mask],
marker="s",
ls="",
color="purple",
label="NACO",
)
sep_ax.scatter(
epoch[grav_mask], gravity_sep, label="GRAVITY", zorder=20, color="hotpink"
)
sep_ax.legend()
pa_ax.errorbar(
epoch[sphere_mask],
pa[sphere_mask],
paerr[sphere_mask],
marker="^",
color="purple",
markeredgecolor="purple",
markerfacecolor="white",
ls="",
)
pa_ax.errorbar(
epoch[naco_mask], pa[naco_mask], paerr[naco_mask], marker="s", ls="", color="purple"
)
pa_ax.scatter(epoch[grav_mask], gravity_pa, zorder=20, color="hotpink")
l, b, w, h = cbar_ax.get_position().bounds
# cbar_ax.set_position([l - 0.05, b, w, h])
cbar_ax.set_position([l - 0.17, b, w, h])
l, b, w, h = pa_ax.get_position().bounds
def confidence_ellipse(
x, y, corr, x_unc, y_unc, ax, n_std=3.0, facecolor="hotpink", alpha=1
):
"""
Create a plot of the covariance confidence ellipse of *x* and *y*.
(shamelessly stolen from matplotlib docs)
"""
# Using a special case to obtain the eigenvalues of this
# two-dimensional dataset.
ell_radius_x = np.sqrt(1 + corr)
ell_radius_y = np.sqrt(1 - corr)
ellipse = patches.Ellipse(
(0, 0),
width=ell_radius_x * 2,
height=ell_radius_y * 2,
facecolor=facecolor,
alpha=alpha,
zorder=20,
)
# Calculating the standard deviation of x from
# the squareroot of the variance and multiplying
# with the given number of standard deviations.
scale_x = x_unc * n_std
mean_x = x
# calculating the standard deviation of y ...
scale_y = y_unc * n_std
mean_y = y
transf = (
transforms.Affine2D()
.rotate_deg(45)
.scale(scale_x, scale_y)
.translate(mean_x, mean_y)
)
ellipse.set_transform(transf + ax.transData)
return ax.add_patch(ellipse)
fig.subplots_adjust(right=0.6)
radec_ax.set_position([0.05, b, w - 0.03, 0.77])
pa_ax.set_position([l - 0.23, b, w - 0.06, h])
sep_ax.set_position([l - 0.23, b + 0.42, w - 0.06, h])
for i in np.arange(len(grav_mask)):
if i > 1:
grav_ax = fig.add_axes([0.82, b, 0.1, h])
else:
grav_ax = fig.add_axes([0.68, b + 0.42 * i, 0.1, h])
for n_std in [1, 2]:
ellipse = confidence_ellipse(
gravity_ra[i],
gravity_dec[i],
gravity_corr[i],
gravity_raerr[i],
gravity_decerr[i],
grav_ax,
n_std=n_std,
alpha=1 - n_std / 4,
)
grav_ax.set_xlim(gravity_ra[i] + 0.3, gravity_ra[i] - 0.3)
grav_ax.set_ylim(gravity_dec[i] - 0.5, gravity_dec[i] + 0.5)
grav_ax.set_aspect("equal")
if i == 0 or i == 2:
grav_ax.set_xlabel("$\Delta$RA [mas]")
if i < 2:
grav_ax.set_ylabel("$\Delta$Dec [mas]")
for j in np.arange(len(sep_ax.lines) - 2):
orbittracks_sep = sep_ax.lines[j].get_ydata()
orbittracks_pa = pa_ax.lines[j].get_ydata()
ra2plot, dec2plot = system.seppa2radec(orbittracks_sep, orbittracks_pa)
grav_ax.plot(ra2plot, dec2plot, color="lightgrey")
grav_ax.text(
gravity_ra[i] + 0.27, gravity_dec[i] + 0.4, "Epoch {}".format(i + 1)
)
pa_ax.set_xlabel("Epoch [year]")
radec_ax.set_aspect("equal")
plt.savefig("{}/orbit.png".format(savedir), dpi=250)