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

[python-package] fix mypy errors in engine.py #4839

Merged
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions python-package/lightgbm/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Callbacks library."""
import collections
from functools import partial
from typing import Any, Callable, Dict, List, Union
from typing import Any, Callable, Dict, List, Tuple, Union

from .basic import _ConfigAliases, _log_info, _log_warning

Expand All @@ -18,15 +18,15 @@ def _lt_delta(curr_score: float, best_score: float, delta: float) -> bool:
class EarlyStopException(Exception):
"""Exception of early stopping."""

def __init__(self, best_iteration: int, best_score: float) -> None:
def __init__(self, best_iteration: int, best_score: List[Tuple[str, str, float, bool]]) -> None:
StrikerRUS marked this conversation as resolved.
Show resolved Hide resolved
"""Create early stopping exception.

Parameters
----------
best_iteration : int
The best iteration stopped.
best_score : float
The score of the best iteration.
best_score : list of (eval_name, metric_name, eval_result, is_higher_better) tuples
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
Scores for each metric, on each validation set, as of the best iteration.
"""
super().__init__()
self.best_iteration = best_iteration
Expand Down
22 changes: 11 additions & 11 deletions python-package/lightgbm/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,38 @@ def train(
name_valid_sets.append(f'valid_{i}')
# process callbacks
if callbacks is None:
callbacks = set()
callbacks_set = set()
else:
for i, cb in enumerate(callbacks):
cb.__dict__.setdefault('order', i - len(callbacks))
callbacks = set(callbacks)
callbacks_set = set(callbacks)

# Most of legacy advanced options becomes callbacks
if verbose_eval != "warn":
_log_warning("'verbose_eval' argument is deprecated and will be removed in a future release of LightGBM. "
"Pass 'log_evaluation()' callback via 'callbacks' argument instead.")
else:
if callbacks: # assume user has already specified log_evaluation callback
if callbacks_set: # assume user has already specified log_evaluation callback
verbose_eval = False
else:
verbose_eval = True
if verbose_eval is True:
callbacks.add(callback.log_evaluation())
callbacks_set.add(callback.log_evaluation())
elif isinstance(verbose_eval, int):
callbacks.add(callback.log_evaluation(verbose_eval))
callbacks_set.add(callback.log_evaluation(verbose_eval))

if early_stopping_rounds is not None and early_stopping_rounds > 0:
callbacks.add(callback.early_stopping(early_stopping_rounds, first_metric_only, verbose=bool(verbose_eval)))
callbacks_set.add(callback.early_stopping(early_stopping_rounds, first_metric_only, verbose=bool(verbose_eval)))

if evals_result is not None:
_log_warning("'evals_result' argument is deprecated and will be removed in a future release of LightGBM. "
"Pass 'record_evaluation()' callback via 'callbacks' argument instead.")
callbacks.add(callback.record_evaluation(evals_result))
callbacks_set.add(callback.record_evaluation(evals_result))

callbacks_before_iter = {cb for cb in callbacks if getattr(cb, 'before_iteration', False)}
callbacks_after_iter = callbacks - callbacks_before_iter
callbacks_before_iter = sorted(callbacks_before_iter, key=attrgetter('order'))
callbacks_after_iter = sorted(callbacks_after_iter, key=attrgetter('order'))
callbacks_before_iter_set = {cb for cb in callbacks_set if getattr(cb, 'before_iteration', False)}
callbacks_after_iter_set = callbacks_set - callbacks_before_iter_set
callbacks_before_iter = sorted(callbacks_before_iter_set, key=attrgetter('order'))
callbacks_after_iter = sorted(callbacks_after_iter_set, key=attrgetter('order'))

# construct booster
try:
Expand Down