Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def split_with_schemas(estimator, all_X, all_y, indices, train_indices=None):
subset_X, subset_y = _safe_split(
estimator, all_X, all_y, indices, train_indices)
if hasattr(all_X, 'json_schema'):
n_rows = subset_X.shape[0]
schema = {
'type': 'array', 'minItems': n_rows, 'maxItems': n_rows,
'items': all_X.json_schema['items']}
lale.datasets.data_schemas.add_schema(subset_X, schema)
if hasattr(all_y, 'json_schema'):
n_rows = subset_y.shape[0]
schema = {
'type': 'array', 'minItems': n_rows, 'maxItems': n_rows,
'items': all_y.json_schema['items']}
lale.datasets.data_schemas.add_schema(subset_y, schema)
return subset_X, subset_y
def transform_schema(self, s_X):
"""Used internally by Lale for type-checking downstream operators."""
if hasattr(self, '_col_tfm'):
return self._transform_schema_col_tfm(s_X, self._col_tfm)
columns = self._hyperparams['columns']
if lale.type_checking.is_schema(columns):
return self._transform_schema_schema(s_X, columns)
if not lale.type_checking.is_schema(s_X):
X = lale.datasets.data_schemas.add_schema(s_X)
self.fit(X)
return self._transform_schema_col_tfm(X.json_schema, self._col_tfm)
return s_X
------
jsonschema.ValidationError
The lhs was an invalid value for super_schema.
SubschemaError
The lhs was or had a schema that was not a subschema of super_schema.
"""
disable_schema = os.environ.get("LALE_DISABLE_SCHEMA_VALIDATION", None)
if disable_schema is not None and disable_schema.lower()=='true':
return True #If schema validation is disabled, always return as valid
if is_schema(lhs):
sub_schema = lhs
else:
import lale.datasets.data_schemas
try:
sub_schema = lale.datasets.data_schemas.to_schema(lhs)
except ValueError as e:
sub_schema = None
if sub_schema is None:
validate_schema(lhs, super_schema)
else:
_validate_subschema(sub_schema, super_schema)
def transform(self, X):
raw = self._wrapped_model.transform(X)
if isinstance(raw, np.ndarray) or isinstance(raw, pd.DataFrame):
s_X = lale.datasets.data_schemas.to_schema(X)
s_result = self.transform_schema(s_X)
result = lale.datasets.data_schemas.add_schema(raw, s_result, recalc=True)
assert result.json_schema == s_result
else:
result = raw
return result
def transform(self, X):
raw = self._wrapped_model.transform(X)
if isinstance(raw, np.ndarray) or isinstance(raw, pd.DataFrame):
s_X = lale.datasets.data_schemas.to_schema(X)
s_result = self.transform_schema(s_X)
result = lale.datasets.data_schemas.add_schema(raw, s_result, recalc=True)
assert result.json_schema == s_result
else:
result = raw
return result
def transform(self, X):
columns = self._col_tfm.transformers[0][2]
if isinstance(X, pd.DataFrame) and isinstance(columns, list):
col_names = [
X.columns[c] if isinstance(c, int) else c for c in columns]
result = X[col_names]
else:
result = self._col_tfm.transform(X)
s_X = lale.datasets.data_schemas.to_schema(X)
s_result = self.transform_schema(s_X)
return lale.datasets.data_schemas.add_schema(result, s_result)
def convert_nested_objects(node):
for element in dir(node):#Looking at only 1 level for now.
try:
value = getattr(node,element)
if isinstance(value, Operator) and hasattr(value._impl_instance(), '_wrapped_model'):
#node is a higher order operator
setattr(node, element, value._impl_instance()._wrapped_model)
stripped = lale.datasets.data_schemas.strip_schema(value)
if value is stripped:
continue
setattr(node, element, stripped)
except BaseException:
#This is an optional processing, so if there is any exception, continue.
#For example, some scikit-learn classes will fail at getattr because they have
#that property defined conditionally.
pass
def transform(self, X):
raw = self._wrapped_model.transform(X)
if isinstance(raw, np.ndarray) or isinstance(raw, pd.DataFrame):
s_X = lale.datasets.data_schemas.to_schema(X)
s_result = self.transform_schema(s_X)
result = lale.datasets.data_schemas.add_schema(raw, s_result, recalc=True)
assert result.json_schema == s_result
else:
result = raw
return result
def _validate_output_schema(self, result, method):
if method == 'transform':
schema = self.output_schema_transform()
elif method == 'predict':
schema = self.output_schema_predict()
elif method == 'predict_proba':
schema = self.output_schema_predict_proba()
elif method == 'decision_function':
schema = self.output_schema_decision_function()
result = lale.datasets.data_schemas.add_schema(result)
try:
lale.type_checking.validate_schema_or_subschema(result, schema)
except Exception as e:
print(f'{self.name()}.{method}() invalid result: {e}')
raise ValueError(f'{self.name()}.{method}() invalid result: {e}') from e
return result
def transform(self, X):
stripped_X = lale.datasets.data_schemas.strip_schema(X)
result = self._wrapped_model.transform(stripped_X)
return result