-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_utils.py
274 lines (244 loc) · 10.6 KB
/
ui_utils.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
import sys
import os
import getpass
from IPython.display import display, HTML
import ipywidgets as widgets
from ipywidgets import Layout, HBox, Label
import numpy as np
from bokeh.plotting import figure, show, hplot
from bokeh.models import ColumnDataSource,CustomJS
from odm2api.ODM2.models import *
def getSecurePassword():
u = getpass.getuser('Enter the ODM2 username')
return u
def selectPlot(s1,selectedResult):
TOOLS = "box_select,lasso_select,help"
for result in selectedResult:
xaxistitle = str(result.variableCode)
p = figure(tools=TOOLS, plot_width=400, plot_height=400, x_axis_type="datetime",
title = xaxistitle)
p.circle('x', 'y', source=s1)
#selected = []
#def toolEventsCallback(attr, old, new):
# print("callback", new)
# python = new[0]['python']
# pypy = new[0]['pypy']
# print("python=%f pypy=%f" % (python, pypy))
# selected.append([python, pypy])
#p.tool_events.on_change("lasso_select", toolEventsCallback)
s2 = ColumnDataSource(data=dict(x=[], y=[]))
data = s1.data
maximum = np.nanmax(data['y'])
minimum = np.nanmin(data['y'])
minimum = minimum[0]
maximum = maximum[0]
#print(minimum)
#print(maximum)
p2 = figure(plot_width=400, plot_height=400, y_range=(minimum*.9, maximum*1.1), x_axis_type="datetime",
tools="", title="Selected Data")
p2.circle('x', 'y', source=s2, alpha=0.6)
s1.callback = CustomJS(args=dict(s2=s2), code="""
var inds = cb_obj.changed.selected['1d'].indices;
var d1 = cb_obj.attributes.data;
var d2 = s2.attributes.data;
d2['x'] = [];
d2['y'] = [];
for (i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]]);
d2['y'].push(d1['y'][inds[i]]);
}
console.log(d2);
var kernel = IPython.notebook.kernel;
IPython.notebook.kernel.execute("inds = " + inds);
s2.trigger('change');
""")
plot = hplot(p, p2)
return plot
def ODM2LoginPrompt():
container = widgets.Box() # would be nice If I could get a container to hold the
# user name and password prompt, getpass doesn't seem to play well with the other
# widgets though
username_text = widgets.Text(
value='', placeholder='Enter username',
description='', disabled=False)
username_output_text = widgets.Text(
value='', placeholder='Enter username',
description='Username',disabled=False)
database_address_text = widgets.Text(
value='', placeholder='Enter database address',
description='',disabled=False)
database_address_output_text = widgets.Text(
value='',placeholder='Enter database address',
description='database address',disabled=False)
database_text = widgets.Text(
value='', placeholder='Enter database name',
description='', disabled=False)
database_output_text = widgets.Text(
value='', placeholder='Enter database name',
description='database name', disabled=False)
def bind_username_to_output(sender):
username_output_text.value = username_text.value
def bind_database_address_to_output(sender):
database_address_output_text.value = database_address_text.value
def bind_database_to_output(sender):
database_output_text.value = database_text.value
def login(sender):
#print('Database address : %s, Username: %s, database name: %s' % (
# database_address_text.value, username_text.value, database_text.value))
container.close()
username_text.on_submit(bind_username_to_output)
login_btn = widgets.Button(description="Login")
login_btn.on_click(login)
container.children = [username_text,database_address_text, database_text, login_btn]
#container
return container
def annotationContainer():
annotation_text = widgets.Text(
value='', placeholder='Enter an annotation',
description='annotation', disabled=False)
annotation_output_text = widgets.Text(
value='', placeholder='Enter an annotation',
description='',disabled=False)
annotation_code_text = widgets.Text(
value='', placeholder='Enter an annotation code or leave blank',
description='annotation code', disabled=False)
annotation_code_output_text = widgets.Text(
value='', placeholder='anotation code',
description='',disabled=False)
def bind_annotation_to_output(sender):
annotation_output_text.value = annotation_text.value
def bind_annotation_code_to_output(sender):
annotation_code_output_text.value = annotation_code_text.value
def submit(sender):
print('Annoatation : %s, Annotation Code: %s' % (
annotation_text.value, annotation_code_text.value))
container.close()
container = widgets.Box()
submit_btn = widgets.Button(description="Submit")
submit_btn.on_click(submit)
container.children = [annotation_text,annotation_code_text, submit_btn]
return container
def startDateEndDateContainer():
start_date_text = widgets.Text(
value='', placeholder='2017-01-01',
description='start date', disabled=False)
start_date_output_text = widgets.Text(
value='', placeholder='2017-01-01',
description='',disabled=False)
end_date_text = widgets.Text(
value='', placeholder='2017-01-06',
description='end date', disabled=False)
end_date_output_text = widgets.Text(
value='', placeholder='2017-01-06',
description='',disabled=False)
def bind_start_date_to_output(sender):
start_date_output_text.value = start_date_text.value
def bind_end_date_to_output(sender):
end_date_output_text.value = end_date_text.value
def submit(sender):
print('start date : %s, end date: %s' % (
start_date_text.value, end_date_text.value))
container.close()
container = widgets.Box()
submit_btn = widgets.Button(description="Submit")
submit_btn.on_click(submit)
container.children = [start_date_text,end_date_text, submit_btn]
return container
def dataqualityWidget(DBSession):
dataquality_code = DBSession.query(CVQualityCode)
def print_dataquality_code_name(code):
print(code)
def on_change(change):
print(change['new'])
dqtids = []
dqtnames = {}
for dq in dataquality_code:
dqtids.append(dq.Name)
namestr = str(dq.Name + " - " + dq.Term)
dqtnames[namestr] = dq.Name
dataquality_codeWidget = widgets.Dropdown(options=dqtnames, layout=Layout(width='50%', height='30px'))
dataquality_codeWidget.label = ''
dqwidget = widgets.interactive(print_dataquality_code_name,code=dataquality_codeWidget)
dqwidget.observe(on_change)
#label = Label('data quality ',layout=Layout(height='30px'))
#box = HBox([label, dataquality_codeWidget]) #dqwidget
return dataquality_codeWidget, dqwidget
def actionWidget(DBSession):
#featureaction = 1700
actions = DBSession.query(Actions).filter_by(ActionTypeCV = 'Observation')
actionids = []
actionnames = {}
def print_action_name(action):
print(action)
def on_change(change):
print(change['new'])
for a in actions:
#print(r.ResultID)
actionids.append(str(a.ActionID))
#action_type=read.CVActionType(name=a.ActionTypeCV)
method = DBSession.query(Methods).filter_by(MethodID =a.MethodID).one()
#detailr = read.getDetailedResultInfo(resultTypeCV = 'Time series coverage',resultID=r.ResultID)
namestr = str(method.MethodCode + "- Begin date - " + str(a.BeginDateTime))
actionnames[namestr]= str(a.ActionID)
#print(detailr.Methods)
print(actionids)
actionWidget = widgets.Dropdown(options=actionnames, layout=Layout(width='50%', height='30px'))
awidget = widgets.interactive(print_action_name,action=actionWidget)
awidget.observe(on_change)
return actionWidget, awidget
def actionWidgetNotPulse(DBSession):
#featureaction = 1700
dsrs = DBSession.query(DataSetsResults).filter_by(DataSetID=17)
dsrids = []
for dsr in dsrs:
dsrids.append(dsr.ResultID)
results = DBSession.query(TimeSeriesResults).filter(~TimeSeriesResults.ResultID.in_(dsrids))
featureactions = DBSession.query(FeatureActions).join(Results).filter(~Results.ResultID.in_(dsrids))
faids =[]
for fa in featureactions:
faids.append(fa.FeatureActionID)
#actions = DBSession.query(Actions).filter_by(ActionTypeCV = 'Observation'
# ).join(FeatureActions).filter(FeatureActions.FeatureActionID.in_(faids))
actionids=[34, 35,21,22,8] #34,
actions = DBSession.query(Actions).filter(Actions.ActionID.in_(actionids))
actionids = []
actionnames = {}
def print_action_name(action):
print(action)
def on_change(change):
print(change['new'])
for a in actions:
#print(r.ResultID)
actionids.append(str(a.ActionID))
#action_type=read.CVActionType(name=a.ActionTypeCV)
method = DBSession.query(Methods).filter_by(MethodID =a.MethodID).one()
#detailr = read.getDetailedResultInfo(resultTypeCV = 'Time series coverage',resultID=r.ResultID)
namestr = str(method.MethodCode + "- Begin date - " + str(a.BeginDateTime))
actionnames[namestr]= str(a.ActionID)
#print(detailr.Methods)
print(actionids)
actionWidget = widgets.RadioButtons(options=actionnames) #layout=Layout(width='50%',min_width='50%'), height='30px'
awidget = widgets.interactive(print_action_name,action=actionWidget)
awidget.observe(on_change)
return actionWidget, awidget
def resultWidget(read, actionid):
results = read.getResults(actionid=actionid)
resultids = []
resultnames = {}
def print_result_name(result):
print(result)
def on_change(change):
print(change['new'])
for r in results:
#print(r.ResultID)
resultids.append(str(r.ResultID))
detailr = read.getDetailedResultInfo(resultTypeCV = 'Time series coverage',resultID=r.ResultID)
for detail in detailr:
namestr = str(str(detail.resultID) + "- " + detail.samplingFeatureCode + "- " + detail.methodCode + "- "+ detail.variableCode + "- " + detail.unitsName)
resultnames[namestr]= detail.resultID
#print(detailr.Methods)
print(resultids)
resultWidget = widgets.RadioButtons(options=resultnames)
rwidget = widgets.interactive(print_result_name,result=resultWidget)
rwidget.observe(on_change)
return resultWidget, rwidget