Skip to content

Commit 6710b40

Browse files
committed
Handling different sklearn versions in unit testing
1 parent 3b44e86 commit 6710b40

1 file changed

Lines changed: 36 additions & 52 deletions

File tree

tests/test_extensions/test_sklearn_extension/test_sklearn_extension.py

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,8 @@ def test_serialize_pipeline(self):
292292
"estimator, or a transformer removed by setting\nit to "\
293293
"'passthrough' or ``None``."
294294
else:
295-
fixture_description = "Pipeline of transforms with a final estimator.\n\nSequentially"\
296-
" apply a list of transforms and a final estimator.\n"\
297-
"Intermediate steps of the pipeline must be 'transforms', that "\
298-
"is, they\nmust implement fit and transform methods.\nThe final"\
299-
" estimator only needs to implement fit.\n\nThe purpose of the "\
300-
"pipeline is to assemble several steps that can be\n"\
301-
"cross-validated together while setting different parameters."\
302-
"\nFor this, it enables setting parameters of the various steps"\
303-
" using their\nnames and the parameter name separated by a '__',"\
304-
" as in the example below.\nA step's estimator may be replaced "\
305-
"entirely by setting the parameter\nwith its name to another "\
306-
"estimator, or a transformer removed by setting\nto None."
295+
fixture_description = self.extension._get_sklearn_description(model)
296+
307297
fixture_structure = {
308298
fixture_name: [],
309299
'sklearn.preprocessing.data.StandardScaler': ['scaler'],
@@ -315,9 +305,6 @@ def test_serialize_pipeline(self):
315305

316306
self.assertEqual(serialization.name, fixture_name)
317307
self.assertEqual(serialization.custom_name, fixture_short_name)
318-
TestBase.logger.info("\n\ntest_serialize_pipeline\n---------------------\n{}\n"
319-
"{}\n\n{}\n\n".format(sklearn.__version__, serialization.description,
320-
fixture_description))
321308
self.assertEqual(serialization.description, fixture_description)
322309
self.assertDictEqual(structure, fixture_structure)
323310

@@ -412,18 +399,7 @@ def test_serialize_pipeline_clustering(self):
412399
"estimator, or a transformer removed by setting\nit to "\
413400
"'passthrough' or ``None``."
414401
else:
415-
fixture_description = "Pipeline of transforms with a final estimator.\n\nSequentially"\
416-
" apply a list of transforms and a final estimator.\n"\
417-
"Intermediate steps of the pipeline must be 'transforms', that "\
418-
"is, they\nmust implement fit and transform methods.\nThe final"\
419-
" estimator only needs to implement fit.\n\nThe purpose of the "\
420-
"pipeline is to assemble several steps that can be\n"\
421-
"cross-validated together while setting different parameters."\
422-
"\nFor this, it enables setting parameters of the various steps"\
423-
" using their\nnames and the parameter name separated by a '__',"\
424-
" as in the example below.\nA step's estimator may be replaced "\
425-
"entirely by setting the parameter\nwith its name to another "\
426-
"estimator, or a transformer removed by setting\nto None."
402+
fixture_description = self.extension._get_sklearn_description(model)
427403
fixture_structure = {
428404
fixture_name: [],
429405
'sklearn.preprocessing.data.StandardScaler': ['scaler'],
@@ -435,9 +411,6 @@ def test_serialize_pipeline_clustering(self):
435411

436412
self.assertEqual(serialization.name, fixture_name)
437413
self.assertEqual(serialization.custom_name, fixture_short_name)
438-
TestBase.logger.info("\n\ntest_serialize_pipeline_clustering\n---------------------\n{}\n"
439-
"{}\n\n{}\n\n".format(sklearn.__version__, serialization.description,
440-
fixture_description))
441414
self.assertEqual(serialization.description, fixture_description)
442415
self.assertDictEqual(structure, fixture_structure)
443416

@@ -518,14 +491,20 @@ def test_serialize_column_transformer(self):
518491
'numeric=sklearn.preprocessing.data.StandardScaler,' \
519492
'nominal=sklearn.preprocessing._encoders.OneHotEncoder)'
520493
fixture_short_name = 'sklearn.ColumnTransformer'
521-
# str obtained from self.extension._get_sklearn_description(model)
522-
fixture_description = 'Applies transformers to columns of an array or pandas DataFrame.\n' \
523-
'\nThis estimator allows different columns or column subsets of the '\
524-
'input\nto be transformed separately and the features generated by '\
525-
'each transformer\nwill be concatenated to form a single feature '\
526-
'space.\nThis is useful for heterogeneous or columnar data, to '\
527-
'combine several\nfeature extraction mechanisms or transformations '\
528-
'into a single transformer.'
494+
495+
if version.parse(sklearn.__version__) >= version.parse("0.21.0"):
496+
# str obtained from self.extension._get_sklearn_description(model)
497+
fixture_description = 'Applies transformers to columns of an array or pandas '\
498+
'DataFrame.\n\nThis estimator allows different columns or '\
499+
'column subsets of the input\nto be transformed separately and '\
500+
'the features generated by each transformer\nwill be '\
501+
'concatenated to form a single feature space.\nThis is useful '\
502+
'for heterogeneous or columnar data, to combine several\nfeature'\
503+
' extraction mechanisms or transformations into a single '\
504+
'transformer.'
505+
else:
506+
fixture_description = self.extension._get_sklearn_description(model)
507+
529508
fixture_structure = {
530509
fixture: [],
531510
'sklearn.preprocessing.data.StandardScaler': ['numeric'],
@@ -584,20 +563,25 @@ def test_serialize_column_transformer_pipeline(self):
584563
fixture_name: [],
585564
}
586565

587-
# str obtained from self.extension._get_sklearn_description(model)
588-
fixture_description = "Pipeline of transforms with a final estimator.\n\nSequentially "\
589-
"apply a list of transforms and a final estimator.\nIntermediate "\
590-
"steps of the pipeline must be 'transforms', that is, they\nmust "\
591-
"implement fit and transform methods.\nThe final estimator only "\
592-
"needs to implement fit.\nThe transformers in the pipeline can be "\
593-
"cached using ``memory`` argument.\n\nThe purpose of the pipeline "\
594-
"is to assemble several steps that can be\ncross-validated together "\
595-
"while setting different parameters.\nFor this, it enables setting "\
596-
"parameters of the various steps using their\nnames and the "\
597-
"parameter name separated by a '__', as in the example below.\nA "\
598-
"step's estimator may be replaced entirely by setting the parameter"\
599-
"\nwith its name to another estimator, or a transformer removed by "\
600-
"setting\nit to 'passthrough' or ``None``."
566+
if version.parse(sklearn.__version__) >= version.parse("0.21.0"):
567+
# str obtained from self.extension._get_sklearn_description(model)
568+
fixture_description = "Pipeline of transforms with a final estimator.\n\nSequentially"\
569+
" apply a list of transforms and a final estimator.\n"\
570+
"Intermediate steps of the pipeline must be 'transforms', that "\
571+
"is, they\nmust implement fit and transform methods.\nThe final"\
572+
" estimator only needs to implement fit.\nThe transformers in "\
573+
"the pipeline can be cached using ``memory`` argument.\n\nThe "\
574+
"purpose of the pipeline is to assemble several steps that can "\
575+
"be\ncross-validated together while setting different "\
576+
"parameters.\nFor this, it enables setting parameters of the "\
577+
"various steps using their\nnames and the parameter name "\
578+
"separated by a '__', as in the example below.\nA step's "\
579+
"estimator may be replaced entirely by setting the parameter\n"\
580+
"with its name to another estimator, or a transformer removed by"\
581+
" setting\nit to 'passthrough' or ``None``."
582+
else:
583+
fixture_description = self.extension._get_sklearn_description(model)
584+
601585
serialization = self.extension.model_to_flow(model)
602586
structure = serialization.get_structure('name')
603587
self.assertEqual(serialization.name, fixture_name)

0 commit comments

Comments
 (0)