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

510 progress bar #559

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -41,6 +41,7 @@
* Add new constraints and objectives in `ThresholdOptimizer`
* Add class `InterpolatedThresholder` to represent the fitted `ThresholdOptimizer`
* Add `fairlearn.datasets` module.
* Add optional `verbose` argument to `GridSearch.fit` to diplay progress using `tqdm`.

### v0.4.6

Expand Down
16 changes: 15 additions & 1 deletion fairlearn/reductions/_grid_search/grid_search.py
Expand Up @@ -18,6 +18,7 @@
logger = logging.getLogger(__name__)

TRADEOFF_OPTIMIZATION = "tradeoff_optimization"
_TQDM_IMPORT_ERROR_MESSAGE = "To use `verbose=True` in GridSearch.fit, please install tqdm."


class GridSearch(BaseEstimator, MetaEstimatorMixin):
Expand Down Expand Up @@ -105,12 +106,24 @@ def fit(self, X, y, **kwargs):
:param sensitive_features: A (currently) required keyword argument listing the
feature used by the constraints object
:type sensitive_features: numpy.ndarray, pandas.DataFrame, pandas.Series, or list (for now)

:param verbose: An optional argument to enable/disable the progress bar.
Default: False
:type verbose: bool
"""
self.predictors_ = []
self.lambda_vecs_ = pd.DataFrame(dtype=np.float64)
self.objectives_ = []
self.gammas_ = pd.DataFrame(dtype=np.float64)
self.oracle_execution_times_ = []
verbose = kwargs.pop('verbose', False)

if verbose:
# We need tqdm to show the progress bar.
try:
from tqdm import tqdm
except ImportError:
raise RuntimeError(_TQDM_IMPORT_ERROR_MESSAGE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than raise an exception, since verbose=True is the default, I think it would be kinder to log a warning and behave as if verbose was false.

Tagging @adrinjalali , who first raised a caution around tqdm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set verbose default to False so that the user ia not forced to install tqdm.


if isinstance(self.constraints, ClassificationMoment):
logger.debug("Classification problem detected")
Expand Down Expand Up @@ -151,7 +164,8 @@ def fit(self, X, y, **kwargs):

# Fit the estimates
logger.debug("Setup complete. Starting grid search")
for i in grid.columns:
grid_iter = tqdm(grid.columns, desc="Grid search progress") if verbose else grid.columns
for i in grid_iter:
lambda_vec = grid[i]
logger.debug("Obtaining weights")
weights = self.constraints.signed_weights(lambda_vec)
Expand Down