-
Notifications
You must be signed in to change notification settings - Fork 2
/
CCF_Widget.py
301 lines (246 loc) · 9.09 KB
/
CCF_Widget.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
"""
Bokeh widget for analyzing CCF data.
"""
import os
from collections import OrderedDict
import sys
import time
import logging
from bokeh.models import ColumnDataSource, Plot, HoverTool
from bokeh.plotting import figure, curdoc
from bokeh.properties import String, Instance
from bokeh.server.app import bokeh_app
from bokeh.server.utils.plugins import object_page
from bokeh.models.widgets import HBox, VBox, VBoxForm, Select
from bokeh.io import hplot
from Analyze_CCF import CCF_Interface
from HDF5_Helpers import Full_CCF_Interface
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Parse command-line arguments
ADDMODE = 'simple'
class CCF_App(VBox):
extra_generated_classes = [["CCF_App", "CCF_App", "VBox"]]
jsmodel = "VBox"
# data sources
main_source = Instance(ColumnDataSource)
current_source = Instance(ColumnDataSource)
# layout boxes
upper_row = Instance(HBox) # Shows CCF height vs. temperature
lower_row = Instance(HBox) # Shows CCF
# plots
mainplot = Instance(Plot)
ccf_plot = Instance(Plot)
par_plot = Instance(Plot)
# inputs
star = String(default=u"HIP 92855")
inst_date = String(default=u"CHIRON/20141015")
star_select = Instance(Select)
inst_date_select = Instance(Select)
input_box = Instance(VBoxForm)
_ccf_interface = Full_CCF_Interface(cache=True, update_cache=False)
_df_cache = {}
def __init__(self, *args, **kwargs):
super(CCF_App, self).__init__(*args, **kwargs)
@classmethod
def create(cls):
"""
This function is called once, and is responsible for
creating all objects (plots, datasources, etc)
"""
# create layout widgets
logging.info('Creating CCF_App')
obj = cls()
obj.upper_row = HBox()
obj.lower_row = HBox()
obj.input_box = VBoxForm()
# create input widgets
obj.set_defaults()
obj.make_star_input()
obj.make_inst_date_input()
# outputs
obj.make_source()
obj.make_plots()
# layout
obj.set_children()
return obj
def set_defaults(self):
stars = self._ccf_interface.list_stars()
self.star = stars[0]
dates = self._ccf_interface.get_observations(self.star)
self.inst_date = '/'.join(dates[0])
#self.star = 'HIP 79199'
#self.inst_date = 'CHIRON/2014-03-18'
def make_star_input(self):
starnames = sorted(self._ccf_interface.list_stars())
self.star_select = Select(
name='Star identifier',
value=self.star,
options=starnames
)
def make_inst_date_input(self):
observations = self._ccf_interface.get_observations(self.star)
observations = ['/'.join(obs).ljust(20, ' ') for obs in observations]
self.inst_date = observations[0]
if isinstance(self.inst_date_select, Select):
self.inst_date_select.update(value=observations[0], options=observations)
else:
self.inst_date_select = Select.create(
name='Instrument/Date',
value=observations[0],
options=observations,
)
def make_source(self):
self.main_source = ColumnDataSource(data=self.df)
def plot_ccf(self, name, T, x_range=None):
# Load the ccf from the HDF5 file.
logging.debug('Plotting ccf name {}'.format(name))
observation = self.inst_date
i = observation.find('/')
instrument = observation[:i]
vel, corr = self._ccf_interface.load_ccf(instrument, name)
# Now, plot
p = figure(
title='{} K'.format(T),
x_range=x_range,
plot_width=600, plot_height=400,
title_text_font_size="10pt",
tools="pan,wheel_zoom,box_select,reset,save"
)
p.line(vel, corr, line_width=2)
p.xaxis[0].axis_label = 'Velocity (km/s)'
p.yaxis[0].axis_label = 'CCF Power'
return p
def plot_Trun(self):
star = self.star
inst_date = self.inst_date
data = self.selected_df
idx = data.groupby(['T']).apply(lambda x: x['ccf_max'].idxmax())
highest = data.ix[idx].copy()
source = ColumnDataSource(data=highest)
self.current_source = source
p = figure(
title="{} - {}".format(star, inst_date),
plot_width=800, plot_height=400,
tools="pan,wheel_zoom,tap,hover,reset",
title_text_font_size="20pt",
)
p.circle("T", "ccf_max",
size=10,
nonselection_alpha=0.6,
source=source
)
p.xaxis[0].axis_label = 'Temperature (K)'
p.yaxis[0].axis_label = 'CCF Peak Value'
hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
("Temperature", "@T"),
("vsini", "@vsini"),
("[Fe/H]", "@feh"),
("log(g)", "@logg"),
("Radial Velocity (km/s)", "@vel_max"),
("ccf peak height", "@ccf_max"),
])
return p, highest
def make_parplot(self):
p = figure(
title="CCF Parameters",
plot_width=500, plot_height=400,
tools="pan,wheel_zoom,box_select,reset",
title_text_font_size="20pt",
)
p.circle("vsini", "feh",
size=12,
nonselection_alpha=0.003,
source=self.main_source
)
p.xaxis[0].axis_label = 'vsini (km/s)'
p.yaxis[0].axis_label = '[Fe/H]'
return p
def make_plots(self):
# Make the main plot (temperature vs ccf max value)
self.mainplot, highest = self.plot_Trun()
# Make the parameter plot (vsini vs. [Fe/H])
self.par_plot = self.make_parplot()
# Finally, make the CCF plot
name, T = highest.sort('ccf_max', ascending=False)[['name', 'T']].values[0]
self.ccf_plot = self.plot_ccf(name, T)
return
def set_children(self):
self.children = [self.upper_row, self.lower_row]
self.upper_row.children = [self.input_box, self.mainplot]
self.input_box.children = [self.star_select, self.inst_date_select]
self.lower_row.children = [self.ccf_plot, self.par_plot]
def star_change(self, obj, attrname, old, new):
logging.debug('Star change!')
self.star = new
self.make_inst_date_input()
self.make_source()
self.make_plots()
self.set_children()
curdoc().add(self)
def inst_date_change(self, obj, attrname, old, new):
logging.debug('Date change!')
self.inst_date = new
self.make_source()
self.make_plots()
self.set_children()
curdoc().add(self)
def setup_events(self):
super(CCF_App, self).setup_events()
if self.current_source:
self.current_source.on_change('selected', self, 'Trun_change')
if self.main_source:
self.main_source.on_change('selected', self, 'par_change')
if self.star_select:
self.star_select.on_change('value', self, 'star_change')
if self.inst_date_select:
self.inst_date_select.on_change('value', self, 'inst_date_change')
def Trun_change(self, obj, attrname, old, new):
idx = int(new['1d']['indices'][0])
T = self.current_source.data['T'][idx]
name = self.current_source.data['name'][idx]
logging.debug('T = {}\nName = {}\n'.format(T, name))
self.ccf_plot = self.plot_ccf(name, T)
self.set_children()
curdoc().add(self)
def par_change(self, obj, attrname, old, new):
# Update plots
self.mainplot, highest = self.plot_Trun()
name, T = highest.sort('ccf_max', ascending=False)[['name', 'T']].values[0]
self.ccf_plot = self.plot_ccf(name, T)
self.set_children()
curdoc().add(self)
@property
def df(self):
# Parse the observation into an instrument and date
observation = self.inst_date
i = observation.find('/')
instrument = observation[:i]
date = observation[i+1:].strip()
# Get the CCF summary
starname = self.star
# Check if this setup has been cached
key = (starname, instrument, date)
if key in self._df_cache:
return self._df_cache[key]
df = self._ccf_interface.make_summary_df(instrument, starname, date, addmode=ADDMODE)
df = df.rename(columns={'[Fe/H]': 'feh'})
#self._df_cache[key] = df.copy()
return df
@property
def selected_df(self):
df = self.df
selected = self.main_source.selected['1d']['indices']
if selected:
df = df.iloc[selected, :]
return df
# The following code adds a "/bokeh/ccf/" url to the bokeh-server. This URL
# will render this CCF_App. If you don't want serve this applet from a Bokeh
# server (for instance if you are embedding in a separate Flask application),
# then just remove this block of code.
@bokeh_app.route("/bokeh/ccf/")
@object_page("ccf")
def make_ccf_app():
app = CCF_App.create()
return app