-
Notifications
You must be signed in to change notification settings - Fork 1
/
dash.py
263 lines (230 loc) · 9.53 KB
/
dash.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
from argparse import ArgumentParser
import plotly.graph_objs as go
import json
import dash
import plotly
import plotly.subplots
import plotly.graph_objs.layout as layout
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import numpy as np
import flask
import pandas as pd
import time
import pathlib
import os
from tools.files import list_files
from tools.files import sorter
from tools.files import conditional_makedir
from tools.files import save_fiducials
from tools.files import load_data
from dash.dependencies import Output, Input, State
from textwrap import dedent as d
from datetime import datetime
from datetime import date
import os
import os.path
import numpy as np
import plotly.graph_objs as go
"""
Determine file paths
"""
# Define paths
paths = {'inputs' : '', 'output' : ''}
selected_pts = dict()
##################### DEFINE SERVER #####################
server = flask.Flask('app')
server.secret_key = os.environ.get('secret_key', 'secret')
##################### DEFINE APP #####################
app = dash.Dash(__name__, server=server, external_stylesheets=['https://codepen.io/chriddyp/pen/bWLwgP.css'])
app.scripts.config.serve_locally = False
# dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-basic-latest.min.js'
##################### DEFINE FIGURE #####################
TemplateHeader = ['I', 'II', 'III', 'AVR', 'AVL', 'AVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'SELECT']
signal = pd.DataFrame({TemplateHeader[i] : np.zeros(1000,) for i in range(len(TemplateHeader))})
fig = plotly.subplots.make_subplots(rows=13, cols=1, shared_xaxes=True, shared_yaxes=False, vertical_spacing=0.01)
for i in range(len(TemplateHeader)):
fig.append_trace({'x':np.arange(len(signal[TemplateHeader[i]])),'y': signal[TemplateHeader[i]],'name': TemplateHeader[i]},i+1,1)
fig['layout']['xaxis'].update(title='', range=[0, 500], autorange=False)
fig.update_traces(xaxis="x13")
fig.layout.xaxis13.spikemode = "across"
fig.layout.xaxis13.spikethickness = 1
fig.layout.xaxis13.rangeslider.visible = True
fig.update_traces(line=dict(width=1.5),)
##################### DEFINE LAYOUT #####################
app.layout = html.Div([
# dcc.Store(id='button-storage', storage_type='local'),
html.H1('TV QRS marker'),
# html.H2('Select input path'),
# html.Div([
# html.Div([
# dcc.Input(
# id='path',
# size=200,
# ),
# ], className="ten columns"),
# html.Div([
# html.Button('Select Folder', id='selectfolder'),
# ], className="two columns")
# ]),
html.H2('Select file'),
dcc.Dropdown(
id='my-dropdown',
options=[]
),
html.Div([
html.Div([
dcc.Graph(
id='basic-interactions',
style={
'height': 1000,
},
figure={
'layout': {
'clickmode': 'event+select'
}
},
),
], className="eight columns"),
html.Div([
html.H2('Control pannel'),
html.Button('Delete Last element', id='button'),
html.Button('Save Data', id='save'),
html.Button('Plot Lines', id='plot_lines'),
html.H2('Click Data'),
dcc.Markdown(d("""Click on points in the graph.""")),
html.Div(id='container-button-basic', children='-'),
# html.Div([
# dash_table.DataTable(
# id='editable_table',
# columns=([{'id': 'fiducial', 'name': 'Fiducials'}]),
# data=[{"fiducial": 0}],
# editable=True
# ),
# ]),
html.Pre(id='click-data', style={'border': 'thin lightgrey solid','overflowX': 'scroll'}),
dcc.Input(id="from", type="text", placeholder="from"),
dcc.Input(id="to", type="text", placeholder="to"),
html.Button('Rescale View', id='plot'),
], className="four columns"),
]),
], className="row")
##################### DEFINE CALLBACKS #####################
def get_lines(array: list or np.ndarray):
return [{
'type':'line', 'line':dict(color="#d62728", width=1.),
'yref':'paper', 'y0':0, 'y1':1,
'xref':'x', 'x0':array[i], 'x1':array[i],
} for i in range(len(array))]
@app.callback(
Output('basic-interactions', 'figure'),
# Output('click-data', 'children')],
[Input('plot', 'n_clicks'),
Input('plot_lines', 'n_clicks'),
Input('my-dropdown', 'value'),],
[State("from", "value"),
State("to", "value"),])
def update_graph(plot_button, plot_lines, selected_dropdown_value, from_value, to_value): # , clicks_data, clickData
# Keep track of what triggered whatever
execution_instruction = dash.callback_context.triggered[0]['prop_id']
# Output default
js = []
# Should always be active to make annotations
if (selected_dropdown_value is not None):
r, fn = os.path.split(selected_dropdown_value)
code = fn.split('.')[0]
if ('my-dropdown.value' in execution_instruction):
signal = pd.read_csv(selected_dropdown_value, index_col=0)
signal.columns = map(str.upper, signal.columns)
for i in range(len(TemplateHeader)):
name = fig.data[i].name
loc = np.where(TemplateHeader == name)[0]
if name == 'SELECT':
fig.data[i].y = np.zeros((signal.shape[0],))
fig.data[i].x = np.arange(len(fig.data[i].y))
else:
fig.data[i].y = signal[name.upper()]
fig.data[i].x = np.arange(len(fig.data[i].y))
# Set X range
if signal.shape[0] == 60001:
fig.layout.xaxis13.range = [0, 6000]
elif signal.shape[0] == 120000:
fig.layout.xaxis13.range = [0, 10000]
else:
if signal.shape[0] > 10000:
fig.layout.xaxis13.range = [0, 6000]
else:
fig.layout.xaxis13.range = [0, signal.shape[0]]
# Set ylim given stupid dash system for this
min_val = []
max_val = []
for j in range(12):
min_val.append(int(np.min(fig.data[j]['y'][from_value:to_value])))
max_val.append(int(np.max(fig.data[j]['y'][from_value:to_value])))
for j in range(12):
if j == 0:
fig.layout['yaxis'].range = [np.min(min_val),np.max(max_val)]
else:
fig.layout['yaxis'+str(j+1)].range = [np.min(min_val),np.max(max_val)]
# Set lines
shapes = get_lines(selected_pts[code])
fig.layout.shapes = tuple([layout.Shape(**shapes[i]) for i in range(len(shapes))])
elif ('plot_lines.n_clicks' in execution_instruction):
# Add vertical lines
shapes = get_lines(selected_pts[code])
fig.layout.shapes = tuple([layout.Shape(**shapes[i]) for i in range(len(shapes))])
elif ('plot.n_clicks' in execution_instruction):
from_value = int(float(from_value)*1000)
to_value = int(float(to_value)*1000)
for j in range(13):
min_val = int(np.min(fig.data[j]['y'][from_value:to_value]))
max_val = int(np.max(fig.data[j]['y'][from_value:to_value]))
if j == 0:
fig.layout['yaxis'].range = [min_val,max_val]
else:
fig.layout['yaxis'+str(j+1)].range = [min_val,max_val]
return fig
@app.callback(
Output('click-data', 'children'),
[Input('button', 'n_clicks'),
Input('basic-interactions', 'clickData'),
Input('my-dropdown', 'value')])
def display_click_data(delete_button, clickData, selected_dropdown_value):
execution_instruction = dash.callback_context.triggered[0]['prop_id']
# Output default
js = []
if selected_dropdown_value is not None:
r, fn = os.path.split(selected_dropdown_value)
code = fn.split('.')[0]
if ('my-dropdown.value' in execution_instruction):
if code not in selected_pts.keys():
selected_pts[code] = []
elif ('basic-interactions.clickData' in execution_instruction):
selected_pts[code].append(clickData['points'][0]['x'])
elif ('button.n_clicks' in execution_instruction) and (len(selected_pts[code]) != 0):
selected_pts[code].pop()
js = selected_pts[code]
return json.dumps(js)
@app.callback(
Output('container-button-basic', 'children'),
[Input('save', 'n_clicks')])
def save_on_click(n_clicks):
if len(selected_pts) != 0:
save_fiducials(selected_pts,paths['output'])
return "Saved at " + str(datetime.now())
##################### EXECUTE APP #####################
if __name__ == '__main__':
# Parse arguments
parser = ArgumentParser()
parser.add_argument('--path', type=str, help='input path', required=True)
parser.add_argument('--output_path', type=str, help='output path', default='./output_{}.csv'.format(datetime.now().isoformat()))
inputs = parser.parse_args()
paths['inputs'] = inputs.path
paths['output'] = inputs.output_path
# Load previous output path
if os.path.isfile(paths['output']):
selected_pts = load_data(paths['output'])
selected_pts.pop('0',None)
app.layout['my-dropdown'].options = list_files(paths['inputs'])
app.run_server()