From a47dea4933b1fc862d81e6e75564dce1768f701d Mon Sep 17 00:00:00 2001 From: Paul Donnelly Date: Fri, 20 Dec 2024 10:32:29 -0600 Subject: [PATCH] export `no_progress_block` from `h2o` module --- h2o-py/h2o/__init__.py | 2 +- h2o-py/h2o/explanation/_explain.py | 33 +++++-------------- h2o-py/h2o/h2o.py | 26 +++++++++++++++ h2o-py/h2o/model/extensions/fairness.py | 6 ++-- .../tests/testdir_misc/explain/pyunit_SHAP.py | 5 ++- .../explain/pyunit_SHAP_NOPASS.py | 5 ++- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/h2o-py/h2o/__init__.py b/h2o-py/h2o/__init__.py index 70ffd42e142d..6167936da440 100644 --- a/h2o-py/h2o/__init__.py +++ b/h2o-py/h2o/__init__.py @@ -26,7 +26,7 @@ from h2o.h2o import (connect, init, api, connection, resume, lazy_import, upload_file, import_file, import_sql_table, import_sql_select, import_hive_table, parse_setup, parse_raw, assign, deep_copy, models, get_model, get_grid, get_frame, - show_progress, no_progress, enable_expr_optimizations, is_expr_optimizations_enabled, + show_progress, no_progress, no_progress_block, enable_expr_optimizations, is_expr_optimizations_enabled, log_and_echo, remove, remove_all, rapids, ls, frame, frames, create_frame, load_frame, download_pojo, download_csv, download_all_logs, save_model, download_model, upload_model, load_model, diff --git a/h2o-py/h2o/explanation/_explain.py b/h2o-py/h2o/explanation/_explain.py index ad898244f79d..da00810f235c 100644 --- a/h2o-py/h2o/explanation/_explain.py +++ b/h2o-py/h2o/explanation/_explain.py @@ -3,7 +3,6 @@ import random import warnings from collections import OrderedDict, Counter, defaultdict -from contextlib import contextmanager from io import StringIO @@ -186,22 +185,6 @@ def _ipython_display_(self): display(v) -@contextmanager -def no_progress_block(): - """ - A context manager that temporarily blocks showing the H2O's progress bar. - Used when a multiple models are evaluated. - """ - progress = h2o.job.H2OJob.__PROGRESS_BAR__ - if progress: - h2o.no_progress() - try: - yield - finally: - if progress: - h2o.show_progress() - - class NumpyFrame: """ Simple class that very vaguely emulates Pandas DataFrame. @@ -699,7 +682,7 @@ def shap_summary_plot( permutation = list(range(frame.nrow)) random.shuffle(permutation) - with no_progress_block(): + with h2o.no_progress_block(): contributions = NumpyFrame(model.predict_contributions(frame, output_format="compact", background_frame=background_frame)) frame = NumpyFrame(frame) contribution_names = contributions.columns @@ -833,7 +816,7 @@ def shap_explain_row_plot( top_n_features = float("inf") row = frame[row_index, :] - with no_progress_block(): + with h2o.no_progress_block(): contributions = NumpyFrame(model.predict_contributions(row, output_format="compact", background_frame=background_frame)) contribution_names = contributions.columns prediction = float(contributions.sum(axis=1)) @@ -842,7 +825,7 @@ def shap_explain_row_plot( key=lambda pair: -abs(pair[1])) if plot_type == "barplot": - with no_progress_block(): + with h2o.no_progress_block(): prediction = model.predict(row)[0, "predict"] row = NumpyFrame(row) @@ -1394,7 +1377,7 @@ def pd_ice_common( show_logodds = is_binomial and binary_response_scale == "logodds" - with no_progress_block(): + with h2o.no_progress_block(): plt.figure(figsize=figsize) if is_ice: res = _handle_ice(model, frame, colormap, plt, target, is_factor, column, show_logodds, centered, @@ -1576,7 +1559,7 @@ def pd_multi_plot( models = [m if isinstance(m, h2o.model.ModelBase) else h2o.get_model(m) for m in models] colors = plt.get_cmap(colormap, len(models))(list(range(len(models)))) - with no_progress_block(): + with h2o.no_progress_block(): plt.figure(figsize=figsize) is_factor = frame[column].isfactor()[0] if is_factor: @@ -2332,7 +2315,7 @@ def model_correlation( models = list(_get_models_from_automl_or_leaderboard(models)) is_classification = frame[models[0].actual_params["response_column"]].isfactor()[0] predictions = [] - with no_progress_block(): + with h2o.no_progress_block(): for idx, model in enumerate(models): predictions.append(model.predict(frame)["predict"]) @@ -2411,7 +2394,7 @@ def residual_analysis_plot( plt = get_matplotlib_pyplot(False, raise_if_not_available=True) _, y = _get_xy(model) - with no_progress_block(): + with h2o.no_progress_block(): predicted = NumpyFrame(model.predict(frame)["predict"]) actual = NumpyFrame(frame[y]) @@ -2964,7 +2947,7 @@ def _get_leaderboard( if row_index is not None: model_ids = [m[0] for m in leaderboard["model_id"].as_data_frame(use_pandas=False, header=False)] - with no_progress_block(): + with h2o.no_progress_block(): preds = h2o.get_model(model_ids[0]).predict(frame[row_index, :]) for model_id in model_ids[1:]: preds = preds.rbind(h2o.get_model(model_id).predict(frame[row_index, :])) diff --git a/h2o-py/h2o/h2o.py b/h2o-py/h2o/h2o.py index 60408518d50a..0f4678fa9bd9 100644 --- a/h2o-py/h2o/h2o.py +++ b/h2o-py/h2o/h2o.py @@ -10,6 +10,7 @@ import tempfile import warnings import webbrowser +from contextlib import contextmanager from .backend import H2OConnection from .backend import H2OConnectionConf @@ -1216,6 +1217,31 @@ def no_progress(): H2OJob.__PROGRESS_BAR__ = False +@contextmanager +def no_progress_block(): + """ + A context manager that temporarily blocks showing the H2O's progress bar. + + :examples: + + >>> with h2o.no_progress_block(): + >>> airlines= h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/allyears2k_headers.zip") + >>> x = ["DayofMonth", "Month"] + >>> model = H2OGeneralizedLinearEstimator(family="binomial", + ... alpha=0, + ... Lambda=1e-5) + >>> model.train(x=x, y="IsDepDelayed", training_frame=airlines) + """ + progress = H2OJob.__PROGRESS_BAR__ + if progress: + no_progress() + try: + yield + finally: + if progress: + show_progress() + + def show_progress(): """Enable the progress bar (it is enabled by default). diff --git a/h2o-py/h2o/model/extensions/fairness.py b/h2o-py/h2o/model/extensions/fairness.py index 695fd588636d..5e8a04e166d3 100644 --- a/h2o-py/h2o/model/extensions/fairness.py +++ b/h2o-py/h2o/model/extensions/fairness.py @@ -106,7 +106,6 @@ def fair_pd_plot(self, frame, column, protected_columns, figsize=(16, 9), autosc if not can_use_numpy(): raise ImportError("numpy is required for fair_pd_plot.") import numpy as np - from h2o.explanation._explain import no_progress_block from h2o.plot import get_matplotlib_pyplot from h2o.utils.typechecks import assert_is_type, is_type @@ -121,7 +120,7 @@ def fair_pd_plot(self, frame, column, protected_columns, figsize=(16, 9), autosc plt.figure(figsize=figsize) results = [] maxes = [] - with no_progress_block(): + with h2o.no_progress_block(): for pg in pgs: pg = [p[0] for p in pg] filtered_hdf = frame @@ -349,7 +348,6 @@ def fair_shap_plot(self, frame, column, protected_columns, autoscale=True, figsi if not can_use_numpy(): raise ImportError("numpy is required for fair_shap_plot.") import numpy as np - from h2o.explanation._explain import no_progress_block from h2o.explanation import H2OExplanation from h2o.explanation._explain import NumpyFrame from h2o.explanation._explain import _density @@ -370,7 +368,7 @@ def fair_shap_plot(self, frame, column, protected_columns, autoscale=True, figsi results = defaultdict(list) maxes = [] contr_columns = [column] - with no_progress_block(): + with h2o.no_progress_block(): for pg in pgs: pg = [p[0] for p in pg] filtered_hdf = frame diff --git a/h2o-py/tests/testdir_misc/explain/pyunit_SHAP.py b/h2o-py/tests/testdir_misc/explain/pyunit_SHAP.py index 2f6300adb778..9dd3c018d6d6 100644 --- a/h2o-py/tests/testdir_misc/explain/pyunit_SHAP.py +++ b/h2o-py/tests/testdir_misc/explain/pyunit_SHAP.py @@ -21,7 +21,6 @@ def tqdm(x, *args, **kwargs): return x from h2o.estimators import * -from h2o.explanation._explain import no_progress_block seed = 6 K = 2 @@ -72,7 +71,7 @@ def test_local_accuracy( print("Testing local accuracy in output space...") else: print("Testing local accuracy...") - with no_progress_block(): + with h2o.no_progress_block(): cf = mod.predict_contributions( test, background_frame=train, output_format=output_format, output_space=output_space, output_per_reference=True @@ -314,7 +313,7 @@ def test_contributions_against_naive(mod, y, train, test, link=False, eps=1e-6): [train[brow, k] == "NA" for k, v in train.types.items() if v == "enum"] ): continue - with no_progress_block(): + with h2o.no_progress_block(): naive_contr = naiveBSHAP(mod, y, train, test, xrow, brow, link=link) contr = mod.predict_contributions( test[xrow, :], diff --git a/h2o-py/tests/testdir_misc/explain/pyunit_SHAP_NOPASS.py b/h2o-py/tests/testdir_misc/explain/pyunit_SHAP_NOPASS.py index 9723d84d6fde..52c14f9aaec4 100644 --- a/h2o-py/tests/testdir_misc/explain/pyunit_SHAP_NOPASS.py +++ b/h2o-py/tests/testdir_misc/explain/pyunit_SHAP_NOPASS.py @@ -21,7 +21,6 @@ def tqdm(x, *args, **kwargs): return x from h2o.estimators import * -from h2o.explanation._explain import no_progress_block seed = 6 K = 5 @@ -57,7 +56,7 @@ def test_local_accuracy( print("Testing local accuracy in output space...") else: print("Testing local accuracy...") - with no_progress_block(): + with h2o.no_progress_block(): cf = mod.predict_contributions( test, background_frame=train, output_format=output_format, output_space=output_space, output_per_reference=True @@ -299,7 +298,7 @@ def test_contributions_against_naive(mod, y, train, test, link=False, eps=1e-6): [train[brow, k] == "NA" for k, v in train.types.items() if v == "enum"] ): continue - with no_progress_block(): + with h2o.no_progress_block(): naive_contr = naiveBSHAP(mod, y, train, test, xrow, brow, link=link) contr = mod.predict_contributions( test[xrow, :],