-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·348 lines (285 loc) · 9.32 KB
/
app.py
File metadata and controls
executable file
·348 lines (285 loc) · 9.32 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
#!/usr/bin/env python
import base64, io
import dash
from dash import dcc, html, dash_table, Dash, callback_context, MATCH, ALL
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
import dash_bootstrap_components as dbc
from dash.dash_table import DataTable
from os.path import isfile, isdir, abspath, join as pjoin, dirname, splitext, basename
from os import makedirs, getenv, remove, listdir
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import logging
from glob import glob
import json
from flask import Flask
server=Flask(__name__)
SCRIPTDIR=dirname(abspath(__file__))
ROOTDIR= getenv("NDA_ROOT")
URL_PREFIX= getenv("DASH_URL_BASE_PATHNAME",'')
if not ROOTDIR:
print('Define env var NDA_ROOT and try again')
exit(1)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css',dbc.themes.BOOTSTRAP,'styles.css']
app = Dash(__name__, external_stylesheets=external_stylesheets, suppress_callback_exceptions=True, title='Missing-Data Tracker', \
assets_folder=ROOTDIR, assets_url_path="/",server=server)
log= logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
with open('subject-id-gen/sites.json') as f:
sites= json.load(f)
sites2=[]
for s in sites:
sites2.append({'label':s['id']+' | '+s['name'], 'value':s['id']})
for n in 'PRONET PRESCIENT AMPSCZ'.split():
sites2.append({'label':n, 'value':n})
sites=sites2.copy()
visits='baseline,month_1,month_2,month_6,month_12,month_24'.split(',')
datatypes='mri,eeg,avl,act,sen,ema,cnb'.split(',')
column_type={
'Index':'text',
'subject_id':'text',
'mri_score':'numeric',
'mri_data':'numeric',
'mri_protocol':'numeric',
'mri_date':'datetime',
'mri_missing':'text',
'eeg_score':'numeric',
'eeg_data':'numeric',
'eeg_protocol':'numeric',
'eeg_date':'datetime',
'eeg_missing':'text',
'avl_score':'numeric',
'avl_data':'numeric',
'avl_protocol':'numeric',
'avl_date':'datetime',
'avl_missing':'text',
'act_score':'numeric',
'act_data':'numeric',
'act_protocol':'numeric',
'act_date':'datetime',
'act_missing':'text',
'sen_score':'numeric',
'sen_data':'numeric',
'sen_protocol':'numeric',
'sen_date':'datetime',
'sen_missing':'text',
'ema_score':'numeric',
'ema_data':'numeric',
'ema_protocol':'numeric',
'ema_date':'datetime',
'ema_missing':'text',
'cnb_score':'numeric',
'cnb_data':'numeric',
'cnb_protocol':'numeric',
'cnb_date':'datetime',
'cnb_missing':'text'
}
app.layout= html.Div(
children= [
html.Details([html.Summary('Collapse/Expand Introduction'),
html.Br(),
dbc.Row([
dbc.Col(html.Img(src='https://avatars.githubusercontent.com/u/75388309?s=400&u=0d32212fdb2b3bf3854ed6624ce9f011ca6de29c&v=4', id='ampscz'),width=2),
dbc.Col([
dbc.Row(dcc.Markdown("""
### AMP SCZ Missing-Data Tracker
Developed by Tashrif Billah and Sylvain Bouix
https://github.com/AMP-SCZ/missing-data-tracker  
[]
(https://doi.org/10.5281/zenodo.13151571)
""")),
dbc.Row([
dbc.Col(dcc.Markdown("""
* Provide value in the box(es) and click `FILTER`
* Click `DOWNLOAD` to download shown in table
* Enter filter queries under each column header:
* `<=0`, `>=0`, `=1`, `>2024-01-01`, `>5`, `is blank`, `>''`, etc.
"""),width='auto'),
dbc.Col(dcc.Markdown("""
* Example of `site`: LA, PA, ME
* Refresh browser to reset all filters
"""),width='auto')
])
])
])], open=True),
html.Br(),
dbc.Row([
# site filter
dbc.Col(html.Div(dcc.Dropdown(id='site',placeholder='site',className='ddown',
options=sites,
value='')),
width=2
),
# password for site
dbc.Col(html.Div(dcc.Input(id='passwd',placeholder='password',
type='password',
debounce=True)),
width=1
),
# datatype filter
dbc.Col(html.Div(dcc.Dropdown(id='datatype', className='ddown',
options=datatypes,
value=datatypes,
multi=True)),
width=2
),
# visit filter
dbc.Col(html.Div(dcc.Dropdown(id='visit', className='ddown',
options=visits,
value='baseline')),
width=2
),
# filter button
dbc.Col(html.Button('Filter', id='filter', n_clicks=0))
]),
dbc.Row(dcc.ConfirmDialog(id='verify', message='Invalid password for the site, try again')),
html.Br(),
# dcc.Loading(html.Div(id='loading'),type='cube'),
html.Br(),
html.Div(id='table'),
html.Br(),
dcc.Store(id='properties'),
html.Br(),
html.Br(),
html.Br(),
html.Br(),
html.Br(),
dbc.Navbar([html.Button('Download', id='download', n_clicks=0),
dcc.Download(id='download-csv')],
fixed='bottom',
color='white'
)
]
)
_passwd=pd.read_csv('.passwd')
_passwd.set_index('site',inplace=True)
# verify password
@app.callback(Output('verify','displayed'),
[Input('site','value'),
Input('passwd','value')])
def verify_passwd(site,passwd):
if site and passwd:
if passwd==_passwd.loc['dpacc','passwd']:
pass
elif passwd==_passwd.loc[site,'passwd']:
pass
else:
# return f'Invalid password for site {site}, try again'
return True
# render table
@app.callback(Output('table','children'),
[Input('site','value'),
Input('visit','value'),
Input('datatype','value'),
Input('passwd','value'),
Input('filter','n_clicks')])
def filter(site,visit,_datatypes,passwd,click):
changed = [p['prop_id'] for p in callback_context.triggered][0]
if not ('filter' in changed and site and visit):
raise PreventUpdate
# verify password
if passwd==_passwd.loc['dpacc','passwd']:
pass
elif passwd==_passwd.loc[site,'passwd']:
pass
else:
raise PreventUpdate
# select file to load
file=f'combined-{site}-data_{visit}-day1to1.csv'
if site=='AMPSCZ':
path=f'{ROOTDIR}/{file}'
else:
path=f'{ROOTDIR}/Pronet_status/{file}'
if not isfile(path):
path=f'{ROOTDIR}/Prescient_status/{file}'
try:
_df=pd.read_csv(path)
except FileNotFoundError:
return html.H4(f'FileNotFoundError : {file}', style={'color':'red'})
_df.rename(columns={'day':'Index'}, inplace=True)
# filter columns for datatype
# MRI, EEG, AVL, CNB
columns=['Index','subject_id']
for d in _datatypes:
for c in _df.columns:
if d in c:
columns.append(c)
df=_df[columns]
# render selected columns/rows
return DataTable(
id='dataframe',
columns=[{'name': i,
'id': i,
'hideable': True,
'type': column_type[i],
} for i in columns],
data=df.to_dict('records'),
filter_action='native',
sort_action='native',
page_size=50,
style_cell={
'textAlign': 'left',
'whiteSpace': 'pre-wrap',
'width': '20px'
},
style_header={
'backgroundColor': 'rgb(230, 230, 230)',
'fontWeight': 'bold'
},
style_data_conditional=[
{
'if': {
'column_id': 'mri_score',
'filter_query': '{mri_score} < 0',
},
'backgroundColor': '#f7fcb9',
'color': 'black',
},
{
'if': {
'column_id': 'mri_data',
'filter_query': '{mri_data} < 0',
},
'backgroundColor': '#f7fcb9',
'color': 'black',
},
{
'if': {
'column_id': 'mri_protocol',
'filter_query': '{mri_protocol} = 0',
},
'backgroundColor': '#ff474c',
'color': 'black',
},
{
'if': {
'column_id': 'mri_missing',
'filter_query': '{mri_missing} > ""',
},
'backgroundColor': '#ff474c',
'color': 'black',
},
],
)
# download filtered data
@app.callback(Output('download-csv','data'),
[Input('table','children'),
Input('site','value'),
Input('visit','value'),
Input('datatype','value'),
Input('download','n_clicks')],
prevent_initial_call=True)
def download(table,site,visit,datatype,click):
changed = [p['prop_id'] for p in callback_context.triggered][0]
if not 'download' in changed:
raise PreventUpdate
filtered=table['props']['derived_virtual_data']
df=pd.DataFrame.from_dict(filtered)
datestamp=datetime.now().strftime('%Y%m%d-%H%M')
dtype='-'.join(datatype)
return dcc.send_data_frame(df.to_csv,f'{site}-{visit}-{dtype}-{datestamp}.csv',index=False)
if __name__=='__main__':
# debug=None allows control via DASH_DEBUG variable
app.run_server(debug=None, host='localhost')