-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
1142 lines (1039 loc) · 45 KB
/
model.py
File metadata and controls
1142 lines (1039 loc) · 45 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
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# %%
import pandas as pd
import numpy as np
import traceback
import sqlite3
import joblib
import io
import os
from sklearn.metrics.pairwise import euclidean_distances
from util import *
def adapt_array(arr):
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
return sqlite3.Binary(out.read())
def convert_array(text):
try:
out = io.BytesIO(text)
out.seek(0)
return np.load(out)
except ValueError:
try:
return int(text)
except Exception:
try:
return float(text)
except Exception:
return text
sqlite3.register_adapter(np.ndarray, adapt_array)
sqlite3.register_converter("array", convert_array)
def clean_file(_type):
for root, _, files in os.walk("saves"):
for name in files:
if _type in name:
os.remove(os.path.join(root, name))
def LockModel(fn:callable):
def wrapper(model, *a, **k):
result = None
if model.busy:
model.log.busy()
return
try:
model.busy = True
result = fn(model, *a, **k)
except Exception:
traceback.print_exc()
model.log.error("Unhandled server error")
model.busy = False
return result
return wrapper
class Model:
def __init__(self, name:str=None):
if name is None:
from datetime import datetime
now = datetime.now()
name = now.strftime("model-%Y_%m_%d_%H_%M_%S")
self.name = name
self.log = Logger()
self.trainset = Model.__newset()
self.devset = Model.__newset()
self.adhocset = None
self.busy = False
self.embedding_model = None
self.reduction_model = None
self.clustering_model = None
self.projection_model = None
self.model_params = {}
self.group_stats = None
self.group_summaries = None
self.group_tree = None
self.group_ctree = None
@classmethod
def __newset(cls):
df = pd.DataFrame(None, columns=['text', 'embedding', 'umap', 'x', 'y', 'ts', 'ref', 'target', 'predict', 'distance'])
df["x"] = df["x"].astype(int)
df["y"] = df["y"].astype(int)
df["predict"] = df["predict"].astype(int)
df["distance"] = df["distance"].astype(int)
return df
def set_client(self, ws):
self.log.ws = ws
def rename(self, name:str=None, **kwargs):
if name is None:
from datetime import datetime
now = datetime.now()
name = now.strftime("model-%Y_%m_%d_%H_%M_%S")
self.name = name
self.log.finished()
@LockModel
def clean_dataset(self, **param):
if "set" not in param:
self.log.invalid("Must specify which set to clean")
if param["set"] == "dev":
self.devset = Model.__newset()
elif param["set"] == "train":
self.trainset = Model.__newset()
self.group_stats = None
self.group_summaries = None
self.group_tree = None
self.group_ctree = None
self.log.finished("Datasets cleaned")
def __delete_rows(self, **param):
if "train" in param:
self.trainset.drop(param["train"], inplace=True)
self.trainset.reset_index(drop=True, inplace=True)
if "dev" in param:
self.devset.drop(param["dev"], inplace=True)
self.devset.reset_index(drop=True, inplace=True)
@LockModel
def delete_rows(self, **param):
try:
self.__delete_rows(**param)
self.log.finished("Rows deleted")
except Exception:
traceback.print_exc()
self.log.error("Failed deleting rows")
@LockModel
def copy_rows(self, **param):
totrain = True if 'to' in param and param['to'] == 'train' else False
src = self.devset if totrain else self.trainset
ismove = 'type' in param and param['type']=='move'
if ismove and not self.__can_modify_trainset():
self.log.error("Cannot modify trainset after model is trained")
return
try:
df = src.loc[param['rows']].copy()
if totrain:
self.trainset = self.trainset.append(df).reset_index(drop=True)
else:
self.devset = self.devset.append(df).reset_index(drop=True)
if ismove:
self.__delete_rows(**{'dev' if totrain else 'train': param['rows']})
#src.drop(index=param['rows'], inplace=True)
self.log.finished("%s %d rows from %s to %s" % (
'Moved' if ismove else 'Copied',
df.shape[0],
'devset' if totrain else 'trainset',
'trainset' if totrain else 'devset'))
except Exception:
traceback.print_exc()
self.log.error("Failed copying rows")
def __reset_columns(self, **param):
try:
isdevset = True if 'set' in param and param['set'] == 'dev' else False
dataset = self.devset if isdevset else self.trainset
for col in ["embedding", "umap", 'x', 'y', 'predict']:
if col in param and param[col]:
dataset[col] = np.nan
if col in ['embedding', 'umap']:
dataset[col] = dataset[col].astype(object)
if col == 'predict' and isdevset:
dataset['distance'] = np.nan
return True
except Exception:
traceback.print_exc()
self.log.error("Failed resetting columns")
return False
@LockModel
def reset_columns(self, **param):
if self.__reset_columns(**param):
self.log.finished("Columns resetted")
def __create_embedding_model(self, **param):
log = self.log
try:
log.working("Creating embedding model...")
if "type" in param: del param["type"]
from sentence_transformers import SentenceTransformer
with LogRedirect(self.log):
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
log.working("Embedding to be done on %s" % device)
self.embedding_model = SentenceTransformer(param["model"], device=device)
return True
except Exception:
traceback.print_exc()
log.error("Embedding model creation failed")
return False
def __create_reduction_model(self, **param):
log = self.log
try:
if param and 'verbose' in param:
del param['verbose']
log.working("Creating reduction model...")
if "type" in param: del param["type"]
from umap import UMAP
self.reduction_model = UMAP(verbose=True, **param)
return True
except Exception:
traceback.print_exc()
log.error("Reduction model creation failed")
return False
def __create_clustering_model(self, **param):
log = self.log
try:
if param and 'prediction_data' in param:
del param['prediction_data']
log.working("Creating clustering model...")
if "type" in param: del param["type"]
from hdbscan import HDBSCAN
self.clustering_model = HDBSCAN(prediction_data=True, **param)
return True
except Exception:
traceback.print_exc()
log.error("Clustering model creation failed")
return False
def __create_projection_model(self, **param):
log = self.log
try:
log.working("Creating projection model...")
from umap import UMAP
self.projection_model = UMAP(metric="euclidean", n_components=2, verbose=True)
return True
except Exception:
traceback.print_exc()
log.error("Projection model creation failed")
return False
@LockModel
def create_model(self, **param):
log = self.log
with LogRedirect(self.log):
log.working("Creating model...")
if "embedding" in param:
self.model_params['embedding'] = param['embedding']
if not self.__create_embedding_model(**param["embedding"]): return
if "umap" in param:
self.model_params['umap'] = param['umap']
if not self.__create_reduction_model(**param["umap"]): return
if "hdbscan" in param:
self.model_params['hdbscan'] = param['hdbscan']
if not self.__create_clustering_model(**param["hdbscan"]): return
if "projection" in param:
self.model_params['projection'] = param['projection']
if not self.__create_projection_model(**param["projection"]): return
log.finished("Model created")
@classmethod
def load(cls, log:Logger, path:str):
try:
with open(path, 'rb') as file:
model = joblib.load(file)
model.log = log
# when saving model, it is always in busy state so have to reset it now
model.busy = False
if model.embedding_model == True:
model.__create_embedding_model(**model.model_params['embedding'])
log.finished("Model loaded")
return model
except Exception:
traceback.print_exc()
log.error("Model loading failed")
return None
@LockModel
def save(self, filename:str):
# Do not save embedding model as it is big and off-the-shelf
embed_model = self.embedding_model
group_tree = self.group_tree
self.embedding_model = (self.embedding_model is not None)
self.group_tree = (self.group_tree is not None)
try:
with open(filename, 'wb') as file:
# Do not save log as it is session dependent
log = self.log
self.log = None
joblib.dump(self, file)
self.log = log
# restore vars
self.embedding_model = embed_model
self.group_tree = group_tree
return True
except Exception:
traceback.print_exc()
self.log = log
return False
def save_for_download(self, filename:str = "./saves/model.model", **kwargs):
log = self.log
if self.save(filename):
log.finished("Model ready to be downloaded", {"path": filename})
else:
log.error("Model saving failed")
@LockModel
def load_from_csv(self, **param):
log = self.log
if not "text" in param:
log.invalid()
return
try:
usecols = [param['text']]
names=['text']
for col in ['ts', 'ref', 'target', 'distance', 'predict', 'set']:
if col in param and not str(param[col]).startswith('fixed:'):
usecols.append(param[col])
names.append(col)
names = np.array(names)[np.argsort(usecols)]
usecols = np.sort(usecols)
print(usecols)
print(names)
filename= param["filename"] if "filename" in param else "uploaded.csv"
comment = None
skiprows = None
if 'comment' in param: comment = param['comment']
if 'skipfirst' in param: skiprows = int(param['skipfirst'])
df = pd.read_csv(filename, usecols=usecols, names=names, encoding='utf-8', header=None, comment=comment, skiprows=skiprows)
for col in ['ts', 'ref', 'target', 'distance', 'predict', 'set']:
if col in param and str(param[col]).startswith('fixed:'):
val = param[col].replace('fixed:', '')
df[col] = val
if "ts" in df: df["ts"] = pd.to_datetime(df["ts"], errors="coerce")
df = df[df["text"].notna()]
if 'set' in df.columns:
self.devset = self.devset.append(df[df.set=='dev'].drop(columns=['set']), ignore_index=True)
self.trainset = self.trainset.append(df[df.set!='dev'].drop(columns=['set', 'distance']), ignore_index=True)
else:
self.trainset = self.trainset.append(df, ignore_index=True)
log.finished("Dataset loaded")
except Exception:
traceback.print_exc()
log.error("Dataset load error")
@LockModel
def load_from_json(self, **param):
log = self.log
if not 'text' in param:
log.invalid()
return
try:
filename= param["filename"] if "filename" in param else "uploaded.json"
df = pd.read_json(filename, orient='index')
columns = {}
keepcolumn = []
for col in ['text', 'ts', 'ref', 'target', 'predict', 'distance', 'set']:
if col in param and not str(param[col]).startswith('fixed:'):
columns[param[col]] = col
keepcolumn.append(param[col])
df = df[keepcolumn].rename(columns = columns)
for col in ['ts', 'ref', 'target', 'predict', 'distance', 'set']:
if col in param and str(param[col]).startswith('fixed:'):
val = param[col].replace('fixed:', '')
df[col] = val
if "ts" in df: df["ts"] = pd.to_datetime(df["ts"], errors="coerce")
df = df[df["text"].notna()]
if not 'text' in df.columns:
log.error("Dataset has not text column")
return
if 'set' in df.columns:
self.devset = self.devset.append(df[df.set=='dev'].drop(columns=['set']), ignore_index=True)
self.trainset = self.trainset.append(df[df.set!='dev'].drop(columns=['set', 'distance']), ignore_index=True)
else:
self.trainset = self.trainset.append(df, ignore_index=True)
log.finished("Dataset loaded")
except Exception:
traceback.print_exc()
log.error("Dataset load error")
@LockModel
def load_from_sqlite(self, **param):
log = self.log
if not 'text' in param:
log.invalid()
return
filename = param["filename"] if "filename" in param else "uploaded.sqlite3"
table = param["table"] if "table" in param else "dataset"
try:
con = sqlite3.connect(filename, detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
cur.execute("SELECT COUNT(*) FROM " + table)
count = cur.fetchone()[0]
log.working("Loading %d records" % count)
sql = 'SELECT %s AS text' % (param['text'])
for col in ['embedding', 'umap', 'x', 'y', 'z', 'ts', 'ref', 'target', 'predict', 'distance', 'set']:
if col in param and not str(param[col]).startswith('fixed:'):
sql += ', %s AS %s' % (param[col], col)
sql += " FROM %s" % (table)
df = pd.read_sql(sql, con)
for col in ['ts', 'ref', 'target', 'predict', 'distance', 'set']:
if col in param and str(param[col]).startswith('fixed:'):
val = param[col].replace('fixed:', '')
df[col] = val
if "ts" in df: df["ts"] = pd.to_datetime(df["ts"], errors="coerce")
df = df[df["text"].notna()]
if 'set' in df.columns:
self.devset = self.devset.append(df[df.set=='dev'].drop(columns=['set']), ignore_index=True)
self.trainset = self.trainset.append(df[df.set!='dev'].drop(columns=['set', 'distance']), ignore_index=True)
else:
self.trainset = self.trainset.append(df, ignore_index=True)
log.finished("Dataset loaded")
except Exception:
traceback.print_exc()
log.error("Dataset load error")
def load_dataset(self, **param):
if 'filename' not in param or 'type' not in param:
self.log.invalid('Specify at least filename or file type')
if 'type' in param:
type = param['type']
del param['type']
else:
import os
_, type = os.path.splitext(param['filename'])
if type == 'csv':
self.load_from_csv(**param)
elif type == 'json':
self.load_from_json(**param)
elif type in ['sqlite', 'sqlite3']:
self.load_from_sqlite(**param)
dataset = self.devset if "set" in param and param['set'] == 'dev' else self.trainset
dataset.drop_duplicates(subset=['text'], inplace=True)
dataset.dropna(subset=['text'], inplace=True)
dataset.reset_index(drop=True, inplace=True)
# @LockModel
# def save_to_sqlite(self, filename:str=None, **kwargs):
# log = self.log
# if self.trainset.shape[0] == 0 and self.devset.shape[0] == 0:
# log.error("Nothing to save")
# return
# if filename is None:
# filename = 'saves/%s.sqlite3' % self.name
# try:
# con = sqlite3.connect(filename, detect_types=sqlite3.PARSE_DECLTYPES)
# dtypes = {
# "embedding": "array",
# "umap": "array"
# }
# self.trainset.to_sql("train", con, dtype=dtypes, if_exists='replace')
# self.devset.to_sql("test", con, dtype=dtypes, if_exists='replace')
# con.close()
# except Exception:
# traceback.print_exc()
# log.error("Dataset save error")
@LockModel
def save_to_csv(self, filename:str=None, **kwargs):
log = self.log
if filename is None:
filename = 'saves/%s.csv' % self.name
log.working("Preparing dataset file, a moment...")
try:
dataset = self.trainset.copy()
dataset['set'] = 'train'
devset = self.devset.copy()
devset['set'] = 'dev'
dataset = dataset.append(devset)
del devset
if dataset.shape[0] == 0:
log.error("Nothing to save")
return
with open(filename, "w", encoding='utf8', newline='') as f:
f.write("#ABCExportv1\n")
dataset.to_csv(f, columns=["text", "ref", "ts", "target", "predict", "distance", "set"], index=False)
log.finished("Dataset ready to be downloaded", {"path": filename})
except Exception:
traceback.print_exc()
log.error("Dataset save error")
# DO NOT use log.finish as this is inner private function with no busy flag rights!
def __create_embeddings(self, **param):
log = self.log
if self.embedding_model is None:
log.invalid("Create embedding model first")
return False
dataset = self.devset if (param and "set" in param and param["set"] == 'dev') else self.trainset
to_embed = dataset[dataset.embedding.isna()]
if to_embed.shape[0] == 0:
log.working("Nothing to do")
return True
log.working("Creating embeddings...")
try:
with LogRedirect(log):
embs = self.embedding_model.encode(to_embed["text"].to_list(), show_progress_bar = True)
for i in range(to_embed.shape[0]):
dataset.at[to_embed.index[i], "embedding"] = embs[i]
log.working("Finished embeddings")
return True
except Exception:
traceback.print_exc()
log.error("Embedding failed")
return False
# DO NOT use log.finish as this is inner private function with no busy flag rights!
def __reduce_dimension(self, **param):
log = self.log
if self.reduction_model is None:
log.invalid("Create reduction model first")
return False
istest = (param and "set" in param and param["set"] == 'dev')
dataset = self.devset if istest else self.trainset
to_reduce = dataset # you must do against the whole set!
if to_reduce.shape[0] == 0:
log.working("Nothing to do")
return True
if dataset["embedding"].isna().sum() > 0:
log.invalid("Some rows not yet has embedding created, create embeddings first")
return False
log.working("Reducing dimension...")
try:
with LogRedirect(log):
embeddings = to_reduce["embedding"].to_list()
if not istest:
if to_reduce["target"].notna().sum() > 0:
self.reduction_model.fit(embeddings, y=to_reduce["target"].fillna(-1))
else:
self.reduction_model.fit(embeddings)
umaps = np.nan_to_num(self.reduction_model.transform(embeddings))
for i in range(to_reduce.shape[0]):
dataset.at[to_reduce.index[i], "umap"] = umaps[i]
log.working("Finished reduction")
return True
except Exception:
traceback.print_exc()
log.error("Reduction failed")
return False
# DO NOT use log.finish as this is inner private function with no busy flag rights!
def __do_clustering(self, **param):
log = self.log
if self.clustering_model is None:
log.invalid("Create clustering model first")
return False
istest = (param and "set" in param and param["set"] == 'dev')
dataset = self.devset if istest else self.trainset
to_cluster = dataset # you must cluster the whole set!
if to_cluster.shape[0] == 0:
log.working("Nothing to do")
return True
if dataset["umap"].isna().sum() > 0:
log.invalid("Some rows not yet has dimension reduced, do dimension reduction first")
return False
log.working("Clustering...")
try:
with LogRedirect(log):
if not istest:
self.clustering_model.fit(to_cluster["umap"].to_list())
clusters = self.clustering_model.labels_
else:
import hdbscan
clusters, _ = hdbscan.approximate_predict(self.clustering_model, to_cluster['umap'].to_list())
dataset.at[dataset.predict.isna(), "predict"] = clusters
log.working("Finished clustering")
return True
except Exception:
traceback.print_exc()
log.error("Clustering failed")
return False
def __soft_clustering(self, **param):
log = self.log
if self.group_summaries is None:
log.invalid("Fully train the model first")
return False
log.working("Clustering...")
try:
dataset = self.devset
dists = euclidean_distances(X=dataset['umap'].to_list(), Y=np.array(self.group_summaries["mean"].to_list()))
ranks = dists.argsort(axis=1)[:, 0]
dists.sort(axis=1)
dists = dists[:, 0]
dataset['predict'] = ranks
dataset['distance'] = dists
log.working("Finished clustering")
return True
except Exception:
traceback.print_exc()
log.error("Clustering failed")
return False
# DO NOT use log.finish as this is inner private function with no busy flag rights!
def __generate_projections(self, **param):
log = self.log
# if self.projection_model is None:
# self.__create_projection_model()
# 15/11 Always generate a new model in case of model change
self.__create_projection_model()
istest = (param and "set" in param and param["set"] == 'dev')
dataset = self.devset if istest else self.trainset
to_project = dataset[dataset.x.isna()]
if to_project.shape[0] == 0:
log.working("Nothing to do")
return True
log.working("Generating 3D projections...")
try:
with LogRedirect(log):
if not istest: self.projection_model.fit(dataset.umap.tolist(), y=dataset.predict.tolist())
xyzs = self.projection_model.transform(dataset.umap.tolist())
for i in range(to_project.shape[0]):
# dataset.at[to_project.index[i], ['x', 'y', 'z']] = xyzs[i]
dataset.at[to_project.index[i], ['x', 'y']] = xyzs[i]
log.working("Finished projections")
return True
except Exception:
traceback.print_exc()
log.error("3D projections generation failed")
return False
# DO NOT use log.finish as this is inner private function with no busy flag rights!
def __summarize_result(self, **kwargs):
log = self.log
try:
dataset = self.trainset
isdev = False
if dataset["predict"].isna().sum() > 0:
log.invalid("Some rows has not yet predicted, do prediction first")
return False
nogroup = dataset[dataset.predict == -1].predict.count()
hasgroup = dataset.shape[0] - nogroup
group_counts = dataset[dataset.predict != -1].groupby("predict")["text"].count()
if dataset.x.isna().sum() > 0:
self.__generate_projections(set="dev" if isdev else "train")
log.working("Gathering infos...")
group_mean = []
group_x = []
group_y = []
# group_z = []
group_distances = []
group_rel_size = []
group_repr_text = []
for i in range(group_counts.shape[0]):
data = dataset[dataset.predict == i]
mean = data.umap.mean()
distances = euclidean_distances(data.umap.to_list(), Y=[mean])
x = data.x.mean()
y = data.y.mean()
# z = data.z.mean()
repr_id = np.argmin(distances)
distance = np.max(distances)-np.min(distances)
group_mean.append(mean)
group_distances.append(distance)
group_x.append(x)
group_y.append(y)
# group_z.append(z)
group_rel_size.append(distance)
group_repr_text.append(data.iloc[repr_id].text)
mean = np.mean(np.array(group_mean), axis=0)
max_distance = float(np.max(euclidean_distances(np.array(group_mean), Y=[mean]))) * 2
self.group_stats = {
"num_group": int(group_counts.shape[0]),
"no_group": int(nogroup),
"has_group": int(hasgroup),
"max_distance": max_distance,
}
self.group_summaries = pd.DataFrame(data={
"group": np.arange(group_counts.shape[0]),
"mean": group_mean,
"x": group_x,
"y": group_y,
# "z": group_z,
"count": group_counts,
"distance": group_distances,
"rel_size": group_rel_size / np.min(group_rel_size),
"repr_text": group_repr_text,
})
log.working("Creating tree and graphs...")
return self.__create_group_ctree()
except Exception:
traceback.print_exc()
log.error("Summary generation failed")
return False
def __create_group_ctree(self):
try:
group_summaries = self.group_summaries
ctree = self.clustering_model.condensed_tree_.to_pandas()
# sel contains one text sample per group
sel = ctree[ctree.child_size==1].groupby("parent").first()
# s2 contains all nodes and group-leaves
s2 = ctree[ctree.child_size > 1].copy()
# put real group number into pd frame
def applygroup(x):
s2.loc[s2.child==int(x.name), "group"] = self.trainset.iloc[int(x.child)]["predict"]
sel.apply(applygroup, axis=1)
# remove -1 group
s2.loc[s2.group==-1, "group"] = np.nan
def nullgroup(x):
s2.loc[s2.child==x, "group"] = np.nan
for i in range(group_summaries.shape[0]):
# remove repeated groups
s3 = s2[s2.group==i]["child"].sort_values()
if s3.count() > 1:
s3.iloc[1:].apply(nullgroup)
# add stats
s2.loc[s2.group==i, "repr_text"] = group_summaries.at[i, "repr_text"]
self.group_ctree = s2
return True
except Exception:
traceback.print_exc()
self.group_ctree = None
self.log.error("CTree generation failed")
return False
def __generate_group_tree_from_ctree(self):
try:
s2 = self.group_ctree
cache = {} # cache for a recusive tree
def topbottom(x):
#
# find parent
#
parentid = int(x.parent)
if parentid not in cache:
node = {"id": -parentid, "children": []}
cache[parentid] = node
self.group_tree = node
parent = cache[parentid]
#
# add to parent as child
#
node = {
# "id": int(x.child),
"count": int(x.child_size),
# "lambda": float(x.lambda_val)
}
# add node to cache
cache[int(x.child)] = node
if 'children' not in parent: # this is a trimmed node, skip it
return
if not np.isnan(x.group):
group = int(x.group)
node["id"] = group
node["label"] = "[%d] %s" % (group, x.repr_text)
else:
lamda = float(x.lambda_val)
node["id"] = -int(x.child)
node["label"] = "[%d] %f" % (int(x.child), lamda)
node["children"] = []
# if "children" in parent:
parent["children"].append(node)
s2.apply(topbottom, axis=1)
return True
except Exception:
traceback.print_exc()
self.group_tree = None
self.log.error("Tree generation failed")
return False
# # DO NOT use log.finish as this is inner private function with no busy flag rights!
# def __create_group_tree(self):
# group_summary = self.group_summaries
# group_counts = group_summary['count']
# try:
# if group_summary is None:
# return False
# try:
# cache = self.clustering_model.condensed_tree_
# except Exception:
# self.create_clustering_model()
# self.do_clustering()
# cache = self.clustering_model.condensed_tree_
# treedf = cache.to_pandas()
# # sel contains one text sample per group
# sel = treedf[treedf.child_size==1].groupby("parent").first()
# # s2 contains all nodes and group-leaves
# s2 = treedf[treedf.child_size > 1].copy()
# #
# # put real group number into pd frame
# #
# def applygroup(x):
# s2.loc[s2.child==int(x.name), "group"] = self.trainset.iloc[int(x.child)]["predict"]
# sel.apply(applygroup, axis=1)
# #
# # remove -1 group or repeated groups
# # Note: repeated groups are trimmed branches
# #
# def nullgroup(x):
# s2.loc[s2.child==x, "group"] = np.nan
# s2.loc[s2.group==-1, "group"] = np.nan
# for i in range(group_counts.shape[0]):
# s3 = s2[s2.group==i]["child"].sort_values()
# if s3.count() > 1:
# s3.iloc[1:].apply(nullgroup)
# #
# # top bottom to build the tree
# # Note: s2 should be a table sorted top-bottom by default
# #
# cache = {} # cache for a recusive tree
# condensed = [] # list for a condensed tree
# def topbottom(x):
# #
# # find parent
# #
# parentid = int(x.parent)
# if parentid not in cache:
# node = {"id": -parentid, "children": []}
# cache[parentid] = node
# self.group_tree = node
# condensed.append({"id": -parentid, "parent": None})
# parent = cache[parentid]
# #
# # add to parent as child
# #
# node = {
# # "id": int(x.child),
# "count": int(x.child_size),
# # "lambda": float(x.lambda_val)
# }
# cnode = {
# # "id": int(x.child),
# "count": int(x.child_size),
# 'parent': -int(x.parent),
# }
# # add node to cache
# cache[int(x.child)] = node
# if 'children' not in parent: # this is a trimmed node, skip it
# return
# if not np.isnan(x.group):
# group = int(x.group)
# node["id"] = group
# node["label"] = "[%d] %s" % (group, group_summary.loc[group, "repr_text"])
# cnode['id'] = group
# cnode['label'] = node["label"]
# else:
# lamda = float(x.lambda_val)
# node["id"] = -int(x.child)
# node["label"] = "[%d] %f" % (int(x.child), lamda)
# node["children"] = []
# cnode['id'] = node["id"]
# cnode['label'] = node["label"]
# # if "children" in parent:
# parent["children"].append(node)
# condensed.append(cnode)
# s2.apply(topbottom, axis=1)
# del cache
# return True
# except Exception:
# traceback.print_exc()
# self.group_tree = None
# self.log.error("Tree generation failed")
# return False
@LockModel
def train(self, **param):
param['set'] = 'train'
self.log.working("Begin model training...")
cols_to_reset = {}
if "reset_embedding" in param and param["reset_embedding"]:
cols_to_reset["embedding"] = True
if "reset_reduction" in param and param["reset_reduction"]:
cols_to_reset["umap"] = True
if "reset_clustering" in param and param["reset_clustering"]:
cols_to_reset["predict"] = True
if "reset_summarize" in param and param["reset_summarize"]:
cols_to_reset["x"] = True
cols_to_reset["y"] = True
#cols_to_reset["z"] = True
if len(cols_to_reset):
cols_to_reset['set'] = 'train'
if not self.__reset_columns(**cols_to_reset): return
if "reset_summarize" in param and param["reset_summarize"]:
self.group_summaries = None
self.group_stats = None
self.group_ctree = None
self.group_tree = None
if not "embedding" in param or param["embedding"]:
if not self.__create_embeddings(**param): return
if not "reduction" in param or param["reduction"]:
if not self.__reduce_dimension(**param): return
if not "clustering" in param or param["clustering"]:
if not self.__do_clustering(**param): return
if not "summarize" in param or param["summarize"]:
if not self.__summarize_result(**param): return
self.log.finished("Finished model training")
@LockModel
def test(self, **param):
param['set'] = 'dev'
self.log.working("Begin testing with devset...")
cols_to_reset = {}
if "reset_embedding" in param and param["reset_embedding"]:
cols_to_reset["embedding"] = True
if "reset_reduction" in param and param["reset_reduction"]:
cols_to_reset["umap"] = True
if "reset_clustering" in param and param["reset_clustering"]:
cols_to_reset["predict"] = True
if "reset_summarize" in param and param["reset_summarize"]:
cols_to_reset["x"] = True
cols_to_reset["y"] = True
if len(cols_to_reset):
cols_to_reset['set'] = 'dev'
if not self.__reset_columns(**cols_to_reset): return
if not "embedding" in param or param["embedding"]:
if not self.__create_embeddings(**param): return
if not "reduction" in param or param["reduction"]:
if not self.__reduce_dimension(**param): return
if not "clustering" in param or param["clustering"]:
if not self.__soft_clustering(**param): return
if not "summarize" in param or param["summarize"]:
if not self.__generate_projections(**param): return
self.log.finished("Testing result ready")
@LockModel
def adhoc_predict(self, texts:list = None, detailed:bool = False, verbose: bool = False, embeddings:list = None, **kwargs):
log = self.log
if self.embedding_model is None:
log.invalid("Create embedding model first")
return
if self.reduction_model is None:
log.invalid("Create reduction model first")
return
if self.trainset.predict.isna().sum() > 0:
log.invalid("Do training first")
return
if self.group_summaries is None:
log.invalid("Generate group summaries first")
return
if texts is None and embeddings is None:
log.invalid("No input")
return
try:
if embeddings is None:
if verbose: log.working("Creating embeddings...")
embeddings = self.embedding_model.encode(texts)
if verbose: log.working("Dimension reduction...")
umaps = self.reduction_model.transform(embeddings)
#from pprint import pprint
#pprint(umaps)
if detailed:
xyzs = self.projection_model.transform(umaps)
xs = xyzs[:, 0]
ys = xyzs[:, 1]
#zs = xyzs[:, 2]
if verbose: log.working("Clustering...")
#
# 2 aproaches:
# 1 is use approximate_predict from hdbscan
# 1 is predict from distance info gathered from generate_group_stats
# (which should be the same as approximate_predict)
#
# # use approximate_predict