From aae854ec2b55f01a8c2d6cdce948d30e4eebbe9f Mon Sep 17 00:00:00 2001 From: mwojc Date: Sat, 23 Oct 2021 18:20:41 +0200 Subject: [PATCH 1/8] Add numpydoc validation for config_context Co-authored-by: majauhar --- maint_tools/test_docstrings.py | 1 - sklearn/_config.py | 43 +++++++++------------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/maint_tools/test_docstrings.py b/maint_tools/test_docstrings.py index a658f13ac8912..e8e81bca2250e 100644 --- a/maint_tools/test_docstrings.py +++ b/maint_tools/test_docstrings.py @@ -19,7 +19,6 @@ ] FUNCTION_DOCSTRING_IGNORE_LIST = [ - "sklearn._config.config_context", "sklearn._config.get_config", "sklearn.base.clone", "sklearn.cluster._affinity_propagation.affinity_propagation", diff --git a/sklearn/_config.py b/sklearn/_config.py index fe2d27f64857c..592f45e65a5da 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -99,38 +99,22 @@ def set_config( @contextmanager def config_context(**new_config): - """Context manager for global scikit-learn configuration + """Context manager for global scikit-learn configuration. Parameters ---------- - assume_finite : bool, default=False - If True, validation for finiteness will be skipped, - saving time, but leading to potential crashes. If - False, validation for finiteness will be performed, - avoiding error. Global default: False. - - working_memory : int, default=1024 - If set, scikit-learn will attempt to limit the size of temporary arrays - to this number of MiB (per job when parallelised), often saving both - computation time and memory on expensive operations that can be - performed in chunks. Global default: 1024. - - print_changed_only : bool, default=True - If True, only the parameters that were set to non-default - values will be printed when printing an estimator. For example, - ``print(SVC())`` while True will only print 'SVC()', but would print - 'SVC(C=1.0, cache_size=200, ...)' with all the non-changed parameters - when False. Default is True. - - .. versionchanged:: 0.23 - Default changed from False to True. + **new_config : dict, optional + Dictionary of parameters to pass to the configuration. + The parameters are documented in the docstring of :func:`set_config`. - display : {'text', 'diagram'}, default='text' - If 'diagram', estimators will be displayed as a diagram in a Jupyter - lab or notebook context. If 'text', estimators will be displayed as - text. Default is 'text'. + Yields + ------ + None. - .. versionadded:: 0.23 + See Also + -------- + set_config : Set global scikit-learn configuration. + get_config : Retrieve current values of the global configuration. Notes ----- @@ -149,11 +133,6 @@ def config_context(**new_config): Traceback (most recent call last): ... ValueError: Input contains NaN, ... - - See Also - -------- - set_config : Set global scikit-learn configuration. - get_config : Retrieve current values of the global configuration. """ old_config = get_config() set_config(**new_config) From d83d989beb03a81e002a19943a39d080bb0074b7 Mon Sep 17 00:00:00 2001 From: mwojc Date: Sat, 23 Oct 2021 22:06:10 +0200 Subject: [PATCH 2/8] Remove **new_config, and include all the arguments in the signature of the function Co-authored-by: majauhar --- sklearn/_config.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index 592f45e65a5da..8db4c4ae3266b 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -98,14 +98,41 @@ def set_config( @contextmanager -def config_context(**new_config): +def config_context( + assume_finite=False, working_memory=1024, print_changed_only=True, display="text" +): """Context manager for global scikit-learn configuration. Parameters ---------- - **new_config : dict, optional - Dictionary of parameters to pass to the configuration. - The parameters are documented in the docstring of :func:`set_config`. + assume_finite : bool, default=False + If True, validation for finiteness will be skipped, + saving time, but leading to potential crashes. If + False, validation for finiteness will be performed, + avoiding error. Global default: False. + + working_memory : int, default=1024 + If set, scikit-learn will attempt to limit the size of temporary arrays + to this number of MiB (per job when parallelised), often saving both + computation time and memory on expensive operations that can be + performed in chunks. Global default: 1024. + + print_changed_only : bool, default=True + If True, only the parameters that were set to non-default + values will be printed when printing an estimator. For example, + ``print(SVC())`` while True will only print 'SVC()', but would print + 'SVC(C=1.0, cache_size=200, ...)' with all the non-changed parameters + when False. Default is True. + + .. versionchanged:: 0.23 + Default changed from False to True. + + display : {'text', 'diagram'}, default='text' + If 'diagram', estimators will be displayed as a diagram in a Jupyter + lab or notebook context. If 'text', estimators will be displayed as + text. Default is 'text'. + + .. versionadded:: 0.23 Yields ------ From 2fd65871f84c9c0ad88c29f7621f7b4332bdfd43 Mon Sep 17 00:00:00 2001 From: mwojc Date: Sun, 24 Oct 2021 08:08:00 +0200 Subject: [PATCH 3/8] Change **new_config in the body of the function to align with the function signature Co-authored-by: majauhar --- sklearn/_config.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index 8db4c4ae3266b..b1be961903f5a 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -162,7 +162,12 @@ def config_context( ValueError: Input contains NaN, ... """ old_config = get_config() - set_config(**new_config) + set_config( + assume_finite=assume_finite, + working_memory=working_memory, + print_changed_only=print_changed_only, + display=display, + ) try: yield From 9f87d1fa9c5a4da162756a4217c3381fa012b709 Mon Sep 17 00:00:00 2001 From: mwojc Date: Sun, 24 Oct 2021 12:32:15 +0200 Subject: [PATCH 4/8] Add leading star in function signature as we should only accept keyword arguments Co-authored-by: majauhar --- sklearn/_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index b1be961903f5a..9c154ea114996 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -99,7 +99,7 @@ def set_config( @contextmanager def config_context( - assume_finite=False, working_memory=1024, print_changed_only=True, display="text" + *, assume_finite=False, working_memory=1024, print_changed_only=True, display="text" ): """Context manager for global scikit-learn configuration. From d0dd7c732efc738824e450984cae55133531369b Mon Sep 17 00:00:00 2001 From: mwojc Date: Mon, 25 Oct 2021 11:07:32 +0200 Subject: [PATCH 5/8] Use None as default marker for all parameters, for consistency with set_config. Co-authored-by: majauhar --- sklearn/_config.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index 9c154ea114996..5f4aabbf87dc3 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -99,38 +99,38 @@ def set_config( @contextmanager def config_context( - *, assume_finite=False, working_memory=1024, print_changed_only=True, display="text" + *, assume_finite=None, working_memory=None, print_changed_only=None, display=None ): """Context manager for global scikit-learn configuration. Parameters ---------- - assume_finite : bool, default=False + assume_finite : bool, default=None If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, - avoiding error. Global default: False. + avoiding error. If None, then it will be set to False. - working_memory : int, default=1024 + working_memory : int, default=None If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be - performed in chunks. Global default: 1024. + performed in chunks. If None, then it will be set to 1024. - print_changed_only : bool, default=True + print_changed_only : bool, default=None If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, ``print(SVC())`` while True will only print 'SVC()', but would print 'SVC(C=1.0, cache_size=200, ...)' with all the non-changed parameters - when False. Default is True. + when False. If None, then it will be set to True. .. versionchanged:: 0.23 Default changed from False to True. - display : {'text', 'diagram'}, default='text' + display : {'text', 'diagram'}, default=None If 'diagram', estimators will be displayed as a diagram in a Jupyter lab or notebook context. If 'text', estimators will be displayed as - text. Default is 'text'. + text. If None, then it will be set to 'text'. .. versionadded:: 0.23 From 81fb9cd8339b27b2f0146fa1f19b2bd41de62c86 Mon Sep 17 00:00:00 2001 From: mwojc Date: Mon, 25 Oct 2021 11:53:44 +0200 Subject: [PATCH 6/8] Adjust wording for default values. Co-authored-by: majauhar --- sklearn/_config.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index 5f4aabbf87dc3..6d5baaab8ae54 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -109,20 +109,23 @@ def config_context( If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, - avoiding error. If None, then it will be set to False. + avoiding error. If None, the existing value won't change. + The default value is False. working_memory : int, default=None If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be - performed in chunks. If None, then it will be set to 1024. + performed in chunks. If None, the existing value won't change. + The default value is 1024. print_changed_only : bool, default=None If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, ``print(SVC())`` while True will only print 'SVC()', but would print 'SVC(C=1.0, cache_size=200, ...)' with all the non-changed parameters - when False. If None, then it will be set to True. + when False. If None, the existing value won't change. + The default value is True. .. versionchanged:: 0.23 Default changed from False to True. @@ -130,7 +133,8 @@ def config_context( display : {'text', 'diagram'}, default=None If 'diagram', estimators will be displayed as a diagram in a Jupyter lab or notebook context. If 'text', estimators will be displayed as - text. If None, then it will be set to 'text'. + text. If None, the existing value won't change. + The default value is 'text'. .. versionadded:: 0.23 From 78186788884784826d1641659c7fe576e79cc3c0 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Fri, 29 Oct 2021 11:21:37 -0400 Subject: [PATCH 7/8] CLN Use ... --- sklearn/_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index fe2b0e4fd999e..3687dac2ec088 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -163,7 +163,7 @@ def config_context( ... assert_all_finite([float('nan')]) Traceback (most recent call last): ... - ValueError: Input contains NaN. + ValueError: Input contains NaN... See Also -------- From dcf0c86910ebf48dcaede0b86b2938a1efee1e12 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Fri, 29 Oct 2021 11:57:02 -0400 Subject: [PATCH 8/8] CLN Fix error --- sklearn/_config.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sklearn/_config.py b/sklearn/_config.py index 3687dac2ec088..ad66a403d3ef1 100644 --- a/sklearn/_config.py +++ b/sklearn/_config.py @@ -164,11 +164,6 @@ def config_context( Traceback (most recent call last): ... ValueError: Input contains NaN... - - See Also - -------- - set_config : Set global scikit-learn configuration. - get_config : Retrieve current values of the global configuration. """ old_config = get_config() set_config(