-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_functions.py
executable file
·974 lines (773 loc) · 30.6 KB
/
helper_functions.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#!/usr/bin/env python
import os, json, nltk, re, string
# from sklearn.externals import joblib
import joblib
import numpy as np
import pickle, gzip
import pdb
import h5py
from collections import defaultdict,OrderedDict
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
mpl.use('Agg')
import matplotlib.pyplot as plt
from ldl_utils import get_data_dict, vectorize,read_json
from gensim.models import LdaModel #https://radimrehurek.com/gensim/utils.html#gensim.utils.SaveLoad.load
import shutil #copy lda max model to final folder
import datetime
# import pymongo
import math
import pandas as pd
import warnings
# from mongo_utils import get_current_mongodb_credentials
#LDA on Language
import gensim
import re
from gensim.utils import simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS
import nltk
from nltk.stem import WordNetLemmatizer, SnowballStemmer
from nltk.stem.porter import *
stemmer = SnowballStemmer('english')
#End on LDA Language
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
def write_results_to_json_only(results,outputdir):
results["Timestamp"] = datetime.datetime.utcnow()
if not os.path.exists(os.path.dirname(outputdir)):
os.makedirs(os.path.dirname(outputdir))
try:
os.remove(outputdir)
except:
pass
with open(outputdir, 'a') as outfile:
json_dump = json.dumps(results, indent=4,default = myconverter)
outfile.write(json_dump)
print ("JSON file saved to "+outputdir)
def get_ids_only(data_items):
message_ids = []
for data_item in data_items:
message_ids.append(data_item)
return message_ids
def read_json_log(json_log_file):
data_file = open(json_log_file)
log = json.load(data_file)
return log['model_selected']
def find_item_distribution_clusters_sklearn(cluster_predicts):
clusters,cluster_counts = np.unique(cluster_predicts,return_counts=True)
max_clusters = max(cluster_counts)
min_clusters = min(cluster_counts)
std_dev_clusters = np.std(cluster_counts).item()
max_items = clusters[np.where(cluster_counts==max_clusters)][0].item()
min_items = clusters[np.where(cluster_counts==min_clusters)][0].item()
result_row = {"max_cluster_items":max_clusters,"max_cluster":max_items,"min_cluster_items":min_clusters,"min_cluster":min_items,"std_cluster_items":std_dev_clusters}
return result_row
def iteration_selection_sklearn(kl_values,results,all_model_path,n_clusters):
#selects the median KL -> selects the iteration -> moves to the final path and deletes rest to save space
median_kl_index = np.argsort(kl_values)[len(kl_values)//2]
median_kl = kl_values[median_kl_index]
result = results[median_kl_index]
result["avg_kl"] = np.mean(kl_values).item()
result["std_kl"] = np.std(kl_values).item()
model_path = all_model_path+str(median_kl_index)
model = joblib.load(model_path+"/CL"+str(n_clusters)+".pkl")
result['max_cluster_items'] = result['max_cluster_items'].item() #fix for error numpy int64 dump to jsob
result['min_cluster_items'] = result['min_cluster_items'].item()
cluster_info = read_json(model_path+"/cluster_info_"+str(n_clusters)+".json")
return model,cluster_info,result
def iteration_selection_gensim(kl_values,results,all_model_path,n_clusters):
#selects the median KL -> selects the iteration -> moves to the final path and deletes rest to save space
median_kl_index = np.argsort(kl_values)[len(kl_values)//2]
median_kl = kl_values[median_kl_index]
result = results[median_kl_index]
result["avg_kl"] = np.mean(kl_values).item()
result["std_kl"] = np.std(kl_values).item()
model_path = all_model_path+"/CL"+str(n_clusters)+"/"+str(median_kl_index)+"_topic.lda"
model = LdaModel.load(model_path, mmap='r')
cluster_info = read_json(all_model_path+"/CL"+str(n_clusters)+"/"+str(median_kl_index)+"_cluster_info.json")
shutil.rmtree(all_model_path+"/CL"+str(n_clusters))
model.save(all_model_path + "/" + str(n_clusters) + "_topic.lda")
write_model_logs_to_json(all_model_path,cluster_info,"cluster_info_"+ str(n_clusters))
del model
return result
def iteration_selection_bnpy(kl_values,results,all_model_path,n_clusters,process_id):
#selects the median KL -> selects the iteration -> moves to the final path and deletes rest to save space
median_kl_index = np.argsort(kl_values)[len(kl_values)//2]
median_kl = kl_values[median_kl_index]
result = results[median_kl_index]
result["avg_kl"] = np.mean(kl_values).item()
result["std_kl"] = np.std(kl_values).item()
model_path = all_model_path+str(median_kl_index)
train_pred = read_json(model_path+"/"+process_id+"_train.json")
dev_pred = read_json(model_path+"/"+process_id+"_dev.json")
test_pred = read_json(model_path+"/"+process_id+"_test.json")
return result,train_pred,dev_pred,test_pred
def sklearn_find_kl(train_vectors,train_preds, cluster_distributions):
KLsum = []
for train_vector,train_pred in zip(train_vectors,train_preds):
train_pred = np.asarray(cluster_distributions[str(train_pred)])
train_vector = np.asarray(train_vectors[train_vector])
KL = KLdivergence(train_vector, train_pred)
if (math.isnan(KL)):
KL = 0.0
KLsum.append(KL)
return np.mean(KLsum)
def gensim_find_kl(train_vectors,train_preds, cluster_distributions):
KLsum = []
for train_vector,train_pred in zip(train_vectors,train_preds):
train_pred = generate_pd(cluster_distributions[train_pred])
train_vector = generate_pd(train_vectors[train_vector])
KL = KLdivergence(train_vector, train_pred)
if (math.isnan(KL)):
KL = 0.0
KLsum.append(KL)
return np.mean(KLsum)
def bnpy_find_kl(train_vectors,train_preds):
KLsum = []
for train_vector,train_pred in zip(train_vectors,train_preds):
train_vector = generate_pd(train_vectors[train_vector])
KL = KLdivergence(train_vector, train_pred)
if (math.isnan(KL)):
KL = 0.0
KLsum.append(KL)
return np.mean(KLsum)
def write_model_logs_to_json(MODEL_LOG_DIR, results_dict, output_name):
create_folder(MODEL_LOG_DIR)
with open(MODEL_LOG_DIR +"/"+ output_name + ".json", "w") as fp:
json_export = json.dumps(results_dict)
fp.write(json_export)
# json.dump(results_dict, fp, sort_keys=True, indent=4)
def save_lda_model(LDA_LOG_DIR, model, output_name, i):
path = LDA_LOG_DIR + output_name + '/models/'
if not os.path.exists(path):
os.makedirs(path)
model.save(path + 'CL' + str(i) + '.lda')
def load_lda_model(path):
model = LdaModel.load(path)
return model
def save_pickle(path,bow):
with open(path,'wb') as fp:
pickle.dump(bow,fp)
fp.close()
print ("Saved "+path)
def load_pickle(path):
with open(path,'rb') as fp:
bow = pickle.load(fp)
return bow
def create_folder(foldername):
if not os.path.exists(foldername):
os.makedirs(foldername)
def save_trained_model_joblib(MODEL_LOG_DIR, model, output_name, i, j):
# http://scikit-learn.org/stable/modules/model_persistence.html
# i in range(LOWER, UPPER)
# j in range(ITERATIONS)
model_dir = MODEL_LOG_DIR + '/CL' + str(i) + '/'
if not os.path.exists(model_dir):
os.makedirs(model_dir)
joblib.dump(model, model_dir + "Iter" + str(j) +'.pkl')
#model.close()
def save_trained_model_joblib_sklearn(MODEL_LOG_DIR, model, output_name, i):
# http://scikit-learn.org/stable/modules/model_persistence.html
# i in range(LOWER, UPPER)
# j in range(ITERATIONS)
model_dir = MODEL_LOG_DIR + '/CL' + str(i) + '/'
if not os.path.exists(model_dir):
os.makedirs(model_dir)
joblib.dump(model, model_dir + '.pkl')
#model.close()
def check_if_model_trained(MODEL_LOG_DIR, model, output_name, LOWER, UPPER):
logs = []
for i in range(LOWER, UPPER):
model_dir = MODEL_LOG_DIR + output_name + '/CL' + str(i) + '/'
if not os.path.exists(model_dir):
logs.append(1)
else:
logs.append(0)
if sum(logs) != 0:
return False
else:
return True
def build_prob_distribution(dataset):
results = []
for data in dataset:
total = sum(data)
data = np.array(data)
data = data.astype(float)
row_to_write = data/total
results.append(row_to_write)
return results
def get_index_of_maximum(values):
return values.index(max(values))
def save_keras_predict(MODEL_LOG_DIR, prediction, cnn_name):
output_name = cnn_name.split("_")[0]
model_dir = MODEL_LOG_DIR + output_name
if not os.path.exists(model_dir):
os.makedirs(model_dir)
path = model_dir + '/' + cnn_name + '_predict.pkl'
with open(path, 'wb') as fp:
pickle.dump(prediction, fp)
fp.close()
del prediction # deletes the existing prediction
def load_keras_predict(MODEL_LOG_DIR, cnn_name):
output_name = cnn_name.split("_")[0]
model_dir = MODEL_LOG_DIR + output_name
path = model_dir + '/' + cnn_name + '_predict.pkl'
with open(path, 'rb') as fp:
prediction = pickle.load(fp)
return prediction
def read_original_split(jsonfile):
#SPLIT_LOG_DIR = "data/split/"
# Read data splits from file, NOT generate each time
#jsonfile = SPLIT_LOG_DIR + output_name + "_" + split_prep + ".json"
#print(jsonfile)
with open(jsonfile) as fp:
results_dict = json.load(fp)
train_items = results_dict['train_set']
dev_items = results_dict['dev_set']
test_items = results_dict['test_set']
return train_items, dev_items, test_items
def data_prep_bnpy(choice_counts, choices):
'''
Structure data in Bag of words format
:param choice_counts: dictionary object {message_id : list_of_answer_counts}
:param choices: possible answer choices
:return:
'''
vocab_list = choices
word_ids_per_doc = [x for x in range(len(vocab_list))]
nWords = len(word_ids_per_doc)
word_id = []
word_count = []
doc_range = [0]
i = 0
# create a list of word ids and non zero word counts for each document
for doc_id in choice_counts.keys():
ans_counts = np.array(choice_counts[doc_id])
# find words with count > 0
nz_word_ids = np.flatnonzero(ans_counts)
nz_word_counts = ans_counts.ravel()[nz_word_ids]
# print(ans_counts, nz_word_ids, nz_word_counts)
# array([2, 1, 0, 4]), array([0, 1, 3]), array([2, 1, 4])
word_id.extend(nz_word_ids.tolist())
word_count.extend(nz_word_counts.tolist())
nWords_in_doc = len(nz_word_ids)
i += nWords_in_doc
doc_range.append(i)
bow_info = {
'word_id' : np.array(word_id),
'word_count' : np.array(word_count),
'doc_range' : np.array(doc_range),
'vocab_size' : np.array(nWords),
'vocabList' : np.array(choices),
'logFunc' : False
}
return bow_info
def validate_file_location(path):
return os.path.isfile(path)
def language_prep_bnpy(message_dict, subitems):
vocab_list = []
for msg_id, message in message_dict.items():
# Naive tokenization
tokens = message.split()
# Advanced tokenization
# tokens = get_normalized_tokens(message, set())
for token in tokens:
if token not in vocab_list:
vocab_list.append(token)
word_ids_per_doc = [x for x in range(len(vocab_list))]
nWords = len(word_ids_per_doc)
word_id = []
word_count = []
doc_range = [0]
i = 0
# create a list of word ids and non zero word counts for each document
for index, msg_id in enumerate(subitems):
message = message_dict[msg_id]
# Naive tokenization
tokens = message.split()
# Advanced tokenization
# tokens = get_normalized_tokens(message, set())
ans_counts = [0] * nWords
for token in tokens:
ans_counts[vocab_list.index(token)] += 1
ans_counts = np.array(ans_counts)
# find words with count > 0
nz_word_ids = np.flatnonzero(ans_counts)
nz_word_counts = ans_counts.ravel()[nz_word_ids]
word_id.extend(nz_word_ids.tolist())
word_count.extend(nz_word_counts.tolist())
nWords_in_doc = len(nz_word_ids)
if nWords_in_doc != 0:
i += nWords_in_doc
doc_range.append(i)
else:
print(index, tokens)
print(ans_counts, nz_word_ids, nz_word_counts, nWords_in_doc, i)
bow_info = {
'word_id' : np.array(word_id),
'word_count' : np.array(word_count),
'doc_range' : np.array(doc_range),
'vocab_size' : np.array(nWords),
'vocabList' : vocab_list,
'logFunc' : False
}
return bow_info
def word_normalizer(w):
p = re.compile(r'^#*[a-z]+[\'-/]*[a-z]*$', re.UNICODE)
pLink = re.compile(r'https*:\S+\.\w+', re.IGNORECASE)
pMention = re.compile(r'@[A-Za-z0-9_]+\b')
pNewLine = re.compile(r'[\r\n]+')
pRetweet = re.compile(r'\brt\b', re.IGNORECASE)
punctuation = {0x2018:0x27, 0x2019:0x27, 0x201C:0x22, 0x201D:0x22}
"""
Returns normalized word or None, if it doesn't have a normalized representation.
"""
if pLink.match(w):
return '[http://LINK]'
if pMention.match(w):
return '[@SOMEONE]'
if len(w) < 1:
return None
if w[0] == '#':
w = w.strip('.,*;-:"\'`?!)(').lower()
else:
w = w.strip(string.punctuation).lower()
if not(p.match(w)):
return None
return w
def stemmer_lemmatizer(tokens):
wnl = nltk.WordNetLemmatizer()
return [wnl.lemmatize(t) for t in tokens]
def get_normalized_tokens(text, stopset):
# naive tokenization
words = text.split()
tokens = []
for w in words:
normalized_word = word_normalizer(w)
# remove stopwords from normalized tweet
if (normalized_word is not None) and (normalized_word not in stopset):
tokens.append(normalized_word)
return stemmer_lemmatizer(tokens)
def save_bnpy_model(model_dir, trained_model, info_dict):
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# store the object
model_dict = {'best_model': trained_model, 'info': info_dict}
with gzip.open(os.path.join(model_dir, 'best_model.pklz'), 'wb') as f:
pickle.dump(model_dict, f)
def load_bnpy_model(model_location):
model_dict = dict()
for (dirpath, dirnames, filenames) in os.walk(model_location):
for di in dirnames:
pickle_fpath = os.path.join(model_location, di) + '/best_model.pklz'
with gzip.open(pickle_fpath, 'rb') as model:
m = pickle.load(model)
nClusters = int(di.strip("CL"))
model_dict[nClusters] = (m['best_model'], m['info']) # tuple
return model_dict
def KLdivergence(P, Q):
# from Q to P
# https://datascience.stackexchange.com/a/26318/30372
"""
Epsilon is used here to avoid conditional code for
checking that neither P nor Q is equal to 0.
"""
epsilon = 0.00001
P = P + epsilon
Q = Q + epsilon
return np.sum(P * np.log(P/Q))
def answer_counters2idxstr_token(answer_counters, message_id, choices):
labels = []
for item in zip(answer_counters[message_id], range(len(choices))):
labels.append(item[0] * str('%02d ' % (item[1]+1)))
return ''.join(labels).split()
def save_max_lda_model(model_file,LDAproba_Y,max_meas_idx,lda_folder,final_lda_dst,proba_mode):
lda_src = model_file
lda_dst = final_lda_dst
shutil.copyfile(lda_src, lda_dst)
with open(lda_folder + "/" + proba_mode+ "_LDAproba_Y.pkl", 'wb') as fp:
pickle.dump(LDAproba_Y, fp)
fp.close()
def save_max_lda_model_trained(model_file,final_lda_dst):
lda_src = LdaModel.load(model_file)
lda_src.save(final_lda_dst)
def save_to_json(output_file,data_to_write):
with open(output_file, "w") as fp:
json.dump(data_to_write, fp, sort_keys=True, indent=4)
print ("Saved to "+output_file)
def save_to_json_foldercheck(data,outputdir):
if not os.path.exists(os.path.dirname(outputdir)):
os.makedirs(os.path.dirname(outputdir))
with open(outputdir, 'w') as outfile:
outfile.write(json.dumps(data, indent=4))
#print ("JSON file saved to "+outputdir)
def copy_json_files(original_file,output_file):
src_data = read_json(original_file)
save_to_json(output_file,src_data)
def load_LDA_proba_Y(path,proba_mode):
with open(path + "/" + proba_mode + "_LDAproba_Y.pkl" ,'rb') as fp:
LDA_proba_Y = pickle.load(fp)
return LDA_proba_Y
def plot_NN_history(history_NN, NN_name, kind):
# plt.style.use('ggplot')
plt.plot(history_NN.history['acc'])
plt.plot(history_NN.history['val_acc'])
plt.legend(['Learning Curve', 'Validation Curve'], loc='best')
plt.title('%s accuracy' % kind)
plt.xlabel('Iterations')
plt.ylabel('Accuracy')
plt.xticks(range(0, 26, 5))
plt.yticks()
plt.savefig("figures/" + NN_name + ("_%s.pdf" % kind))
plt.close()
def read_labeled_data(filename):
answer_counters = defaultdict(list)
JSONfile = read_json(filename)
message_dict = compile_tweet_dict(JSONfile["data"])
(fdict, label_dict) = get_data_dict(JSONfile["dictionary"])
answer_counters = get_feature_vectors(fdict, JSONfile["data"])
return answer_counters,message_dict,label_dict
def read_labeled_data_NBP(filename):
answer_counters = defaultdict(list)
JSONfile = read_json(filename)
message_dict = compile_tweet_dict(JSONfile["data"])
(fdict, label_dict) = get_data_dict(JSONfile["dictionary"])
answer_counters = get_feature_vectors_NBP(fdict, JSONfile["data"])
return answer_counters,message_dict,label_dict
def read_labeled_data_KMeans(filename):
answer_counters = defaultdict(list)
JSONfile = read_json(filename)
message_dict = compile_tweet_dict(JSONfile["data"])
(fdict, label_dict) = get_data_dict(JSONfile["dictionary"])
answer_counters = get_feature_vectors_only(fdict, JSONfile["data"])
return answer_counters,message_dict,label_dict
def read_labeled_data_sklearn(filename):
answer_counters = defaultdict(list)
JSONfile = read_json(filename)
message_dict = compile_tweet_dict(JSONfile["data"])
(fdict, label_dict) = get_data_dict(JSONfile["dictionary"])
answer_counters = get_feature_vectors_only(fdict, JSONfile["data"])
return answer_counters,message_dict,label_dict
def get_feature_vectors(fdict, data):
#output = {}
output = defaultdict(list)
for item in data:
vect = vectorize(fdict, item["labels"])
item["message_id"] = int(item["message_id"])
output[item["message_id"]] = vect
return output
def get_data_labels_only(label_data):
data = []
for label_value in sorted(label_data.keys()):
data.append(label_data[label_value])
return np.asarray(data)
def get_feature_vectors_NBP(fdict, data):
#output = {}
output = defaultdict(list)
for item in data:
vect = vectorize(fdict, item["labels"])
total_labels = float(sum(vect))
vect[:] = [x /total_labels for x in vect]
item["message_id"] = item["message_id"]
output[item["message_id"]] = vect
return output
def get_feature_vectors_only(fdict, data):
#output = {}
output = defaultdict(list)
for item in data:
vect = vectorize(fdict, item["labels"])
total_labels = float(sum(vect))
vect[:] = [x /total_labels for x in vect]
item["message_id"] = item["message_id"]
output[item["message_id"]] = vect
return output
def id_to_label(measure):
labels = []
for measure_mode in measure:
if "KL" in measure_mode:
labels.append("KL-Divergence")
elif "CH" in measure_mode:
labels.append("Chebyshev distance")
elif "EU" in measure_mode:
labels.append("Euclidean distance")
elif "CA" in measure_mode:
labels.append("Canberra metric")
elif "CS" in measure_mode:
labels.append("Cosine similarity")
return labels
def generate_pd_data(result):
total = float(sum(result[0]))
result = result/total
return result
def compile_tweet_dict(json_list):
result = {int(x["message_id"]): x["message"] for x in json_list}
return result
def plot_graphs_NBP_results(dframe_results,data_item_name,output_dir):
plt.plot(dframe_results['Epsilon'],dframe_results['KL-divergence'])
#plt.plot(dframe_results['Epsilon'],dframe_results['Test accuracy'])
plt.legend(['KL-Divergence'], loc='best')
#plt.legend(['KL-Divergence','Test accuracy'], loc='best')
plt.title('%s KL-Divergence' % data_item_name)
plt.xlabel('Epsilon')
plt.ylabel('KL-Divergence')
plt.xticks()
plt.yticks()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
plt.savefig(output_dir + ("/%s.pdf" % data_item_name))
print (output_dir + ("/%s.pdf" % data_item_name))
plt.close()
def plot_graphs_NBP(results,ylabel,measure,epsilon,data_item_name,output_dir):
results_to_write = {'Epsilon':epsilon}
results_dframe = pd.DataFrame(results_to_write)
for measure_mode in measure:
result = []
for row in results:
result.append(row[measure_mode])
results_dframe[measure_mode] = result
results_dframe = results_dframe.sort_values(by=['Epsilon'])
plt.plot(results_dframe['Epsilon'],results_dframe[measure_mode])
plot_labels = id_to_label(measure)
plt.legend(plot_labels, loc='best')
#plt.title('%s KL-Divergence (NBP stage)' % data_item_name)
plt.xlabel('Radius (r)')
plt.ylabel(ylabel)
plt.xticks()
plt.yticks()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
plt.savefig(output_dir + ("/%s_NBP.pdf" % data_item_name))
print (output_dir + ("/%s_NBP.pdf" % data_item_name))
plt.close()
def write_NBP_results_to_mongodb(process_id,measures,results_to_write,run_location,db_name,epsilon):
mongo_client = pymongo.MongoClient(get_current_mongodb_credentials())
mongo_db = mongo_client[db_name]
mongo_col = mongo_db[process_id]
n = len(epsilon)
for epsilon_value,i,result in zip(epsilon,range(n),results_to_write):
results = defaultdict(list)
results["Run Location"] = run_location
results["date"] = datetime.datetime.utcnow()
for measure in measures:
results[measure] = result[measure]
results[measure+"_NAvg"] = result[measure+"_NAvg"]
results[measure+"_NMin"] = result[measure+"_NMin"]
results[measure+"_NMax"] = result[measure+"_NMax"]
# if math.isnan(measures[measure][i]):
# results[measure] = 0.0
# else:
# results[measure] = measures[measure][i]
results["Epsilon"] = epsilon_value
x = mongo_col.insert_one(results)
print ("Result saved to the database")
print ("All results saved to DB")
def write_NBP_result_to_mongodb(process_id,measures,results_to_write,run_location,db_name,epsilon):
mongo_client = pymongo.MongoClient(get_current_mongodb_credentials())
mongo_db = mongo_client[db_name]
mongo_col = mongo_db[process_id]
n = len(epsilon)
results = defaultdict(list)
results["Run Location"] = run_location
results["date"] = datetime.datetime.utcnow()
for epsilon_value,i,result in zip(epsilon,range(n),results_to_write):
results = defaultdict(list)
results["Run Location"] = run_location
results["date"] = datetime.datetime.utcnow()
for measure in measures:
results[measure] = result[measure]
# if math.isnan(measures[measure][i]):
# results[measure] = 0.0
# else:
# results[measure] = measures[measure][i]
results["Epsilon"] = epsilon_value
x = mongo_col.insert_one(results)
print ("Result saved to the database")
print ("All results saved to DB")
def JSdivergence(P, Q):
# from Q to P
# https://datascience.stackexchange.com/a/26318/30372
"""
Epsilon is used here to avoid conditional code for
checking that neither P nor Q is equal to 0.
"""
M = 0.5*(P+Q)
KL1 = KLdivergence(P,M)
KL2 = KLdivergence(Q,M)
JS = 0.5*(KL1+KL2)
return JS
def convert_to_majority(labels):
output = defaultdict(list)
for label in labels:
label_set = labels[label]
zero_ary = np.zeros(len(label_set))
max_index = get_index_of_maximum(label_set)
zero_ary[max_index] = label_set[max_index]
output[label] = zero_ary
return output
def convert_to_majority_array(labels):
output = []
for label_set in labels:
label_set = np.array(label_set)
zero_ary = np.zeros(len(label_set))
max_index = np.argmax(label_set)
zero_ary[max_index] = 1
output.append(zero_ary)
return output
def data_prep_bnpy(choice_counts, choices):
'''
Structure data in Bag of words format
:param choice_counts: dictionary object {message_id : list_of_answer_counts}
:param choices: possible answer choices
:return:
'''
vocab_list = choices
word_ids_per_doc = [x for x in range(len(vocab_list))]
nWords = len(word_ids_per_doc)
word_id = []
word_count = []
doc_range = [0]
i = 0
# create a list of word ids and non zero word counts for each document
for doc_id in choice_counts.keys():
ans_counts = np.array(choice_counts[doc_id])
# find words with count > 0
nz_word_ids = np.flatnonzero(ans_counts)
nz_word_counts = ans_counts.ravel()[nz_word_ids]
# print(ans_counts, nz_word_ids, nz_word_counts)
# array([2, 1, 0, 4]), array([0, 1, 3]), array([2, 1, 4])
word_id.extend(nz_word_ids.tolist())
word_count.extend(nz_word_counts.tolist())
nWords_in_doc = len(nz_word_ids)
i += nWords_in_doc
doc_range.append(i)
bow_info = {
'word_id' : np.array(word_id),
'word_count' : np.array(word_count),
'doc_range' : np.array(doc_range),
'vocab_size' : np.array(nWords),
'vocabList' : np.array(choices),
'logFunc' : False
}
return bow_info
def save_bnpy_model(model_dir, trained_model, info_dict):
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# store the object
model_dict = {'best_model': trained_model, 'info': info_dict}
with gzip.open(os.path.join(model_dir, 'best_model.pklz'), 'wb') as f:
pickle.dump(model_dict, f)
def load_bnpy_model(model_location):
model_dict = dict()
for (dirpath, dirnames, filenames) in os.walk(model_location):
for di in dirnames:
pickle_fpath = os.path.join(model_location, di) + '/best_model.pklz'
with gzip.open(pickle_fpath, 'rb') as model:
m = pickle.load(model)
nClusters = int(di.strip("CL"))
model_dict[nClusters] = (m['best_model'], m['info']) # tuple
return model_dict
def get_index_of_minimum(values):
return values.index(min(values))
def save_max_sklearn_model_trained(model_file,final_kmeans_dst,output_name):
kmeans_src = joblib.load(model_file)
final_dump_dst = final_kmeans_dst +"/"+output_name+".pkl"
joblib.dump(kmeans_src,final_dump_dst)
def generate_pd(result):
original_result = result
try:
total = np.sum(result)
result = np.array(result, dtype='float64')
result = result/total
except RuntimeWarning:
result = original_result
return result
def median(lst):
sortedLst = sorted(lst)
lstLen = len(lst)
index = (lstLen - 1) // 2
if (lstLen % 2):
return sortedLst[index]
else:
return sortedLst[index + 1]
def get_index_of_best_iteration(values,model_selection_measure):
value_of_selected = median(values)
index_of_selected = values.index(value_of_selected)
return index_of_selected
# old model selection from
# if model_selection_measure == "cross":
# return values.index(min(values))
# else:
# return values.index(max(values))
def map_probability_to_label(choices,prediction):
result = {}
for x,y in zip(choices.values(),prediction):
result[x] = y
return result
def convert_pd_to_labels(label_distribution):
total_labels = len(label_distribution)
converted = [x*total_labels for x in label_distribution]
converted = [round(x) for x in converted]
converted = [int(x) for x in converted]
return converted
def convert_pd_to_labels_sampling(label_distribution,n_votes):
total_labels = n_votes
converted = [x*total_labels for x in label_distribution]
converted = [round(x) for x in converted]
converted = [int(x) for x in converted]
return converted
topic_dict = {}
def generate_topics_dict(topics_dist):
topic_dict = {}
for id,topic in zip(range(len(topics_dist)),topics_dist):
topic_id = '"0%s"' % str(1)
text = str(round(topic[0],3))+"*"+topic_id
for i in range(len(topic)-1):
topic_id = '"0%s"' % str(i+2)
text+=" + "+str(round(topic[i+1],3))+"*"+topic_id
topic_dict.update({id:text})
return topic_dict
def cluster_dist_to_write(dist_by_cluster):
topics_dist = [generate_pd(x) for x in dist_by_cluster]
topics_dict = generate_topics_dict(topics_dist)
return topics_dict
def lemmatize_stemming(text):
return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
def preprocess_stem_clean(text):
result = []
text = remove_url(text)
for token in gensim.utils.simple_preprocess(text):
if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > 2:
result.append(lemmatize_stemming(token))
return result
def remove_url(text):
text = re.sub(r"http\S+", "", text) #remove URLs from the text
return text
def match_cluster_to_label_dist(cluster_assigned_values,cluster_label_dist):
predicitions = []
for predicition in cluster_assigned_values:
cluster_id = np.argmax(predicition)
label_dist = cluster_label_dist[cluster_id]
predicitions.append(label_dist)
return np.array(predicitions)
def map_raw_label_to_label_choice(choices,prediction):
result = {}
for x,y in zip(choices,prediction):
result[x] = y
return result
def relu(value):
if value>0:
return value
else:
return 0
def transform_for_lda(vectors):
result_vectors = [relu(vector) for vector in vectors] #3 for 50 window size
return result_vectors
def delete_model_drive(path):
if os.path.exists(path):
try:
os.remove(path)
print("model file deleted")
except:
os.rmdir(path)
print("model folder deleted")