forked from smoh/imageviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
191 lines (163 loc) · 5.59 KB
/
app.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
import csv
from flask import Flask, render_template, request, redirect, url_for
import requests
from pager import Pager
import pandas as pd
import numpy as np
import h5py
APPNAME = "BL Event Viewer"
STATIC_FOLDER = 'static'
L_TABLE_FILE = 'static/lband2019/lband2019_events.h5'
S_TABLE_FILE = 'static/sband2019/sband2019_events.h5'
P_TABLE_FILE = 'static/pband2019/pband2019_events.h5'
def read_table(url):
"""Return a list of dict"""
with h5py.File(url) as h5:
dd = dict([(col, h5[col][:]) for col in h5.keys()])
for k, dset in dd.items():
if isinstance(dset[0], bytes):
dd[k] = np.char.decode(dset, 'ascii')
df= pd.DataFrame.from_dict(dd)
df['ID'] = df['ID'].astype(str)
return df
app = Flask(__name__, static_folder=STATIC_FOLDER)
app.config.update(
APPNAME=APPNAME,
)
l_table = read_table(L_TABLE_FILE)
s_table = read_table(S_TABLE_FILE)
p_table = read_table(P_TABLE_FILE)
l_pager = Pager(len(l_table))
s_pager = Pager(len(s_table))
p_pager = Pager(len(p_table))
def get_db(band):
band = band.upper().strip()
hc = _load_hits(band)
if band == 'L':
l_table['HitCategory'] = hc
return l_table, l_pager
elif band == 'S':
s_table['HitCategory'] = hc
return s_table, s_pager
elif band == 'P':
p_table['HitCategory'] = hc
return p_table, p_pager
def _load_hits(band):
""" Reload hit category -- only call from get_db """
band = band.upper().strip()
if band == 'L':
h5 = h5py.File(L_TABLE_FILE)
elif band == 'S':
h5 = h5py.File(S_TABLE_FILE)
elif band == 'P':
h5 = h5py.File(P_TABLE_FILE)
hc = np.char.decode(h5['HitCategory'][:], 'ascii')
h5.close()
return hc
def _update_hits(band, idx, val):
""" Reload hit category -- only call from get_db """
band = band.upper().strip()
if band == 'L':
h5 = h5py.File(L_TABLE_FILE)
elif band == 'S':
h5 = h5py.File(S_TABLE_FILE)
elif band == 'P':
h5 = h5py.File(P_TABLE_FILE)
h5['HitCategory'][idx] = val
print("Updating db: %s" % val)
h5.close()
def sort_table(stbl, sortidx):
if sortidx in ('freq', 'frequency', 'freqs'):
stbl = stbl.sort_values('FreqMid')
elif sortidx in ('src', 'source'):
stbl = stbl.sort_values('Source')
elif sortidx in ('driftrate', 'drate'):
stbl = stbl.sort_values('DriftRateMax')
elif sortidx in ('nevent', 'events', 'event'):
stbl = stbl.sort_values('Nevent')
return stbl
@app.route('/')
@app.route('/l')
def index():
return redirect('/l/0')
@app.route('/s')
def indexS():
return redirect('/s/0')
@app.route('/<string:band>/<int:ind>/')
def image_view(band='L', ind=None):
table, pager = get_db(band)
if ind >= pager.count:
return render_template("404.html"), 404
else:
pager.current = ind
return render_template(
'imageview.html',
index=ind,
band=band,
pager=pager,
data=dict(table.iloc[ind])
)
@app.route('/<string:band>/viewall/')
def view_all_all0(band):
return redirect('/%s/viewall/freqs/all' %band )
@app.route('/<string:band>/viewall/<string:sortidx>')
def view_all_all(band='L', sortidx=None):
return redirect('/%s/viewall/%s/all' % (band, sortidx))
@app.route('/<string:band>/viewall/<string:sortidx>/<string:srcid>/<int:fstart>/<int:fstop>')
@app.route('/<string:band>/viewall/<string:sortidx>/<string:srcid>/<float:fstart>/<float:fstop>')
def image_view_fstart_fstop(band='L', sortidx=None, srcid=None, fstart=None, fstop=None):
table, pager = get_db(band)
if srcid == 'candidates':
stbl = table[table['HitCategory'].str.contains('follow up')]
elif srcid is not None and srcid != 'all':
stbl = table[table['Source'].str.lower() == srcid.lower()]
else:
stbl = table
if fstart is not None:
stbl = stbl[stbl['FreqMid'] > fstart]
if fstop is not None:
stbl = stbl[stbl['FreqMid'] < fstop]
stbl = sort_table(stbl, sortidx)
return render_template(
'imageview_all.html',
srcid=srcid,
band=band,
data=list(zip(stbl.index, stbl['PngFile']))
)
@app.route('/<string:band>/viewall/<string:sortidx>/<string:srcid>')
def image_view_all(band, sortidx=None, srcid=None):
table, pager = get_db(band)
if srcid == 'candidates':
stbl = table[table['HitCategory'].str.contains('follow up')]
elif srcid is not None and srcid != 'all':
stbl = table[table['Source'].str.lower() == srcid.lower()]
else:
stbl = table
stbl = sort_table(stbl, sortidx)
return render_template(
'imageview_all.html',
srcid=srcid,
band=band,
data=list(zip(stbl.index, stbl['PngFile']))
)
@app.route('/goto', methods=['POST', 'GET'])
def goto():
return redirect('/' + request.form['band'] + '/' + request.form['index'])
@app.route('/<string:band>/hitsinoff/<int:ind>')
def dbupdate_hits_in_off(band, ind):
_update_hits(band, ind, 'Hits in off')
return "Nothing"
@app.route('/<string:band>/followup/<int:ind>')
def dbupdate_followup(band, ind):
_update_hits(band, ind, 'Requires follow up')
return "Nothing"
@app.route('/<string:band>/plottingissue/<int:ind>')
def dbupdate_plotting_issue(band, ind):
_update_hits(band, ind, 'Plotting issue')
return "Nothing"
@app.route('/<string:band>/interestingnotet/<int:ind>')
def dbupdate_interesting_not_et(band, ind):
_update_hits(band, ind, 'Interesting but not ET')
return "Nothing"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8001)