-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_statistics.py
465 lines (395 loc) · 16.4 KB
/
cli_statistics.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import click
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from datasets import load_dataset
from flask.cli import AppGroup
from .models import (
AnnotationSession,
DeletedAnnotationSession,
DeletedQuestion,
Paragraph,
Question,
Status,
)
statistics_cli = AppGroup("statistics")
plt.style.use("seaborn-notebook")
DOMAINS = ["news", "review", "books"]
LANGUAGES = {
"de": "german",
"zh": "chinese",
"tr": "turkish",
}
MAX_WORDS_PER_LANGUAGE = {
"en": 10,
"de": 10,
"tr": 9,
"zh": 22, # character count without spaces and punctuation
}
@statistics_cli.command("table_annotator_question_quality")
def create_table_annotator_question_quality():
"""
Create a table with the number of annotators, questions, questions checked and questions quality
"""
annotation_sessions = AnnotationSession.query.all()
deleted_annotation_sessions = DeletedAnnotationSession.query.all()
all_good_annotators = set(
[
annotation_session.annotator
for annotation_session in annotation_sessions
if annotation_session.annotator.quality_checked and annotation_session.annotator.good_quality
]
)
all_bad_quality_annotators = set(
[annotation_session.annotator for annotation_session in deleted_annotation_sessions]
)
print(" \\begin{tabular}{l|cc|ccc}")
print(" \\toprule")
print(
" \\multicolumn{1}{c|}{Language} & \\multicolumn{2}{c|}{Annotators} &"
" \\multicolumn{3}{c}{Questions} \\\\"
)
print(" ~ & Kept & Rejected & Kept & Rejected & Checked\\\\")
for language in LANGUAGES:
good_annotators = []
bad_annotators = []
good_annotators = [
annotator
for annotator in all_good_annotators
if annotator.annotation_session_list[0].paragraph_list[0].language == language
]
bad_annotators = [
annotator
for annotator in all_bad_quality_annotators
if Paragraph.query.filter(Paragraph.id == annotator.deleted_question_list[0].paragraph_id).first().language
== language
]
questions_kept = Question.query.filter(Question.paragraph.has(language=language)).count()
questions_rejected = DeletedQuestion.query.join(Paragraph).filter(Paragraph.language == language).count()
questions_checked = 10 * (len(good_annotators) + len(bad_annotators))
if questions_kept + questions_rejected == 0:
questions_checked_percentage = 0
else:
questions_checked_percentage = questions_checked / (questions_kept + questions_rejected) * 100
print(" \\midrule")
print(
f" {LANGUAGES[language]} & {len(good_annotators)} &"
f" {len(bad_annotators)} & {questions_kept} & {questions_rejected} &"
f" {questions_checked} ({questions_checked_percentage:.2f}\%) \\\\ "
)
print(" \\bottomrule")
print(" \end{tabular}")
@statistics_cli.command("annotator_statistic")
@click.argument("study_id", nargs=1, required=False)
def create_annotator_statistics(study_id=None):
"""
Create plots showing the time distribution of annotators for the tutorial and the paragraphs
"""
if study_id is not None:
annotation_sessions = AnnotationSession.query.filter_by(study_id=study_id).all()
deleted_annotation_sessions = DeletedAnnotationSession.query.filter_by(study_id=study_id).all()
else:
annotation_sessions = AnnotationSession.query.all()
deleted_annotation_sessions = DeletedAnnotationSession.query.all()
annotators = set([annotation_session.annotator for annotation_session in annotation_sessions])
bad_quality_annotators = set(
[annotation_session.annotator_id for annotation_session in deleted_annotation_sessions]
)
tutorial_times = []
for annotator in annotators:
if annotator.tutorial_end_time is not None and annotator.tutorial_start_time is not None:
time_delta = annotator.tutorial_end_time - annotator.tutorial_start_time
if time_delta.seconds < 1200:
tutorial_times.append(time_delta.seconds)
paragraph_times = []
paragraphs = [
paragraph for annotation_session in annotation_sessions for paragraph in annotation_session.paragraph_list
]
for paragraph in paragraphs:
if paragraph.end_time is not None and paragraph.start_time is not None:
time_delta = paragraph.end_time - paragraph.start_time
if time_delta.seconds < 600:
paragraph_times.append(time_delta.seconds)
quality_checked_annotators = [annotator for annotator in annotators if annotator.quality_checked]
if len(quality_checked_annotators) > 0:
percentage = (
len(bad_quality_annotators) / (len(quality_checked_annotators) + len(bad_quality_annotators)) * 100.0
)
print(f"{percentage}% of annotators have bad quality")
# Plotting
plt.subplots(2, 1, tight_layout=True)
plt.subplot(2, 1, 1)
plt.hist(tutorial_times, bins=20, label="Histogram")
plt.axvline(
x=np.median(tutorial_times),
color="red",
linestyle="--",
label=f"Median: {np.median(tutorial_times)}s",
)
plt.axvline(
x=np.mean(tutorial_times),
color="yellow",
linestyle="--",
label=f"Mean: {np.mean(tutorial_times):.1f}s",
)
plt.title("Time taken for the tutorial")
plt.xlabel("Time in seconds")
plt.ylabel("Number of annotators")
plt.legend()
plt.subplot(2, 1, 2)
plt.hist(paragraph_times, bins=30, label="Histogram")
plt.axvline(
x=np.median(paragraph_times),
color="red",
linestyle="--",
label=f"Median: {np.median(paragraph_times)}s",
)
plt.axvline(
x=np.mean(paragraph_times),
color="yellow",
linestyle="--",
label=f"Mean: {np.mean(paragraph_times):.1f}s",
)
plt.title("Time per paragraph taken")
plt.xlabel("Time in seconds")
plt.ylabel("Number of paragraphs")
plt.legend()
plt.show()
print(
"Median time tutorial + 11 * median time per"
f" paragraph:{(np.median(tutorial_times) + 11 * np.median(paragraph_times) ) / 60} minutes"
)
print("Median time 12 * median time per" f" paragraph:{12 * np.median(paragraph_times) / 60} minutes")
@statistics_cli.command("table_questions_finished_overview")
def table_questions_finished_overview():
"""
Provides an overview of the amount of questions finished per domain per language
"""
df = pd.DataFrame(
columns=[domain.capitalize() for domain in DOMAINS],
index=[language.capitalize() for language in LANGUAGES.values()],
)
df_unfinished = pd.DataFrame(
columns=[domain.capitalize() for domain in DOMAINS],
index=[language.capitalize() for language in LANGUAGES.values()],
)
for language in LANGUAGES:
for domain in DOMAINS:
df.loc[LANGUAGES[language].capitalize(), domain.capitalize()] = Paragraph.query.filter_by(
domain=domain, language=language, status=Status.finished
).count()
all_finished_paragraphs = Paragraph.query.filter_by(
domain=domain, language=language, status=Status.finished
).all()
unfinished_count = 0
for paragraph in all_finished_paragraphs:
annotation_session = AnnotationSession.query.filter_by(id=paragraph.annotation_session_id).first()
if annotation_session is not None and not annotation_session.finished:
unfinished_count += 1
df_unfinished.loc[LANGUAGES[language].capitalize(), domain.capitalize()] = unfinished_count
print(f"Paragraphs:\n{df}\n")
# to keep track of the current progress during annotation
print(f"from unfinished sessions:\n{df_unfinished}\n")
# Questions
df_questions = pd.DataFrame(
columns=[domain.capitalize() for domain in DOMAINS],
index=[language.capitalize() for language in LANGUAGES.values()],
)
for language in LANGUAGES:
for domain in DOMAINS:
# Question has only paragraph_id and no information about domain and language
# Thus we must go through the paragraphs
unfiltered_questions = (
Question.query.join(Paragraph)
.filter(
Paragraph.domain == domain,
Paragraph.language == language,
)
.all()
)
# Filter with max word limit
count = 0
# This filter has no effect, because we already filter when the questions get submitted
for question in unfiltered_questions:
# don't include questions where the answers are too long
if question.answerable:
if question.paragraph.language == "zh":
if (
len(
question.answer_text.replace(" ", "")
.replace(",", "")
.replace(",", "")
.replace(",", "")
.replace("。", "")
.replace("、", "")
)
> MAX_WORDS_PER_LANGUAGE["zh"]
):
continue
elif len(question.answer_text.split(" ")) > MAX_WORDS_PER_LANGUAGE[question.paragraph.language]:
continue
count += 1
df_questions.loc[LANGUAGES[language].capitalize(), domain.capitalize()] = count
print("Questions:")
print(df_questions)
@statistics_cli.command("table_question_statistics")
def create_table_question_statistics():
statistics = {language: {} for language in LANGUAGES}
for language in LANGUAGES:
questions = Question.query.filter(Question.paragraph.has(language=language)).all()
answer_text_words = [len(question.answer_text.split(" ")) for question in questions]
statistics[language]["mean words in answer"] = np.mean(answer_text_words)
statistics[language]["median words in answer"] = np.median(answer_text_words)
statistics[language]["answer_text_words"] = answer_text_words
# Plot histograms
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
axes = axes.flatten()
for i, ax in enumerate(axes):
if i < len(list(LANGUAGES)):
language = list(LANGUAGES)[i]
ax.hist(statistics[language]["answer_text_words"], bins=20)
ax.set_title(LANGUAGES[language])
ax.set_xlabel("Number of words")
ax.set_ylabel("Number of questions")
ax.axvline(
x=statistics[language]["median words in answer"],
color="red",
linestyle="--",
label=f"Median: {statistics[language]['median words in answer']}",
)
ax.legend()
fig.tight_layout()
plt.show()
for language in LANGUAGES:
print(f"{LANGUAGES[language]}:")
print("Median words: ", statistics[language]["median words in answer"])
print("Mean words: ", statistics[language]["mean words in answer"])
print("")
@statistics_cli.command("xquad_statistics")
def xquad_statistics():
percentile = 97
for language in ["en", "de", "tr"]:
xquad_dataset = load_dataset("xquad", f"xquad.{language}")
xquad_answer_words = [
len(question["answers"]["text"][0].split(" ")) for question in xquad_dataset["validation"]
]
percentile_value = np.percentile(xquad_answer_words, percentile)
print(f"{language}: {percentile} percentile {percentile_value}")
# For chinese: split by character
xquad_dataset = load_dataset("xquad", "xquad.zh")
xquad_answer_words = [
len(
question["answers"]["text"][0]
.replace(" ", "")
.replace(",", "")
.replace(",", "")
.replace(",", "")
.replace("。", "")
.replace("、", "")
)
for question in xquad_dataset["validation"]
]
percentile_value = np.percentile(xquad_answer_words, percentile)
print(f"chinese: {percentile} percentile {percentile_value}")
@statistics_cli.command("compare_german_with_xquad")
def create_comparison_german_with_xquad():
"""
Compares the German questions-answer pairs with the XQuAD questions regarding the amount of words in the question and answer
"""
for language in ["de", "zh"]: # TODO: add "zh" when we have more data
# Load XQuAD questions
xquad_dataset = load_dataset("xquad", "xquad.de")
# Load German questions with domains: news, books or review
german_questions = (
Question.query.join(Question.paragraph)
.filter(
Paragraph.language == "de",
Paragraph.domain.in_(["news", "books", "review"]),
)
.filter(Question.answerable)
.all()
)
# Get amount of words in question and answer
xquad_question_words = [len(question["question"].split(" ")) for question in xquad_dataset["validation"]]
xquad_answer_words = [
len(question["answers"]["text"][0].split(" ")) for question in xquad_dataset["validation"]
]
german_question_words = [len(question.question.split(" ")) for question in german_questions]
german_answer_words = [len(question.answer_text.split(" ")) for question in german_questions]
print(
"Percentage of German answers with more than 8 words:"
f" {sum([1 for answer in german_answer_words if answer > 10]) / len(german_answer_words) * 100}"
)
# Plot histograms
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes = axes.flatten()
axes[0].hist(xquad_question_words, bins=15)
axes[0].set_title("XQuAD questions")
axes[0].set_xlabel("Number of words")
axes[0].set_ylabel("Number of questions")
axes[0].axvline(
x=np.median(xquad_question_words),
color="red",
linestyle="--",
label=f"Median: {np.median(xquad_question_words)}",
)
axes[0].axvline(
x=np.mean(xquad_question_words),
color="green",
linestyle="--",
label=f"Mean: {np.mean(xquad_question_words)}",
)
axes[0].legend()
axes[1].hist(xquad_answer_words, bins=20)
axes[1].set_title("XQuAD answers")
axes[1].set_xlabel("Number of words")
axes[1].set_ylabel("Number of questions")
axes[1].axvline(
x=np.median(xquad_answer_words),
color="red",
linestyle="--",
label=f"Median: {np.median(xquad_answer_words)}",
)
axes[1].axvline(
x=np.mean(xquad_answer_words),
color="green",
linestyle="--",
label=f"Mean: {np.mean(xquad_answer_words)}",
)
axes[1].legend()
axes[2].hist(german_question_words, bins=15)
axes[2].set_title("M2QA German questions (only answerable)")
axes[2].set_xlabel("Number of words")
axes[2].set_ylabel("Number of questions")
axes[2].axvline(
x=np.median(german_question_words),
color="red",
linestyle="--",
label=f"Median: {np.median(german_question_words)}",
)
axes[2].axvline(
x=np.mean(german_question_words),
color="green",
linestyle="--",
label=f"Mean: {np.mean(german_question_words)}",
)
axes[2].legend()
axes[3].hist(german_answer_words, bins=20)
axes[3].set_title("M2QA German answers (only answerable)")
axes[3].set_xlabel("Number of words")
axes[3].set_ylabel("Number of questions")
axes[3].axvline(
x=np.median(german_answer_words),
color="red",
linestyle="--",
label=f"Median: {np.median(german_answer_words)}",
)
axes[3].axvline(
x=np.mean(german_answer_words),
color="green",
linestyle="--",
label=f"Mean: {np.mean(german_answer_words)}",
)
axes[3].legend()
fig.tight_layout()
plt.show()