-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_apogee.py
358 lines (328 loc) · 12.8 KB
/
utils_apogee.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
from astropy.stats import mad_std as mad
from astroNN.config import MAGIC_NUMBER
def target_name_conversion(targetname):
"""
NAME:
target_name_conversion
PURPOSE:
to convert targetname to string used to plot graph
INPUT:
targetname (string)
OUTPUT:
converted name (string)
HISTORY:
2017-Nov-25 - Written - Henry Leung (University of Toronto)
"""
targetname.replace("/H]", "")
targetname.replace("/Fe]", "")
targetname.replace("[", "")
if targetname == "C1":
fullname = "CI"
elif len(targetname) < 3:
fullname = f"[{targetname}/H]"
elif targetname == "teff":
fullname = r"$T_{\mathrm{eff}}$"
elif targetname == "alpha":
fullname = "[Alpha/M]"
elif targetname == "logg":
fullname = "[Log(g)]"
elif targetname == "Ti2":
fullname = "[TiII/H]"
else:
fullname = targetname
return fullname
def aspcap_windows_url_correction(targetname):
"""
NAME:
target_name_conversion
PURPOSE:
to convert targetname to string used to get ASPCAP windows url
INPUT:
targetname (string)
OUTPUT:
converted name (string)
HISTORY:
2017-Nov-25 - Written - Henry Leung (University of Toronto)
"""
targetname.replace("/H]", "")
targetname.replace("/Fe]", "")
targetname.replace("[", "")
if targetname == "C1":
fullname = "CI"
elif len(targetname) < 3:
fullname = f"{targetname}"
elif targetname == "teff":
fullname = "SurfaceTemperature"
elif targetname == "alpha":
fullname = "[Alpha/M]"
elif targetname == "logg":
fullname = "[Log(g)]"
elif targetname == "Ti2":
fullname = "TiII"
else:
fullname = targetname
return fullname
class ASPCAP_plots:
def aspcap_residue_plot(
self, test_predictions, test_labels, test_pred_error=None, test_labels_err=None
):
"""
NAME:
aspcap_residue_plot
PURPOSE:
plot aspcap residue
INPUT:
test_predictions (ndarray): Test result from neural network
test_labels (ndarray): Gound truth for tests result
test_pred_error (ndarray): (Optional) 1-sigma error for tests result from Baysian neural network.
test_labels_err (ndarray): (Optional) Ground truth for tests result
OUTPUT:
None, just plots to be saved
HISTORY:
2018-Jan-28 - Written - Henry Leung (University of Toronto)
"""
import pylab as plt
import numpy as np
print("Start plotting residues")
resid = test_predictions - test_labels
# Some plotting variables for asthetics
plt.rcParams["axes.facecolor"] = "white"
plt.rcParams["axes.grid"] = True
plt.rcParams["grid.color"] = "gray"
plt.rcParams["grid.alpha"] = "0.4"
x_lab = "ASPCAP"
y_lab = "astroNN"
fullname = self.targetname
aspcap_residue_path = os.path.join(self.fullfilepath, "ASPCAP_residue")
if not os.path.exists(aspcap_residue_path):
os.makedirs(aspcap_residue_path)
mad_labels = np.zeros(test_labels.shape[1])
for i in range(test_labels.shape[1]):
not9999_index = np.where(test_labels[:, i] != MAGIC_NUMBER)
mad_labels[i] = mad((test_labels[:, i])[not9999_index], axis=0)
if test_pred_error is None:
# To deal with prediction from non-Bayesian Neural Network
test_pred_error = np.zeros(test_predictions.shape)
for i in range(self._labels_shape["output"]):
plt.figure(figsize=(15, 11), dpi=200)
plt.axhline(0, ls="--", c="k", lw=2)
not9999 = np.where(test_labels[:, i] != -9999.0)[0]
plt.errorbar(
(test_labels[:, i])[not9999],
(resid[:, i])[not9999],
yerr=(test_pred_error[:, i])[not9999],
markersize=2,
fmt="o",
ecolor="g",
capthick=2,
elinewidth=0.5,
)
plt.xlabel("ASPCAP " + target_name_conversion(fullname[i]), fontsize=25)
plt.ylabel(
r"$\Delta$ "
+ target_name_conversion(fullname[i])
+ "\n("
+ y_lab
+ " - "
+ x_lab
+ ")",
fontsize=25,
)
plt.tick_params(labelsize=20, width=1, length=10)
if self._labels_shape["output"] == 1:
plt.xlim(
[
np.min((test_labels[:])[not9999]),
np.max((test_labels[:])[not9999]),
]
)
else:
plt.xlim(
[
np.min((test_labels[:, i])[not9999]),
np.max((test_labels[:, i])[not9999]),
]
)
ranges = (
np.max((test_labels[:, i])[not9999])
- np.min((test_labels[:, i])[not9999])
) / 2
plt.ylim([-ranges, ranges])
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2)
bias = np.median((resid[:, i])[not9999], axis=0)
scatter = mad((resid[:, i])[not9999], axis=0)
plt.figtext(
0.6,
0.75,
r"$\widetilde{m}$="
+ "{0:.3f}".format(bias)
+ r" $\widetilde{s}$="
+ "{0:.3f}".format(scatter / float(mad_labels[i]))
+ " s="
+ "{0:.3f}".format(scatter),
size=25,
bbox=bbox_props,
)
plt.tight_layout()
plt.savefig(aspcap_residue_path + f"/{fullname[i]}_test.png")
plt.close("all")
plt.clf()
if test_labels_err is not None:
for i in range(self._labels_shape["output"]):
plt.figure(figsize=(15, 11), dpi=200)
plt.axhline(0, ls="--", c="k", lw=2)
not9999 = np.where(test_labels[:, i] != -9999.0)[0]
plt.scatter(
(test_labels_err[:, i])[not9999], (resid[:, i])[not9999], s=0.7
)
plt.xlabel(
r"ASPCAP Error of " + target_name_conversion(fullname[i]),
fontsize=25,
)
plt.ylabel(
r"$\Delta$ "
+ target_name_conversion(fullname[i])
+ "\n("
+ y_lab
+ " - "
+ x_lab
+ ")",
fontsize=25,
)
plt.tick_params(labelsize=20, width=1, length=10)
if self._labels_shape["output"] == 1:
plt.xlim(
[
np.percentile((test_labels_err[:])[not9999], 5),
np.percentile((test_labels_err[:])[not9999], 95),
]
)
else:
plt.xlim(
[
np.min((test_labels_err[:, i])[not9999]),
np.percentile((test_labels_err[:, i])[not9999], 90),
]
)
ranges = np.percentile((resid[:, i])[not9999], 5) - np.percentile(
(resid[:, i])[not9999], 95
)
plt.ylim([-ranges, ranges])
plt.tight_layout()
plt.savefig(aspcap_residue_path + f"/{fullname[i]}_test_err.png")
plt.close("all")
plt.clf()
print("Finished plotting residues")
def jacobian_aspcap(self, jacobian=None, dr=14):
"""
NAME: cal_jacobian
PURPOSE: calculate jacobian
INPUT:
OUTPUT:
HISTORY:
2017-Nov-20 Henry Leung
"""
import pylab as plt
import numpy as np
import matplotlib.ticker as ticker
from astroNN.apogee.chips import wavelength_solution, chips_split
from urllib.request import urlopen
from urllib.error import HTTPError
import pandas as pd
if jacobian is None:
raise ValueError("Please provide jacobian to plot")
if len(jacobian.shape) == 3:
jacobian = np.mean(jacobian, axis=-1)
elif len(jacobian.shape) == 2:
pass
else:
raise ValueError("Unknown jacobian shape!!")
# Some plotting variables for asthetics
plt.rcParams["axes.facecolor"] = "white"
plt.rcParams["axes.grid"] = False
plt.rcParams["grid.color"] = "gray"
plt.rcParams["grid.alpha"] = "0.4"
path = os.path.join(self.fullfilepath, "jacobian")
if not os.path.exists(path):
os.makedirs(path)
fullname = self.targetname
lambda_blue, lambda_green, lambda_red = wavelength_solution(dr=dr)
for j in range(self._labels_shape["output"]):
fig = plt.figure(figsize=(45, 30), dpi=150)
scale = np.max(np.abs((jacobian[j, :])))
scale_2 = np.min((jacobian[j, :]))
blue, green, red = chips_split(jacobian[j, :], dr=dr)
blue, green, red = blue[0], green[0], red[0]
ax1 = fig.add_subplot(311)
fig.suptitle(f"{fullname[j]}", fontsize=50)
ax1.set_ylabel(
r"$\partial$" + fullname[j] + "/" + r"$\partial\lambda$", fontsize=40
)
ax1.set_ylim(scale_2, scale)
ax1.plot(lambda_blue, blue, linewidth=0.9, label="astroNN")
ax2 = fig.add_subplot(312)
ax2.set_ylabel(
r"$\partial$" + fullname[j] + "/" + r"$\partial\lambda$", fontsize=40
)
ax2.set_ylim(scale_2, scale)
ax2.plot(lambda_green, green, linewidth=0.9, label="astroNN")
ax3 = fig.add_subplot(313)
ax3.set_ylim(scale_2, scale)
ax3.set_ylabel(
r"$\partial$" + fullname[j] + "/" + r"$\partial\lambda$", fontsize=40
)
ax3.plot(lambda_red, red, linewidth=0.9, label="astroNN")
ax3.set_xlabel(r"Wavelength $\lambda$ (Angstrom)", fontsize=40)
ax1.axhline(0, ls="--", c="k", lw=2)
ax2.axhline(0, ls="--", c="k", lw=2)
ax3.axhline(0, ls="--", c="k", lw=2)
try:
if dr == 14:
url = (
f"https://svn.sdss.org/public/repo/apogee/idlwrap/trunk/lib/l31c/"
f"{aspcap_windows_url_correction(self.targetname[j])}.mask"
)
df = np.array(pd.read_csv(urlopen(url), header=None, sep="\t"))
else:
raise ValueError("Only support DR14")
aspcap_windows = df * scale
aspcap_windows = aspcap_windows.T # Fix the shape to the one I expect
aspcap_blue, aspcap_green, aspcap_red = chips_split(
aspcap_windows, dr=dr
)
print(
f"Found {aspcap_windows_url_correction(self.targetname[j])} ASPCAP window at: {url}"
)
ax1.plot(
lambda_blue, aspcap_blue[0], linewidth=0.9, label="ASPCAP windows"
)
ax2.plot(
lambda_green, aspcap_green[0], linewidth=0.9, label="ASPCAP windows"
)
ax3.plot(
lambda_red, aspcap_red[0], linewidth=0.9, label="ASPCAP windows"
)
except HTTPError:
print(
f"No ASPCAP window data for {aspcap_windows_url_correction(self.targetname[j])}"
)
tick_spacing = 50
ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax2.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing / 1.5))
ax3.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing / 1.7))
ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax1.tick_params(labelsize=30, width=2, length=20, which="major")
ax1.tick_params(width=2, length=10, which="minor")
ax2.tick_params(labelsize=30, width=2, length=20, which="major")
ax2.tick_params(width=2, length=10, which="minor")
ax3.tick_params(labelsize=30, width=2, length=20, which="major")
ax3.tick_params(width=2, length=10, which="minor")
ax1.legend(loc="best", fontsize=40)
plt.tight_layout()
plt.subplots_adjust(left=0.05)
plt.savefig(path + f"/{self.targetname[j]}_jacobian.png")
plt.close("all")
plt.clf()