11import numpy as np
2+ import pytest
23
34from eff_conv .convexity import SimilaritySpace
45from eff_conv .ib .language import IBLanguage
@@ -32,56 +33,127 @@ class TestConvexity:
3233 )
3334
3435 # Tests for convexity.py
36+ def test_similarity_space_check (self ):
37+ with pytest .raises (ValueError ):
38+ space_wrong_shape = SimilaritySpace (np .array ([1 ]))
39+ with pytest .raises (ValueError ):
40+ space_wrong_prior_len = SimilaritySpace (
41+ np .array ([[1 ]]), point_prior = np .array ([1 , 2 ])
42+ )
43+ with pytest .raises (ValueError ):
44+ space_wrong_prior_shape = SimilaritySpace (
45+ np .array ([[1 ]]), point_prior = np .array ([[1 ]])
46+ )
47+ with pytest .raises (ValueError ):
48+ space_invalid_prior = SimilaritySpace (
49+ np .array ([[1 ], [2 ]]), point_prior = np .array ([1 , 2 ])
50+ )
51+ with pytest .raises (ValueError ):
52+ space_zero_prior = SimilaritySpace (
53+ np .array ([[1 ], [2 ]]), point_prior = np .array ([1 , 0 ])
54+ )
55+ with pytest .raises (ValueError ):
56+ space_negative_prior = SimilaritySpace (
57+ np .array ([[1 ], [2 ]]), point_prior = np .array ([2 , - 1 ])
58+ )
59+
3560 def test_convexity_calculation (self ):
3661 assert (
37- self .sim_space .encoder_convexity (np .ones ((9 , 1 )) / 9 , np .array ([1 ])) - 1
62+ abs (
63+ self .sim_space .encoder_convexity (np .ones ((9 , 1 )) / 9 , np .array ([1 ])) - 1
64+ )
3865 < IB_EPSILON
3966 )
4067 # Epsilon is higher because rounding errors are more common beacuse of the nature of the algorithm
4168 assert (
42- self .sim_space .encoder_convexity (
43- np .array ([[0.2 ], [0 ], [0.2 ], [0 ], [0.2 ], [0 ], [0.2 ], [0 ], [0.2 ]]),
44- np .array ([1 ]),
69+ abs (
70+ self .sim_space .skinner_encoder_convexity (
71+ np .array (
72+ [
73+ # fmt: off
74+ [0.2 , 0 ],
75+ [0 , 1 ],
76+ [0.2 , 0 ],
77+ [0 , 0 ],
78+ [0.2 , 0 ],
79+ [0 , 0 ],
80+ [0.2 , 0 ],
81+ [0 , 0 ],
82+ [0.2 , 0 ],
83+ # fmt: on
84+ ]
85+ ),
86+ np .array ([0.5 , 0.5 ]),
87+ )
88+ - 19 / 27
89+ )
90+ < 0.005
91+ )
92+ assert (
93+ abs (
94+ self .sim_space .encoder_convexity (
95+ np .array (
96+ [
97+ # fmt: off
98+ [0.2 , 0 ],
99+ [0 , 1 ],
100+ [0.2 , 0 ],
101+ [0 , 0 ],
102+ [0.2 , 0 ],
103+ [0 , 0 ],
104+ [0.2 , 0 ],
105+ [0 , 0 ],
106+ [0.2 , 0 ],
107+ # fmt: on
108+ ]
109+ ),
110+ np .array ([0.5 , 0.5 ]),
111+ )
112+ - 14 / 18
45113 )
46- - 5 / 9
47114 < 0.005
48115 )
49116
50117 def test_projection_down (self ):
51118 # This also tests 1d spaces
52119 assert (
53- self .sim_space_large .encoder_convexity (
54- np .array (
55- [
56- # fmt: off
120+ abs (
121+ self .sim_space_large .encoder_convexity (
122+ np .array (
123+ [
124+ # fmt: off
57125 [0.33 ], [0 ], [0 ], [0 ],
58126 [0 ], [0 ], [0 ], [0 ],
59127 [0.33 ], [0 ], [0 ], [0 ],
60128 [0.33 ], [0 ], [0 ], [0 ],
61- # fmt: on
62- ]
63- ),
64- np .array ([1 ]),
129+ # fmt: on
130+ ]
131+ )
132+ / 0.99 ,
133+ np .array ([1 ]),
134+ )
135+ - 3 / 4
65136 )
66- - 3 / 4
67137 < 0.005
68138 )
69139 assert (
70- self .sim_space_large .encoder_convexity (
71- np .array (
72- [
73- # fmt: off
140+ abs (
141+ self .sim_space_large .encoder_convexity (
142+ np .array (
143+ [
144+ # fmt: off
74145 [0.5 ], [0 ], [0 ], [0 ],
75146 [0 ], [0 ], [0 ], [0 ],
76147 [0 ], [0 ], [0 ], [0 ],
77148 [0.5 ], [0 ], [0 ], [0 ],
78- # fmt: on
79- ]
80- ),
81- np .array ([1 ]),
82- steps = 1000 ,
149+ # fmt: on
150+ ]
151+ ),
152+ np .array ([1 ]),
153+ steps = 1000 ,
154+ )
155+ - 1 / 2
83156 )
84- - 1 / 2
85157 < 0.005
86158 )
87159
@@ -96,17 +168,57 @@ def test_language_convexity(self):
96168 )
97169 simple_lang = IBLanguage (simple_struct , np .ones ((1 , 9 )))
98170 assert (
99- self .sim_space .language_convexity (simple_lang )
100- - self .sim_space .encoder_convexity (np .ones ((9 , 1 )) / 9 , np .array ([1 ]))
171+ abs (
172+ self .sim_space .language_convexity (simple_lang )
173+ - self .sim_space .encoder_convexity (np .ones ((9 , 1 )) / 9 , np .array ([1 ]))
174+ )
101175 < IB_EPSILON
102176 )
103177 # This will fail on remote if the epsilon is not higher
104178 assert (
105- self .sim_space .language_convexity (simple_lang , referents = True , steps = 1000 )
106- - self .sim_space .encoder_convexity (
107- np .array ([[0.465 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.465 ]]).T ,
108- np .array ([1 ]),
109- steps = 1000 ,
179+ abs (
180+ self .sim_space .language_convexity (
181+ simple_lang , referents = True , steps = 1000
182+ )
183+ - self .sim_space .encoder_convexity (
184+ np .array (
185+ [[0.465 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.01 , 0.465 ]]
186+ ).T ,
187+ np .array ([1 ]),
188+ steps = 1000 ,
189+ )
110190 )
111191 < 0.005
112192 )
193+
194+ def test_convexity_check (self ):
195+ with pytest .raises (ValueError ):
196+ input_wrong_shape = self .sim_space .quasi_convexity (np .ones ((9 , 1 )), 100 )
197+ with pytest .raises (ValueError ):
198+ input_wrong_size = self .sim_space .quasi_convexity (np .ones (8 ) / 8 , 100 )
199+ with pytest .raises (ValueError ):
200+ input_invalid = self .sim_space .quasi_convexity (np .ones (9 ), 100 )
201+ with pytest .raises (ValueError ):
202+ input_negative = self .sim_space .quasi_convexity (
203+ np .array ([- 7 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]), 100
204+ )
205+ with pytest .raises (ValueError ):
206+ steps_negative = self .sim_space .quasi_convexity (np .ones (9 ) / 9 , - 1 )
207+
208+ def test_encoder_check (self ):
209+ with pytest .raises (ValueError ):
210+ prior_invalid = self .sim_space .encoder_convexity (
211+ np .ones ((9 , 1 )) / 9 , np .array ([0.9 ])
212+ )
213+ with pytest .raises (ValueError ):
214+ prior_invalid_skinner = self .sim_space .skinner_encoder_convexity (
215+ np .ones ((9 , 1 )) / 9 , np .array ([0.9 ])
216+ )
217+ with pytest .raises (ValueError ):
218+ prior_negative = self .sim_space .encoder_convexity (
219+ np .ones ((9 , 2 )) / 9 , np .array ([- 1 , 2 ])
220+ )
221+ with pytest .raises (ValueError ):
222+ prior_negative_skinner = self .sim_space .skinner_encoder_convexity (
223+ np .ones ((9 , 2 )) / 9 , np .array ([- 1 , 2 ])
224+ )
0 commit comments