forked from igorandreoni/RubinToO2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_lc.py
342 lines (311 loc) · 15 KB
/
plot_lc.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
337
338
339
340
341
342
# author: Igor Andreoni <[email protected]>
import os
import glob
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from matplotlib.ticker import MaxNLocator
from astropy.cosmology import Planck18 as cosmo
import astropy.cosmology as ac
import astropy.units as u
def get_kne_filename(inj_params_list=None, datadir='models/'):
"""Given kilonova parameters, get the filename from the grid of models
developed by M. Bulla
Parameters
----------
inj_params_list : `list` [`dict`]
parameters for the kilonova model such as
mass of the dynamical ejecta (mej_dyn), mass of the disk wind ejecta
(mej_wind), semi opening angle of the cylindrically-symmetric ejecta
fan ('phi'), and viewing angle ('theta'). For example
inj_params_list = [{'mej_dyn': 0.005,
'mej_wind': 0.050,
'phi': 30,
'theta': 25.8}]
"""
# Get files, model grid developed by M. Bulla
file_list = glob.glob(os.path.join(datadir, "*.dat"))
# If no specific parameters passed - return everything.
if inj_params_list is None or len(inj_params_list) == 0:
return file_list
# Otherwise find the parameters for each file and
# then find the relevant matches.
params = {}
matched_files = []
for filename in file_list:
key = filename.replace(".dat", "").split("/")[-1]
params[key] = {}
params[key]["filename"] = filename
key_split = key.split("_")
# Binary neutron star merger models
# FIXME reinstate difference nsns and bns
# if key_split[0] == "nsns":
if True:
mejdyn = float(key_split[2].replace("mejdyn", ""))
mejwind = float(key_split[3].replace("mejwind", ""))
phi0 = float(key_split[4].replace("phi", ""))
theta = float(key_split[5])
dist_Mpc = float(key_split[6].replace("dMpc", ""))
params[key]["mej_dyn"] = mejdyn
params[key]["mej_wind"] = mejwind
params[key]["phi"] = phi0
params[key]["theta"] = theta
params[key]["dMpc"] = dist_Mpc
# Neutron star--black hole merger models
#elif key_split[0] == "nsbh":
# mej_dyn = float(key_split[2].replace("mejdyn", ""))
# mej_wind = float(key_split[3].replace("mejwind", ""))
# phi = float(key_split[4].replace("phi", ""))
# theta = float(key_split[5])
# params[key]["mej_dyn"] = mej_dyn
# params[key]["mej_wind"] = mej_wind
# params[key]["phi"] = phi
# params[key]["theta"] = theta
for key in params.keys():
for inj_params in inj_params_list:
match = all([np.isclose(params[key][var], inj_params[var])
for var in inj_params.keys()])
if match:
matched_files.append(params[key]["filename"])
print(f"Found match for {inj_params}")
print(
f"Found matches for {len(matched_files)}/{len(inj_params_list)} \
sets of parameters"
)
return matched_files
def getRawPotential(rate, area, time_window, maglim, M):
"""
How many kilonovae shall we expect given an intrinsic
BNS merger rate, an area, a time window, given our magnitude
limit and the peak magnitude of the kilonova?
Parameters
----------
rate float
in events/Gpc3/y
area float
in deg2
time_window float
in days
maglim float
magnitude limit (AB)
Magpeak float
peak absolute magnitude
Returns
-------
n float
number of expected events
"""
# Find the redshift
z = ac.z_at_value(cosmo.distmod, (maglim - M)*u.mag)
vol = cosmo.comoving_volume(z).to("Gpc3")
n = rate * (time_window / 365.) * vol.value * area / 41253
return n
def doPlotLc(strategies, t, delay_hr, xlim=[0, 7], ylim=[28, 18],
doShow=True, doSave=True, n_interp=120,
offset_filt_hr=0,
outfile_base="plot_lc", outfile_format='pdf',
event_name="merger",
filters_color_dict={'u': 'b', 'g': 'g', 'r': 'r', 'i': 'yellow',
'z': 'k', 'y': 'orange'},
linestyle="-"):
# Make sure the time starts from zero in the model
if t["t[days]"][0] == 0:
pass
else:
# Add one row at the top
t.add_row([0.] + [99. for x in np.arange(len(t.colnames) - 1)])
# Re-sort
t.sort("t[days]")
# For each strategy make a plot
strategy_names = list(strategies.keys())
for i in range(len(strategy_names)):
print(f"\n Strategy name: {strategy_names[i]}")
# Epochs of the strategy in days
days_strategy = np.array(strategies[strategy_names[i]]["cadence_hr"]) / 24
depths_strategy = strategies[strategy_names[i]]["depths"]
filters_strategy = strategies[strategy_names[i]]["filters"]
# Initialize detections and non-detections
detections = []
non_detections = []
# Initialize the possible time offset by filter
offset_filt_hr_tot = 0
# Initialize the figure
fig, ax = plt.subplots()
# Blank plot for the detections and non-detections
ax.plot([], [], label="Det.", color="k",
marker="o", linestyle="none", markersize=8)
ax.plot([], [], label="UL", color="k",
marker="v", linestyle="none", markersize=8)
for filt in t.colnames:
# Ignore anything not LSST
if filt[0] != "l":
continue
# Ignore time column
if filt == "t[days]":
continue
# Add the offset by filter
offset_filt_hr_tot += offset_filt_hr
# Interpolate to reduce noise
idx = [i for i in np.arange(len(t)) if np.isnan(t[filt][i]) == False]
idx = [i for i in idx if not t[filt][i] == np.inf]
xnew = np.linspace(t["t[days]"][idx].min(), t["t[days]"][idx].max(), n_interp)
f = interpolate.interp1d(t["t[days]"][idx], t[filt][idx])
label = f"{filt.replace('lsst', '')}"
# Plot the model
ax.plot(xnew, f(xnew), linestyle=linestyle, label=label,
color=filters_color_dict[filt.replace("lsst", "")])
# for each epoch, check if there is the given filter
for day_strategy, filter_strategy, depth_strategy_epoch in zip(days_strategy, filters_strategy, depths_strategy):
# Apply the delay between the event and the obs. window
day_strategy += delay_hr / 24 # from hours to days
# Apply a delay between filters to show overlapping points
day_strategy += offset_filt_hr_tot / 24
if not (filt.replace('lsst', '') in filter_strategy):
continue
else:
# Find depth for the given filter
idx = filter_strategy.index(filt.replace('lsst', ''))
depth = depth_strategy_epoch[idx]
# Detection or non-detection?
if f(day_strategy) <= depth:
detections.append((filt.replace('lsst', ''), day_strategy, f(day_strategy)))
# Plot the detection
ax.plot(day_strategy, f(day_strategy), linestyle="none",
marker="o", markersize=8,
color=filters_color_dict[filt.replace("lsst", "")])
else:
non_detections.append((filt.replace('lsst', ''), day_strategy, depth))
# Plot the non-detection
ax.plot(day_strategy, depth, linestyle="none",
marker="v", markersize=8,
color=filters_color_dict[filt.replace("lsst", "")])
# Add horizontal dashed lines for fiveSigmaDepth magnitudes for the lsstr, lssti, and lsstg bands
ax.axhline(y=24.5, color='r', linestyle='dotted', linewidth=2) # lsstr
ax.axhline(y=23, color='yellow', linestyle='dotted', linewidth=2) # lssti
ax.axhline(y=25, color='g', linestyle='dotted', linewidth=2) # lsstg
# Set plot parameters
plt.rcParams['xtick.labelsize'] = 20
ax.legend(fontsize=11, loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ax.set_xlabel(f"Time from {event_name} (days)", fontsize=13)
ax.set_ylabel("AB Magnitude", fontsize=13)
ax.set_title(strategy_names[i], fontsize=13)
ax.tick_params(axis='both', labelsize=13)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
# Save to file
if doSave is True:
# Generate a valid filename by replacing invalid characters such as spaces with underscores
out_filename = f"{outfile_base}_{strategy_names[i]}.{outfile_format}"
out_filename = out_filename.replace(" ", "_") # Replace spaces with underscores
out_filename = out_filename.replace("<", "lt") # Replace < with 'lt' or any other valid character
out_filename = out_filename.replace(">", "gt") # Replace > with 'gt' or any other valid character
# Add any additional filename sanitization steps if necessary
plt.savefig(out_filename, bbox_inches='tight')
if doShow is True:
plt.show()
def doPlotLc2(strategies, t, delay_hr, xlim=[0, 7], ylim=[28, 18],
doShow=True, doSave=True, n_interp=120,
offset_filt_hr=0,
outfile_base="plot_lc", outfile_format='pdf',
event_name="merger",
filters_color_dict={'u': 'b', 'g': 'g', 'r': 'r', 'i': 'yellow',
'z': 'k', 'y': 'orange'},
linestyle="-"):
# Make sure the time starts from zero in the model
if t["t[days]"][0] == 0:
pass
else:
# Add one row at the top
t.add_row([0.] + [99. for x in np.arange(len(t.colnames) - 1)])
# Re-sort
t.sort("t[days]")
# For each strategy make a plot
strategy_names = list(strategies.keys())
for i in range(len(strategy_names)):
print(f"\n Strategy name: {strategy_names[i]}")
# Epochs of the strategy in days
days_strategy = np.array(strategies[strategy_names[i]]["cadence_hr"]) / 24
depths_strategy = strategies[strategy_names[i]]["depths"]
filters_strategy = strategies[strategy_names[i]]["filters"]
# Initialize detections and non-detections
detections = []
non_detections = []
# Initialize the possible time offset by filter
offset_filt_hr_tot = 0
# Initialize the figure
fig, ax = plt.subplots()
# Blank plot for the detections and non-detections
ax.scatter([], [], label="Det.", color="k",
marker="o", s=100, edgecolor='black')
ax.scatter([], [], label="UL", color="k",
marker="v", s=100,edgecolor='blue')
for filt in t.colnames:
# Ignore anything not LSST
if filt[0] != "l":
continue
# Ignore time column
if filt == "t[days]":
continue
# Add the offset by filter
offset_filt_hr_tot += offset_filt_hr
# Interpolate to reduce noise
idx = [i for i in np.arange(len(t)) if np.isnan(t[filt][i]) == False]
idx = [i for i in idx if not t[filt][i] == np.inf]
xnew = np.linspace(t["t[days]"][idx].min(), t["t[days]"][idx].max(), n_interp)
f = interpolate.interp1d(t["t[days]"][idx], t[filt][idx])
label = f"{filt.replace('lsst', '')}"
# Plot the model
ax.plot(xnew, f(xnew), linestyle=linestyle, label=label,
color=filters_color_dict[filt.replace("lsst", "")])
# for each epoch, check if there is the given filter
for day_strategy, filter_strategy, depth_strategy_epoch in zip(days_strategy, filters_strategy, depths_strategy):
# Apply the delay between the event and the obs. window
day_strategy += delay_hr / 24 # from hours to days
# Apply a delay between filters to show overlapping points
day_strategy += offset_filt_hr_tot / 24
if not (filt.replace('lsst', '') in filter_strategy):
continue
else:
# Find depth for the given filter
idx = filter_strategy.index(filt.replace('lsst', ''))
depth = depth_strategy_epoch[idx]
# Detection or non-detection?
if f(day_strategy) <= depth:
detections.append((filt.replace('lsst', ''), day_strategy, f(day_strategy)))
# Plot the detection using scatter with black edge color
ax.scatter(day_strategy, f(day_strategy), marker="o", s=100, edgecolor='black',
color=filters_color_dict[filt.replace("lsst", "")])
else:
non_detections.append((filt.replace('lsst', ''), day_strategy, depth))
# Plot the non-detection using scatter with blue edge color
ax.scatter(day_strategy, depth, marker="v", s=100, edgecolor='blue',
color=filters_color_dict[filt.replace("lsst", "")])
# Add horizontal dashed lines for fiveSigmaDepth magnitudes for the lsstr, lssti, and lsstg bands
ax.axhline(y=24.5, color='r', linestyle='dotted', linewidth=2) # lsstr
ax.axhline(y=23, color='yellow', linestyle='dotted', linewidth=2) # lssti
ax.axhline(y=25, color='g', linestyle='dotted', linewidth=2) # lsstg
# Set plot parameters
plt.rcParams['xtick.labelsize'] = 20
ax.legend(fontsize=11, loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ax.set_xlabel(f"Time from {event_name} (days)", fontsize=13)
ax.set_ylabel("AB Magnitude", fontsize=13)
ax.set_title(strategy_names[i], fontsize=13,)
ax.tick_params(axis='both', labelsize=13)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
# Add the annotation "Bright KN" at the top-right corner
plt.text(4, 19, "Bright KN", fontsize=20,
horizontalalignment='right', verticalalignment='top')
# Save to file
if doSave is True:
# Generate a valid filename by replacing invalid characters such as spaces with underscores
out_filename = f"{outfile_base}_{strategy_names[i]}.{outfile_format}"
out_filename = out_filename.replace(" ", "_") # Replace spaces with underscores
out_filename = out_filename.replace("<", "lt") # Replace < with 'lt' or any other valid character
out_filename = out_filename.replace(">", "gt") # Replace > with 'gt' or any other valid character
# Add any additional filename sanitization steps if necessary
plt.savefig(out_filename, bbox_inches='tight')
if doShow is True:
plt.show()