11import numpy as np
2- from spotpython .utils .metrics import calculate_xai_consistency
2+ from spotpython .utils .metrics import calculate_xai_consistency_corr , calculate_xai_consistency_cosine , calculate_xai_consistency_euclidean
33
44
5- def test_xai_consistency ():
5+ def test_xai_consistency_corr ():
66 # Mock data for testing
77
88 dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
9- row_sum_dl = np .sum (dl_attr_test_sum , axis = 0 )
10- if row_sum_dl == 0 :
11- row_sum_dl += 1e-10
12- scaled_attribution_dl = dl_attr_test_sum / row_sum_dl
9+ l2_norm = np .linalg .norm (dl_attr_test_sum )
10+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
1311
1412 ig_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
15- row_sum_ig = np .sum (ig_attr_test_sum , axis = 0 )
16- if row_sum_ig == 0 :
17- row_sum_ig += 1e-10
18- scaled_attribution_ig = ig_attr_test_sum / row_sum_ig
13+ l2_norm = np .linalg .norm (ig_attr_test_sum )
14+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
1915
2016 attributions = [scaled_attribution_dl , scaled_attribution_ig ]
21- result = calculate_xai_consistency (attributions )
17+ result = calculate_xai_consistency_corr (attributions )
2218 print ("XAI Consistency Result:" )
2319 print (result )
2420
2521 # Assert that the result is 1
2622 assert abs (result - 1 ) < 1e-10
2723
2824
29- def test_xai_consistency_negative ():
25+ def test_xai_consistency_negative_corr ():
3026 # Mock data for testing negative consistency
3127
3228 dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
33- row_sum_dl = np .sum (dl_attr_test_sum , axis = 0 )
34- if row_sum_dl == 0 :
35- row_sum_dl += 1e-10
36- scaled_attribution_dl = dl_attr_test_sum / row_sum_dl
29+ l2_norm = np .linalg .norm (dl_attr_test_sum )
30+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
3731
38- ig_attr_test_sum = [- 1 , - 2 , - 3 , - 4 , - 5 ]
39- row_sum_ig = np .sum (np .abs (ig_attr_test_sum ), axis = 0 )
40- if row_sum_ig == 0 :
41- row_sum_ig += 1e-10
42- scaled_attribution_ig = ig_attr_test_sum / row_sum_ig
32+ ig_attr_test_sum = [- 2 , - 3 , - 4 , - 5 , - 6 ]
33+ l2_norm = np .linalg .norm (ig_attr_test_sum )
34+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
4335
4436 attributions = [scaled_attribution_dl , scaled_attribution_ig ]
45- result = calculate_xai_consistency (attributions )
37+ result = calculate_xai_consistency_corr (attributions )
4638 print ("XAI Consistency Result (Negative):" )
4739 print (result )
4840
4941 # Assert that the result is -1
50- assert abs (result + 1 ) < 1e-10
42+ assert abs (result + 1 ) < 1e-10
43+
44+
45+ def test_xai_consistency_cosine ():
46+ # Mock data for testing cosine consistency
47+
48+ dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
49+ l2_norm = np .linalg .norm (dl_attr_test_sum )
50+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
51+ ig_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
52+ l2_norm = np .linalg .norm (ig_attr_test_sum )
53+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
54+
55+ attributions = [scaled_attribution_dl , scaled_attribution_ig ]
56+ result = calculate_xai_consistency_cosine (attributions )
57+ print ("XAI Consistency Cosine Result:" )
58+ print (result )
59+ # Assert that the result is 1
60+ assert abs (result - 1 ) < 1e-10
61+
62+
63+ def test_xai_consistency_negative_cosine ():
64+ # Mock data for testing negative cosine consistency
65+
66+ dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
67+ l2_norm = np .linalg .norm (dl_attr_test_sum )
68+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
69+ ig_attr_test_sum = [- 1 , - 2 , - 3 , - 4 , - 5 ]
70+ l2_norm = np .linalg .norm (ig_attr_test_sum )
71+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
72+
73+ attributions = [scaled_attribution_dl , scaled_attribution_ig ]
74+ result = calculate_xai_consistency_cosine (attributions )
75+ print ("XAI Consistency Cosine Result (Negative):" )
76+ print (result )
77+
78+ # Assert that the result is -1
79+ assert abs (result + 1 ) < 1e-10
80+
81+
82+ def test_xai_consistency_euclidean ():
83+ # Mock data for testing Euclidean consistency
84+
85+ dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
86+ l2_norm = np .linalg .norm (dl_attr_test_sum )
87+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
88+ ig_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
89+ l2_norm = np .linalg .norm (ig_attr_test_sum )
90+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
91+
92+ attributions = [scaled_attribution_dl , scaled_attribution_ig ]
93+ result = calculate_xai_consistency_euclidean (attributions )
94+ print ("XAI Consistency Euclidean Result:" )
95+ print (result )
96+
97+ # Assert that the result is close to zero
98+ assert abs (result ) < 1e-10
99+
100+
101+ def test_xai_consistency_negative_euclidean ():
102+ # Mock data for testing negative Euclidean consistency
103+
104+ dl_attr_test_sum = [1 , 2 , 3 , 4 , 5 ]
105+ l2_norm = np .linalg .norm (dl_attr_test_sum )
106+ scaled_attribution_dl = dl_attr_test_sum / l2_norm if l2_norm != 0 else dl_attr_test_sum
107+ ig_attr_test_sum = [- 1 , - 2 , - 3 , - 4 , - 5 ]
108+ l2_norm = np .linalg .norm (ig_attr_test_sum )
109+ scaled_attribution_ig = ig_attr_test_sum / l2_norm if l2_norm != 0 else ig_attr_test_sum
110+
111+ attributions = [scaled_attribution_dl , scaled_attribution_ig ]
112+ result = calculate_xai_consistency_euclidean (attributions )
113+ print ("XAI Consistency Euclidean Result (Negative):" )
114+ print (result )
115+
116+ # Assert that the result is close to two
117+ assert abs (result - 2 ) < 1e-10
0 commit comments