Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-16453 - export no_progress_block from h2o module #16465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion h2o-py/h2o/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 8 additions & 25 deletions h2o-py/h2o/explanation/_explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import random
import warnings
from collections import OrderedDict, Counter, defaultdict
from contextlib import contextmanager

from io import StringIO

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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, :]))
Expand Down
26 changes: 26 additions & 0 deletions h2o-py/h2o/h2o.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tempfile
import warnings
import webbrowser
from contextlib import contextmanager

from .backend import H2OConnection
from .backend import H2OConnectionConf
Expand Down Expand Up @@ -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).

Expand Down
6 changes: 2 additions & 4 deletions h2o-py/h2o/model/extensions/fairness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions h2o-py/tests/testdir_misc/explain/pyunit_SHAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, :],
Expand Down
5 changes: 2 additions & 3 deletions h2o-py/tests/testdir_misc/explain/pyunit_SHAP_NOPASS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, :],
Expand Down