-
Notifications
You must be signed in to change notification settings - Fork 3
/
RADCATr.py
290 lines (227 loc) · 8.81 KB
/
RADCATr.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
RADCATr - RADCAT Review Interface
Merck, Winter 2018
"""
import sys
import os
import logging
import csv
import re
import argparse
from Tkinter import *
import ttk
# In case running from /examples folder in git
sys.path.insert(0, os.path.abspath('../DixelKit'))
from diana.apis.report import RadiologyReport
__desc__ = """
Open a CSV dump from Montage and audit entries with a Tkinter GUI.
Results are saved as they are generated as `source+audit.csv`.
"""
__version__ = "0.1.2"
__prog__ = "RADCATr"
root = Tk()
current = 0
items = []
fieldnames = []
def load_data():
global items, fieldnames
fp = os.path.join(data_root, fn)
with open(fp, 'rU') as f:
rows = csv.DictReader(f)
for row in rows:
# logging.debug(row)
items.append(row)
if fn.find("+audit") < 0:
# Add answer columns
fieldnames = rows.fieldnames + ['audit_radcat', 'audit_radcat3', 'agrees']
else:
fieldnames = rows.fieldnames
def save_data():
if fn.find("+audit") < 0:
out_fn = "{}+audit{}".format(os.path.splitext(fn)[0], os.path.splitext(fn)[1])
else:
out_fn = fn
fp = os.path.join(data_root, out_fn)
with open(fp, 'w') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(items)
def make_ui():
def submit(*args):
# logging.debug("setting entries")
unscored_btn.state(['!disabled'])
match = re.match("RADCAT(?P<radcat>\d)\+?(?P<radcat3>3)?", radcat_entry.get())
item = items[current]
item['audit_radcat'] = match.group('radcat')
item['audit_radcat3'] = "Yes" if match.group('radcat3') else "No"
if item['audit_radcat'] == "3":
item['audit_radcat'] = 2
item['audit_radcat3'] = "Yes"
agrees = (item['audit_radcat'] == item['radcat']) and (item['audit_radcat3'] == item['radcat3'])
logging.debug(item['audit_radcat'])
logging.debug(item['audit_radcat3'])
logging.info("Report {:<3} RADCAT grade agreement: {} (originally scored {})".format(
"{}:".format(current+1), item['audit_radcat']==item['radcat'], item['radcat']))
logging.info(" RADCAT f/u agreement: {} (originally scored {})".format(
item['audit_radcat3']==item['radcat3'], item['radcat3']))
item['agrees'] = "Yes" if agrees else "No"
save_data()
def go_first():
go(0)
def go_next():
if current+1 < len(items):
go(current+1)
def go_unscored():
for i in range(current+1, len(items)):
if not items[i].get('audit_radcat'):
go(i)
break
def go_prev():
if current-1 >= 0:
go(current-1)
def go(new):
global current
unscored_btn.state(['disabled'])
current = new
update_ui()
def update_ui():
if items[current].get('audit_radcat'):
audit_radcat = items[current]['audit_radcat']
audit_radcat3 = items[current]['audit_radcat3']
if audit_radcat == 2 and audit_radcat3:
audit_radcat = 3
audit_radcat3 = False
entry = "RADCAT{}{}".format(
audit_radcat,
"+3" if audit_radcat3 == "Yes" else ""
)
radcat_entry.set(entry)
unscored_btn.state(['!disabled'])
else:
radcat_entry.set('')
# fu_entry.set(False)
item = items[current]
r = Report(text=item['report_text'])
# logging.debug(r.text)
# extractions = r.extractions()
# item['radcat'] = int(extractions.get('radcat'))
# item['radcat3'] = "Yes" if extractions.get('radcat3') else "No"
complete = [k for k in items if k.get('audit_radcat')]
task_label_str.set("Report {} of {} ({} complete)\n{age}yo {sex} {status} {bp} {mod}".format(
current+1, len(items), len(complete),
status=item['status'],
age=item['age'],
sex=item['sex'][0],
bp=item['body_part'],
mod=item['modality'])
)
report_text['state'] = 'normal'
report_text.delete('1.0', 'end')
report_text.insert('1.0', r.anonymized())
report_text['state'] = 'disabled'
# logging.debug(item['radcat'])
# logging.debug(item['radcat3'])
root.title("RADCAT Reviewer")
"""
GUI layout row/column design
+--------------+---+---------------+
| | | label | 0
+ | +---------------+
| | | select | 1
+ text | s +---------------+
| | | f/u | 2
+ | +-------+-------+
| | | <prev | next> | 3
+ + +-------+-------+
| | | | unsc> | 4
+--------------+---+-------+-------+
0 1 2 3
"""
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=80)
mainframe.columnconfigure(1, weight=4)
mainframe.columnconfigure(2, weight=10)
mainframe.columnconfigure(3, weight=10)
mainframe.rowconfigure(0, weight=18)
mainframe.rowconfigure(1, weight=2)
mainframe.rowconfigure(2, weight=2)
mainframe.rowconfigure(2, weight=2)
report_text = Text(mainframe, width=80, height=24)
report_text.grid(row=0, column=0, rowspan=5, sticky=(N,S,E,W))
report_scroll = ttk.Scrollbar(mainframe, orient=VERTICAL, command=report_text.yview)
report_scroll.grid(row=0, column=1, rowspan=5, sticky=(N,S))
report_text['yscrollcommand'] = report_scroll.set
task_label = ttk.Label(mainframe, text="hello there")
task_label_str = StringVar()
task_label['textvariable'] = task_label_str
task_label.grid(row=0, column=2, columnspan=2, sticky=N )
radcat_entry = StringVar()
radcat_combo = ttk.Combobox(mainframe, textvariable=radcat_entry, state='readonly')
radcat_combo['values'] = ('', 'RADCAT1', 'RADCAT2', 'RADCAT3', 'RADCAT4', 'RADCAT4+3', 'RADCAT5', 'RADCAT5+3')
radcat_combo.grid(row=1, column=2, sticky=(S), columnspan=2)
radcat_combo.bind('<<ComboboxSelected>>', submit)
# fu_entry = BooleanVar()
# fu_chk = ttk.Checkbutton(mainframe, text='Follow up (RADCAT3)', variable=fu_entry,
# onvalue=True, offvalue=False, command=submit)
# fu_chk.grid(row=2, column=2, columnspan=2)
prev_btn = ttk.Button(mainframe, text="< Back", command=go_prev)
prev_btn.grid(row=3, column=2, sticky=(W,E))
first_btn = ttk.Button(mainframe, text="<< First", command=go_first)
first_btn.grid(row=4, column=2, sticky=(W,E) )
next_btn = ttk.Button(mainframe, text="Next >", command=go_next)
next_btn.grid(row=3, column=3, sticky=(W,E) )
unscored_btn = ttk.Button(mainframe, text="Unscored >>", command=go_unscored)
unscored_btn.grid(row=4, column=3, sticky=(W,E) )
def key_callback(event):
# logging.debug(event.keysym)
if event.keysym == "1":
radcat_combo.current(1)
submit()
elif event.keysym == "2":
radcat_combo.current(2)
submit()
elif event.keysym == "3":
fu_entry.set( not fu_entry.get() )
submit()
elif event.keysym == "4":
radcat_combo.current(3)
submit()
elif event.keysym == "5":
radcat_combo.current(4)
submit()
elif event.keysym == "Right":
go_next()
elif event.keysym == "Left":
go_prev()
elif event.keysym == "space" and unscored_btn.instate(["!disabled",]):
go_unscored()
elif event.keysym == "0":
go_first()
mainframe.bind_all('<Key>', key_callback )
go(0)
def parse_args():
parser = argparse.ArgumentParser(prog=__prog__, description=__desc__)
parser.add_argument('csv_fp', help="Fully qualified path to Montage `source.csv` file.")
parser.add_argument('--verbose', '-v', help="Report match info on console", action='count')
parser.add_argument('--version', action="version", version='%(prog)s {}'.format(__version__))
opts = parser.parse_args()
global fn, data_root
data_root = os.path.split(opts.csv_fp)[0]
fn = os.path.split(opts.csv_fp)[1]
return opts
if __name__=="__main__":
opts = parse_args()
if opts.verbose >= 2:
logging_level = logging.DEBUG
if opts.verbose == 1:
logging_level = logging.INFO
else:
logging_level = logging.WARN
# Then just go ahead and put it in DEBUG anyway...
logging.basicConfig(level=logging.DEBUG)
# logging.debug(logging_level)
load_data()
# logging.debug(items)
make_ui()
root.mainloop()