-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcategorization.py
More file actions
1816 lines (1433 loc) · 58.4 KB
/
categorization.py
File metadata and controls
1816 lines (1433 loc) · 58.4 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 os
import PIL.Image as Image
import numpy as np
import tensorflow as tf
import pandas as pd
from itertools import combinations
from scipy.spatial.distance import pdist, squareform, cdist
from scipy import stats
from treelib import Tree
from HiPart.clustering import IPDDP
import sklearn.metrics as metrics
# List folders
def build_categories_from_ecoset(path, synsets=None, maxImgs=None, includeSub=True):
"""
Return dictionary of the images and counts for each category in path.
Only keep synsets in the list synsets if defined and only keep the first
maxImgs images.
"""
cats = {}
for name in os.listdir(path):
if os.path.isdir(os.path.join(path, name)):
cats[name] = {}
# List folders in that folder
for name2 in os.listdir(os.path.join(path, name)):
if os.path.isdir(os.path.join(path, name, name2)):
if includeSub:
cats[name][name2] = {}
# List images in that folder
images = os.listdir(os.path.join(path, name, name2))
# Split images into their categories
subCats = [img.split("_")[0] for img in images]
# Keep unique
subCats = list(set(subCats))
# Filter synsets
if synsets is not None:
subCats = [
subCat for subCat in subCats if subCat in synsets
]
# Fill dictionary with each category and its images
for subCat in subCats:
imgs = [
os.path.join(path, name, name2, img)
for img in images
if subCat in img
]
# Keep only the first maxImgs images
if maxImgs is not None:
imgs = imgs[:maxImgs]
cats[name][name2][subCat] = imgs
else:
imgs = os.listdir(os.path.join(path, name, name2))
if synsets is not None:
imgs = [img for img in imgs if img.split("_")[0] in synsets]
if maxImgs is not None:
imgs = imgs[:maxImgs]
cats[name][name2] = [
os.path.join(path, name, name2, img) for img in imgs
]
# Get counts of each image in each category
counts = {}
for cat in cats:
counts[cat] = {}
for subCat in cats[cat]:
if includeSub:
counts[cat][subCat] = {}
for img in cats[cat][subCat]:
counts[cat][subCat][img] = len(cats[cat][subCat][img])
else:
counts[cat][subCat] = len(cats[cat][subCat])
return cats, counts
def build_df_from_dir(directory, cats=[]):
"""
Return a dataframe where each row is an image recursively where each row
is an image with extra columns based on how deep in the directory structure
it is.
"""
# Create pandas dataframe
df = pd.DataFrame(
columns=["path", "name", "cat1"] + [f"cat{i + 2}" for i in range(len(cats))]
)
fileList = os.listdir(directory)
# Ignore hidden files
fileList = [file for file in fileList if not file.startswith(".")]
for name in fileList:
if os.path.isdir(os.path.join(directory, name)):
newRow = build_df_from_dir(
os.path.join(directory, name), cats=cats + [name]
)
newRow
else:
newRow = pd.DataFrame(
[[os.path.join(directory, name), name] + cats + [name]],
columns=["path", "name", "cat1"]
+ [f"cat{i + 2}" for i in range(len(cats))],
)
df = pd.concat([df, newRow], sort=False, ignore_index=True)
# If name is equal to the last column, remove last column
if df["name"].equals(df.iloc[:, -1]):
df = df.iloc[:, :-1]
return df
def get_images_from_cat(cats, preprocFun=None):
"""
Return a dictionary of loaded images from path recursively.
"""
imgs = {}
for key, values in cats.items():
if isinstance(values, dict):
imgs[key] = get_images_from_cat(values, preprocFun=preprocFun)
else:
tmp = [Image.open(img) for img in values]
if preprocFun is not None:
tmp = [preprocFun(img) for img in tmp]
# Stack tmp
tmp = tf.concat(tmp, axis=0)
imgs[key] = tmp
return imgs
def gcm_sim(rep1, rep2, r=2.0, c=1.0, p=1.0):
"""
Return the GCM similarity between two representations with equal attention
weights.
"""
weights = np.ones(rep1.shape[0]) / rep1.shape[0]
dist = np.sum(weights * (np.abs(rep1 - rep2) ** r)) ** (1.0 / r)
return np.exp(-c * dist**p)
def gcm_sim_thresholded(rep1, rep2, r=2.0, c=1.0, p=1.0, threshold=1):
"""
Return the GCM similarity between two representations with equal attention
but only calculate the distance between features that are above threshold.
"""
# Figure out which features are above threshold
rep1Thresh = rep1 > threshold
rep2Thresh = rep2 > threshold
rep1Threshed = rep1[rep1Thresh | rep2Thresh]
rep2Threshed = rep2[rep1Thresh | rep2Thresh]
return gcm_sim(rep1Threshed, rep2Threshed, r=r, c=c, p=p)
def prod_sim(rep1, rep2):
"""
Return the similarity between two representations using the product rule.
"""
assert np.all(rep1.shape == rep2.shape)
# Copy reps
rep1 = rep1.copy()
rep2 = rep2.copy()
# Normalize both reps between 0 and 1
rep1 = rep1 / np.sum(rep1)
rep2 = rep2 / np.sum(rep2)
# Compute absolute difference
diff = np.abs(rep1 - rep2)
# Flip such that 1 is perfectly matched
diff = np.abs(diff - 1)
return np.prod(diff)
def prod_sim_binary(rep1, rep2, s=0.3, threshold=0.5):
"""
Return the simliarity between two representations using the product rule
after binarizing the representations based on similarity threshold.
"""
assert np.all(rep1.shape == rep2.shape)
# Copy reps
rep1 = rep1.copy()
rep2 = rep2.copy()
# Normalize both reps between 0 and 1
rep1 = rep1 / np.sum(rep1)
rep2 = rep2 / np.sum(rep2)
# Compute absolute difference
diff = np.abs(rep1 - rep2)
# Flip such that 1 is perfectly matched
diff = np.abs(diff - 1)
# Binarize
diff[diff < threshold] = s
return np.prod(diff)
def contrast_sim(rep1, rep2, threshold=0.0):
"""
Return similarity based on the contrast model where a feature is not
present if the value of a feature is equal or less than the threshold.
Uses equal weighting for overlap and distinct features for each
representation.
"""
assert np.all(rep1.shape == rep2.shape)
# Copy reps
rep1 = rep1.copy()
rep2 = rep2.copy()
# Binarize reps
rep1[rep1 <= threshold] = 0
rep2[rep2 <= threshold] = 0
rep1[rep1 > threshold] = 1
rep2[rep2 > threshold] = 1
# Compute overlap
overlap = np.sum(rep1 * rep2)
# Count distinct feature
distinct = np.sum(np.abs(rep1 - rep2))
if (sim := overlap - distinct) < 0:
sim = 0.0
return sim
def calculate_typicality(reps, simFun, nExemplars=None):
"""
Return the typicality of each item given a category defined by the
representation.
"""
if isinstance(reps, list):
reps = np.concatenate(reps, axis=0)
typicalities = np.empty(reps.shape[0])
for i, rep in enumerate(reps):
# Remove rep row from reps
reps_ = reps.copy()
reps_ = np.delete(reps_, i, axis=0)
if nExemplars is not None:
# Keep only nExemplars
reps_ = reps_[np.random.choice(reps_.shape[0], nExemplars, replace=False)]
# Calculate typicality
typ = np.sum(np.apply_along_axis(lambda x: simFun(rep, x), 1, reps_))
typicalities[i] = typ
return typicalities
def feature_select(rep, b=0.0, d=0.8):
"""
Return an logical array for the features selected in rep based on the
threshold activation b and the a threshold porotion of d.
"""
# Binarize representation
rep = rep > b
# Sum features across samples
repCount = np.sum(rep, axis=0)
# Return which features exceed proportion
return repCount > (rep.shape[0] * d)
def redist_evidence(
testRep, targetRep, altRep, simFun, b=0.0, d=0.8, dist_penalty=False
):
"""
Calculate evidence that testRep is the category targetRep against the
alternative altRep given redundancy and distinctiveness using a threshold
activation b and a threshold proportion of d.
"""
# Determine what features each category has
targetFeatures = feature_select(targetRep, b=b, d=d)
# Filter for selected features in the test representation
testRepSelected = testRep[targetFeatures]
targetRepsSelected = targetRep[:, targetFeatures]
# Calculate similarity between test and target representations
sim = np.sum(
np.apply_along_axis(lambda x: simFun(x, testRepSelected), 1, targetRepsSelected)
)
if dist_penalty:
altFeatures = feature_select(altRep, b=b, d=d)
# Find conjunctions between target and alternative
overlap = np.logical_and(targetFeatures, altFeatures)
# Select features that are overlapped
testOverlapRep = testRep[overlap]
altOverlapRep = altRep[:, overlap]
# Calculate similarity between test and alternative
distPenalty = np.sum(
np.apply_along_axis(lambda x: simFun(x, testOverlapRep), 1, altOverlapRep)
)
sim = sim - distPenalty
return sim
def sim_prob(rep, cat1Rep, cat2Rep, simFun, equalize=False, nExemplars=None):
"""
Return a probability of responding one of two categories represented by
cat1Rep and cat2Rep to a given representation rep. Assumes that the first
dimension of category representations are each exemplar and the second
dimension are features. If either category representations are a list,
concatenate them. If equalize, then the number of exemplar for each
category is equalized by randomly sampling without replacement from the
larger category equal to the smaller category. If nExemplars is not None,
limit the exemplar counts in each representation to nExemplars by random
sampling.
"""
if isinstance(cat1Rep, list):
cat1Rep = np.concatenate(cat1Rep, axis=0)
if isinstance(cat2Rep, list):
cat2Rep = np.concatenate(cat2Rep, axis=0)
if nExemplars is not None and nExemplars < cat1Rep.shape[0]:
cat1Rep = cat1Rep[np.random.choice(cat1Rep.shape[0], nExemplars, False)]
if nExemplars is not None and nExemplars < cat2Rep.shape[0]:
cat2Rep = cat2Rep[np.random.choice(cat2Rep.shape[0], nExemplars, False)]
if equalize:
if cat1Rep.shape[0] < cat2Rep.shape[0]:
cat2Rep = cat2Rep[
np.random.choice(cat2Rep.shape[0], cat1Rep.shape[0], replace=False)
]
else:
cat1Rep = cat1Rep[
np.random.choice(cat1Rep.shape[0], cat2Rep.shape[0], replace=False)
]
rep1 = np.sum(np.apply_along_axis(lambda x: simFun(rep, x), 1, cat1Rep))
rep2 = np.sum(np.apply_along_axis(lambda x: simFun(rep, x), 1, cat2Rep))
return rep1 / (rep1 + rep2), rep2 / (rep1 + rep2)
def LBA_deterministic(d1, d2, k=0, b=1, t0=0):
"""
Return response and response time for a 2 alternate decision task where
each accumulator only differ in their drift rate
"""
rt1 = ((b - k) / d1) + t0
rt2 = ((b - k) / d2) + t0
if rt1 < rt2:
return 1, rt1
else:
return 2, rt2
def get_evidence(rep, catRep, simFun, maxExemplars=None):
"""
Return evidence for this catRep given the rep using simFun. If maxExemplars
is not None, then limit the number of exemplars in the category.
"""
if maxExemplars is not None and maxExemplars < catRep.shape[0]:
choices = np.random.choice(catRep.shape[0], maxExemplars, replace=False)
catRep = catRep[choices]
return np.sum(np.apply_along_axis(lambda x: simFun(rep, x), 1, catRep))
def simulate_cat_verification(
testReps,
memoryReps,
testImgInfo,
memoryImgInfo,
categoryCol,
modelName,
simFun,
criterion,
maxImgs=None,
catRepIdxs=None,
):
"""
Return a dataframe simulating the results of a category verification task.
"""
# Setup dataframe
performance = pd.DataFrame(
columns=[
"seed",
"model",
"image",
"category",
"level",
"response",
"RT",
"crit",
"maxImgs",
]
)
# Get number of models
nModels = len(testReps)
# Get categories
categories = np.unique(testImgInfo[categoryCol].dropna())
# Loop through categories
for category in categories:
# Get the representations of this category
catIdx = testImgInfo[testImgInfo[categoryCol] == category].index
if catRepIdxs is not None:
catIdxs = catRepIdxs[category]
# Loop through models
for i in range(nModels):
# Get reps for this model
memoryModelReps = memoryReps[i, catIdx, :]
testModelReps = testReps[i, catIdx, :]
if catRepIdxs is not None:
catIdxs = np.unique(np.concatenate(catRepIdxs[category]))
memoryModelReps = memoryModelReps[:, :, :, catIdxs]
testModelReps = testModelReps[:, :, :, catIdxs]
# Flatten reps
memoryModelReps = memoryModelReps.reshape(memoryModelReps.shape[0], -1)
testModelReps = testModelReps.reshape(testModelReps.shape[0], -1)
# Loop through images
for j, imgRep in enumerate(testModelReps):
# Image rows
imgInfo = testImgInfo.iloc[catIdx[j]]
if maxImgs is not None:
catReps = memoryModelReps[
np.random.choice(memoryModelReps.shape[0], maxImgs, False)
]
else:
catReps = memoryModelReps[:]
# Simulate trial
evidence = get_evidence(imgRep, catReps, simFun)
drift = evidence / (evidence + criterion)
resp, rt = LBA_deterministic(drift, 1 - drift, b=0.5)
resp = "yes" if resp == 1 else "no"
# Add trial to performance df
performance = pd.concat(
[
performance,
pd.DataFrame(
{
"seed": i + 1,
"model": modelName,
"image": imgInfo["name"],
"category": category,
"level": categoryCol,
"response": resp,
"RT": rt,
"crit": criterion,
"maxImgs": maxImgs,
},
index=[0],
),
]
)
return performance
def cat_verification_from_mat(
simMat: np.ndarray,
imgInfo: pd.DataFrame,
modelName: str,
criterion: float = None,
maxImgs: int = None,
) -> pd.DataFrame:
"""
Simulate a category verification task given a similarity matrix with image
info using an LBA with a criterion. If criterion is None, set the criterion
to result in 95% accuracy.
"""
# Find the unique categories at each level
levelCats = {
"super": list(np.unique(imgInfo["super"].dropna())),
"basic": list(np.unique(imgInfo["basic"].dropna())),
"sub": list(np.unique(imgInfo["sub"].dropna())),
}
# Make the index a column
imgInfo = imgInfo.reset_index()
# Setup dataframe
performance = pd.DataFrame(
columns=[
"model",
"image",
"category",
"level",
"response",
"RT",
"crit",
"maxImgs",
]
)
# Loop through the levels
for level in ["super", "basic", "sub"]:
# Loop through categories in that level
for category in levelCats[level]:
# Get the indices of the images in the training set and test set
trainIdxs = imgInfo[
(imgInfo[level] == category) & (imgInfo["set"] == "train")
].index
testIdxs = imgInfo[
(imgInfo[level] == category) & (imgInfo["set"] == "test")
].index
# Filter similarity matrix for only the images we need
catSimMat = simMat[testIdxs, :][:, trainIdxs]
if maxImgs is not None:
# Create a new similarity matrix with only maxImgs images
newSimMat = np.zeros((catSimMat.shape[0], maxImgs))
for i, row in enumerate(catSimMat):
newSimMat[i, :] = np.random.choice(row, maxImgs, False)
# Save over
catSimMat = newSimMat
evidences = np.sum(catSimMat, axis=1)
if criterion is None:
# Find a criterion where 95% of the time it is correct
crit = np.quantile(evidences, 0.05)
else:
crit = criterion
# Loop through the test images
for i, evidence in enumerate(evidences):
# Calculate drift
drift = evidence / (evidence + crit)
resp, rt = LBA_deterministic(drift, 1 - drift, b=0.5)
resp = "yes" if resp == 1 else "no"
# Add performance to dataframe
performance = pd.concat(
[
performance,
pd.DataFrame(
{
"model": modelName,
"image": imgInfo.loc[testIdxs[i], "name"],
"category": category,
"level": level,
"response": resp,
"RT": rt,
"crit": crit,
"maxImgs": maxImgs,
},
index=[0],
),
]
)
return performance
class SimCluster:
def __init__(self, simMat, imgInfo):
self.simMat = simMat
self.imgInfo = imgInfo
# Figure out level map from imgInfo
self.levelMap = {
"super": list(imgInfo["super"].dropna().unique()),
"basic": list(imgInfo["basic"].dropna().unique()),
"sub": list(imgInfo["sub"].dropna().unique()),
}
# Figure out sets
self.sets = list(imgInfo["set"].unique())
def calculate_index(
self, imgSet=None, level=None, category=None, within_level=False
):
# Both level and category cannot be set together
if level is not None and category is not None:
raise ValueError("Both level and category cannot be set together")
if imgSet is not None:
# Filter imgInfo by sets
imgInfo = self.imgInfo[self.imgInfo["set"] == imgSet]
else:
imgInfo = self.imgInfo
# Handle average level indices first
if level is not None:
# Get categories
categories = self.levelMap[level]
# Preallocate array for cluster indices
clusters = np.zeros(len(categories), dtype=np.float32)
# Loop through categories
for k, cat in enumerate(categories):
loc = imgInfo[level] == cat
withinIdxs = imgInfo.loc[loc, "name"].index
if within_level and level != "super":
hier = list(self.levelMap.keys())
higherLevel = hier[hier.index(level) - 1]
# Get the higher level category
higherCat = imgInfo.loc[withinIdxs, higherLevel].unique()[0]
loc = (imgInfo[level] != cat) & (imgInfo[higherLevel] == higherCat)
betweenIdxs = imgInfo.loc[loc, "name"].index
else:
loc = imgInfo[level] != cat
betweenIdxs = imgInfo.loc[loc, "name"].index
withinSum = 0
withinCount = 0
for i, j in combinations(withinIdxs, 2):
withinSum += self.simMat[i, j]
withinCount += 1
betweenSum = 0
betweenCount = 0
for i in withinIdxs:
for j in betweenIdxs:
betweenSum += self.simMat[i, j]
betweenCount += 1
clusters[k] = (withinSum / withinCount) - (betweenSum / betweenCount)
return np.mean(clusters)
elif category is not None:
# Find the level of the category
for level, categories in self.levelMap.items():
if category in categories:
break
loc = imgInfo[level] == category
withinIdxs = imgInfo.loc[loc, "name"].index
if within_level and level != "super":
hier = list(self.levelMap.keys())
higherLevel = hier[hier.index(level) - 1]
# Get the higher level category
higherCat = imgInfo.loc[withinIdxs, higherLevel].unique()[0]
loc = (imgInfo[level] != category) & (imgInfo[higherLevel] == higherCat)
betweenIdxs = imgInfo.loc[loc, "name"].index
else:
loc = imgInfo[level] != category
betweenIdxs = imgInfo.loc[loc, "name"].index
withinSum = 0
withinCount = 0
for i, j in combinations(withinIdxs, 2):
withinSum += self.simMat[i, j]
withinCount += 1
betweenSum = 0
betweenCount = 0
for i in withinIdxs:
for j in betweenIdxs:
betweenSum += self.simMat[i, j]
betweenCount += 1
return (withinSum / withinCount) - (betweenSum / betweenCount)
else:
raise ValueError("Either level or category must be set")
def calculate_all(self, within_level=False):
for imgSet in self.sets:
for level in self.levelMap.keys():
for category in self.levelMap[level]:
val = self.calculate_index(
imgSet=imgSet, category=category, within_level=within_level
)
print(f"{imgSet}-{level}-{category}: {val}")
print("--")
def default_gcm_sim_mat(reps, c=1.0):
"""
Calculate a similarity matrix using GCM with r=2, c=1, p=1.
"""
return np.exp(
-c
* squareform(pdist(reps, metric="euclidean"))
* ((1 / reps.shape[1]) ** (1 / 2))
)
def default_gcm_cdist(reps1, reps2, c=1.0):
"""
Calculate pairwise similarity between reps1 and reps 2 using GCM with r=2,
c=1, p=1
"""
return np.exp(
-c * cdist(reps1, reps2, metric="euclidean") * ((1 / reps1.shape[1]) ** (1 / 2))
)
def exemplar_maker(n, center, radius=1, radius_density="uniform", relu=False):
nDims = len(center)
# Generate random numbers as needed
coords = np.random.normal(loc=0, scale=1, size=(n, nDims))
uniforms = np.random.uniform(low=0, high=1, size=n)
if radius_density == "power":
radii = (uniforms ** (1 / nDims)) * radius
elif radius_density == "normal":
radii = np.abs(np.random.normal(loc=0, scale=1, size=n)) * radius
elif radius_density == "uniform":
radii = uniforms * radius
elif radius_density == "lognormal":
radii = np.random.lognormal(mean=0, sigma=1 / 3, size=n) * radius
else:
raise ValueError("Density type not recognized")
coords = coords.T / np.linalg.norm(
coords, axis=1
) # Uniformly distributed directions
coords = coords * radii # Change radii
coords = coords.T + center
# If relu, apply relu
if relu:
coords[coords < 0] = 0
return coords
def make_categories(
*,
cat_rad,
radius_density="power",
relu=False,
super_rad,
basic_rad,
sub_rad,
nFeatures,
nImages,
):
def _centroids_maker(center, r):
"""
Create two centroids on a surface of a hypersphere with radius r. The
first centroid is randomly selected from the surface of a n-sphere
"""
nFeatures = center.shape[0]
coords = stats.multivariate_normal.rvs(mean=np.zeros((nFeatures,)), cov=1)
# Change coordinates to unit length
coords = coords / np.linalg.norm(coords)
# Multiply coords by radius of sphere
coords = coords * r
# Return coordinates plus and minus center
return center + coords, center - coords
# Make superordinate centroids
superCentroids = _centroids_maker(
center=np.zeros((nFeatures,), dtype=np.float32), r=super_rad
)
# Make basic centroids
basicCentroids = np.zeros((4, nFeatures), dtype=np.float32)
for i, center in enumerate(superCentroids):
basicCentroids[(i * 2) : (i * 2 + 2)] = _centroids_maker(
center=center, r=basic_rad
)
# Make subordinate centroids
subCentroids = np.zeros((8, nFeatures), dtype=np.float32)
for i, center in enumerate(basicCentroids):
subCentroids[(i * 2) : (i * 2 + 2)] = _centroids_maker(center=center, r=sub_rad)
# Generate exemplars
subExemplars = np.zeros((nImages * 8, nFeatures), dtype=np.float32)
subLabels = np.zeros((nImages * 8,), dtype=np.int32)
for i, center in enumerate(subCentroids):
subExemplars[(i * nImages) : (i * nImages + nImages)] = exemplar_maker(
nImages,
center=center,
radius=cat_rad,
radius_density=radius_density,
relu=relu,
)
subLabels[(i * nImages) : (i * nImages + nImages)] = i
return subExemplars, subCentroids, subLabels
class diana:
def __init__(self, data, metric, max_clusters=None, verbose=False):
self.data = data
self.metric = metric
indices = np.arange(data.shape[0])
self.tree = Tree()
self.verbose = verbose
self.tree.create_node(
"root",
0,
data={
"indices": indices,
},
)
if max_clusters is None:
max_clusters = data.shape[0]
while len(self.tree.leaves()) < max_clusters:
if self.verbose:
print(
f"We have {len(self.tree.leaves())} clusters, running diana step..."
)
# Pick cluster with largest diameter
nid = self.pick_cluster().identifier
# Split cluster
self.split_cluster(nid)
def _mean_diss(self, simMatrix):
return np.sum(simMatrix, axis=0) / (simMatrix.shape[0] - 1)
def split_cluster(self, nid):
node = self.tree.get_node(nid)
oldCluster = np.copy(node.data["indices"])
clusterSim = squareform(pdist(self.data[oldCluster,], metric=self.metric))
# Find the item that is most dissimilar to the rest of the cluster
mostDissIdx = np.argmax(self._mean_diss(clusterSim))
newCluster = oldCluster[mostDissIdx]
# Remove most dissimilar index from old cluster
oldCluster = np.delete(oldCluster, mostDissIdx)
while len(oldCluster) > 1:
# Compute dissimilarity of old cluster
oldDiss = squareform(pdist(self.data[oldCluster,], metric=self.metric))
oldDiss = self._mean_diss(oldDiss)
# Now compute similarity of each item in the old cluster with the new cluster
oldClusterData = self.data[oldCluster, :]
newClusterData = self.data[newCluster, :]
# if new cluster data is 1D, reshape to 2D
if len(newClusterData.shape) == 1:
newClusterData = newClusterData.reshape(1, -1)
newDiss = (
np.sum(
cdist(oldClusterData, newClusterData, metric=self.metric),
axis=1,
)
/ newClusterData.shape[0]
)
# Find new item to remove from old cluster
dissDiff = oldDiss - newDiss
mostDissIdx = np.argmax(dissDiff)
# Check if most dissimilar item is more dissimilar than the new cluster
if dissDiff[mostDissIdx] < 0:
break
# Update clusters
newCluster = np.append(newCluster, oldCluster[mostDissIdx])
oldCluster = np.delete(oldCluster, mostDissIdx)
# Figure out level
level = self.tree.level(nid) + 1
# Figure out how many nodes are at this level
nodesAtLevel = len(
[
node
for node in self.tree.all_nodes()
if self.tree.level(node.identifier) == level
]
)
# Figure out the highest nid
highestNid = np.max([node.identifier for node in self.tree.all_nodes()])
self.tree.create_node(
f"level{level}.{nodesAtLevel}",
highestNid + 1,
parent=nid,
data={
"indices": oldCluster,
},
)
# If new cluster is only 1 element, make it an array
if not isinstance(newCluster, np.ndarray):
newCluster = np.array([newCluster])
self.tree.create_node(
f"level{level}.{nodesAtLevel + 1}",
highestNid + 2,
parent=nid,
data={
"indices": newCluster,
},
)
if self.verbose:
print(
f"Split cluster {nid} into {highestNid + 1} and {highestNid + 2} at level {level}"
)
print(f"Cluster {highestNid + 1} has {len(oldCluster)} objects")
print(f"Cluster {highestNid + 2} has {len(newCluster)} objects")
def pick_cluster(self):
# Get every leaf
leaves = self.tree.leaves()
# Calculate diameter of each leaf
diameters = np.zeros(len(leaves))
for i, leaf in enumerate(leaves):
leafData = self.data[leaf.data["indices"], :]
if len(leafData) == 1:
diameters[i] = 0
else:
diameters[i] = np.max(pdist(leafData, metric=self.metric))
# Pick the leaf with the largest diameter
return leaves[np.argmax(diameters)]
def prune_tree(self, level):
level += 1
# Loop through all nodes and delete nodes just after the target level
for node in self.tree.all_nodes():
if (
self.tree.get_node(node.identifier) is not None
and self.tree.level(node.identifier) == level
):
self.tree.remove_node(node.identifier)
def linkage_matrix(self, calc_dist=False):
# Copy tree
tree = Tree(self.tree.subtree(self.tree.root), deep=True)
nData = self.data.shape[0]
# Start building linkage matrix
linkage = np.zeros((nData - 1, 4))
rowCount = 0
# Loop through leaves
for leaf in tree.leaves():
# Each leaf is its own cluster, so stick together every object into a bigger and bigger cluster
cluster = leaf.data["indices"]
# If the cluster is only one object, just give it a nodeID of itself
if len(cluster) == 1:
leaf.data["linkID"] = cluster[0]
continue
# Calculate the average distance between objects in the cluster
if calc_dist:
clusterReps = self.data[cluster, :]
clusterDist = np.mean(pdist(clusterReps, metric=self.metric))
else: