-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNR_main.py
executable file
·287 lines (225 loc) · 10.1 KB
/
SNR_main.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
#USING FITRESULTS FROM FILE
#USING NUMPY
# 09-10-2017
# foliveira
import os
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, HoverTool, TapTool, OpenURL
from bokeh.models.widgets import Select, Slider
from bokeh.layouts import row, column, widgetbox, gridplot
from dashboard.bokeh.helper import get_data, get_exposure_ids, \
init_xy_plot, get_url_args, get_arms_and_spectrographs
import numpy as np
QLF_API_URL = os.environ.get(
'QLF_API_URL',
'http://localhost:8000/dashboard/api'
)
# Get url query args
args = get_url_args(curdoc)
selected_exposure = args['exposure']
selected_arm = args['arm']
selected_spectrograph = args['spectrograph']
data_model = {
'x': [],
'y': [],
'fiber_id': [],
'ra': [],
'dec': []
}
data_fit = {'xfit': [], 'yfit': []}
elg = ColumnDataSource(data=data_model.copy())
lrg = ColumnDataSource(data=data_model.copy())
qso = ColumnDataSource(data=data_model.copy())
star = ColumnDataSource(data=data_model.copy())
elg_fit = ColumnDataSource(data = data_fit.copy() )
lrg_fit = ColumnDataSource(data = data_fit.copy() )
qso_fit = ColumnDataSource(data = data_fit.copy() )
star_fit = ColumnDataSource(data = data_fit.copy() )
params = [
'ELG_SNR_MAG', 'ELG_FIBERID',
'LRG_SNR_MAG', 'LRG_FIBERID',
'QSO_SNR_MAG', 'QSO_FIBERID',
'STAR_SNR_MAG', 'STAR_FIBERID',
'RA', 'DEC'
]
params_fit = ['ELG_FITRESULTS', 'LRG_FITRESULTS', 'QSO_FITRESULTS', 'STAR_FITRESULTS']
def update(arm, spectrograph, exposure_id):
exp_zfill = str(exposure_id).zfill(8)
# get the data
qa_snr = 'ql-snr-{}-{}.yaml'.format(arm + spectrograph, exp_zfill)
data = get_data(qa_snr, params)
data2 = get_data(qa_snr, params_fit)
if not data2.empty:
a_elg, b_elg, c_elg = data2['ELG_FITRESULTS'][0]
a_lrg, b_lrg, c_lrg = data2['LRG_FITRESULTS'][0]
a_qso, b_qso, c_qso = data2['QSO_FITRESULTS'][0]
a_star, b_star, c_star = data2['STAR_FITRESULTS'][0]
if not data.empty:
# drop rows that have ELG_FIBERID null
elg_data = data[data.ELG_FIBERID.notnull()]
# create the bokeh column data sources
elg.data['x'] = elg_data.ELG_SNR_MAG[1]
elg.data['y'] = elg_data.ELG_SNR_MAG[0]
elg.data['fiber_id'] = elg_data.ELG_FIBERID.tolist()
elg.data['ra'] = elg_data.RA.tolist()
elg.data['dec'] = elg_data.DEC.tolist()
fitx = elg_data.ELG_SNR_MAG[1]
fitx = sorted(fitx)
fity = [ c_elg* x**2 + b_elg *x + a_elg for x in fitx] #PIPELINE FIT
a,b,c = np.polyfit(elg_data.ELG_SNR_MAG[1],elg_data.ELG_SNR_MAG[0],2) #numpy FIT
fity = [ a* x**2 + b*x + c for x in fitx] #numpy FIT
print( '\n\n\nFits\t\na: {}, b: {}, c: {}'.format(a_elg, b_elg, c_elg) )
print( 'Fits npy \na: {}, b: {}, c: {}'.format(c, b, a) )
#print('\n\n\nfitx\n', len(fitx),'\n\n', len(fity) )
elg_fit.data['xfit'] = fitx
elg_fit.data['yfit'] = fity
elg.stream(elg.data, 30)
# drop rows that have ELG_FIBERID null
lrg_data = data[data.LRG_FIBERID.notnull()]
lrg.data['x'] = lrg_data.LRG_SNR_MAG[1]
lrg.data['y'] = lrg_data.LRG_SNR_MAG[0]
lrg.data['fiber_id'] = lrg_data.LRG_FIBERID.dropna().tolist()
lrg.data['ra'] = lrg_data.RA.dropna().tolist()
lrg.data['dec'] = lrg_data.DEC.dropna().tolist()
fitx = lrg_data.LRG_SNR_MAG[1]
lrg_fit.data['xfit'] = sorted(fitx)
lrg_fit.data['yfit'] = [ c_lrg* x**2 + b_lrg *x + a_lrg for x in sorted(fitx)]
a,b,c = np.polyfit(lrg_data.LRG_SNR_MAG[1], lrg_data.LRG_SNR_MAG[0],2) #numpy FIT
lrg_fit.data['xfit'] = np.linspace(np.min(fitx), np.max(fitx), 200) #numpy FIT
lrg_fit.data['yfit'] = [ a* x**2 + b*x + c for x in lrg_fit.data['xfit']] #numpy FIT
lrg.stream(lrg.data, 30)
# drop rows that have QSO_FIBERID null
qso_data = data[data.QSO_FIBERID.notnull()]
qso.data['x'] = qso_data.QSO_SNR_MAG[1]
qso.data['y'] = qso_data.QSO_SNR_MAG[0]
qso.data['fiber_id'] = qso_data.QSO_FIBERID.dropna().tolist()
qso.data['ra'] = qso_data.RA.dropna().tolist()
qso.data['dec'] = qso_data.DEC.dropna().tolist()
fitx = qso_data.QSO_SNR_MAG[1]
qso_fit.data['xfit'] = sorted(fitx)
qso_fit.data['yfit'] = [ c_qso* x**2 + b_qso *x + a_qso for x in sorted(fitx)]
a,b,c = np.polyfit(qso_data.QSO_SNR_MAG[1], qso_data.QSO_SNR_MAG[0],2) #numpy FIT
qso_fit.data['xfit'] = np.linspace(np.min(fitx), np.max(fitx), 200) #numpy FIT
qso_fit.data['yfit'] = [ a* x**2 + b*x + c for x in qso_fit.data['xfit']] #numpy FIT
qso.stream(qso.data, 30)
# drop rows that have STAR_FIBERID null
star_data = data[data.STAR_FIBERID.notnull()]
star.data['x'] = star_data.STAR_SNR_MAG[1]
star.data['y'] = star_data.STAR_SNR_MAG[0]
star.data['fiber_id'] = star_data.STAR_FIBERID.dropna().tolist()
star.data['ra'] = star_data.RA.dropna().tolist()
star.data['dec'] = star_data.DEC.dropna().tolist()
fitx = star_data.STAR_SNR_MAG[1]
star_fit.data['xfit'] = sorted(fitx)
star_fit.data['yfit'] = [ c_star* x**2 + b_star *x + a_star for x in sorted(fitx)]
a,b,c = np.polyfit(star_data.STAR_SNR_MAG[1], star_data.STAR_SNR_MAG[0],2) #numpy FIT
star_fit.data['xfit'] = np.linspace(np.min(fitx), np.max(fitx), 200) #numpy FIT
star_fit.data['yfit'] = [ a* x**2 + b*x + c for x in star_fit.data['xfit']] #numpy FIT
star.stream(star.data, 30)
# configure bokeh widgets
exposure = get_exposure_ids()
if not exposure:
exposure.append(int(selected_exposure))
exposure = sorted(exposure)
exp_slider = Slider(
start=int(exposure[0]), end=int(exposure[-1]),
value=int(selected_exposure), step=1,
title="Exposure ID")
cameras = get_arms_and_spectrographs()
if not cameras["spectrographs"]:
cameras["spectrographs"].append(selected_spectrograph)
if not cameras["arms"]:
cameras["arms"].append(selected_arm)
# we can filter by spectrograph
spectrograph_select = Select(
title="Spectrograph:",
value=selected_spectrograph,
options=cameras["spectrographs"],
width=100)
# and arm
arm_select = Select(
title="Arm:",
options=cameras['arms'],
value=selected_arm,
width=100)
def arm_handler(attr, old, value):
update(value, spectrograph_select.value, exp_slider.value)
def spectrograph_handler(attr, old, value):
update(arm_select.value, value, exp_slider.value)
def exposure_handler(attr, old, value):
update(arm_select.value, spectrograph_select.value, value)
arm_select.on_change("value", arm_handler)
spectrograph_select.on_change("value", spectrograph_handler)
exp_slider.on_change("value", exposure_handler)
# here we make the plots
html_tooltip = """
<div>
<div>
<span style="font-size: 12px; font-weight: bold; color: #303030;">SNR: </span>
<span style="font-size: 13px; color: #515151;">@y</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold; color: #303030;">DECAM_R: </span>
<span style="font-size: 13px; color: #515151;">@x</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold; color: #303030;">Fiber ID: </span>
<span style="font-size: 13px; color: #515151;">@fiber_id</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold; color: #303030;">RA: </span>
<span style="font-size: 13px; color: #515151;">@ra</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold; color: #303030;">Dec: </span>
<span style="font-size: 13px; color: #515151">@dec</span>
</div>
</div>
"""
url = "http://legacysurvey.org/viewer?ra=@ra&dec=@dec&zoom=16&layer=decals-dr3"
from bokeh.plotting import Figure
hover = HoverTool(tooltips=html_tooltip, names=["data"]) #names=['data'] in order to not show hover in line
elg_plot = init_xy_plot(hover=hover)
elg_plot.circle(x='x', y='y', source=elg, name = "data",color="blue", size=5)
elg_plot.xaxis.axis_label = "DECAM_R"
elg_plot.yaxis.axis_label = "SNR"
elg_plot.title.text = "ELG"
elg_plot.line(x='xfit', y='yfit', source = elg_fit)
taptool = elg_plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
hover = HoverTool(tooltips=html_tooltip, names=["data"])
lrg_plot = init_xy_plot(hover=hover)
lrg_plot.circle(x='x', y='y', source=lrg, name = "data", color="red", size=5)
lrg_plot.xaxis.axis_label = "DECAM_R"
lrg_plot.yaxis.axis_label = "SNR"
lrg_plot.title.text = "LRG"
lrg_plot.line(x='xfit', y='yfit', source = lrg_fit)
taptool = lrg_plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
hover = HoverTool(tooltips=html_tooltip, names=["data"])
qso_plot = init_xy_plot(hover=hover)
qso_plot.circle(x='x', y='y', source=qso, name = "data", color="green", size=5)
qso_plot.xaxis.axis_label = "DECAM_R"
qso_plot.yaxis.axis_label = "SNR"
qso_plot.title.text = "QSO"
qso_plot.line(x='xfit', y='yfit', source = qso_fit)
taptool = qso_plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
hover = HoverTool(tooltips=html_tooltip, names=["data"])
star_plot = init_xy_plot(hover=hover)
star_plot.circle(x='x', y='y', source=star, name = "data", color="black", size=5)
star_plot.xaxis.axis_label = "DECAM_R"
star_plot.yaxis.axis_label = "SNR"
star_plot.title.text = "STAR"
star_plot.line(x='xfit', y='yfit', source = star_fit)
taptool = star_plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
update(selected_arm, selected_spectrograph, selected_exposure)
plot = gridplot([[elg_plot, lrg_plot], [qso_plot, star_plot]], responsive=True)
# and create the final layout
layout = column(widgetbox(exp_slider, responsive=True),
row(widgetbox(arm_select, width=130),
widgetbox(spectrograph_select, width=130)),
plot, responsive=True)
curdoc().add_root(layout)
curdoc().title = "SNR"