コード例 #1
0
ファイル: test_type_checking.py プロジェクト: MSaber9/lale
    def test_disable_schema_validation_individual_op(self):
        os.environ["LALE_DISABLE_SCHEMA_VALIDATION"] = "True"
        import lale.schemas as schemas
        from lale.lib.sklearn import PCA

        pca_input = schemas.Object(
            X=schemas.AnyOf(
                [
                    schemas.Array(schemas.Array(schemas.String())),
                    schemas.Array(schemas.String()),
                ]
            )
        )

        foo = PCA.customize_schema(input_fit=pca_input)

        pca_output = schemas.Object(
            X=schemas.AnyOf(
                [
                    schemas.Array(schemas.Array(schemas.String())),
                    schemas.Array(schemas.String()),
                ]
            )
        )

        foo = foo.customize_schema(output_transform=pca_output)

        abc = foo()
        trained_pca = abc.fit(self.X_train)
        trained_pca.transform(self.X_test)
        os.environ["LALE_DISABLE_SCHEMA_VALIDATION"] = "False"
コード例 #2
0
ファイル: test_type_checking.py プロジェクト: gbdrt/lale
    def test_enable_schema_validation_individual_op(self):
        existing_flag = disable_data_schema_validation
        set_disable_data_schema_validation(False)
        import lale.schemas as schemas
        from lale.lib.sklearn import PCA

        pca_input = schemas.Object(X=schemas.AnyOf([
            schemas.Array(schemas.Array(schemas.String())),
            schemas.Array(schemas.String()),
        ]))

        foo = PCA.customize_schema(input_fit=pca_input)

        pca_output = schemas.Object(X=schemas.AnyOf([
            schemas.Array(schemas.Array(schemas.String())),
            schemas.Array(schemas.String()),
        ]))

        foo = foo.customize_schema(output_transform=pca_output)

        abc = foo()
        with self.assertRaises(ValueError):
            trained_pca = abc.fit(self.X_train)
            trained_pca.transform(self.X_test)
        set_disable_data_schema_validation(existing_flag)
コード例 #3
0
 def test_override_output2(self):
     init_output_schema = self.sk_pca.get_schema("output_transform")
     pca_output = schemas.AnyOf([
         schemas.Array(schemas.Array(schemas.Float())),
         schemas.Array(schemas.Float()),
     ])
     expected = {
         "anyOf": [
             {
                 "type": "array",
                 "items": {
                     "type": "array",
                     "items": {
                         "type": "number"
                     }
                 },
             },
             {
                 "type": "array",
                 "items": {
                     "type": "number"
                 }
             },
         ]
     }
     foo = self.sk_pca.customize_schema(output_transform=pca_output)
     self.assertEqual(foo.get_schema("output_transform"), expected)
     lale.type_checking.validate_is_schema(foo._schemas)
     self.assertEqual(self.sk_pca.get_schema("output_transform"),
                      init_output_schema)
コード例 #4
0
 def test_override_output2(self):
     init_output_schema = self.sk_pca.get_schema('output')
     pca_output = schemas.AnyOf([
         schemas.Array(schemas.Array(schemas.Float())),
         schemas.Array(schemas.Float())
     ])
     expected = {
         'anyOf': [{
             'type': 'array',
             'items': {
                 'type': 'array',
                 'items': {
                     'type': 'number'
                 }
             }
         }, {
             'type': 'array',
             'items': {
                 'type': 'number'
             }
         }]
     }
     foo = self.sk_pca.customize_schema(output=pca_output)
     self.assertEqual(foo.get_schema('output'), expected)
     helpers.validate_is_schema(foo._schemas)
     self.assertEqual(self.sk_pca.get_schema('output'), init_output_schema)
コード例 #5
0
 def test_add_constraint(self):
     init = self.sk_pca.hyperparam_schema()
     expected = {
         'allOf': [{
             'type': 'object',
             'properties': {}
         }, {
             'anyOf': [{
                 'type': 'object',
                 'properties': {
                     'n_components': {
                         'not': {
                             'enum': ['mle']
                         },
                     }
                 },
             }, {
                 'type': 'object',
                 'properties': {
                     'svd_solver': {
                         'enum': ['full', 'auto']
                     },
                 }
             }]
         }]
     }
     foo = self.sk_pca.customize_schema(constraint=schemas.AnyOf([
         schemas.Object(
             {'n_components': schemas.Not(schemas.Enum(['mle']))}),
         schemas.Object({'svd_solver': schemas.Enum(['full', 'auto'])})
     ]))
     self.assertEqual(foo.hyperparam_schema(), expected)
     helpers.validate_is_schema(foo._schemas)
     self.assertEqual(self.sk_pca.hyperparam_schema(), init)
コード例 #6
0
ファイル: test_custom_schemas.py プロジェクト: hirzel/lale
 def test_add_constraint(self):
     init = self.sk_pca.hyperparam_schema()
     init_expected = {
         "allOf": [
             {
                 "type": "object",
                 "relevantToOptimizer": [],
                 "additionalProperties": False,
                 "properties": {
                     "n_components": {"default": None},
                     "copy": {"default": True},
                     "whiten": {"default": False},
                     "svd_solver": {"default": "auto"},
                     "tol": {"default": 0.0},
                     "iterated_power": {"default": "auto"},
                     "random_state": {"default": None},
                 },
             }
         ]
     }
     self.assertEqual(init, init_expected)
     expected = {
         "allOf": [
             init_expected["allOf"][0],
             {
                 "anyOf": [
                     {
                         "type": "object",
                         "properties": {
                             "n_components": {
                                 "not": {"enum": ["mle"]},
                             }
                         },
                     },
                     {
                         "type": "object",
                         "properties": {
                             "svd_solver": {"enum": ["full", "auto"]},
                         },
                     },
                 ]
             },
         ]
     }
     foo = self.sk_pca.customize_schema(
         constraint=schemas.AnyOf(
             [
                 schemas.Object(n_components=schemas.Not(schemas.Enum(["mle"]))),
                 schemas.Object(svd_solver=schemas.Enum(["full", "auto"])),
             ]
         )
     )
     self.assertEqual(foo.hyperparam_schema(), expected)
     lale.type_checking.validate_is_schema(foo._schemas)
     self.assertEqual(self.sk_pca.hyperparam_schema(), init)
コード例 #7
0
ファイル: test_custom_schemas.py プロジェクト: sreev/lale
 def test_override_any_param(self):
     init = self.ll_pca.hyperparam_schema('iterated_power')
     expected = {'anyOf': [
         {'type': 'integer'},
         {'enum': ['auto', 'full']}],
         'default': 'auto'}
     foo = self.ll_pca.customize_schema(
         iterated_power=schemas.AnyOf([schemas.Int(),
                                       schemas.Enum(['auto', 'full'])], default='auto'))
     self.assertEqual(foo.hyperparam_schema('iterated_power'), expected)
     lale.type_checking.validate_is_schema(foo._schemas)
     self.assertEqual(self.ll_pca.hyperparam_schema('iterated_power'), init)
コード例 #8
0
ファイル: test_custom_schemas.py プロジェクト: hirzel/lale
 def test_override_any_param(self):
     init = self.ll_pca.hyperparam_schema("iterated_power")
     expected = {
         "anyOf": [{"type": "integer"}, {"enum": ["auto", "full"]}],
         "default": "auto",
     }
     foo = self.ll_pca.customize_schema(
         iterated_power=schemas.AnyOf(
             [schemas.Int(), schemas.Enum(["auto", "full"])], default="auto"
         )
     )
     self.assertEqual(foo.hyperparam_schema("iterated_power"), expected)
     lale.type_checking.validate_is_schema(foo._schemas)
     self.assertEqual(self.ll_pca.hyperparam_schema("iterated_power"), init)
コード例 #9
0
    def test_disable_schema_validation_pipeline(self):
        os.environ["LALE_DISABLE_SCHEMA_VALIDATION"]='True'
        from lale.lib.sklearn import PCA, LogisticRegression
        import lale.schemas as schemas

        lr_input = schemas.Object(required=['X', 'y'], X=schemas.AnyOf([
            schemas.Array(
                schemas.Array(
                    schemas.String())),
            schemas.Array(
                schemas.String())]),
            y=schemas.Array(schemas.String()))

        foo = LogisticRegression.customize_schema(input_fit=lr_input)
        abc = foo()
        pipeline = PCA() >> abc
        trained_pipeline = pipeline.fit(self.X_train, self.y_train)
        trained_pipeline.predict(self.X_test)
        os.environ["LALE_DISABLE_SCHEMA_VALIDATION"]='False'
コード例 #10
0
    def test_enable_schema_validation_pipeline(self):
        with EnableSchemaValidation():
            import lale.schemas as schemas
            from lale.lib.sklearn import PCA, LogisticRegression

            lr_input = schemas.Object(
                required=["X", "y"],
                X=schemas.AnyOf([
                    schemas.Array(schemas.Array(schemas.String())),
                    schemas.Array(schemas.String()),
                ]),
                y=schemas.Array(schemas.String()),
            )

            foo = LogisticRegression.customize_schema(input_fit=lr_input)
            abc = foo()
            pipeline = PCA() >> abc
            with self.assertRaises(ValueError):
                trained_pipeline = pipeline.fit(self.X_train, self.y_train)
                trained_pipeline.predict(self.X_test)
コード例 #11
0
    def test_disable_schema_validation_pipeline(self):
        existing_flag = disable_data_schema_validation
        set_disable_data_schema_validation(True)
        import lale.schemas as schemas
        from lale.lib.sklearn import PCA, LogisticRegression

        lr_input = schemas.Object(
            required=["X", "y"],
            X=schemas.AnyOf([
                schemas.Array(schemas.Array(schemas.String())),
                schemas.Array(schemas.String()),
            ]),
            y=schemas.Array(schemas.String()),
        )

        foo = LogisticRegression.customize_schema(input_fit=lr_input)
        abc = foo()
        pipeline = PCA() >> abc
        trained_pipeline = pipeline.fit(self.X_train, self.y_train)
        trained_pipeline.predict(self.X_test)
        set_disable_data_schema_validation(existing_flag)
コード例 #12
0
ファイル: test_custom_schemas.py プロジェクト: sreev/lale
 def test_add_constraint(self):
     init = self.sk_pca.hyperparam_schema()
     init_expected = {'allOf': [
         {   'type': 'object',
             'relevantToOptimizer': [],
             'additionalProperties': False,
             'properties': {
                 'n_components': {'default': None},
                 'copy': {'default': True},
                 'whiten': {'default': False},
                 'svd_solver': {'default': 'auto'},
                 'tol': {'default': 0.0},
                 'iterated_power': {'default': 'auto'},
                 'random_state': {'default': None}}}]}
     self.assertEqual(init, init_expected)
     expected = {'allOf': [
         init_expected['allOf'][0],
         {'anyOf': [
             {'type': 'object',
              'properties': {
                  'n_components': {
                      'not': {
                          'enum': ['mle']},
                  }},
              },
             {'type': 'object',
              'properties': {
                  'svd_solver': {
                      'enum': ['full', 'auto']},
              }}]}]}
     foo = self.sk_pca.customize_schema(
         constraint=schemas.AnyOf([
             schemas.Object(n_components=schemas.Not(schemas.Enum(['mle']))),
             schemas.Object(svd_solver=schemas.Enum(['full', 'auto']))
         ]))
     self.assertEqual(foo.hyperparam_schema(), expected)
     lale.type_checking.validate_is_schema(foo._schemas)
     self.assertEqual(self.sk_pca.hyperparam_schema(), init)