示例#1
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)
示例#2
0
    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"
示例#3
0
    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)
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
 def test_override_object_param(self):
     init = self.sk_pca.get_schema('input_fit')
     expected = {'type': 'object',
                 'required': ['X'],
                 'additionalProperties': False,
                 'properties': {
                     'X': {'type': 'array',
                           'items': {
                               'type': 'number'}}}}
     foo = self.sk_pca.customize_schema(
         input_fit=schemas.Object(required=['X'],
                                  additionalProperties=False,
                                  X=schemas.Array(schemas.Float())))
     self.assertEqual(foo.get_schema('input_fit'), expected)
     lale.type_checking.validate_is_schema(foo.get_schema('input_fit'))
     self.assertEqual(self.sk_pca.get_schema('input_fit'), init)
示例#7
0
 def test_override_object_param(self):
     init = self.sk_pca.get_schema("input_fit")
     expected = {
         "type": "object",
         "required": ["X"],
         "additionalProperties": False,
         "properties": {"X": {"type": "array", "items": {"type": "number"}}},
     }
     foo = self.sk_pca.customize_schema(
         input_fit=schemas.Object(
             required=["X"],
             additionalProperties=False,
             X=schemas.Array(schemas.Float()),
         )
     )
     self.assertEqual(foo.get_schema("input_fit"), expected)
     lale.type_checking.validate_is_schema(foo.get_schema("input_fit"))
     self.assertEqual(self.sk_pca.get_schema("input_fit"), init)
示例#8
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'
示例#9
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)
示例#10
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)
示例#11
0
 def test_override_object_param(self):
     init = self.sk_pca.get_schema('input_fit')
     expected = {
         '$schema': 'http://json-schema.org/draft-04/schema#',
         'type': 'object',
         'required': ['X'],
         'additionalProperties': False,
         'properties': {
             'X': {
                 'type': 'array',
                 'items': {
                     'type': 'number'
                 }
             }
         }
     }
     foo = self.sk_pca.customize_schema(input_fit=schemas.Object(
         required=['X'],
         additionalProperties=False,
         properties={'X': schemas.Array(schemas.Float())}))
     self.assertEqual(foo.get_schema('input_fit'), expected)
     helpers.validate_is_schema(foo.get_schema('input_fit'))
     self.assertEqual(self.sk_pca.get_schema('input_fit'), init)