Skip to content

Commit

Permalink
Pretty-printing for the case where a hyperparameter to a higher-order…
Browse files Browse the repository at this point in the history
… operator is a plain sklearn operator that has not been wrapped. (#330)
  • Loading branch information
hirzel authored Apr 28, 2020
1 parent ab3ed8c commit a6ede3e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lale/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ def visualize(self, ipython_display:bool=True):

def pretty_print(self, show_imports:bool=True, combinators:bool=True, ipython_display:Union[bool,str]=False):
"""Returns the Python source code representation of the operator.
Parameters
----------
show_imports : bool, default True
Whether to include import statements in the pretty-printed code.
combinators : bool, default True
If True, pretty-print with combinators (`>>`, `|`, `&`). Otherwise, pretty-print with functions (`make_pipeline`, `make_choice`, `make_union`) instead.
ipython_display : union type, default False
- False
Return the pretty-printed code as a plain old Python string.
- True:
Pretty-print in notebook cell output with syntax highlighting.
- 'input'
Create a new notebook cell with pretty-printed code as input.
"""
result = lale.pretty_print.to_string(self, show_imports, combinators, call_depth=2)
if ipython_display == False:
Expand Down
8 changes: 8 additions & 0 deletions lale/pretty_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def value_to_string(value):
else:
gen.imports.append(f'import {value.__module__} as {module}')
return f'{module}.{value.__name__}'
elif hasattr(value, 'get_params'):
module = value.__module__
name = value.__class__.__name__
if gen is not None:
gen.imports.append(f'import {module}')
printed = pprint.pformat(value, width=10000, compact=True)
compacted = printed.replace('\n', ' ')
return f'{module}.{compacted}'
else:
return pprint.pformat(value, width=10000, compact=True)
strings = [f'{k}={value_to_string(v)}' for k, v in hps.items()]
Expand Down
25 changes: 24 additions & 1 deletion test/test_json_pretty_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def test_autoai_libs_numpy_replace_missing_values(self):
pipeline = numpy_replace_missing_values >> LR()"""
self._roundtrip(expected, lale.pretty_print.to_string(pipeline))

def test_autoai_libs_tam(self):
def test_autoai_libs_tam_1(self):
from autoai_libs.cognito.transforms.transform_utils import TAM
import autoai_libs.cognito.transforms.transform_extras
import numpy as np
Expand All @@ -351,6 +351,29 @@ def test_autoai_libs_tam(self):
pipeline = tam >> LR()"""
self._roundtrip(expected, lale.pretty_print.to_string(pipeline))

def test_autoai_libs_tam_2(self):
from lale.lib.autoai_libs import TAM
import numpy as np
from lightgbm import LGBMClassifier
from sklearn.decomposition import PCA
from lale.operators import make_pipeline
pca = PCA(copy=False)
tam = TAM(tans_class=pca, name='pca', col_names=['a', 'b', 'c'], col_dtypes=[np.dtype('float32'), np.dtype('float32'), np.dtype('float32')])
lgbm_classifier = LGBMClassifier(class_weight='balanced', learning_rate=0.18)
pipeline = make_pipeline(tam, lgbm_classifier)
import lale.helpers
expected = \
"""from lale.lib.autoai_libs import TAM
import sklearn.decomposition.pca
import numpy as np
from lightgbm import LGBMClassifier
from lale.operators import make_pipeline
tam = TAM(tans_class=sklearn.decomposition.pca.PCA(copy=False, iterated_power='auto', n_components=None, random_state=None, svd_solver='auto', tol=0.0, whiten=False), name='pca', col_names=['a', 'b', 'c'], col_dtypes=[np.dtype('float32'), np.dtype('float32'), np.dtype('float32')])
lgbm_classifier = LGBMClassifier(class_weight='balanced', learning_rate=0.18)
pipeline = make_pipeline(tam, lgbm_classifier)"""
self._roundtrip(expected, lale.pretty_print.to_string(pipeline, combinators=False))

def test_autoai_libs_ta1(self):
from autoai_libs.cognito.transforms.transform_utils import TA1
import numpy as np
Expand Down

0 comments on commit a6ede3e

Please sign in to comment.