diff --git a/lale/operators.py b/lale/operators.py index f9bedfd57..7d4f43359 100644 --- a/lale/operators.py +++ b/lale/operators.py @@ -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: diff --git a/lale/pretty_print.py b/lale/pretty_print.py index 7d11f2602..45e0831b2 100644 --- a/lale/pretty_print.py +++ b/lale/pretty_print.py @@ -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()] diff --git a/test/test_json_pretty_viz.py b/test/test_json_pretty_viz.py index c65d934d1..f8456bc1e 100644 --- a/test/test_json_pretty_viz.py +++ b/test/test_json_pretty_viz.py @@ -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 @@ -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