-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.py
773 lines (589 loc) · 39.8 KB
/
experiments.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
###############
# CRF EXPERIMENTS #
###############
def elegiac_heatmap(self):
column_names = ["predictor", "predictee", "score"]
df_long = pd.DataFrame(columns = column_names)
df_short = pd.DataFrame(columns = column_names)
df_elision = pd.DataFrame(columns = column_names)
ovid_elegiac_df = util.merge_sequence_label_lists(util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_elegiac'), 'OV'), util.cf.get('Pickle', 'df_crf_path_elegiac'))
tib_elegiac_df = util.merge_sequence_label_lists(util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_elegiac'), 'TIB'), util.cf.get('Pickle', 'df_crf_path_elegiac'))
prop_elegiac_df = util.merge_sequence_label_lists(util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_elegiac'), 'PROP'), util.cf.get('Pickle', 'df_crf_path_elegiac'))
verg_df = util.Pickle_read(util.cf.get('Pickle', 'df_crf_path_hexameter'), 'VERG-aene.pickle')
hexameter_df = util.merge_sequence_label_lists(util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_hexameter'), '.pickle'), util.cf.get('Pickle', 'df_crf_path_hexameter'))
elegiac_df = util.merge_sequence_label_lists(util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_elegiac'), '.pickle'), util.cf.get('Pickle', 'df_crf_path_elegiac'))
# First, use Aneid to predict Ovid and Tibullus
predictor_texts = [('verg',verg_df), ('hex',hexameter_df), ('eleg',elegiac_df)]
predictee_texts = [('ovid',ovid_elegiac_df), ('tib',tib_elegiac_df), ('prop',prop_elegiac_df)]
for predictor_text in predictor_texts:
predictor_X, predictor_y = self.convert_text_to_feature_sets(predictor_text[1])
crf_model = self.fit_model(predictor_X, predictor_y)
for predictee_text in predictee_texts:
predictee_X, predictee_y = self.convert_text_to_feature_sets(predictee_text[1])
# Using the predictor model, predict for the predictee test set
result = self.predict_model(crf_model, predictee_X, predictee_y)
predictor = predictor_text[0] # get names
predictee = predictee_text[0]
print(predictor, predictee)
# exit(0)
score_long = float(round(result['long']['f1-score'] * 100,1)) # save the score
score_short = float(round(result['short']['f1-score'] * 100,1)) # save the score
score_elision = float(round(result['elision']['f1-score'] * 100,1)) # save the score
# Add score to the dataframe for our heatmap
new_line_long = {'predictor': predictor, 'predictee': predictee, 'score': score_long}
new_line_short = {'predictor': predictor, 'predictee': predictee, 'score': score_short}
new_line_elision = {'predictor': predictor, 'predictee': predictee, 'score': score_elision}
df_long = df_long.append(new_line_long, ignore_index=True)
df_short = df_short.append(new_line_short, ignore_index=True)
df_elision = df_elision.append(new_line_elision, ignore_index=True)
# pivot the dataframe to be usable with the seaborn heatmap
heatmap_data = pd.pivot_table(df_long, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- long syllables',
filename = 'confusionmatrix_elegiac_long.png')
heatmap_data = pd.pivot_table(df_short, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- short syllables',
filename = 'confusionmatrix_elegiac_short.png')
heatmap_data = pd.pivot_table(df_elision, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- elided syllables',
filename = 'confusionmatrix_elegiac_elision.png')
# exit(0)
def hexameter_heatmap(self):
############################
# CREATE HEXAMETER HEATMAP #
############################
column_names = ["predictor", "predictee", "score"]
df_long = pd.DataFrame(columns = column_names)
df_short = pd.DataFrame(columns = column_names)
df_elision = pd.DataFrame(columns = column_names)
predictor_texts = util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_hexameter'), '.pickle')
predictee_texts = util.Create_files_list(util.cf.get('Pickle', 'df_crf_path_hexameter'), '.pickle')
# Let every text be the predictor once
for predictor_text in predictor_texts:
predictor_df = util.Pickle_read(util.cf.get('Pickle', 'df_crf_path_hexameter'), predictor_text)
predictor_X, predictor_y = self.convert_text_to_feature_sets(predictor_df)
crf_model = self.fit_model(predictor_X, predictor_y)
# For every predictor, get results for every predictee. This includes predicting itself
for predictee_text in predictee_texts:
predictee_df = util.Pickle_read(util.cf.get('Pickle', 'df_crf_path_hexameter'), predictee_text)
predictee_X, predictee_y = self.convert_text_to_feature_sets(predictee_df)
# Using the predictor model, predict for the predictee test set
result = self.predict_model(crf_model, predictee_X, predictee_y)
predictor = predictor_text.split('-')[0].capitalize() # get names
predictee = predictee_text.split('-')[0].capitalize()
score_long = float(round(result['long']['f1-score'] * 100,1)) # save the score
score_short = float(round(result['short']['f1-score'] * 100,1)) # save the score
score_elision = float(round(result['elision']['f1-score'] * 100,1)) # save the score
# Add score to the dataframe for our heatmap
new_line_long = {'predictor': predictor, 'predictee': predictee, 'score': score_long}
new_line_short = {'predictor': predictor, 'predictee': predictee, 'score': score_short}
new_line_elision = {'predictor': predictor, 'predictee': predictee, 'score': score_elision}
df_long = df_long.append(new_line_long, ignore_index=True)
df_short = df_short.append(new_line_short, ignore_index=True)
df_elision = df_elision.append(new_line_elision, ignore_index=True)
# df_long.to_csv('./csv/long_1_char_only.csv')
# df_short.to_csv('./csv/short_1_char_only.csv')
# df_elision.to_csv('./csv/elision_1_char_only.csv')
# pivot the dataframe to be usable with the seaborn heatmap
heatmap_data = pd.pivot_table(df_long, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- long syllables',
filename = 'confusionmatrix_hexameter_long.png')
heatmap_data = pd.pivot_table(df_short, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- short syllables',
filename = 'confusionmatrix_hexameter_short.png')
heatmap_data = pd.pivot_table(df_elision, values='score', index=['predictor'], columns='predictee')
self.create_heatmap(dataframe = heatmap_data,
xlabel = 'predictee',
ylabel = 'predictor',
title = 'Confusion matrix -- elided syllables',
filename = 'confusionmatrix_hexameter_elision.png')
def create_heatmap(self, dataframe, xlabel, ylabel, title, filename):
# Simple function to create a heatmap
sn.set(font_scale=1.4)
sn.heatmap(dataframe, annot=True, fmt='g', annot_kws={"size": 16}, cmap='Blues')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.savefig(filename, bbox_inches='tight')
plt.clf()
##################################
# LSTM LSTM LSTM
def run_idx_lstm_single_text(self, text,
do_evaluate=True, do_metric_report=True, do_confusion_matrix=True, print_stats=True):
# Retrieve meta data
all_text_syllables = self.retrieve_syllables_from_sequence_label_list(text)
max_sentence_length = self.retrieve_max_sentence_length(text)
unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING) # needs to be sorted for word2idx consistency!
# # Create dictionary
word2idx = {w: i for i, w in enumerate(unique_syllables)}
label2idx = {t: i for i, t in enumerate(self.LABELS)}
# now we map the sentences and labels to a sequence of numbers
X = [[word2idx[w[0]] for w in s] for s in text] # key 0 are labels
y = [[label2idx[w[1]] for w in s] for s in text] # key 1 are labels
# and then (post)pad the sequences using the PADDING label.
X = pad_sequences(maxlen=max_sentence_length, sequences=X, padding="post", value=word2idx[self.PADDING]) # value is our padding key
y = pad_sequences(maxlen=max_sentence_length, sequences=y, padding="post", value=label2idx[self.PADDING])
# for training the network we also need to change the labels to categorial.
y = np.array([to_categorical(i, num_classes=len(self.LABELS)) for i in y])
# we split in train and test set.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.split_size)
# get the model we want
model = self.get_model( max_len = max_sentence_length,
num_syllables = len(unique_syllables),
num_labels = len(self.LABELS),
X = X_train,
y = y_train,
epochs = FLAGS.epochs,
create_model = FLAGS.create_model,
save_model = FLAGS.save_model)
if do_evaluate:
loss, accuracy = self.evaluate_model(model, X_test, y_test)
result_string = 'RESULT: loss -> {0}, accuracy -> {1}. Length of test set: {2}\n'.format(loss, accuracy, len(y_test))
print(result_string)
# return loss, accuracy, len(y_train), len(y_test)
if do_metric_report:
metrics_report = self.create_metrics_report(model, X_test, y_test, output_dict=False)
print(metrics_report)
if do_confusion_matrix:
confusion_matrix = self.create_confusion_matrix(model, X_test, y_test)
df_confusion_matrix = pd.DataFrame(confusion_matrix, index = ['long', 'short', 'elision', 'space', 'padding'],
columns = ['long', 'short', 'elision', 'space', 'padding'])
# Drop the padding labels, as we don't need them (lstm scans them without confusion): delete both row and column
df_confusion_matrix = df_confusion_matrix.drop('padding')
df_confusion_matrix = df_confusion_matrix.drop(columns=['padding', 'space'])
df_confusion_matrix = df_confusion_matrix.drop('space')
# df_confusion_matrix = df_confusion_matrix.drop(columns=['padding'])
util.create_heatmap(dataframe = df_confusion_matrix,
xlabel = 'TRUTH',
ylabel = 'PREDICTED',
title = 'CONFUSION MATRIX',
filename = 'confusion_matrix_lstm_single_text',
# vmax = 500
)
#################
# TEMP PRINTING #
#################
if print_stats:
# Keep track of wrong scansions
sentence_incorrect_counter = 0
syllable_incorrect_counter = 0
# X = X_test
# y = y_test
# from collections import Counter
# result = Counter(x for xs in y for x in set(xs))
# print('RESULT', result)
# Make a prediction over the whole dataset
y_pred = model.predict(X)
y_pred = np.argmax(y_pred, axis=-1)
for idx, line in enumerate(X):
# for every line in X, create a prediction and truth
sentence = X[idx]
y_pred_current = y_pred[idx]
y_true_current = np.argmax(y[idx], axis=-1)
# If a line is not perfectly scanned, lets investigate
if not (y_pred_current == y_true_current).all():
# count the syllables for every line that are not scanned correctly
syllable_incorrect_counter += (max_sentence_length - np.count_nonzero(y_pred_current==y_true_current))
# retrieve the syllables from the hashes so we can read the sentence again 11
syllable_list = []
for item in sentence:
syllable_list.append(list(word2idx.keys())[list(word2idx.values()).index(item)])
# now, for each line, print some information to investigate the problems
while 'PADDING' in syllable_list: syllable_list.remove('PADDING')
# while 'PADDING' in syllable_list: syllable_list.remove('PADDING')
print('syllables : ', syllable_list)
# print('prediction: ', y_pred_current)
# print('truth: ', y_true_current)
# also, for latinists, print the scansion
y_pred_labels = ['—' if j==0 else '⏑' if j==1 else 'e' if j==2 else ' ' if j==3 else j for j in y_pred_current]
y_train_labels = ['—' if j==0 else '⏑' if j==1 else 'e' if j==2 else ' ' if j==3 else j for j in y_true_current]
while 4 in y_pred_labels: y_pred_labels.remove(4) # padding.
while 4 in y_train_labels: y_train_labels.remove(4)
print('prediction: ', y_pred_labels)
print('truth : ', y_train_labels)
print('\n##########################\n')
# count the sentence as incorrect
sentence_incorrect_counter += 1
# after all scrutiny, print the final statistics
score_sentences = round(sentence_incorrect_counter/len(X)*100,2)
# score_syllables = round(syllable_incorrect_counter/len(all_text_syllables)*100,2)
print('SENTENCES SCANNED WRONGLY: ', sentence_incorrect_counter)
print('PERCENTAGE WRONG: {0}%'.format(score_sentences))
print('SYLLABLES SCANNED WRONGLY: ', syllable_incorrect_counter)
# print('PERCENTAGE WRONG: {0}%'.format(score_syllables)) # This also counts spaces: needs fixing
if FLAGS.exp_hexameter:
train_texts = ['VERG-aene.pickle', 'IVV-satu.pickle', 'LVCR-rena.pickle', 'OV-meta.pickle', 'PERS-satu.pickle']
test_texts = ['VERG-aene.pickle', 'IVV-satu.pickle', 'LVCR-rena.pickle', 'OV-meta.pickle', 'PERS-satu.pickle']
# train_texts = ['VERG-aene.pickle', 'PROP-ele.pickle', 'OV-ele.pickle']
# test_texts = ['VERG-aene.pickle', 'PROP-ele.pickle', 'OV-ele.pickle']
# train_texts = ['PERS-satu.pickle','IVV-satu.pickle']
# test_texts = ['PERS-satu.pickle','IVV-satu.pickle']
sequence_labels_all_set = util.merge_sequence_label_lists(train_texts, util.cf.get('Pickle', 'path_sequence_labels')) # Merge them into one big file list
all_text_syllables = self.retrieve_syllables_from_sequence_label_list(sequence_labels_all_set)
# We need to extract the max sentence length over all these texts to get the padding correct later
max_sentence_length = self.retrieve_max_sentence_length(sequence_labels_all_set)
# And we need to create a list of all unique syllables for our word2idx one-hot encoding
unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING)
word2idx, label2idx = self.create_idx_dictionaries(unique_syllables, self.LABELS)
# twice test texts because they are identical and merge_sequence_label_lists has a bug
self.do_experiment(test_texts, test_texts, max_sentence_length, unique_syllables, word2idx, label2idx, exp_name='hexameter', plot_title='Cross author evaluation')
if FLAGS.exp_transfer_boeth:
# To make the LSTM with integer hashing working, we need to make a list of all syllables from all the texts we are looking at
all_sequence_label_pickles = util.Create_files_list(util.cf.get('Pickle', 'path_sequence_labels'), 'pickle') # Find all pickle files
sequence_labels_all_set = util.merge_sequence_label_lists(all_sequence_label_pickles, util.cf.get('Pickle', 'path_sequence_labels')) # Merge them into one big file list
# all_text_syllables = self.retrieve_syllables_from_sequence_label_list(sequence_labels_all_set)
# # We need to extract the max sentence length over all these texts to get the padding correct later
# max_sentence_length = self.retrieve_max_sentence_length(sequence_labels_all_set)
# text = util.Pickle_read(util.cf.get('Pickle', 'path_sequence_labels'), 'HEX_ELE-all.pickle')
all_text_syllables = self.retrieve_syllables_from_sequence_label_list(sequence_labels_all_set)
max_sentence_length = self.retrieve_max_sentence_length(sequence_labels_all_set)
# unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING) # needs to be sorted for word2idx consistency!
# And we need to create a list of all unique syllables for our word2idx one-hot encoding
unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING)
word2idx, label2idx = self.create_idx_dictionaries(unique_syllables, self.LABELS)
train_texts = ['VERG-aene.pickle', 'HEX-all.pickle', 'ELE-all.pickle', 'HEX_ELE-all.pickle']
test_texts = ['BOETH-cons.pickle', 'TIB-ele.pickle', 'ENN-anna.pickle', 'HOR-arpo.pickle',
'LVCAN-phar.pickle', 'CATVLL-carm.pickle', 'STAT-theb.pickle']
self.do_experiment(train_texts, test_texts, max_sentence_length, unique_syllables, word2idx, label2idx, exp_name='boethius', plot_title='Scanning Unseen Texts')
if FLAGS.exp_transfer:
# Here we test whether training on elegiac and hexameter gives better results
train_texts = ['VERG-aene.pickle', 'HEX-all.pickle', 'ELE-all.pickle', 'HEX_ELE-all.pickle']
test_texts = ['SEN-aga.pickle']
self.do_experiment(train_texts, test_texts, max_sentence_length, unique_syllables, word2idx, label2idx, exp_name='seneca', plot_title='Scanning Iambic Trimeter')
if FLAGS.exp_elegiac:
# Pick all the elegiac texts and let Virgil do his best :D
train_texts = ['VERG-aene.pickle', 'TIB-ele.pickle', 'PROP-ele.pickle', 'OV-ele.pickle']
test_texts = ['VERG-aene.pickle','TIB-ele.pickle', 'PROP-ele.pickle', 'OV-ele.pickle']
self.do_experiment(train_texts, test_texts, exp_name='elegiac')
if FLAGS.exp_train_test:
train_texts = ['VERG-aene.pickle', 'IVV-satu.pickle', 'LVCR-rena.pickle', 'OV-meta.pickle']
# train_texts = ['VERG-aene.pickle']
for text in train_texts:
current_text = util.Pickle_read(util.cf.get('Pickle', 'path_sequence_labels'), text)
current_text = random.sample(current_text, 3600)
all_text_syllables = self.retrieve_syllables_from_sequence_label_list(current_text)
max_sentence_length = self.retrieve_max_sentence_length(current_text)
unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING) # needs to be sorted for word2idx consistency!
word2idx, label2idx = self.create_idx_dictionaries(unique_syllables, self.LABELS)
X, y = self.create_X_y_sets(current_text, word2idx, label2idx, max_sentence_length)
# we split in train and test set.
X_test = X[:720] # 20% of 3600
y_test = y[:720]
X_train = X[720:]
y_train = y[720:]
splits = 30
X_train_list = np.array_split(X_train, splits)
y_train_list = np.array_split(y_train, splits)
X_train_list_2 = X_train_list[splits-1]
y_train_list_2 = y_train_list[splits-1]
# print(X_train_list[1])
for i in range(splits):
# get the model we want
model = self.get_model( max_len = max_sentence_length,
num_syllables = len(unique_syllables),
num_labels = len(self.LABELS),
X = X_train_list_2,
y = y_train_list_2,
epochs = FLAGS.epochs,
create_model = True,
save_model = False)
loss, accuracy = self.evaluate_model(model, X_test, y_test)
result = '{0},{1},{2}\n'.format(text, accuracy, len(X_train_list_2))
# Open a file with access mode 'a'
file_name = './plots/size_' + text + '.txt'
file_object = open(file_name, 'a')
# Append 'hello' at the end of file
file_object.write(result)
# Close the file
file_object.close()
# Now increase the train list and repeat the experiment
X_train_list_2 = np.append(X_train_list_2, X_train_list[i], axis=0)
y_train_list_2 = np.append(y_train_list_2, y_train_list[i], axis=0)
def create_seaborn_heatmap(self, confusion_matrix):
if self.FLAGS.anceps:
labels = ['long', 'short', 'elision', 'space', 'padding', 'anceps']
else:
labels = ['long', 'short', 'elision', 'space', 'padding']
df_confusion_matrix = pd.DataFrame(confusion_matrix, index = labels,
columns = labels)
# Drop the padding labels, as we don't need them (lstm scans them without confusion): delete both row and column
df_confusion_matrix = df_confusion_matrix.drop('padding')
df_confusion_matrix = df_confusion_matrix.drop(columns=['padding', 'space'])
df_confusion_matrix = df_confusion_matrix.drop('space')
if self.FLAGS.anceps:
df_confusion_matrix = df_confusion_matrix.drop(columns=['anceps'])
df_confusion_matrix = df_confusion_matrix.drop('anceps')
# df_confusion_matrix = df_confusion_matrix.drop(columns=['padding'])
util.create_heatmap(dataframe = df_confusion_matrix,
xlabel = 'TRUTH',
ylabel = 'PREDICTED',
title = 'CONFUSION MATRIX',
filename = 'new_confusion_matrix',
# vmax = 500
)
def do_experiment(self, train_texts, test_texts, max_sentence_length, unique_syllables, word2idx, label2idx, exp_name='default', plot_title='default'):
# import matplotlib.pyplot as plt
column_names = ["predictor", "predictee", "score"]
# df = pd.DataFrame(columns = [])
df = pd.DataFrame(columns = ["epochs", "anapest_long", "anapest_short",
'dimeter_long', 'dimeter_short',
'tetrameter_long','tetrameter_short',
'glyconee_long', 'glyconee_short',
'hendycasyllable_long', 'hendycasyllable_short',
'trimeter_long', 'trimeter_short'])
# df.to_csv('epoch_learning_rate_testing.csv', mode='w', index=False, header=True)
new_line = {'epochs': self.num_epochs}
df = df.append(new_line, ignore_index=True)
df_long = pd.DataFrame(columns = column_names)
df_short = pd.DataFrame(columns = column_names)
df_elision = pd.DataFrame(columns = column_names)
# Merge the training and test texts and retrieve from it all data we need to get the model working
# all_texts = train_texts + test_texts
# sequence_labels_all_set = util.merge_sequence_label_lists(all_texts, util.cf.get('Pickle', 'path_sequence_labels'))
# all_text_syllables = self.retrieve_syllables_from_sequence_label_list(sequence_labels_all_set)
# max_sentence_length = self.retrieve_max_sentence_length(sequence_labels_all_set)
# unique_syllables = np.append(sorted(list(set(all_text_syllables))), self.PADDING)
# word2idx, label2idx = self.create_idx_dictionaries(unique_syllables, self.LABELS)
for train_text in train_texts:
sequence_labels_training_text = util.Pickle_read(util.cf.get('Pickle', 'path_sequence_labels'), train_text)
X_train, y_train = self.create_X_y_sets(sequence_labels_training_text, word2idx, label2idx, max_sentence_length)
model = self.get_model( max_len = max_sentence_length,
num_syllables = len(unique_syllables),
num_labels = len(self.LABELS),
X = X_train,
y = y_train,
epochs = FLAGS.epochs,
create_model = FLAGS.create_model,
save_model = FLAGS.save_model,
model_name = train_text.split('.')[0])
for test_text in test_texts:
sequence_labels_test_text = util.Pickle_read(util.cf.get('Pickle', 'path_sequence_labels'), test_text)
X_test, y_test = self.create_X_y_sets(sequence_labels_test_text, word2idx, label2idx, max_sentence_length)
classification_report = self.create_classification_report(model, X_test, y_test)
# print(test_text, classification_report)
predictee = test_text.split('.')[0]#.capitalize()
score_long = float(round(classification_report['0']['f1-score'],2))
score_short = float(round(classification_report['1']['f1-score'],2))
# score_elision = float(round(classification_report['2']['f1-score'],2))
# print(score_long, score_short, predictee)
# new_line_long = {'epochs': self.num_epochs, 'predictee': predictee+'_long', 'long': score_long, 'short': score_short}
# new_line_short = {'epochs': self.num_epochs, 'predictee': predictee+'_short', 'short': score_short}
predictee_long = predictee+'_long'
predictee_short = predictee+'_short'
df.loc[df['epochs'] == self.num_epochs, [predictee_long]] = score_long
df.loc[df['epochs'] == self.num_epochs, [predictee_short]] = score_short
# df = df.append(new_line, ignore_index=True)
print(df)
continue
# exit(0)
# if self.FLAGS.metrics_report:
# print_metrics_report = self.create_confusion_matrix(model, X_test, y_test)
# # self.create_seaborn_heatmap(print_metrics_report)
# print(print_metrics_report)
# print_metrics_report = self.create_metrics_report(model, X_test, y_test, output_dict=True)
# print(print_metrics_report)
# exit(0)
predictor = train_text.split('-')[0].capitalize() # get names
predictee = test_text.split('-')[0].capitalize()
score_long = float(round(metrics_report['long']['f1-score'],4)) # save the score
score_short = float(round(metrics_report['short']['f1-score'],4)) # save the score
score_elision = float(round(metrics_report['elision']['f1-score'],4)) # save the score
# Add score to the dataframe for our heatmap
new_line_long = {'predictor': predictor, 'predictee': predictee, 'score': score_long}
new_line_short = {'predictor': predictor, 'predictee': predictee, 'score': score_short}
new_line_elision = {'predictor': predictor, 'predictee': predictee, 'score': score_elision}
df_long = df_long.append(new_line_long, ignore_index=True)
df_short = df_short.append(new_line_short, ignore_index=True)
df_elision = df_elision.append(new_line_elision, ignore_index=True)
# df.to_csv('epoch_learning_rate_testing.csv', mode='a', index=False, header=False)
df.to_csv('epoch_learning_rate_testing2.csv', mode='w', index=False, header=True)
print(df)
exit(0)
time = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
heatmap_data = pd.pivot_table(df_long, values='score', index=['predictor'], columns='predictee')
heatmap_data.to_csv('./csv/{0}_f1-scores_long.csv'.format(time))
myplot = plt.figure(figsize=(16,10))
myplot = plt.subplot(2, 2, 1)
myplot = util.create_heatmap(dataframe = heatmap_data,
xlabel = 'test',
ylabel = 'train',
title = '{0}: Long f1-scores'.format(plot_title),
filename = '{0}-long'.format(exp_name),
path = './plots/experiments/')
heatmap_data = pd.pivot_table(df_short, values='score', index=['predictor'], columns='predictee')
heatmap_data.to_csv('./csv/{0}_f1-scores_short.csv'.format(time))
myplot = plt.subplot(2, 2, 2)
myplot = util.create_heatmap(dataframe = heatmap_data,
xlabel = 'test',
ylabel = 'train',
title = '{0}: Short f1-scores'.format(plot_title),
filename = '{0}-short'.format(exp_name),
path = './plots/experiments/')
heatmap_data = pd.pivot_table(df_elision, values='score', index=['predictor'], columns='predictee')
heatmap_data.to_csv('./csv/{0}_f1-scores_elision.csv'.format(time))
myplot = plt.subplot(2, 2, 3)
myplot = util.create_heatmap(dataframe = heatmap_data,
xlabel = 'test',
ylabel = 'train',
title = '{0}: Elision f1-scores'.format(plot_title),
filename = '{0}-elision'.format(exp_name),
path = './plots/experiments/')
def print_length_sequence_label_files(self):
my_list = sorted(util.create_files_list(conf.SEQUENCE_LABELS_FOLDER, '.pickle'))
for text in my_list:
current_text = util.Pickle_read(conf.SEQUENCE_LABELS_FOLDER, text)
print(text, len(current_text))
def combine_sequence_label_lists(self, key, name):
# TO COMBINE LISTS
name = name + '.pickle'
my_list = sorted(util.create_files_list(conf.SEQUENCE_LABELS_FOLDER, key))
temp = util.merge_sequence_label_lists(my_list, conf.SEQUENCE_LABELS_FOLDER)
util.Pickle_write(conf.SEQUENCE_LABELS_FOLDER, name, temp)
def kfold_model(self, sequence_labels, X, y, splits, max_sentence_length, unique_syllables):
"""Performs a kfold cross validation of a LSTM model fitted and trained on the given data
Args:
sequence_labels (list): of sequence labels and their features
X (numpy array): of training/test examples
y (numpy array): of labels
splits (int): of number of splits required
Returns:
dict: with results of the cross validation
"""
if util.cf.get('Util', 'verbose'): print('Predicting the model')
report_list = []
# Convert the list of numpy arrays to a numpy array with numpy arrays
# X = np.array(X, dtype=object)
# y = np.array(y, dtype=object)
kf = KFold(n_splits=splits, shuffle=True, random_state=42)
for train_index, test_index in kf.split(sequence_labels):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model = self.get_model( max_len = max_sentence_length,
num_syllables = len(unique_syllables),
num_labels = len(self.LABELS),
X = X_train,
y = y_train,
epochs = FLAGS.epochs,
create_model = True,
save_model = FLAGS.save_model)
metrics_report = self.create_metrics_report(model, X_test, y_test, output_dict=True)
# TODO: just print a score for each run to terminal
report_list.append(metrics_report)
result = util.merge_kfold_reports(report_list)
return result
def custom_train_test(self):
train_texts = ['HEX_ELE-all.pickle'] #['VERG-aene.pickle']#
test_texts = ['anapest.pickle', 'dimeter.pickle', 'hendycasyllable.pickle', 'glyconee.pickle', 'tetrameter.pickle', 'trimeter.pickle'] #FLAGS.test
self.do_experiment(
train_texts,
test_texts,
self.max_sentence_length,
self.unique_syllables,
self.word2idx,
self.label2idx,
exp_name='seneca_precise',
plot_title='Trimeter Texts'
)
def do_experiment(self, train_texts, test_texts, max_sentence_length, unique_syllables, word2idx, label2idx, exp_name='default', plot_title='default'):
# import matplotlib.pyplot as plt
column_names = ["predictor", "predictee", "score"]
# df = pd.DataFrame(columns = [])
df = pd.DataFrame(columns = ["epochs", "anapest_long", "anapest_short",
'dimeter_long', 'dimeter_short',
'tetrameter_long','tetrameter_short',
'glyconee_long', 'glyconee_short',
'hendycasyllable_long', 'hendycasyllable_short',
'trimeter_long', 'trimeter_short'])
# df.to_csv('epoch_learning_rate_testing.csv', mode='w', index=False, header=True)
new_line = {'epochs': self.num_epochs}
df = df.append(new_line, ignore_index=True)
df_long = pd.DataFrame(columns = column_names)
df_short = pd.DataFrame(columns = column_names)
df_elision = pd.DataFrame(columns = column_names)
for train_text in train_texts:
sequence_labels_training_text = util.Pickle_read(conf.SEQUENCE_LABELS_FOLDER, train_text)
X_train, y_train = self.create_X_y_sets(sequence_labels_training_text, word2idx, label2idx, max_sentence_length)
model = self.get_model( max_len = max_sentence_length,
num_syllables = len(unique_syllables),
num_labels = len(self.LABELS),
X = X_train,
y = y_train,
epochs = FLAGS.epochs,
create_model = FLAGS.create_model,
save_model = FLAGS.save_model,
model_name = train_text.split('.')[0])
for test_text in test_texts:
sequence_labels_test_text = util.Pickle_read(conf.SEQUENCE_LABELS_FOLDER, test_text)
X_test, y_test = self.create_X_y_sets(sequence_labels_test_text, word2idx, label2idx, max_sentence_length)
classification_report = self.create_classification_report(model, X_test, y_test)
# print(test_text, classification_report)
predictee = test_text.split('.')[0]#.capitalize()
score_long = float(round(classification_report['0']['f1-score'],2))
score_short = float(round(classification_report['1']['f1-score'],2))
# score_elision = float(round(classification_report['2']['f1-score'],2))
# print(score_long, score_short, predictee)
# new_line_long = {'epochs': self.num_epochs, 'predictee': predictee+'_long', 'long': score_long, 'short': score_short}
# new_line_short = {'epochs': self.num_epochs, 'predictee': predictee+'_short', 'short': score_short}
predictee_long = predictee+'_long'
predictee_short = predictee+'_short'
df.loc[df['epochs'] == self.num_epochs, [predictee_long]] = score_long
df.loc[df['epochs'] == self.num_epochs, [predictee_short]] = score_short
# df = df.append(new_line, ignore_index=True)
print(df)
continue
# df.to_csv('epoch_learning_rate_testing.csv', mode='a', index=False, header=False)
df.to_csv('epoch_learning_rate_testing2.csv', mode='w', index=False, header=True)
print(df)
exit(0)
def create_line_plot(plots, ylabel, xlabel, plot_titles, title, plotname):
# Simple function to easily create plots
path = './plots/'
time = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
full_file_name = '{0}{1}_{2}.png'.format(path, plotname, time)
for plot_line in plots:
plt.plot(plot_line)
plt.ylabel(ylabel)
plt.xlabel(xlabel)
plt.legend(plot_titles, loc='lower right')
plt.title(title)
plt.savefig(full_file_name)
plt.show()
# plt.clf()
def create_heatmap(dataframe ,xlabel, ylabel, title, filename, vmin=None, vmax=None, path='./plots/'):
# Simple function to create a heatmap
# dataframe.to_numpy().max()
plt.clf()
path = path
time = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
full_file_name = '{0}{1}_{2}.png'.format(path, time, filename)
sn.set(font_scale=1.2)
# sn.heatmap(dataframe, annot=True, fmt='g', annot_kws={"size": 16}, cmap='Blues', vmin=vmin, vmax=vmax)
sn.heatmap(dataframe, annot=True, fmt='g', annot_kws={"size": 12}, cmap='Blues', vmin=vmin, vmax=vmax, cbar=False)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
# plt.title(title)
plt.savefig(full_file_name, bbox_inches='tight')
return plt
def restricted_float(x):
# Used in argparser
try:
x = float(x)
except ValueError:
print("%r not a floating-point literal" % (x,))
if x < 0.0 or x > 1.0:
print("%r not in range [0.0, 1.0]"%(x,))
return x