-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
450 lines (339 loc) · 14.1 KB
/
app.py
File metadata and controls
450 lines (339 loc) · 14.1 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
from flask import Flask
import pandas as pd
import numpy as np
import os
from io import BytesIO
import base64
import cv2
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table
from dash.dependencies import Input, Output
import plotly
from plotly.subplots import make_subplots
import plotly_express as px
from io import BytesIO
import base64
import matplotlib.pyplot as plt
import json
app = Flask(__name__)
application = dash.Dash(__name__, server=app,url_base_pathname='/')
videos=[]
for file in os.listdir('/project/DSone/jaj4zcf/Videos/ResultsSodiqCSV'):
if file.endswith(".csv"):
videos.append(file[0:-4])
videos
def buildTopImage(center_frame, n_images, vid):
if n_images[0]=='1':
frames=[0]
if n_images=='3':
frames=[-1,0,1]
if n_images=='5':
frames=[-2,-1,0,1,2]
if n_images=='7':
frames=[-3,-2,-1,0,1,2,3]
images=[]
labels=[]
for i,offset in enumerate(frames):
impath='/project/DSone/jaj4zcf/Videos/v'+str(vid)[-2:]+'/'+str(int(center_frame) + offset)+'.png' ## may need to be updated for final!
impathdirect='/vids/v'+str(vid)[-2:]+'/'+str(center_frame + offset)+'.png'
## Only add if file exists
try:
if live==True:
images.append(html.Td(html.Div(html.Img(src=impathdirect, style={'max-width': '250px','width': '100%'}), className='zoomlow')))
else:
encoded_image = base64.b64encode(open(impath, 'rb').read()).decode("ascii").replace("\n", "")
images.append(html.Td(html.Div(html.Img(src='data:image/png;base64,{}'.format(encoded_image), style={'max-width': '250px','width': '100%'}), className='zoomlow')))
## add labels
if offset==0:
labels.append(html.Td('Selected Frame: ' + str(center_frame)))
else:
labels.append(html.Td('Frame: ' + str(center_frame + offset)))
except:
'poo'
image_table=html.Table(html.Tr(images),style={'text-align':'center','margin':'auto'} )
return image_table
def buildimages(vid, table):
frames=[i['index'] for i in table] #[item for item in range(-10,10)] #[-3,-2,-1,0,1,2,3]
images=[]
labels=[]
for i,offset in enumerate(frames):
impath='/project/DSone/jaj4zcf/Videos/v'+str(vid)[-2:]+'/'+str(offset)+'.png' ## may need to be updated for final!
impathdirect='/vids/v'+str(vid)[-2:]+'/'+str(offset)+'.png'
## Only add if file exists
try:
if live==True:
images.append(html.Tr(html.Td(html.Div(html.Img(src=impathdirect, style={'width': '100%'}), className='zoom'))))
else:
encoded_image = base64.b64encode(open(impath, 'rb').read()).decode("ascii").replace("\n", "")
images.append(html.Tr(html.Td(html.Div(html.Img(src='data:image/png;base64,{}'.format(encoded_image), style={'width': '100%'}), className='zoom'))))
## add labels
if offset==0:
labels.append(html.Td('Selected Frame: ' + str(row)))
else:
labels.append(html.Td('Frame: ' + str(offset)))
except:
'poo'
images=html.Table(images,style={'width': '99%', 'float':'left'} )
return images
vidLabes=[]
for vid in videos:
vidLabes.append({'label': 'Model Result: '+ str(vid), 'value':str(vid)})
labelsdf=pd.read_csv('/project/DSone/jaj4zcf/Videos/ResultsSodiqCSV/'+str(videos[2])+'.csv')
labelsdf=labelsdf.replace(np.nan, '', regex=True)
labelsdf=labelsdf.reset_index()
timeline=dcc.Graph(
id='timeline' )
graph_height=300
videoSelect=dcc.Dropdown(
id='videoSelect',
options=vidLabes,
value=videos[0]
)
PAGE_SIZE=10
COLUMNS=[{"name": i, "id": i} for i in labelsdf[['index','sectNorm', 'time', 'TractSect1', 'Pathology', 'Notes', 'small bowelabNormal']].columns]
COLUMNS[6].update({ "name":"Prob Abnormal",'type': 'numeric', 'format': FormatTemplate.percentage(1)})
COLUMNS[3].update({ "presentation": "dropdown"})
table=html.Div(dash_table.DataTable(
id='table',
editable=True,
page_size = PAGE_SIZE,
sort_action = 'native',
filter_action = 'native',
row_selectable='single',
data=labelsdf[['index','sectNorm', 'time', 'TractSect1', 'Pathology', 'Notes', 'small bowelabNormal']].to_dict('records'),
columns=COLUMNS,
#columns[-1][-1]={'name': 'small bowelabNormal', 'id': 'Prob. SB abNorm.'},
dropdown={
'TractSect1': {
'options': [
{'label': i, 'value': i}
for i in ['colon', 'small bowel', 'stomach', 'pylorus', 'esophagus']
]
}},
style_data_conditional=[
{
'if': {
'filter_query': '{{small bowelabNormal}} > {}'.format(.5),
},
'backgroundColor': '#fff3f5',
},
],
style_cell={
'whiteSpace': 'normal',
'height': '40px',
'overflow': 'hidden',
'textOverflow': 'ellipsis',
'maxWidth': 0,
},
),style={'width':'auto','overflow':'hidden'})
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
application.layout = html.Div(
[
html.H2('Deep VCE Results Explorer'),
html.H5('Choose a Video and Model Prediction Result to Begin:'),
videoSelect,
html.H5('Select points or peaks to explore results:'),
html.Div(id='scrub_display', style={'display':'inline-block', 'width':'99%', 'margin':'auto'}),
dcc.Loading(
id="loading-2",
children=[html.Div([timeline], style={'max-height': '65px'})],
type="circle",
),
dcc.Slider(
id='scrub_frame',
min=0,
max=labelsdf.shape[1],
step=1,
value=0,
),
dcc.Dropdown(
id='n_images',
options=[
{'label': '1', 'value': '1'},
{'label': '3', 'value': '3'},
{'label': '5', 'value': '5'},
{'label': '7', 'value': '7'}
],
value='1'
),
html.Div([
html.Button('Previous', id='prev', n_clicks=0),
html.Button('Next', id='next', n_clicks=0),
html.P(id='test'),
], style={'display':'inline'}),
html.H5('Use the Data Table Below to explore Results - rows are clickable.'),
html.Div([
html.Div( [
html.Div(id='imagecontainertop', style={'height':'80px'}),
html.Div(id='imagecontainer', style={'height':'1000px'}),
], id='imagecontainerwrap', style={'width':'100px','float':'left','display':'inline-block'}),
table
],
style={'display':'inline-block', 'width':'100%'}),
dcc.Slider(
id='imSize',
min=100,
max=512,
step=1,
value=100,
),
dcc.Slider(
id='num_rows',
min=3,
max=20,
step=1,
value=10,
),
html.Div(id='table-var', style={'display': 'none'}), #where to store the table values.
html.Div(id='offset-var', children=[0], style={'display': 'none'}), #where to store the offset from table to index.
html.Div(id='frame-var',children=[0], style={'display': 'none'}), #store the current frame
html.Div(id='prev-click',children=[0], style={'display': 'none'}),
html.Div(id='num-prev',children=[0], style={'display': 'none'}),
html.Div(id='num-next',children=[0], style={'display': 'none'}),
html.Div(id='abnormal_probs',children=[0], style={'display': 'none'}),
html.P(id='abnormals',children=[0])
])
@application.callback(
Output('test', 'children'),
[Input('table', 'selected_rows'), Input('videoSelect', 'value')])
def update_image_table(selected_rows, value):
if value[-2].isnumeric():
video=value[-2:]
else:
video=value[-1]
if selected_rows is None:
return 'no selection'
else:
return selected_rows
@application.callback(
Output('imagecontainer', 'children'),
[ Input('videoSelect', 'value'), Input('table', 'derived_viewport_data') ])
def update_image_div(value, table):
if value[-2].isnumeric():
video=value[-2:]
else:
video=value[-1]
try:
images = buildimages(video, table)
return images
except:
return None
@application.callback(
Output('scrub_display', 'children'),
[Input('scrub_frame', 'value'), Input('videoSelect', 'value'), Input('n_images', 'value')])
def update_image_div(center_frame, vid, n_images):
if vid[-2].isnumeric():
vid=vid[-2:]
else:
vid=vid[-1]
return buildTopImage(center_frame, n_images, vid)
####
### Front and Back Buttons
@application.callback(
[Output('frame-var', 'children'), Output('num-next', 'children')],
[Input('timeline', 'clickData'), Input('next', 'n_clicks')],
[State('frame-var', 'children'),State('num-next', 'children') ])
def update_output(clickData, n_next_clicks, frame,n_next_state):
# Check to see if num of clicks changed
if n_next_clicks != n_next_state:
return [int(frame[0]) + 1], n_next_clicks
else:
return [clickData['points'][0]['x']], n_next_clicks
#except:
#return 0,0
## Change table selection based on current frame and using offset (table does not start at 0)
@application.callback(
Output('scrub_frame', 'value'),
[Input('timeline', 'clickData')])
def display_click_data(clickData):
try:
val=clickData['points'][0]['x']
return val #[int(test['points'][0]['x'])]
except:
return 0
## Set current page of table so that selected frame is visisble.
@application.callback(
Output('table', 'page_current'),
[Input('table', 'derived_virtual_selected_rows')]) # Input('table', "derived_virtual_selected_rows")
def display_click_data(sel_rows):
try:
if sel_rows[0]>3:
return sel_rows[0]/PAGE_SIZE-3/PAGE_SIZE
else:
return sel_rows[0]/PAGE_SIZE
except:
return None
@application.callback(
[Output('table', 'style_data'), Output('imagecontainerwrap', 'style')],
[Input('imSize', 'value')]) # Input('table', "derived_virtual_selected_rows")
def tablepicsize(value):
cellwidth=str(value)+'px'
style_cell={'whiteSpace': 'normal', 'height': cellwidth}
style_wrap={'width':cellwidth,'float':'left','display':'inline-block'}
return style_cell, style_wrap
@application.callback(
Output('table', 'page_size'),
[Input('num_rows', 'value')]) # Input('table', "derived_virtual_selected_rows")
def pages(value):
return value
## Callback to get abnormal frames - by threshold.
@application.callback(
Output('abnormals', 'children'),
[Input('abnormal_probs', 'children')]) # Input('table', "derived_virtual_selected_rows")
def return_abnormalframes(indexes):
indexes=pd.read_json(indexes)
test=indexes[indexes['small bowelabNormal']>.9]['index'].to_json()
return test
### Callback must
@application.callback([Output('timeline', 'figure'),
Output('table', 'data') , Output('offset-var', 'children'),Output('table-var', 'children'),
Output('scrub_frame', 'min') , Output('scrub_frame', 'max'),
Output('abnormal_probs', 'children')],
[Input('videoSelect', 'value')])
def return_data(value):
vid=value
try:
labelsdf=pd.read_csv('/project/DSone/jaj4zcf/Videos/ResultsSodiqCSV/'+str(vid)+'.csv')
except:
labelsdf=pd.read_csv('/project/DSone/jaj4zcf/Videos/ResultsSodiqCSV/'+videos[0]+'.csv')
labelsdf=labelsdf.replace(np.nan, '', regex=True)
offsetvar=labelsdf['index'].min()
## Update Timeline
try:
#labelsdfBar=labelsdf[labelsdf['small bowelabNormal']>=.3]
#labelsdfScat=labelsdf[labelsdf['sectNorm']=='small bowelabNormal']
#colorsIdx = {'mouth': 'rgb(240,128,128)', 'stomach': 'rgb(255,160,122)', 'pylorus': 'rgb(100,149,237)', 'small bowel': 'rgb(147,112,219)', 'colon': 'rgb(205,133,63)'}
#cols = labelsdf['TractSect1'].map(colorsIdx)
#fig = make_subplots(specs=[[{"secondary_y": True}]])
#fig.add_bar(secondary_y=False, y=100*labelsdfBar['small bowelabNormal'], width=1, x=labelsdfBar['index'],text=labelsdfBar['small bowelabNormal'], marker_color='red', opacity=1)
#fig.add_scatter(secondary_y=True, mode='markers',y=labelsdfScat.Pathology, x=labelsdfScat['index'], text=labelsdfScat.time, customdata=labelsdfScat['small bowelabNormal'], marker=dict(size=3, color=cols),
# hovertemplate="Pathology: %{y}<br>index: %{x}<br>time: %{text}<br>Prob. Abnormal: %{customdata}<extra></extra> ")
#fig.update_layout(plot_bgcolor='rgb(250,250,250)', yaxis_title="Probability of Abnormality (%)", margin={'t': 5, 'b':5}) #fig
znumsprob=np.array(list(labelsdf['small bowelabNormal']))
xindex=list(labelsdf['index'])
znumsprob[znumsprob<=0.2]=None
fig = go.Figure()
fig.add_trace(go.Heatmap(
z=znumsprob,
x=xindex,
y=np.ones(len(xindex)),
colorscale='reds', showscale=False,zmin=0, zmax=1, hoverongaps = False) )
fig.update_layout(margin={'t': 5, 'b':5, 'l':0,'r':0}, height=70, plot_bgcolor='white')
fig.update_yaxes(showticklabels=False, gridcolor=None)
fig.update_xaxes(showticklabels=False, gridcolor=None)
except:
fig = make_subplots(specs=[[{"secondary_y": True}]])
labelsdf=labelsdf[['index','sectNorm', 'time', 'TractSect1', 'Pathology', 'Notes', 'small bowelabNormal']]
min_index=labelsdf['index'].min()
max_index=labelsdf['index'].max()
indexes=labelsdf[['index', 'small bowelabNormal']]
return fig, labelsdf.to_dict('records') , offsetvar, labelsdf.to_json(orient='split'), min_index, max_index, indexes.to_json()
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')