From e22e9a5d9007b366f03ff9dc8a8f415dd3f0d33f Mon Sep 17 00:00:00 2001 From: NovusEdge Date: Tue, 30 Mar 2021 21:42:10 +0530 Subject: [PATCH 1/7] added f-string --- python-package/lightgbm/callback.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index b5f2438545a8..3b5732af23b5 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -38,12 +38,12 @@ def __init__(self, best_iteration, best_score): def _format_eval_result(value, show_stdv=True): """Format metric string.""" if len(value) == 4: - return '%s\'s %s: %g' % (value[0], value[1], value[2]) + return f'{value[0]}\'s {value[1]}: {value[2]}' elif len(value) == 5: if show_stdv: - return '%s\'s %s: %g + %g' % (value[0], value[1], value[2], value[4]) + return f'{value[0]}\'s {value[1]}: {value[2]} + {value[4]}' else: - return '%s\'s %s: %g' % (value[0], value[1], value[2]) + return f'{value[0]}\'s {value[1]}: {value[2]}' else: raise ValueError("Wrong metric value") @@ -66,7 +66,7 @@ def print_evaluation(period=1, show_stdv=True): def _callback(env): if period > 0 and env.evaluation_result_list and (env.iteration + 1) % period == 0: result = '\t'.join([_format_eval_result(x, show_stdv) for x in env.evaluation_result_list]) - _log_info('[%d]\t%s' % (env.iteration + 1, result)) + _log_info(f'[{env.iteration+1}]\t{result}') _callback.order = 10 return _callback @@ -128,8 +128,7 @@ def _callback(env): for key, value in kwargs.items(): if isinstance(value, list): if len(value) != env.end_iteration - env.begin_iteration: - raise ValueError("Length of list {} has to equal to 'num_boost_round'." - .format(repr(key))) + raise ValueError(f"Length of list {repr(key)} has to equal to 'num_boost_round'.") new_param = value[env.iteration - env.begin_iteration] else: new_param = value(env.iteration - env.begin_iteration) @@ -186,7 +185,7 @@ def _init(env): 'at least one dataset and eval metric is required for evaluation') if verbose: - _log_info("Training until validation scores don't improve for {} rounds".format(stopping_rounds)) + _log_info(f"Training until validation scores don't improve for {stopping_rounds} rounds") # split is needed for " " case (e.g. "train l1") first_metric[0] = env.evaluation_result_list[0][1].split(" ")[-1] @@ -203,10 +202,10 @@ def _init(env): def _final_iteration_check(env, eval_name_splitted, i): if env.iteration == env.end_iteration - 1: if verbose: - _log_info('Did not meet early stopping. Best iteration is:\n[%d]\t%s' % ( - best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]]))) + best_score_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) + _log_info(f'Did not meet early stopping. Best iteration is:\n[{best_iter[i]+1}]\t{best_score_str}' if first_metric_only: - _log_info("Evaluated only: {}".format(eval_name_splitted[-1])) + _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) def _callback(env): @@ -230,10 +229,9 @@ def _callback(env): continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info('Early stopping, best iteration is:\n[%d]\t%s' % ( - best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]]))) + _log_info(f"Early stopping, best iteration is:\n[{best_iter[i]+1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}" if first_metric_only: - _log_info("Evaluated only: {}".format(eval_name_splitted[-1])) + _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) _final_iteration_check(env, eval_name_splitted, i) _callback.order = 30 From 1dbcd8beda8cc426e5ef6c19ec52e05747a6ccc1 Mon Sep 17 00:00:00 2001 From: NovusEdge Date: Wed, 31 Mar 2021 07:18:02 +0530 Subject: [PATCH 2/7] fixed indent issues --- python-package/lightgbm/callback.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 3b5732af23b5..568e7e20cdc1 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -203,7 +203,8 @@ def _final_iteration_check(env, eval_name_splitted, i): if env.iteration == env.end_iteration - 1: if verbose: best_score_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) - _log_info(f'Did not meet early stopping. Best iteration is:\n[{best_iter[i]+1}]\t{best_score_str}' + _log_info('Did not meet early stopping.' + f'Best iteration is:\n[{best_iter[i]+1}]\t{best_score_str}') if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) @@ -229,7 +230,10 @@ def _callback(env): continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info(f"Early stopping, best iteration is:\n[{best_iter[i]+1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}" + _log_info( + "Early stopping, best iteration is:\n' + f'[{best_iter[i]+1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}" + ) if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) From c1571ac0fe0f159347fd4c7e60f326d8b3e9fea0 Mon Sep 17 00:00:00 2001 From: NovusEdge Date: Wed, 31 Mar 2021 07:30:03 +0530 Subject: [PATCH 3/7] fixed some linting issues --- python-package/lightgbm/callback.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 568e7e20cdc1..5f01978007f4 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -230,10 +230,8 @@ def _callback(env): continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info( - "Early stopping, best iteration is:\n' - f'[{best_iter[i]+1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}" - ) + _log_info("Early stopping, best iteration is:\n' + f'[{best_iter[i] + 1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}") if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) From 96187c5c23b44773e80c45083962035e04ac150a Mon Sep 17 00:00:00 2001 From: NovusEdge Date: Wed, 31 Mar 2021 07:35:11 +0530 Subject: [PATCH 4/7] trying to fix E225 for 233:65 --- python-package/lightgbm/callback.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 5f01978007f4..0c669107e2ea 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -204,7 +204,7 @@ def _final_iteration_check(env, eval_name_splitted, i): if verbose: best_score_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) _log_info('Did not meet early stopping.' - f'Best iteration is:\n[{best_iter[i]+1}]\t{best_score_str}') + f'Best iteration is:\n[{best_iter[i] + 1}]\t{best_score_str}') if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) @@ -230,7 +230,7 @@ def _callback(env): continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info("Early stopping, best iteration is:\n' + _log_info("Early stopping, best iteration is : \n' f'[{best_iter[i] + 1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}") if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") From 0051265ea587d246b2777e744ed10e4a390bde3c Mon Sep 17 00:00:00 2001 From: James Lamb Date: Mon, 10 May 2021 22:18:53 -0500 Subject: [PATCH 5/7] Update python-package/lightgbm/callback.py --- python-package/lightgbm/callback.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 0c669107e2ea..a8528f6a177c 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -230,8 +230,8 @@ def _callback(env): continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info("Early stopping, best iteration is : \n' - f'[{best_iter[i] + 1}]\t{ '\t'.join([_format_eval_result(x) for x in best_score_list[i]])}") + _log_info("Early stopping, best iteration is: \n" + f"[{best_iter[i] + 1}]\t{'\t'.join([_format_eval_result(x) for x in best_score_list[i]])}") if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) From 677a8f727264d94ccf3cf7f38c9aba2e429e2387 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Mon, 10 May 2021 22:30:56 -0500 Subject: [PATCH 6/7] first_metric_only log message --- python-package/lightgbm/callback.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index a44ea41d4796..9cbe50f39cc5 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -204,7 +204,7 @@ def _final_iteration_check(env: CallbackEnv, eval_name_splitted: List[str], i: i if env.iteration == env.end_iteration - 1: if verbose: best_score_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) - _log_info('Did not meet early stopping.' + _log_info('Did not meet early stopping. ' f'Best iteration is:\n[{best_iter[i] + 1}]\t{best_score_str}') if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") @@ -231,8 +231,8 @@ def _callback(env: CallbackEnv) -> None: continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info("Early stopping, best iteration is: \n" - f"[{best_iter[i] + 1}]\t{'\t'.join([_format_eval_result(x) for x in best_score_list[i]])}") + eval_result_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) + _log_info(f"Early stopping, best iteration is:\n[{best_iter[i] + 1}]\t{eval_result_str}") if first_metric_only: _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) From 1b4410fac730647cd73ff3923601f01af7a22f2d Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 11 May 2021 12:39:49 -0500 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Nikita Titov --- python-package/lightgbm/callback.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 9cbe50f39cc5..fc151d1e82c0 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -39,12 +39,12 @@ def __init__(self, best_iteration: int, best_score: float) -> None: def _format_eval_result(value: list, show_stdv: bool = True) -> str: """Format metric string.""" if len(value) == 4: - return f'{value[0]}\'s {value[1]}: {value[2]}' + return f"{value[0]}'s {value[1]}: {value[2]:g}" elif len(value) == 5: if show_stdv: - return f'{value[0]}\'s {value[1]}: {value[2]} + {value[4]}' + return f"{value[0]}'s {value[1]}: {value[2]:g} + {value[4]:g}" else: - return f'{value[0]}\'s {value[1]}: {value[2]}' + return f"{value[0]}'s {value[1]}: {value[2]:g}" else: raise ValueError("Wrong metric value") @@ -67,7 +67,7 @@ def print_evaluation(period: int = 1, show_stdv: bool = True) -> Callable: def _callback(env: CallbackEnv) -> None: if period > 0 and env.evaluation_result_list and (env.iteration + 1) % period == 0: result = '\t'.join([_format_eval_result(x, show_stdv) for x in env.evaluation_result_list]) - _log_info(f'[{env.iteration+1}]\t{result}') + _log_info(f'[{env.iteration + 1}]\t{result}') _callback.order = 10 # type: ignore return _callback