-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
1239 lines (1136 loc) · 50.4 KB
/
evaluation.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
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
"""
Read from all the datasets report folder, gather the best metrics for each dataset
"""
import itertools
import json
import math
from pathlib import Path
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from skimage.metrics import structural_similarity as ssim
from sklearn.metrics.pairwise import cosine_similarity
from utils.constants import REPORT_DIR
from utils.logger import get_logger
DATASETS = [
"Mitcham",
"PubMed",
"CiteSeer",
"Cora",
"AMAZON_COMPUTERS",
"AMAZON_PHOTO",
"NELL",
"CitationsFull_Cora",
"CitationsFull_CiteSeer",
"CitationsFull_PubMed",
"CitationsFull_Cora_ML",
"CitationsFull_DBLP",
"Cora_Full",
"Coauther_CS",
"Coauther_Physics",
"Flickr",
"Yelp",
"AttributedGraphDataset_Wiki",
"AttributedGraphDataset_Cora",
"AttributedGraphDataset_CiteSeer",
"AttributedGraphDataset_Pubmed",
"AttributedGraphDataset_BlogCatalog",
"AttributedGraphDataset_PPI",
"AttributedGraphDataset_Flickr",
"AttributedGraphDataset_Facebook",
"WEBKB_Cornell",
"WEBKB_Texas",
"WEBKB_Wisconsin",
"HeterophilousGraphDataset_Roman_empire",
"HeterophilousGraphDataset_Amazon_ratings",
"HeterophilousGraphDataset_Minesweeper",
"HeterophilousGraphDataset_Tolokers",
"HeterophilousGraphDataset_Questions",
"Actor",
"GitHub",
"TWITCH_DE",
"TWITCH_EN",
"TWITCH_ES",
"TWITCH_FR",
"TWITCH_PT",
"TWITCH_RU",
"PolBlogs",
"EllipticBitcoinDataset",
"DGraphFin",
"Reddit",
"AMAZON_PRODUCTS",
]
MODELS = [
"node2vec",
"gae",
"gcn",
"graph_sage",
"random_input",
]
SUMMARY_MODEL_COLUMN_ORDER = [
"dataset_name",
"model",
"sub_model",
"svm_accuracy_score",
"knn_accuracy_score",
"rf_accuracy_score",
"svm_macro_f_beta_score",
"knn_macro_f_beta_score",
"rf_macro_f_beta_score",
"svm_macro_precision",
"knn_macro_precision",
"rf_macro_precision",
"svm_macro_recall",
"knn_macro_recall",
"rf_macro_recall",
"svm_micro_f_beta_score",
"knn_micro_f_beta_score",
"rf_micro_f_beta_score",
"svm_micro_precision",
"knn_micro_precision",
"rf_micro_precision",
"svm_micro_recall",
"knn_micro_recall",
"rf_micro_recall",
"svm_roc_auc",
"knn_roc_auc",
"rf_roc_auc",
"direct_task_accuracy_score",
]
class IIDMetricsEvaluation:
def __init__(self):
self.logger = get_logger("IIDMetricsEvaluation")
self.metric_names = [
"accuracy_score",
"macro_f_beta_score",
"macro_precision",
"macro_recall",
"micro_f_beta_score",
"micro_precision",
"micro_recall",
"roc_auc",
]
similarity_dir = REPORT_DIR / "iid" / "summary" / "similarity_matrix"
similarity_dir.mkdir(parents=True, exist_ok=True)
@staticmethod
def drop_duplicate(filename: Path):
df = pd.read_csv(filename)
df.drop_duplicates(inplace=True)
# also round the number to 4 decimal places
df = df.round(3)
df.to_csv(filename, index=False)
def gather_metrics(self, dataset_name: str):
metrics = []
report_dir = REPORT_DIR / dataset_name
self.logger.info(f"dataset_name: {dataset_name}")
# -------------------------------------------------------
# get the centrality and feature_1433 json in root folder
feature_1433_json = report_dir / "feature_1433_metrics.json"
feature_centrality_json = report_dir / "feature_centrality_metrics.json"
feature_1433_metric = {
"dataset_name": dataset_name,
"model": "feature_1433",
"sub_model": "feature_1433",
}
feature_centrality_metric = {
"dataset_name": dataset_name,
"model": "feature_centrality",
"sub_model": "feature_centrality",
}
if feature_1433_json.exists():
feature_1433_metric = self.get_best_metrics(
feature_1433_json, feature_1433_metric, is_json=True
)
metrics.append(feature_1433_metric)
if feature_centrality_json.exists():
feature_centrality_metric = self.get_best_metrics(
feature_centrality_json, feature_centrality_metric, is_json=True
)
metrics.append(feature_centrality_metric)
# ------------------------------------------------------------------
# loop the folders in models, get the json file
for model in MODELS:
model_report_dir = report_dir / model
if not model_report_dir.exists():
continue
# recursively find the json file, and get the best metrics within each json file
json_files = list(model_report_dir.glob("**/*.json"))
self.logger.info(f"json_files: {model}({len(json_files)})")
if len(json_files) == 0:
continue
# get the best metrics
for json_file in json_files:
model_metric = {
"dataset_name": dataset_name,
}
# get the name difference between the json file and the model report dir
self.logger.info(f"json_file: {json_file}")
folder_name = (
json_file.as_posix()
.replace(f"{model_report_dir.as_posix()}/", "")
.rsplit("/", 1)[0]
.replace("/", "-")
)
model_metric["model"] = model
model_metric["sub_model"] = folder_name
self.logger.info(f"model_metric: {model_metric}")
self.logger.info(f"folder_name: {folder_name}")
if folder_name.endswith(".json") and "random" not in folder_name:
"""
If it is json, there are two cases:
- root folder, feature_1433 and centrality, it will have SVM/KNN/RF, which we have handled above
- sub folder, feature_1433 and centrality, it will be the performance with direct task
"""
json_metric = json.load(json_file.open())
# self.logger.info(f"model_metric: {json_metric}")
for metric_name in self.metric_names:
if metric_name != "accuracy_score":
continue
model_metric[f"direct_task_{metric_name}"] = json_metric[
metric_name
]
else:
model_metric = self.get_best_metrics(Path(json_file), model_metric)
metrics.append(model_metric)
# ------------------------------------------------------------------
# output should be in the following format to a csv file
"""
headers: dataset_name, model, svm_accuracy_score, knn_accuracy_score, rf_accuracy_score, ...
"""
self.logger.info(f"metrics: {metrics}")
for metric in metrics:
for metric_key in SUMMARY_MODEL_COLUMN_ORDER:
if metric_key not in metric:
metric[metric_key] = None
# write to csv, summary folder
summary_file = REPORT_DIR / "iid" / "summary" / "summary_models.csv"
# convert the list to dataframe
if len(metrics) == 0:
return
df = pd.DataFrame(metrics)
df = df[SUMMARY_MODEL_COLUMN_ORDER]
# if the file exists, append to the file
if summary_file.exists():
df.to_csv(summary_file, mode="a", header=False, index=False)
else:
# write to csv
df.to_csv(summary_file, index=False)
self.drop_duplicate(summary_file)
def get_best_metrics( # noqa
self, file: Path, model_metric: dict, is_json: bool = False
):
"""
For each metric
- accuracy_score
- f1_score
- precision_score
- recall_score
- roc_auc_score
get the best metrics
Parameters
----------
file
model_metric
is_json
Returns
-------
"""
for metric_key in self.metric_names:
metrics = json.load(file.open())
best_metrics = {
"SVM": 0,
"KNN": 0,
"RF": 0,
}
if is_json:
for item in metrics:
if item["name"] == "SVM":
if (
item[metric_key] is not None
and item[metric_key] > best_metrics["SVM"]
):
best_metrics["SVM"] = item[metric_key]
elif item["name"] == "KNN":
if (
item[metric_key] is not None
and item[metric_key] > best_metrics["KNN"]
):
best_metrics["KNN"] = item[metric_key]
elif item["name"] == "RF":
if (
item[metric_key] is not None
and item[metric_key] > best_metrics["RF"]
):
best_metrics["RF"] = item[metric_key]
else:
for key, value in metrics.items():
if type(value) is not list:
continue
for item in value:
if item[metric_key] is not None and item["name"] == "SVM":
if item[metric_key] > best_metrics["SVM"]:
best_metrics["SVM"] = item[metric_key]
elif item[metric_key] is not None and item["name"] == "KNN":
if item[metric_key] > best_metrics["KNN"]:
best_metrics["KNN"] = item[metric_key]
elif item[metric_key] is not None and item["name"] == "RF":
if item[metric_key] > best_metrics["RF"]:
best_metrics["RF"] = item[metric_key]
self.logger.info(f"best_metrics: {best_metrics}")
model_metric[f"svm_{metric_key}"] = best_metrics["SVM"]
model_metric[f"knn_{metric_key}"] = best_metrics["KNN"]
model_metric[f"rf_{metric_key}"] = best_metrics["RF"]
return model_metric
@staticmethod
def rank_models(logger=None):
"""
Rank the models based on the metrics
return will be like
dataset, rank_1_model, rank_1_model_score, rank_2_model, rank_2_model_score, rank_3_model, rank_3_model_score
Returns
-------
"""
# read the csv file
if not logger:
logger = get_logger("IIDMetricsEvaluation")
(REPORT_DIR / "iid" / "summary" / "rank").mkdir(parents=True, exist_ok=True)
summary_file = REPORT_DIR / "iid" / "summary" / "summary_models.csv"
df = pd.read_csv(summary_file)
# for each dataset, and each metric, rank the model
for metric_name in [
"accuracy_score",
"roc_auc",
"macro_f_beta_score",
"macro_precision",
"macro_recall",
"micro_f_beta_score",
"micro_precision",
"micro_recall",
]:
dataset_metrics = []
for dataset_name in DATASETS:
models_metric = df[df["dataset_name"] == dataset_name]
# check accuracy score
metrics_list = []
for _, row in models_metric.iterrows():
row_dict = row.to_dict()
for key in row_dict:
if metric_name in key:
# print(row_dict[key])
# print(key)
if pd.isna(row_dict[key]):
continue
metrics_list.append(
(
f"{row_dict['model']}_{row_dict['sub_model']}_{key}",
row_dict[key],
)
)
metrics_list.sort(key=lambda x: x[1], reverse=True)
# map the list to dict, and format will be rank_1_model_name, rank_1_model_score,
# rank_2_model_name, rank_2_model_score
metrics_dict = {}
# logger.info(f"metrics_list length: {len(metrics_list)}")
if len(metrics_list) <= 30:
logger.critical(
f"dataset {dataset} has less than 30 models to evaluate"
)
for index, item in enumerate(metrics_list):
metrics_dict[f"rank_{index + 1}_model_name"] = item[0]
metrics_dict[f"rank_{index + 1}_model_score"] = item[1]
metrics_dict["dataset"] = dataset_name
dataset_metrics.append(metrics_dict)
# output the rank to csv
rank_file = (
REPORT_DIR
/ "iid"
/ "summary"
/ "rank"
/ f"rank_{metric_name}_models.csv"
)
rank_df = pd.DataFrame(dataset_metrics)
# put the dataset column to the first column
cols = rank_df.columns.tolist()
# get the index of dataset
dataset_index = cols.index("dataset")
cols = (
[cols[dataset_index]] + cols[:dataset_index] + cols[dataset_index + 1:]
)
rank_df = rank_df[cols]
rank_df.to_csv(rank_file, index=False)
def plot_performance_distribution(self, logger=None): # noqa
"""
plot the performance distribution for each dataset,
and the x-axis will be the same sequence for all dataset
-------
"""
# read the csv file
if not logger:
logger = get_logger("IIDMetricsEvaluation")
# for each dataset, and each metric, rank the model
for metric_name in [
"accuracy_score",
"roc_auc",
"macro_f_beta_score",
"macro_precision",
"macro_recall",
"micro_f_beta_score",
"micro_precision",
"micro_recall",
]:
# output the rank to csv
rank_file = (
REPORT_DIR
/ "iid"
/ "summary"
/ "rank"
/ f"rank_{metric_name}_models.csv"
)
rank_df = pd.read_csv(rank_file)
logger.debug(rank_df.columns.tolist())
model_names = []
for column in rank_df.columns.tolist():
if "model_name" in column:
model_names += list(set(rank_df[column].tolist()))
# remove pd.nan from the list
model_names = [item for item in model_names if type(item) is str]
# remove the model_name start with graph_sage_graph_sage_metrics.
model_names = [
item
for item in model_names
if "graph_sage_graph_sage_metrics." not in item
]
model_names = list(set(model_names))
model_names.sort()
logger.info(model_names)
logger.debug(len(model_names))
x_axis_sequence = model_names
# for each dataset, plot the distribution, rank_model_score is the y, rank_model_name is the x
# we will first need to construct the dataset
# iterate the dataset for rows
finished_dataset = []
metric_vectors = {}
for _, row in rank_df.iterrows():
performance_data = {"dataset": row.dataset}
row = row.to_dict()
output_dir = REPORT_DIR / "iid" / "summary" / metric_name
output_dir.mkdir(parents=True, exist_ok=True)
for column in rank_df.columns.tolist():
if "score" in column:
rank = column.split("_")[1]
rank_model_name_column = f"rank_{rank}_model_name"
if row[rank_model_name_column] not in x_axis_sequence:
continue
if pd.isna(row[column]):
continue
else:
performance_data[row[rank_model_name_column]] = row[column]
logger.debug(performance_data)
for model_name in x_axis_sequence:
if model_name not in performance_data:
performance_data[model_name] = 0
# temporarily remove other node2vec models, only keep node2vec_embedding_dim one
# logic will be: loop the dict, if node2vec in the key and node2vec_embedding_dim not in the key
# then remove the key/value pair
for key in list(performance_data.keys()):
if "node2vec" in key and "node2vec_embedding_dim" not in key:
del performance_data[key]
# plot the performance distribution, x-axis will be the model name, y-axis will be the score
# also x-axis will be the same for all dataset, use the x_axis_sequence, use plotly to plot
sorted_keys = [
key for key in x_axis_sequence if key in performance_data
]
sorted_values = [performance_data[key] for key in sorted_keys]
# for each sorted_key, remove metric_name from the string
sorted_keys = [sorted_key.replace(f"_{metric_name}", '') for sorted_key in sorted_keys]
# for key start with feature_1433, feature_centrality, random_input, remove the first feature_1433, feature_centrality, random_input in the string
updated_sorted_keys = []
for sorted_key in sorted_keys:
if sorted_key.startswith("feature_1433"):
new_sorted_key = sorted_key.replace("feature_1433_", "", 1)
elif sorted_key.startswith("feature_centrality"):
new_sorted_key = sorted_key.replace("feature_centrality_", "", 1)
elif sorted_key.startswith("random_input"):
new_sorted_key = sorted_key.replace("random_input_", "", 1)
elif "direct_task" in sorted_key:
if ("gcn" in sorted_key) and ("centrality" in sorted_key):
new_sorted_key = "gcn_centrality_direct_task"
elif ("gcn" in sorted_key) and ("feature" in sorted_key):
new_sorted_key = "gcn_feature_direct_task"
elif ("graph_sage" in sorted_key) and ("centrality" in sorted_key):
new_sorted_key = "graph_sage_centrality_direct_task"
elif ("graph_sage" in sorted_key) and ("feature" in sorted_key):
new_sorted_key = "graph_sage_feature_direct_task"
else:
print(sorted_key)
new_sorted_key = None
else:
new_sorted_key = sorted_key
updated_sorted_keys.append(new_sorted_key)
# replace - to _, replace feature_1433 to feature
updated_sorted_keys = [sorted_key.replace("-", "_") for sorted_key in updated_sorted_keys]
updated_sorted_keys = [sorted_key.replace("feature_1433_", "feature_") for sorted_key in
updated_sorted_keys]
# replace number 1 in the key
updated_sorted_keys = [sorted_key.replace("1", "") for sorted_key in updated_sorted_keys]
updated_sorted_keys = [
"feature_knn",
"feature_rf",
"feature_svm",
"centrality_knn",
"centrality_rf",
"centrality_svm",
"gae_gcn_centrality_knn",
"gae_gcn_centrality_rf",
"gae_gcn_centrality_svm",
"gae_gcn_feature_knn",
"gae_gcn_feature_rf",
"gae_gcn_feature_svm",
"gae_graph_sage_centrality_knn",
"gae_graph_sage_centrality_rf",
"gae_graph_sage_centrality_svm",
"gae_graph_sage_feature_knn",
"gae_graph_sage_feature_rf",
"gae_graph_sage_feature_svm",
"gcn_centrality_knn",
"gcn_centrality_rf",
"gcn_centrality_svm",
"gcn_feature_knn",
"gcn_feature_rf",
"gcn_feature_svm",
"gcn_centrality_direct_task",
"gcn_feature_direct_task",
"graph_sage_centrality_knn",
"graph_sage_centrality_rf",
"graph_sage_centrality_svm",
"graph_sage_feature_knn",
"graph_sage_feature_rf",
"graph_sage_feature_svm",
"graph_sage_centrality_direct_task",
"graph_sage_feature_direct_task",
"node2vec_knn",
"node2vec_rf",
"node2vec_svm",
"random_knn",
"random_rf",
"random_svm"
]
def get_symbol(sort_key_name):
if sort_key_name.endswith('knn'):
return 'circle'
elif sort_key_name.endswith('rf'):
return 'square'
elif sort_key_name.endswith('svm'):
return 'diamond'
elif sort_key_name.endswith('direct_task'):
return 'cross'
else:
return 'circle' # default shape
symbols = [get_symbol(key) for key in updated_sorted_keys]
# first 6 will be similar colors, but two different color
color_list = [
# First 6 (3 + 3)
'#FF0000', '#FF0000', '#FF0000', '#FF3300', '#FF3300', '#FF3300',
# 7-18 (6 + 6, with 3 + 3 within each 6)
'#00FF00', '#00FF00', '#00FF00', '#33FF00', '#33FF00', '#33FF00',
'#00FF33', '#00FF33', '#00FF33', '#33FF33', '#33FF33', '#33FF33',
# Next 6 + 2 (same pattern)
'#0000FF', '#0000FF', '#0000FF', '#3300FF', '#3300FF', '#3300FF',
'#0033FF', '#0033FF',
# Next 6 + 2 (same pattern)
'#FF00FF', '#FF00FF', '#FF00FF', '#FF33FF', '#FF33FF', '#FF33FF',
'#FF00CC', '#FF00CC',
# Last 6 (3 + 3)
'#00FFFF', '#00FFFF', '#00FFFF', 'black', 'black', 'black'
]
fig = go.Figure(
data=go.Scatter(
x=updated_sorted_keys,
y=sorted_values,
mode="markers+text",
text=sorted_values,
textposition="top center",
marker=dict(
symbol=symbols,
size=12,
color=color_list,
opacity=0.7,
line=dict(
color='DarkSlateGrey',
width=0.5
)
),
textfont=dict(
size=14,
color='black'
)
)
)
dataset_name = row["dataset"]
if "auther" in dataset_name:
dataset_name = dataset_name.replace("auther", "author")
fig.update_layout(
title=dict(
text=f"Scatter Plot of {' '.join(metric_name.upper().split('_'))}, Dataset: {dataset_name}",
font=dict(size=24)),
xaxis_title="Model",
xaxis_tickangle=-45,
yaxis_title=' '.join(metric_name.upper().split('_')),
width=1500,
height=800,
yaxis=dict(range=[0, 1.05], gridcolor='lightgray', showgrid=True),
font=dict(size=18),
plot_bgcolor='white',
paper_bgcolor='white',
xaxis=dict(
gridcolor='lightgray',
showgrid=True,
)
)
fig.write_image(
REPORT_DIR
/ "iid"
/ "summary"
/ metric_name
/ f"{row['dataset']}_{metric_name}.pdf"
)
# if all values > 0, then it is a finished dataset
if all(value > 0 for value in sorted_values):
if row["dataset"] != "Mitcham":
finished_dataset.append(row["dataset"])
# ----------
# treat the sorted_values as vectors
metric_vectors[row["dataset"]] = sorted_values
# ----------
if metric_name == "accuracy_score" or metric_name == 'macro_f_beta_score':
finished_dataset = list(set(finished_dataset))
finished_dataset.sort()
# loop to log all the finished dataset
for fd in finished_dataset:
logger.info(f"finished dataset: {fd}")
logger.info(f"finished dataset length: {len(finished_dataset)}")
logger.info(finished_dataset)
# also plot the similarity matrix for the network metrics
# read the summary.csv file, and get all the related metrics for datasets within finished_dataset
network_summary_df = pd.read_csv(
REPORT_DIR
/ "iid"
/ "summary"
/ "summary_network_metrics_min_max.csv"
)
# apply log to the num_edges
network_summary_df["ln_num_edges"] = network_summary_df[
"num_edges"
].apply(lambda x: math.log(x))
network_summary_df = network_summary_df[
network_summary_df["dataset"].isin(finished_dataset)
]
self.logger.info(f"network_summary_df: {network_summary_df.shape}")
# aim for different subset of columns combination, and plot the similarity matrix
# row_columns_list = [
# "num_edges",
# "k",
# "ln_n",
# "std_k",
# "k_min",
# "k_max",
# "d_max_random",
# "k2",
# "gamma_degree",
# "diameter",
# "average_clustering_coefficient",
# "average_shortest_path_length",
# "num_classes",
# "num_features",
# "num_nodes",
# "transitivity",
# # p_k_max
# # ks_statistic
# ]
row_columns_list = [
"ln_num_edges",
"ln_num_features",
"ln_num_classes",
"ln_n",
"k",
"std_k",
"ln_k2",
"k_min",
"ln_k_max",
"ln_d_max_random",
"gamma_degree",
"ln_average_shortest_path_length",
"ln_diameter",
"average_clustering_coefficient",
"transitivity",
# p_k_max
# ks_statistic
]
if metric_name == 'accuracy_score':
base_similarity = self.plot_similarity_matrix(
metric_vectors,
REPORT_DIR
/ "iid"
/ "summary"
/ "similarity_matrix"
/ f"similarity_matrix_{metric_name}.pdf",
title=f"Similarity Matrix for {metric_name}",
)
metric_vectors_df = pd.DataFrame(metric_vectors)
# transpose the metric_vectors_df
metric_vectors_df = metric_vectors_df.transpose()
metric_vectors_df.to_csv(REPORT_DIR / "iid" / "summary" / "accuracy_score_metrics_vector.csv")
if metric_name == 'macro_f_beta_score':
f1_similarity = self.plot_similarity_matrix(
metric_vectors,
REPORT_DIR
/ "iid"
/ "summary"
/ "similarity_matrix"
/ f"similarity_matrix_{metric_name}.pdf",
title=f"Similarity Matrix for {metric_name}",
)
# f1 score SSIM with base
self.logger.critical("ffffffffffffffffffffffffffffffffffffffffff")
self.logger.critical(self.ssim(base_similarity, f1_similarity))
# get metric vectors to pandas and to csv
# Generate all combinations of the elements ranging from size 2 to the length of the list
row_columns_combinations = []
for r in range(2, len(row_columns_list) + 1):
row_columns_combinations.extend(
itertools.combinations(row_columns_list, r)
)
#
row_columns_combinations = [
('ln_num_edges', 'ln_num_classes', 'ln_average_shortest_path_length'),
("ln_num_classes", "ln_n", "ln_k_max"),
("ln_num_classes", "ln_n", "ln_average_shortest_path_length"),
(
"ln_num_classes",
"ln_n",
"ln_k_max",
"ln_average_shortest_path_length",
),
('ln_num_edges', 'ln_num_classes', 'ln_k2')
]
# row_columns_combinations = [('ln_num_edges', 'num_features', 'ln_num_classes', 'ln_n', 'k', 'std_k',
# 'ln_k2', 'k_min', 'ln_k_max',
# 'ln_d_max_random', 'gamma_degree', 'ln_average_shortest_path_length',
# 'ln_diameter',
# 'average_clustering_coefficient', 'transitivity')]
# need a list of list to store the combinations results
# and then sort the list by the least value of the diff
diff_results = []
self.logger.info(
f"row_columns_combinations: {len(row_columns_combinations)}"
)
for row_columns in row_columns_combinations:
self.logger.critical(row_columns)
network_metric_vectors = {}
for _, row in network_summary_df.iterrows():
network_metric_vectors[row["dataset"]] = row[list(row_columns)]
# self.logger.info(f"metric_vectors: {network_metric_vectors}")
compare_similarity = self.plot_similarity_matrix(
network_metric_vectors,
REPORT_DIR
/ "iid"
/ "summary"
/ "similarity_matrix"
/ f"similarity_matrix_network_{'_'.join(row_columns)}.pdf",
title=f"Similarity Matrix for Network Metrics ({row_columns})",
)
# there are several ways to compare the similarity between matrices
# 1. total difference
# 2. relative error for the Frobenius norm
# 3. Frobenius distance
# 4. cosine similarity
# 5. spectral distance
# 6. matrix correlation
total_diff_similarity = self.total_diff_similarity(
base_similarity, compare_similarity
)
relative_error = self.relative_error(
base_similarity, compare_similarity
)
fro_similarity = self.frobenius_distance(
base_similarity, compare_similarity
)
cosine_similarity_value = self.cosine_similarity(
base_similarity, compare_similarity
)
spectral_distance = self.spectral_distance(
base_similarity, compare_similarity
)
matrix_correlation = self.matrix_correlation(
base_similarity, compare_similarity
)
dot_product = self.dot_product(base_similarity, compare_similarity)
average_row_dot_product = self.element_wise_dot_product(
base_similarity, compare_similarity
)
row_by_row_cosine_similarity = self.row_by_row_cosine_similarity(
base_similarity, compare_similarity
)
ssim_similarity = self.ssim(base_similarity, compare_similarity)
self.logger.critical(f"ssim_similarity: {ssim_similarity}")
diff_results.append(
(
row_columns,
total_diff_similarity,
relative_error,
fro_similarity,
cosine_similarity_value,
spectral_distance,
matrix_correlation,
dot_product,
average_row_dot_product,
row_by_row_cosine_similarity,
ssim_similarity,
)
)
self.logger.critical(
"--------------------------------------------------------"
)
# diff_results.sort(key=lambda x: x[1])
# for diff_result in diff_results:
# self.logger.critical(diff_result)
# # convert to pandas and then save to csv
diff_results_df = pd.DataFrame(diff_results)
# give df the column names
diff_results_df.columns = [
"row_columns",
"total_diff_similarity",
"relative_error",
"fro_similarity",
"cosine_similarity_value",
"spectral_distance",
"matrix_correlation",
"dot_product",
"average_row_dot_product",
"row_by_row_cosine_similarity",
"ssim_similarity",
]
# sort by the row_by_row_cosine_similarity descending
diff_results_df.sort_values(
by=["row_by_row_cosine_similarity"], ascending=False, inplace=True
)
diff_results_df.to_csv(
REPORT_DIR
/ "iid"
/ "summary"
/ "similarity_matrix_network_diff_results_1114.csv",
index=False,
)
# ----------------------------------------------------------
# we will want to calculate similarity between the vectors, and plot the similarity matrix
self.plot_similarity_matrix(
metric_vectors,
REPORT_DIR
/ "iid"
/ "summary"
/ "similarity_matrix"
/ f"similarity_matrix_{metric_name}.pdf",
title=f"Similarity Matrix for {metric_name}",
)
@staticmethod
def plot_similarity_matrix(metrics_vectors: dict, output_image: Path, title: str):
# calculate the similarity matrix
# we will use cosine similarity
names = list(metrics_vectors.keys())
names.sort()
vectors = [metrics_vectors[name] for name in names]
# Compute pairwise cosine similarity
similarity_matrix = cosine_similarity(vectors)
A = (similarity_matrix - similarity_matrix.min()) / (similarity_matrix.max() - similarity_matrix.min())
# Create a structured result
# result = {}
# for i, name1 in enumerate(names):
# for j, name2 in enumerate(names):
# result[(name1, name2)] = similarity_matrix[i][j]
# plot the similarity matrix, get it to be a square matrix
fig = go.Figure(data=go.Heatmap(z=A, x=names, y=names))
# get the x and y label to be larger
fig.update_layout(
xaxis=dict(
tickfont=dict(
size=20,
)
),
yaxis=dict(
tickfont=dict(
size=20,
),
autorange="reversed", # Set y-axis to be reversed
),
)
fig.update_layout(width=2000, height=2000)
# add title
fig.update_layout(title=title)
# get the title to be larger
fig.update_layout(title_font_size=30)
fig.write_image(output_image)
return similarity_matrix
@staticmethod
def total_diff_similarity(A, B):
A = (A - A.min()) / (A.max() - A.min())
B = (B - B.min()) / (B.max() - B.min())
diff = np.abs(A - B)
# Sum up the absolute differences
total_difference = np.sum(diff)
return total_difference
@staticmethod
def relative_error(A, B):
"""
Compute the relative error between two matrices A and B using the Frobenius norm.
Args:
- A (np.ndarray): Reference matrix
- B (np.ndarray): Matrix to compare
Returns:
- float: Relative error
"""
A = (A - A.min()) / (A.max() - A.min())
B = (B - B.min()) / (B.max() - B.min())
# Compute the Frobenius norm of the difference
diff_norm = np.linalg.norm(A - B, "fro")
# Compute the Frobenius norm of the reference matrix A
ref_norm = np.linalg.norm(A, "fro")
# Handle the case where the reference matrix is the zero matrix
if ref_norm == 0:
raise ValueError(
"The reference matrix is the zero matrix, so relative error is undefined."
)
return diff_norm / ref_norm
@staticmethod
def frobenius_distance(A, B):
A = (A - A.min()) / (A.max() - A.min())
B = (B - B.min()) / (B.max() - B.min())
return np.linalg.norm(A - B, "fro")
@staticmethod
def cosine_similarity(A, B):
A = (A - A.min()) / (A.max() - A.min())
B = (B - B.min()) / (B.max() - B.min())
A_vec = A.ravel()
B_vec = B.ravel()
dot_product = np.dot(A_vec, B_vec)
norm_A = np.linalg.norm(A_vec)
norm_B = np.linalg.norm(B_vec)
return dot_product / (norm_A * norm_B)
@staticmethod
def dot_product(A, B):
A = (A - A.min()) / (A.max() - A.min())
B = (B - B.min()) / (B.max() - B.min())
dot_p = np.dot(A, B)
return np.linalg.norm(dot_p, 2)
@staticmethod