Quantco / glum

High performance Python GLMs with all the features!
https://glum.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
308 stars 24 forks source link

possible bug with verbose=True #381

Closed ChristophAymannsQC closed 3 years ago

ChristophAymannsQC commented 3 years ago

Running with verbose=1 I get AttributeError: 'tqm' object has no attribute '_update', see here.

tqm is installed.

I think the line should simply be self.t.update(0), rather than self.t._update(0).

Minimal example:

# %%
import numpy as np
from quantcore.glm import GeneralizedLinearRegressor
# %%
n = 1000
X = np.random.normal(size=(n, 1))
p = 1.0 / (1. + np.exp(-3 * X + 1))
y = (np.random.uniform(size=(n, )) < p[:, 0]) * 1
# %%
# works
model = GeneralizedLinearRegressor(family="binomial")
model = model.fit(X, y)
# %%
# fails with
# AttributeError: 'tqdm' object has no attribute '_update'
model = GeneralizedLinearRegressor(family="binomial", verbose=1)
model = model.fit(X, y)
# %%

Traceback

# AttributeError                            Traceback (most recent call last)
# /home/repos/mf-renewal/playground/bug_glm_ver.py in
#       12 model = GeneralizedLinearRegressor(family="binomial", verbose=1)
# ----> 13 model = model.fit(X, y)
# /opt/conda/envs/mf-renewal/lib/python3.8/site-packages/quantcore/glm/_glm.py in fit(self, X, y, sample_weight, offset, weights_sum)
#    2117                     )
#    2118                 )
# -> 2119             coef = self.solve(
#    2120                 X=X,
#    2121                 y=y,
# /opt/conda/envs/mf-renewal/lib/python3.8/site-packages/quantcore/glm/_glm.py in solve(self, X, y, weights, P2, P1, coef, offset, lower_bounds, upper_bounds, A_ineq, b_ineq)
#     933             )
#     934             if self._solver == "irls-ls":
# --> 935                 coef, self.n_iter_, self._n_cycles, self.diagnostics_ = _irls_solver(
#     936                     _least_squares_solver, coef, irls_data
#     937                 )
# /opt/conda/envs/mf-renewal/lib/python3.8/site-packages/quantcore/glm/_solvers.py in _irls_solver(inner_solver, coef, data)
#     306     with ProgressBar(state.norm_min_subgrad, data.gradient_tol, data.verbose) as pb:
#     307         while state.n_iter < data.max_iter and not state.converged:
# --> 308             pb._update(state.n_iter, state.iteration_runtime, state.norm_min_subgrad)
#     309
#     310             state.old_active_set = state.active_set
# /opt/conda/envs/mf-renewal/lib/python3.8/site-packages/quantcore/glm/_solvers.py in _update(self, n_iter, iteration_runtime, cur_grad_norm)
#     408         # round to two digits for beauty
#     409         self.t.n = np.round(step, 2)
# --> 410         self.t._update(0)
#     411
#     412
# AttributeError: 'tqdm' object has no attribute '_update'
MarcAntoineSchmidtQC commented 3 years ago

Thanks! I tracked down the bug and it appeared back when we refactored the code to make it flake8-compliant. It should indeed be update and not _update. I'll do a PR soon.

ChristophAymannsQC commented 3 years ago

Cool. Thanks for the quick fix.