@@ -82,7 +82,7 @@ def test_invalid_data_config():
82
82
)
83
83
84
84
85
- def test_data_bias_config ():
85
+ def test_bias_config ():
86
86
label_values = [1 ]
87
87
facet_name = "F1"
88
88
facet_threshold = 0.3
@@ -103,52 +103,122 @@ def test_data_bias_config():
103
103
assert expected_config == data_bias_config .get_config ()
104
104
105
105
106
- def test_data_bias_config_multi_facet ():
107
- label_values = [1 ]
108
- facet_name = ["Facet1" , "Facet2" ]
109
- facet_threshold = [[0 ], [1 , 2 ]]
110
- group_name = "A151"
111
-
112
- data_bias_config = BiasConfig (
113
- label_values_or_threshold = label_values ,
114
- facet_name = facet_name ,
115
- facet_values_or_threshold = facet_threshold ,
116
- group_name = group_name ,
117
- )
106
+ def test_invalid_bias_config ():
107
+ # Empty facet list,
108
+ with pytest .raises (AssertionError , match = "Please provide at least one facet" ):
109
+ BiasConfig (
110
+ label_values_or_threshold = [1 ],
111
+ facet_name = [],
112
+ )
118
113
119
- expected_config = {
120
- "label_values_or_threshold" : label_values ,
121
- " facet" : [
122
- { "name_or_index" : facet_name [ 0 ], "value_or_threshold" : facet_threshold [ 0 ]},
123
- { "name_or_index" : facet_name [ 1 ], "value_or_threshold" : facet_threshold [ 1 ]},
124
- ],
125
- "group_variable" : group_name ,
126
- }
127
- assert expected_config == data_bias_config . get_config ( )
114
+ # Two facets but only one value
115
+ with pytest . raises (
116
+ ValueError , match = "The number of facet names doesn't match the number of facet values"
117
+ ):
118
+ BiasConfig (
119
+ label_values_or_threshold = [ 1 ],
120
+ facet_name = [ "Feature1" , "Feature2" ] ,
121
+ facet_values_or_threshold = [[ 1 ]],
122
+ )
128
123
129
124
130
- def test_data_bias_config_multi_facet_not_all_with_value ():
125
+ @pytest .mark .parametrize (
126
+ "facet_name,facet_values_or_threshold,expected_result" ,
127
+ [
128
+ # One facet, assume that it is binary and value 1 indicates the sensitive group
129
+ [
130
+ "Feature1" ,
131
+ [1 ],
132
+ {
133
+ "facet" : [{"name_or_index" : "Feature1" , "value_or_threshold" : [1 ]}],
134
+ },
135
+ ],
136
+ # The same facet as above, facet value is not specified. (Clarify will compute bias metrics
137
+ # for each binary value).
138
+ [
139
+ "Feature1" ,
140
+ None ,
141
+ {
142
+ "facet" : [{"name_or_index" : "Feature1" }],
143
+ },
144
+ ],
145
+ # Assume that the 2nd column (index 1, zero-based) of the dataset as facet, it has
146
+ # four categories and two of them indicate the sensitive group.
147
+ [
148
+ 1 ,
149
+ ["category1, category2" ],
150
+ {
151
+ "facet" : [{"name_or_index" : 1 , "value_or_threshold" : ["category1, category2" ]}],
152
+ },
153
+ ],
154
+ # The same facet as above, facet values are not specified. (Clarify will iterate
155
+ # the categories and compute bias metrics for each category).
156
+ [
157
+ 1 ,
158
+ None ,
159
+ {
160
+ "facet" : [{"name_or_index" : 1 }],
161
+ },
162
+ ],
163
+ # Assume that the facet is numeric value in range [0.0, 1.0]. Given facet threshold 0.5,
164
+ # interval (0.5, 1.0] indicates the sensitive group.
165
+ [
166
+ "Feature3" ,
167
+ [0.5 ],
168
+ {
169
+ "facet" : [{"name_or_index" : "Feature3" , "value_or_threshold" : [0.5 ]}],
170
+ },
171
+ ],
172
+ # Multiple facets
173
+ [
174
+ ["Feature1" , 1 , "Feature3" ],
175
+ [[1 ], ["category1, category2" ], [0.5 ]],
176
+ {
177
+ "facet" : [
178
+ {"name_or_index" : "Feature1" , "value_or_threshold" : [1 ]},
179
+ {"name_or_index" : 1 , "value_or_threshold" : ["category1, category2" ]},
180
+ {"name_or_index" : "Feature3" , "value_or_threshold" : [0.5 ]},
181
+ ],
182
+ },
183
+ ],
184
+ # Multiple facets, no value or threshold
185
+ [
186
+ ["Feature1" , 1 , "Feature3" ],
187
+ None ,
188
+ {
189
+ "facet" : [
190
+ {"name_or_index" : "Feature1" },
191
+ {"name_or_index" : 1 },
192
+ {"name_or_index" : "Feature3" },
193
+ ],
194
+ },
195
+ ],
196
+ # Multiple facets, specify values or threshold for some of them
197
+ [
198
+ ["Feature1" , 1 , "Feature3" ],
199
+ [[1 ], None , [0.5 ]],
200
+ {
201
+ "facet" : [
202
+ {"name_or_index" : "Feature1" , "value_or_threshold" : [1 ]},
203
+ {"name_or_index" : 1 },
204
+ {"name_or_index" : "Feature3" , "value_or_threshold" : [0.5 ]},
205
+ ],
206
+ },
207
+ ],
208
+ ],
209
+ )
210
+ def test_facet_of_bias_config (facet_name , facet_values_or_threshold , expected_result ):
131
211
label_values = [1 ]
132
- facet_name = ["Facet1" , "Facet2" ]
133
- facet_threshold = [[0 ], None ]
134
- group_name = "A151"
135
-
136
- data_bias_config = BiasConfig (
212
+ bias_config = BiasConfig (
137
213
label_values_or_threshold = label_values ,
138
214
facet_name = facet_name ,
139
- facet_values_or_threshold = facet_threshold ,
140
- group_name = group_name ,
215
+ facet_values_or_threshold = facet_values_or_threshold ,
141
216
)
142
-
143
217
expected_config = {
144
218
"label_values_or_threshold" : label_values ,
145
- "facet" : [
146
- {"name_or_index" : facet_name [0 ], "value_or_threshold" : facet_threshold [0 ]},
147
- {"name_or_index" : facet_name [1 ]},
148
- ],
149
- "group_variable" : group_name ,
219
+ ** expected_result ,
150
220
}
151
- assert expected_config == data_bias_config .get_config ()
221
+ assert bias_config .get_config () == expected_config
152
222
153
223
154
224
def test_model_config ():
0 commit comments