-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
443 lines (371 loc) · 24.6 KB
/
app.py
File metadata and controls
443 lines (371 loc) · 24.6 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
###############################################################################
#
# COPYRIGHT NOTICE
# Mark O. Hatfield Clinical Research Center
# National Institutes of Health
# United States Department of Health and Human Services
#
# This software was developed and is owned by the National Institutes of
# Health Clinical Center (NIHCC), an agency of the United States Department
# of Health and Human Services, which is making the software available to the
# public for any commercial or non-commercial purpose under the following
# open-source BSD license.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# (1) Redistributions of source code must retain this copyright
# notice, this list of conditions and the following disclaimer.
#
# (2) Redistributions in binary form must reproduce this copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# (3) Neither the names of the National Institutes of Health Clinical
# Center, the National Institutes of Health, the U.S. Department of
# Health and Human Services, nor the names of any of the software
# developers may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# (4) Please acknowledge NIHCC as the source of this software by including
# the phrase "Courtesy of the U.S. National Institutes of Health Clinical
# Center"or "Source: U.S. National Institutes of Health Clinical Center."
#
# THIS SOFTWARE IS PROVIDED BY THE U.S. GOVERNMENT AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.
#
# You are under no obligation whatsoever to provide any bug fixes,
# patches, or upgrades to the features, functionality or performance of
# the source code ("Enhancements") to anyone; however, if you choose to
# make your Enhancements available either publicly, or directly to
# the National Institutes of Health Clinical Center, without imposing a
# separate written license agreement for such Enhancements, then you hereby
# grant the following license: a non-exclusive, royalty-free perpetual license
# to install, use, modify, prepare derivative works, incorporate into
# other computer software, distribute, and sublicense such Enhancements or
# derivative works thereof, in binary and source code form.
#
###############################################################################
import os, sys
import curses
import locale
import warnings
from itertools import combinations
from load_data import settings
from menu import menu_system
from validation import validation_menu_functions
from error_checks import error_checks_menu_functions
from stats import statistics_menu_functions
from evaluation import evaluation_menu_functions
from discrepancy_analysis import discrepancy_analysis_menu_functions
warnings.filterwarnings("ignore")
sys.setrecursionlimit(10**6)
def run_app(stdscr):
if len(list(settings.corpus.keys())) == 1:
file_names = list(settings.corpus.keys())
else:
file_names = ['corpus'] + list(settings.corpus.keys())
if len(list(set([n for f, c in settings.corpus.items()
for a, b in c.items()
for n in b['annotation_sets']]))) == 1:
set_names = list(set([n for f, c in settings.corpus.items()
for a, b in c.items()
for n in b['annotation_sets']]))
else:
set_names = ['all_sets'] + list(set([n for f, c in settings.corpus.items()
for a, b in c.items()
for n in b['annotation_sets']]))
if len(list(set([a for k, v in settings.corpus.items()
for a in v.keys()]))) == 1:
annotators = list(set([a for k, v in settings.corpus.items()
for a in v.keys()]))
else:
annotators = ['team'] + list(set([a for k, v in settings.corpus.items()
for a in v.keys()]))
if len(['{}-{}'.format(x, y) for x, y in combinations(annotators[1:], 2)]) == 1:
annotator_pairs = ['{}-{}'.format(x, y) for x, y in combinations(annotators[1:], 2)]
else:
annotator_pairs = ['team'] + ['{}-{}'.format(x, y) for x, y in combinations(annotators[1:], 2)]
if len(settings.schema.get_entity_names()) == 1:
annotation_types = settings.schema.get_entity_names()
else:
annotation_types = ['all_types'] + settings.schema.get_entity_names()
file_name_options = menu_system.MenuToolBarOptions(option_rack=file_names)
set_name_options = menu_system.MenuToolBarOptions(option_rack=set_names)
if len(file_names) > 1:
file_name_options_no_general = menu_system.MenuToolBarOptions(option_rack=file_names[1:])
else:
file_name_options_no_general = menu_system.MenuToolBarOptions(option_rack=file_names)
if len(set_names) > 1:
set_name_options_no_general = menu_system.MenuToolBarOptions(option_rack=set_names[1:])
else:
set_name_options_no_general = menu_system.MenuToolBarOptions(option_rack=set_names)
annotator_options = menu_system.MenuToolBarOptions(option_rack=annotators)
if len(annotators) > 1:
annotator_options_no_general = menu_system.MenuToolBarOptions(option_rack=annotators[1:])
else:
annotator_options_no_general = menu_system.MenuToolBarOptions(option_rack=annotators)
annotator_pair_options = menu_system.MenuToolBarOptions(option_rack=annotator_pairs)
annotation_types_options = menu_system.MenuToolBarOptions(option_rack=annotation_types)
if len(annotation_types) > 1:
annotation_types_options_no_general = menu_system.MenuToolBarOptions(option_rack=annotation_types[1:])
else:
annotation_types_options_no_general = menu_system.MenuToolBarOptions(option_rack=annotation_types)
measure_options = menu_system.MenuToolBarOptions(option_rack=['Average', 'Strict', 'Lenient'])
error_checks_tool_bar = menu_system.MenuToolBar(title = 'Error Checks Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(annotator_pair_options.title,
annotator_pair_options.return_options)],
slots = ['File', 'Annotator Pair'])
corpus_viewer_tool_bar = menu_system.MenuToolBar(title = 'Corpus Viewer Tool Bar',
options=[menu_system.MenuAction(file_name_options_no_general.title,
file_name_options_no_general.return_options),
menu_system.MenuAction(annotator_options_no_general.title,
annotator_options_no_general.return_options),
menu_system.MenuAction(set_name_options_no_general.title,
set_name_options_no_general.return_options),
menu_system.MenuAction(annotation_types_options_no_general.title,
annotation_types_options_no_general.return_options)],
slots = ['File', 'Annotator', 'Set Name', 'Annotation Type'])
validation_tool_bar = menu_system.MenuToolBar(title = 'Validation Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(set_name_options.title,
set_name_options.return_options),
menu_system.MenuAction(annotator_options.title,
annotator_options.return_options),
menu_system.MenuAction(annotation_types_options.title,
annotation_types_options.return_options)],
slots = ['File', 'Set Name', 'Annotator', 'Entity'])
entity_lvl_tool_bar = menu_system.MenuToolBar(title = 'Entity Level Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(set_name_options.title,
set_name_options.return_options),
menu_system.MenuAction(annotator_options_no_general.title,
annotator_options_no_general.return_options),
menu_system.MenuAction(annotator_options_no_general.title,
annotator_options_no_general.return_options),
menu_system.MenuAction(annotation_types_options.title,
annotation_types_options.return_options),
menu_system.MenuAction(measure_options.title,
measure_options.return_options)],
slots = ['File', 'Set Name', 'Key', 'Response', 'Entity', 'Measure'])
token_lvl_tool_bar = menu_system.MenuToolBar(title = 'Token Level Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(set_name_options.title,
set_name_options.return_options),
menu_system.MenuAction(annotator_options_no_general.title,
annotator_options_no_general.return_options),
menu_system.MenuAction(annotator_options_no_general.title,
annotator_options_no_general.return_options),
menu_system.MenuAction(annotation_types_options.title,
annotation_types_options.return_options)],
slots = ['File', 'Set Name', 'Key', 'Response', 'Entity'])
cohens_kappa_tool_bar = menu_system.MenuToolBar(title = 'Cohens Kappa Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(set_name_options.title,
set_name_options.return_options),
menu_system.MenuAction(annotator_options.title,
annotator_options.return_options),
menu_system.MenuAction(annotator_options.title,
annotator_options.return_options)],
slots = ['File', 'Set Name', 'Key', 'Response'])
discrepancy_tool_bar = menu_system.MenuToolBar(title = 'Discrepancy Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(annotator_pair_options.title,
annotator_pair_options.return_options)],
slots = ['File', 'Annotator Pair'])
statistics_tool_bar = menu_system.MenuToolBar(title = 'Validation Menu Tool Bar',
options=[menu_system.MenuAction(file_name_options.title,
file_name_options.return_options),
menu_system.MenuAction(set_name_options.title,
set_name_options.return_options),
menu_system.MenuAction(annotator_options.title,
annotator_options.return_options),
menu_system.MenuAction(annotation_types_options.title,
annotation_types_options.return_options)],
slots = ['File', 'Set Name', 'Annotator', 'Entity'])
error_checks_menu = menu_system.Menu(title='Document Validations',
options=[menu_system.MenuAction('Text Differences',
error_checks_menu_functions.check_text_differences),
menu_system.MenuAction('Set Name Differences',
error_checks_menu_functions.check_set_name_differences,),
menu_system.MenuAction('Generate Report',
error_checks_menu_functions.generate_error_checks_report)],
tool_bar=error_checks_tool_bar,
screen=stdscr,
inherit_settings=True)
if [y for x, y in settings.schema.entities.items() if y.is_sub_entity()]:
validation_menu = menu_system.Menu(title='Annotation Validations',
options=[menu_system.MenuAction('Annotation Overlaps',
validation_menu_functions.validate_overlaps),
menu_system.MenuAction('Subentity Boundaries',
validation_menu_functions.validate_subentity_boundaries),
menu_system.MenuAction('Annotation Boundaries', validation_menu_functions.validate_annotation_boundaries),
menu_system.MenuAction('Zero Length Annotations', validation_menu_functions.validate_annotation_zero_length),
menu_system.MenuAction('Negative Length Annotations', validation_menu_functions.validate_annotation_negative_length),
menu_system.MenuAction('Document Scope',
validation_menu_functions.validate_annotation_scope),
menu_system.MenuAction('Validate Schema',
validation_menu_functions.validate_schema_values),
menu_system.MenuAction('Generate Report',
validation_menu_functions.generate_validation_report)],
tool_bar=validation_tool_bar,
screen=stdscr,
inherit_settings=True)
else:
validation_menu = menu_system.Menu(title='Annotation Validations',
options=[menu_system.MenuAction('Annotation Overlaps',
validation_menu_functions.validate_overlaps),
menu_system.MenuAction('Annotation Boundaries', validation_menu_functions.validate_annotation_boundaries),
menu_system.MenuAction('Zero Length Annotations', validation_menu_functions.validate_annotation_zero_length),
menu_system.MenuAction('Negative Length Annotations', validation_menu_functions.validate_annotation_negative_length),
menu_system.MenuAction('Document Scope',
validation_menu_functions.validate_annotation_scope),
menu_system.MenuAction('Validate Schema',
validation_menu_functions.validate_schema_values),
menu_system.MenuAction('Generate Report',
validation_menu_functions.generate_validation_report)],
tool_bar=validation_tool_bar,
screen=stdscr,
inherit_settings=True)
if settings.task == 'sequence_labelling':
entity_level_menu = menu_system.Menu(title='Entity Level',
options=[menu_system.MenuAction('Evaluate', evaluation_menu_functions.entity_eval),
menu_system.MenuAction('Generate Report', evaluation_menu_functions.generate_entity_level_report)],
tool_bar = entity_lvl_tool_bar,
screen=stdscr,
inherit_settings=True)
token_level_menu = menu_system.Menu(title='Token Level',
options=[menu_system.MenuAction('Evaluate',
evaluation_menu_functions.token_eval),
menu_system.MenuAction('Generate Report',
evaluation_menu_functions.generate_token_level_report)],
tool_bar = token_lvl_tool_bar,
screen=stdscr,
inherit_settings=True)
evaluation_menu = menu_system.Menu(title='Evaluation',
options=[entity_level_menu, token_level_menu],
screen=stdscr,
inherit_settings=True)
if settings.task == 'classification':
cohens_kappa_menu = menu_system.Menu(title='Cohens Kappa',
options=[menu_system.MenuAction('Evaluate', evaluation_menu_functions.cohen_kappa_eval),
menu_system.MenuAction('Generate Report')],
tool_bar = cohens_kappa_tool_bar,
screen=stdscr,
inherit_settings=True)
evaluation_menu = menu_system.Menu(title='Evaluation',
options=[cohens_kappa_menu],
screen=stdscr,
inherit_settings=True)
discrepancy_menu = menu_system.Menu(title='Discrepancy Analysis',
options=[menu_system.MenuAction('Compare', discrepancy_analysis_menu_functions.compare_annotations),
menu_system.MenuAction('Generate Report', discrepancy_analysis_menu_functions.generate_discrepancy_report)],
tool_bar = discrepancy_tool_bar,
screen=stdscr,
inherit_settings=True)
if [e for n, e in settings.schema.entities.items() if e.features]:
statistics_menu = menu_system.Menu(title='Statistics',
options=[menu_system.MenuAction('Entity Distribution',
statistics_menu_functions.count_entities),
menu_system.MenuAction('Entity/Features Distribution',
statistics_menu_functions.count_entities_with_features),
menu_system.MenuAction('Entity Token Length Distribution',
statistics_menu_functions.entity_token_stats),
menu_system.MenuAction('Entity/Features Token Length Distribution',
statistics_menu_functions.entity_with_features_token_stats),
menu_system.MenuAction('Generate Report',
statistics_menu_functions.generate_statistics_report)],
tool_bar = statistics_tool_bar,
screen=stdscr,
inherit_settings=True)
else:
statistics_menu = menu_system.Menu(title='Statistics',
options=[menu_system.MenuAction('Entity Distribution',
statistics_menu_functions.count_entities),
menu_system.MenuAction('Entity Token Length Distribution',
statistics_menu_functions.entity_token_stats),
menu_system.MenuAction('Generate Report',
statistics_menu_functions.generate_statistics_report)],
tool_bar = statistics_tool_bar,
screen=stdscr,
inherit_settings=True)
help_menu = menu_system.HelpMenu(title='Help',
screen=stdscr,
inherit_settings=True)
schema_viewer = menu_system.SchemaMenu(title='Annotation Schema',
options=[],
screen=stdscr,
inherit_settings=True)
corpus_viewer = menu_system.CorpusViewerMenu(title='Corpus Viewer',
options=[],
screen=stdscr,
tool_bar = corpus_viewer_tool_bar,
inherit_settings=True)
main_menu = menu_system.Menu(title='QA4IE Main Menu',
options=[error_checks_menu,
validation_menu,
statistics_menu,
evaluation_menu,
discrepancy_menu,
menu_system.MenuAction('Generate Reports', generate_all_reports),
corpus_viewer,
schema_viewer,
help_menu],
screen=stdscr)
try:
try:
curses.noecho()
curses.curs_set(0)
main_menu.run_menu()
except curses.error as e:
print(e)
finally:
curses.nocbreak(); stdscr.keypad(0); curses.echo(); curses.curs_set(1)
curses.endwin()
def generate_all_reports():
try:
error_checks_menu_functions.generate_error_checks_report()
validation_menu_functions.generate_validation_report()
statistics_menu_functions.generate_statistics_report()
evaluation_menu_functions.generate_token_level_report()
evaluation_menu_functions.generate_entity_level_report()
discrepancy_analysis_menu_functions.generate_discrepancy_report()
return 'reports generated in {}'.format(settings.output_dir)
except BlockingIOError as e:
return '{}'.format(str(e))
def main():
try:
if len(sys.argv) == 2:
path = sys.argv[1]
else:
path = ' '.join([x for x in sys.argv[1:]])
settings.init(path)
except IndexError as e:
print('Usage: python {} <path_to_config_file>'.format(__file__))
else:
try:
curses.wrapper(run_app)
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(0)
except:
# curses.nocbreak()
# curses.keypad(0)
# curses.echo()
# curses.endwin()
#curses.wrapper(run_app)
raise
if __name__ == '__main__':
locale.setlocale(locale.LC_ALL, '')
encoding = locale.getpreferredencoding()
main()