20
20
from pkg .apis .manager .v1beta1 .python import api_pb2
21
21
from pkg .suggestion .v1beta1 .skopt .service import SkoptService
22
22
23
+ import utils
24
+
23
25
24
26
class TestSkopt (unittest .TestCase ):
25
27
def setUp (self ):
26
28
servicers = {
27
- api_pb2 .DESCRIPTOR .services_by_name [' Suggestion' ]: SkoptService (
29
+ api_pb2 .DESCRIPTOR .services_by_name [" Suggestion" ]: SkoptService (
28
30
)
29
31
}
30
32
@@ -177,8 +179,8 @@ def test_get_suggestion(self):
177
179
178
180
get_suggestion = self .test_server .invoke_unary_unary (
179
181
method_descriptor = (api_pb2 .DESCRIPTOR
180
- .services_by_name [' Suggestion' ]
181
- .methods_by_name [' GetSuggestions' ]),
182
+ .services_by_name [" Suggestion" ]
183
+ .methods_by_name [" GetSuggestions" ]),
182
184
invocation_metadata = {},
183
185
request = request , timeout = 1 )
184
186
@@ -188,124 +190,126 @@ def test_get_suggestion(self):
188
190
self .assertEqual (2 , len (response .parameter_assignments ))
189
191
190
192
def test_validate_algorithm_settings (self ):
191
- experiment_spec = [ None ]
192
-
193
- def call_validate ():
194
- experiment = api_pb2 . Experiment ( name = "test" , spec = experiment_spec [ 0 ])
195
- request = api_pb2 . ValidateAlgorithmSettingsRequest ( experiment = experiment )
196
-
197
- validate_algorithm_settings = self . test_server . invoke_unary_unary (
198
- method_descriptor = ( api_pb2 . DESCRIPTOR
199
- . services_by_name [ 'Suggestion' ]
200
- . methods_by_name [ 'ValidateAlgorithmSettings' ]) ,
201
- invocation_metadata = {},
202
- request = request , timeout = 1 )
193
+ # Valid cases.
194
+ experiment_spec = api_pb2 . ExperimentSpec (
195
+ algorithm = api_pb2 . AlgorithmSpec (
196
+ algorithm_name = "bayesianoptimization" ,
197
+ algorithm_settings = [
198
+ api_pb2 . AlgorithmSetting (
199
+ name = "random_state" ,
200
+ value = "10"
201
+ )
202
+ ] ,
203
+ )
204
+ )
203
205
204
- return validate_algorithm_settings .termination ()
206
+ _ , _ , code , _ = utils .call_validate (self .test_server , experiment_spec )
207
+ self .assertEqual (code , grpc .StatusCode .OK )
205
208
206
- # valid cases
207
- algorithm_spec = api_pb2 .AlgorithmSpec (
208
- algorithm_name = "bayesianoptimization" ,
209
- algorithm_settings = [
210
- api_pb2 .AlgorithmSetting (
211
- name = "random_state" ,
212
- value = "10"
213
- )
214
- ],
209
+ # Invalid cases.
210
+ # Unknown algorithm name.
211
+ experiment_spec = api_pb2 .ExperimentSpec (
212
+ algorithm = api_pb2 .AlgorithmSpec (algorithm_name = "unknown" )
215
213
)
216
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (algorithm = algorithm_spec )
217
- self .assertEqual (call_validate ()[2 ], grpc .StatusCode .OK )
218
214
219
- # invalid cases
220
- # unknown algorithm name
221
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
222
- algorithm = api_pb2 .AlgorithmSpec (algorithm_name = "unknown" ))
223
- _ , _ , code , details = call_validate ()
215
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
224
216
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
225
- self .assertEqual (details , ' unknown algorithm name unknown' )
217
+ self .assertEqual (details , " unknown algorithm name unknown" )
226
218
227
- # unknown config name
228
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
219
+ # Unknown config name.
220
+ experiment_spec = api_pb2 .ExperimentSpec (
229
221
algorithm = api_pb2 .AlgorithmSpec (
230
222
algorithm_name = "bayesianoptimization" ,
231
223
algorithm_settings = [
232
224
api_pb2 .AlgorithmSetting (name = "unknown_conf" , value = "1111" )]
233
- ))
234
- _ , _ , code , details = call_validate ()
225
+ )
226
+ )
227
+
228
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
235
229
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
236
- self .assertEqual (details , ' unknown setting unknown_conf for algorithm bayesianoptimization' )
230
+ self .assertEqual (details , " unknown setting unknown_conf for algorithm bayesianoptimization" )
237
231
238
- # unknown base_estimator
239
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
232
+ # Unknown base_estimator
233
+ experiment_spec = api_pb2 .ExperimentSpec (
240
234
algorithm = api_pb2 .AlgorithmSpec (
241
235
algorithm_name = "bayesianoptimization" ,
242
236
algorithm_settings = [
243
237
api_pb2 .AlgorithmSetting (name = "base_estimator" , value = "unknown estimator" )]
244
- ))
245
- _ , _ , code , details = call_validate ()
246
- wrong_algorithm_setting = experiment_spec [0 ].algorithm .algorithm_settings [0 ]
238
+ )
239
+ )
240
+ wrong_algorithm_setting = experiment_spec .algorithm .algorithm_settings [0 ]
241
+
242
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
247
243
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
248
244
self .assertEqual (details ,
249
245
"{name} {value} is not supported in Bayesian optimization" .format (
250
246
name = wrong_algorithm_setting .name ,
251
247
value = wrong_algorithm_setting .value ))
252
248
253
- # wrong n_initial_points
254
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
249
+ # Wrong n_initial_points
250
+ experiment_spec = api_pb2 .ExperimentSpec (
255
251
algorithm = api_pb2 .AlgorithmSpec (
256
252
algorithm_name = "bayesianoptimization" ,
257
253
algorithm_settings = [
258
254
api_pb2 .AlgorithmSetting (name = "n_initial_points" , value = "-1" )]
259
- ))
260
- _ , _ , code , details = call_validate ()
261
- wrong_algorithm_setting = experiment_spec [0 ].algorithm .algorithm_settings [0 ]
255
+ )
256
+ )
257
+ wrong_algorithm_setting = experiment_spec .algorithm .algorithm_settings [0 ]
258
+
259
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
262
260
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
263
261
self .assertEqual (details , "{name} should be great or equal than zero" .format (name = wrong_algorithm_setting .name ))
264
262
265
- # unknown acq_func
266
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
263
+ # Unknown acq_func
264
+ experiment_spec = api_pb2 .ExperimentSpec (
267
265
algorithm = api_pb2 .AlgorithmSpec (
268
266
algorithm_name = "bayesianoptimization" ,
269
267
algorithm_settings = [
270
268
api_pb2 .AlgorithmSetting (name = "acq_func" , value = "unknown" )]
271
- ))
272
- _ , _ , code , details = call_validate ( )
269
+ )
270
+ )
273
271
wrong_algorithm_setting = experiment_spec [0 ].algorithm .algorithm_settings [0 ]
272
+
273
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
274
274
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
275
275
self .assertEqual (details ,
276
276
"{name} {value} is not supported in Bayesian optimization" .format (
277
277
name = wrong_algorithm_setting .name ,
278
278
value = wrong_algorithm_setting .value
279
279
))
280
280
281
- # unknown acq_optimizer
282
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
281
+ # Unknown acq_optimizer
282
+ experiment_spec = api_pb2 .ExperimentSpec (
283
283
algorithm = api_pb2 .AlgorithmSpec (
284
284
algorithm_name = "bayesianoptimization" ,
285
285
algorithm_settings = [
286
286
api_pb2 .AlgorithmSetting (name = "acq_optimizer" , value = "unknown" )]
287
- ))
288
- _ , _ , code , details = call_validate ( )
287
+ )
288
+ )
289
289
wrong_algorithm_setting = experiment_spec [0 ].algorithm .algorithm_settings [0 ]
290
+
291
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
290
292
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
291
293
self .assertEqual (details ,
292
294
"{name} {value} is not supported in Bayesian optimization" .format (
293
295
name = wrong_algorithm_setting .name ,
294
296
value = wrong_algorithm_setting .value
295
297
))
296
298
297
- # wrong random_state
298
- experiment_spec [ 0 ] = api_pb2 .ExperimentSpec (
299
+ # Wrong random_state
300
+ experiment_spec = api_pb2 .ExperimentSpec (
299
301
algorithm = api_pb2 .AlgorithmSpec (
300
302
algorithm_name = "bayesianoptimization" ,
301
303
algorithm_settings = [
302
304
api_pb2 .AlgorithmSetting (name = "random_state" , value = "-1" )]
303
- ))
304
- _ , _ , code , details = call_validate ()
305
- wrong_algorithm_setting = experiment_spec [0 ].algorithm .algorithm_settings [0 ]
305
+ )
306
+ )
307
+ wrong_algorithm_setting = experiment_spec .algorithm .algorithm_settings [0 ]
308
+
309
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
306
310
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
307
311
self .assertEqual (details , "{name} should be great or equal than zero" .format (name = wrong_algorithm_setting .name ))
308
312
309
313
310
- if __name__ == ' __main__' :
314
+ if __name__ == " __main__" :
311
315
unittest .main ()
0 commit comments