-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_models.py
More file actions
447 lines (316 loc) · 17 KB
/
plot_models.py
File metadata and controls
447 lines (316 loc) · 17 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import sys
import cycler
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pyLIMA.fits.objective_functions
import pygtc
from bokeh.layouts import gridplot
from bokeh.models import Arrow, OpenHead
from bokeh.models import BasicTickFormatter
from bokeh.plotting import figure
from matplotlib.ticker import MaxNLocator
from pyLIMA.astrometry import astrometric_positions
from pyLIMA.parallax import parallax
from pyLIMA.toolbox import fake_telescopes, plots
plot_lightcurve_windows = 0.2
plot_residuals_windows = 0.21
MAX_PLOT_TICKS = 2
MARKER_SYMBOLS = np.array(
[['o', '.', '*', 'v', '^', '<', '>', 's', 'p', 'd', 'x'] * 10])
# this is a pointer to the module object instance itself.
thismodule = sys.modules[__name__]
thismodule.list_of_fake_telescopes = []
thismodule.saved_model = None
def create_telescopes_to_plot_model(microlensing_model, pyLIMA_parameters):
if microlensing_model == thismodule.saved_model:
list_of_fake_telescopes = thismodule.list_of_fake_telescopes
else:
list_of_fake_telescopes = []
if len(list_of_fake_telescopes) == 0:
# Photometry first
Earth = True
for tel in microlensing_model.event.telescopes:
if tel.lightcurve_flux is not None:
if tel.location == 'Space':
model_time = np.arange(
np.min(tel.lightcurve_magnitude['time'].value),
np.max(tel.lightcurve_magnitude['time'].value),
0.1).round(2)
model_time = np.r_[
model_time, tel.lightcurve_magnitude['time'].value]
model_time.sort()
if Earth and tel.location == 'Earth':
model_time1 = np.arange(np.min((np.min(
tel.lightcurve_magnitude['time'].value),
pyLIMA_parameters.t0 - 5 *
pyLIMA_parameters.tE)),
np.max((np.max(
tel.lightcurve_magnitude['time'].value),
pyLIMA_parameters.t0 + 5 *
pyLIMA_parameters.tE)),
1).round(2)
model_time2 = np.arange(
pyLIMA_parameters.t0 - 1 * pyLIMA_parameters.tE,
pyLIMA_parameters.t0 + 1 * pyLIMA_parameters.tE,
1).round(2)
model_time = np.r_[model_time1, model_time2]
for telescope in microlensing_model.event.telescopes:
if telescope.location == 'Earth':
model_time = np.r_[
model_time, telescope.lightcurve_magnitude[
'time'].value]
symmetric = 2 * pyLIMA_parameters.t0 - \
telescope.lightcurve_magnitude['time'].value
model_time = np.r_[model_time, symmetric]
model_time.sort()
if (tel.location == 'Space') | (Earth and tel.location == 'Earth'):
model_time = np.unique(model_time)
model_lightcurve = np.c_[
model_time, [0] * len(model_time), [0.1] * len(model_time)]
model_telescope = fake_telescopes.create_a_fake_telescope(
light_curve=model_lightcurve)
model_telescope.name = tel.name
model_telescope.filter = tel.filter
model_telescope.location = tel.location
model_telescope.ld_gamma = tel.ld_gamma
model_telescope.ld_sigma = tel.ld_sigma
model_telescope.ld_a1 = tel.ld_a1
model_telescope.ld_a2 = tel.ld_a2
model_telescope.location = tel.location
if tel.location == 'Space':
model_telescope.spacecraft_name = tel.spacecraft_name
model_telescope.spacecraft_positions = tel.spacecraft_positions
if microlensing_model.parallax_model[0] != 'None':
model_telescope.initialize_positions()
model_telescope.compute_parallax(
microlensing_model.parallax_model,
microlensing_model.event.North
,
microlensing_model.event.East) # ,
# microlensing_model.event.ra/180*np.pi)
list_of_fake_telescopes.append(model_telescope)
if tel.location == 'Earth' and Earth:
Earth = False
# Astrometry
for tel in microlensing_model.event.telescopes:
if tel.astrometry is not None:
if tel.location == 'Space':
model_time = np.arange(np.min(tel.astrometry['time'].value),
np.max(tel.astrometry['time'].value),
0.1).round(2)
else:
model_time1 = np.arange(
np.min((np.min(tel.lightcurve_magnitude['time'].value),
pyLIMA_parameters.t0 - 5 * pyLIMA_parameters.tE)),
np.max((np.max(tel.lightcurve_magnitude['time'].value),
pyLIMA_parameters.t0 + 5 * pyLIMA_parameters.tE)),
1).round(2)
model_time2 = np.arange(
pyLIMA_parameters.t0 - 1 * pyLIMA_parameters.tE,
pyLIMA_parameters.t0 + 1 * pyLIMA_parameters.tE,
0.1).round(2)
model_time = np.r_[model_time1, model_time2]
model_time = np.r_[model_time, telescope.astrometry['time'].value]
symmetric = 2 * pyLIMA_parameters.t0 - telescope.astrometry[
'time'].value
model_time = np.r_[model_time, symmetric]
model_time.sort()
model_time = np.unique(model_time)
model_astrometry = np.c_[
model_time, [0] * len(model_time), [0.1] * len(model_time), [
0] * len(model_time), [0.1] * len(model_time)]
model_telescope = fake_telescopes.create_a_fake_telescope(
astrometry_curve=model_astrometry,
astrometry_unit=tel.astrometry['ra'].unit)
model_telescope.name = tel.name
model_telescope.filter = tel.filter
model_telescope.location = tel.location
model_telescope.ld_gamma = tel.ld_gamma
model_telescope.ld_sigma = tel.ld_sigma
model_telescope.ld_a1 = tel.ld_a1
model_telescope.ld_a2 = tel.ld_a2
model_telescope.pixel_scale = tel.pixel_scale
if tel.location == 'Space':
model_telescope.spacecraft_name = tel.spacecraft_name
model_telescope.spacecraft_positions = tel.spacecraft_positions
if microlensing_model.parallax_model[0] != 'None':
model_telescope.initialize_positions()
model_telescope.compute_parallax(microlensing_model.parallax_model,
microlensing_model.event.North
,
microlensing_model.event.East) # ,
# microlensing_model.event.ra / 180)# * np.pi)
list_of_fake_telescopes.append(model_telescope)
thismodule.saved_model = microlensing_model
thismodule.list_of_fake_telescopes = list_of_fake_telescopes
return list_of_fake_telescopes
def plot_light_curve_magnitude(time, mag, mag_error=None, figure_axe=None, color=None,
linestyle='-', marker=None, name=None):
"""
Plot a lightcurve in magnitude
Parameters
----------
time : array, the time to plot
mag : array, the magnitude to plot
mag_error : array, the magnitude error
figure_axe : matplotlib.axe, an axe to plot
color : str, a color string
linestyle : str, the matplotlib linestyle desired
marker : str, the matplotlib marker
name : str, the points name
"""
if figure_axe:
pass
else:
figure, figure_axe = plt.subplots()
if mag_error is None:
figure_axe.plot(time, mag, c=color, label=name, linestyle=linestyle,lw=3)
else:
figure_axe.errorbar(time, mag, mag_error, color=color, marker=marker,
label=name, linestyle='')
def plot_LCmodel(microlensing_model, model_parameters, bokeh_plot=None):
# Change matplotlib default colors
n_telescopes = len(microlensing_model.event.telescopes)
# color = plt.cm.jet(
# np.linspace(0.01, 0.99, n_telescopes)) # This returns RGBA; convert:
# # hexcolor = map(lambda rgb: '#%02x%02x%02x' % (rgb[0] * 255, rgb[1] * 255,
# # rgb[2] * 255),
# # tuple(color[:, 0:-1]))
# hexcolor = ['#' + format(int(i[0] * 255), 'x').zfill(2) + format(int(i[1] * 255),
# 'x').zfill(2) +
# format(int(i[2] * 255), 'x').zfill(2) for i in color]
# matplotlib.rcParams['axes.prop_cycle'] = cycler.cycler(color=hexcolor)
mat_figure, mat_figure_axes = initialize_light_curves_plot(
event_name=microlensing_model.event.name)
bokeh_lightcurves = None
bokeh_residuals = None
if len(model_parameters) != len(microlensing_model.model_dictionnary):
telescopes_fluxes = microlensing_model.find_telescopes_fluxes(model_parameters)
telescopes_fluxes = [getattr(telescopes_fluxes, key) for key in
telescopes_fluxes._fields]
model_parameters = np.r_[model_parameters, telescopes_fluxes]
plot_aligned_data(mat_figure_axes, microlensing_model, model_parameters,
plot_unit='Mag',
bokeh_plot=bokeh_lightcurves)
plot_photometric_models(mat_figure_axes, microlensing_model, model_parameters,
plot_unit='Mag',
bokeh_plot=bokeh_lightcurves)
mat_figure_axes.invert_yaxis()
mat_figure_axes.legend(shadow=True, fontsize='large',
bbox_to_anchor=(0, 1.02, 1, 0.2),
loc="lower left",
mode="expand", borderaxespad=0, ncol=3)
try:
bokeh_lightcurves.legend.click_policy = "mute"
# legend = bokeh_lightcurves.legend[0]
except AttributeError:
pass
figure_bokeh = gridplot([[bokeh_lightcurves], [bokeh_residuals]],
toolbar_location=None)
return mat_figure, figure_bokeh
def initialize_light_curves_plot(plot_unit='Mag', event_name='A microlensing event'):
fig_size = [10, 10]
mat_figure, mat_figure_axes = plt.subplots(1, 1, figsize=(fig_size[0], fig_size[1]), dpi=75)
plt.subplots_adjust(top=0.8, bottom=0.15, left=0.2, right=0.9, wspace=0.1,
hspace=0.1)
mat_figure_axes.grid()
mat_figure_axes.set_ylabel(r'$' + plot_unit + '$',
fontsize=5 * fig_size[1] * 3 / 4.0)
mat_figure_axes.yaxis.set_major_locator(MaxNLocator(4))
mat_figure_axes.tick_params(axis='y', labelsize=3.5 * fig_size[1] * 3 / 4.0)
mat_figure_axes.tick_params(axis='x', labelsize=3.5 * fig_size[1] * 3 / 4.0)
mat_figure_axes.text(0.01, 0.96, 'provided by pyLIMA', style='italic',
fontsize=10,
transform=mat_figure_axes.transAxes)
mat_figure_axes.set_xlabel(r'$JD$', fontsize=20)
return mat_figure, mat_figure_axes
def plot_photometric_models(figure_axe, microlensing_model, model_parameters,
bokeh_plot=None, plot_unit='Mag'):
pyLIMA_parameters = microlensing_model.compute_pyLIMA_parameters(model_parameters)
list_of_telescopes = create_telescopes_to_plot_model(microlensing_model,
pyLIMA_parameters)
telescopes_names = np.array([i.name for i in microlensing_model.event.telescopes])
# plot models
index = 0
for tel in list_of_telescopes:
if tel.lightcurve_flux is not None:
magni = microlensing_model.model_magnification(tel, pyLIMA_parameters)
microlensing_model.derive_telescope_flux(tel, pyLIMA_parameters, magni)
f_source = getattr(pyLIMA_parameters, 'fsource_' + tel.name)
f_blend = getattr(pyLIMA_parameters, 'fblend_' + tel.name)
if index == 0:
ref_source = f_source
ref_blend = f_blend
index += 1
magnitude = pyLIMA.toolbox.brightness_transformation.ZERO_POINT - 2.5 * \
np.log10(ref_source * magni + ref_blend)
name = tel.name
index_color = np.where(name == telescopes_names)[0][0]
color = plt.rcParams["axes.prop_cycle"].by_key()["color"][index_color]
if tel.location == 'Earth':
name = tel.location
linestyle = '-'
else:
linestyle = '--'
plot_light_curve_magnitude(tel.lightcurve_magnitude['time'].value,
magnitude, figure_axe=figure_axe,
name=name, color='darkblue',
linestyle=linestyle)
def plot_aligned_data(figure_axe, microlensing_model, model_parameters, bokeh_plot=None,
plot_unit='Mag'):
pyLIMA_parameters = microlensing_model.compute_pyLIMA_parameters(model_parameters)
# plot aligned data
index = 0
list_of_telescopes = create_telescopes_to_plot_model(microlensing_model,
pyLIMA_parameters)
ref_names = []
ref_locations = []
ref_magnification = []
ref_fluxes = []
for ref_tel in list_of_telescopes:
model_magnification = microlensing_model.model_magnification(ref_tel,
pyLIMA_parameters)
microlensing_model.derive_telescope_flux(ref_tel, pyLIMA_parameters,
model_magnification)
f_source = getattr(pyLIMA_parameters, 'fsource_' + ref_tel.name)
f_blend = getattr(pyLIMA_parameters, 'fblend_' + ref_tel.name)
# model_magnification = (model['photometry']-f_blend)/f_source
ref_names.append(ref_tel.name)
ref_locations.append(ref_tel.location)
ref_magnification.append(model_magnification)
ref_fluxes.append([f_source, f_blend])
for ind, tel in enumerate(microlensing_model.event.telescopes):
if tel.lightcurve_flux is not None:
if tel.location == 'Earth':
ref_index = np.where(np.array(ref_locations) == 'Earth')[0][0]
else:
ref_index = np.where(np.array(ref_names) == tel.name)[0][0]
residus_in_mag = \
pyLIMA.fits.objective_functions.photometric_residuals_in_magnitude(
tel, microlensing_model,
pyLIMA_parameters)
if ind == 0:
reference_source = ref_fluxes[ind][0]
reference_blend = ref_fluxes[ind][1]
index += 1
# time_mask = [False for i in range(len(ref_magnification[ref_index]))]
time_mask = []
for time in tel.lightcurve_flux['time'].value:
time_index = np.where(list_of_telescopes[ref_index].lightcurve_flux[
'time'].value == time)[0][0]
time_mask.append(time_index)
# model_flux = ref_fluxes[ref_index][0] * ref_magnification[ref_index][
# time_mask] + ref_fluxes[ref_index][1]
model_flux = reference_source * ref_magnification[ref_index][
time_mask] + reference_blend
magnitude = pyLIMA.toolbox.brightness_transformation.ZERO_POINT - 2.5 * \
np.log10(model_flux)
color = {'F146':'b', 'LSST_u':'c', 'LSST_g':'g', 'LSST_r':'y', 'LSST_i':'r', 'LSST_z':'m', 'LSST_y':'k'}
# color = plt.rcParams["axes.prop_cycle"].by_key()["color"][ind]
marker = 'o'
plot_light_curve_magnitude(tel.lightcurve_magnitude['time'].value,
magnitude + residus_in_mag,
tel.lightcurve_magnitude['err_mag'].value,
figure_axe=figure_axe, color=color[tel.name],
marker=marker, name=tel.name)