From 79feceb27b2c67685484312f951a625ac69c4507 Mon Sep 17 00:00:00 2001 From: joaopfonseca Date: Thu, 29 Feb 2024 18:20:40 +0000 Subject: [PATCH] ENH add sharp.show_versions() function (#28) --- sharp/__init__.py | 18 +++-- sharp/utils/_show_versions.py | 92 +++++++++++++++++++++++++ sharp/utils/tests/test_show_versions.py | 67 ++++++++++++++++++ 3 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 sharp/utils/_show_versions.py create mode 100644 sharp/utils/tests/test_show_versions.py diff --git a/sharp/__init__.py b/sharp/__init__.py index ec277bb..c603df8 100644 --- a/sharp/__init__.py +++ b/sharp/__init__.py @@ -29,9 +29,19 @@ # We are not importing the rest of sharp during the build # process, as it may not be compiled yet else: - # from . import utils - - from ._version import __version__ + from . import qoi + from . import visualization + from . import utils from .base import ShaRP + from ._version import __version__ + from .utils._show_versions import show_versions - __all__ = ["__version__", "ShaRP"] + __all__ = [ + "qoi", + "visualization", + "utils", + # Non-modules: + "ShaRP", + "show_versions", + "__version__", + ] diff --git a/sharp/utils/_show_versions.py b/sharp/utils/_show_versions.py new file mode 100644 index 0000000..ac61536 --- /dev/null +++ b/sharp/utils/_show_versions.py @@ -0,0 +1,92 @@ +""" +Utility method which prints system info to help with debugging, +and filing issues on GitHub. +Adapted from :func:`sklearn.show_versions`, +which was adapted from :func:`pandas.show_versions` +""" + +from .. import __version__ + + +def _get_deps_info(): + """Overview of the installed version of main dependencies + + Returns + ------- + deps_info: dict + version information on relevant Python libraries + """ + deps = [ + "pip", + "setuptools", + "sharp", + "numpy", + "pandas", + "scikit-learn", + "tqdm", + "matplotlib", + "Cython", + "scipy", + "keras", + "tensorflow", + "joblib", + ] + + deps_info = { + "sharp": __version__, + } + + from importlib.metadata import PackageNotFoundError, version + + for modname in deps: + try: + deps_info[modname] = version(modname) + except PackageNotFoundError: + deps_info[modname] = None + return deps_info + + +def show_versions(github=False): + """Print debugging information. + + .. versionadded:: 0.4.3 + + Parameters + ---------- + github : bool, + If true, wrap system info with GitHub markup. + """ + + from sklearn.utils._show_versions import _get_sys_info + + _sys_info = _get_sys_info() + _deps_info = _get_deps_info() + _github_markup = ( + "
" + "System, Dependency Information\n\n" + "**System Information**\n\n" + "{0}\n" + "**Python Dependencies**\n\n" + "{1}\n" + "
" + ) + + if github: + _sys_markup = "" + _deps_markup = "" + + for k, stat in _sys_info.items(): + _sys_markup += f"* {k:<16}: `{stat}`\n" + for k, stat in _deps_info.items(): + _deps_markup += f"* {k:<16}: `{stat}`\n" + + print(_github_markup.format(_sys_markup, _deps_markup)) + + else: + print("\nSystem:") + for k, stat in _sys_info.items(): + print(f"{k:>16}: {stat}") + + print("\nPython dependencies:") + for k, stat in _deps_info.items(): + print(f"{k:>16}: {stat}") diff --git a/sharp/utils/tests/test_show_versions.py b/sharp/utils/tests/test_show_versions.py new file mode 100644 index 0000000..57aebea --- /dev/null +++ b/sharp/utils/tests/test_show_versions.py @@ -0,0 +1,67 @@ +"""Test for the show_versions helper. Based on the sklearn tests.""" + +from sharp.utils._show_versions import _get_deps_info, show_versions + + +def test_get_deps_info(): + _deps_info = _get_deps_info() + + assert "pip" in _deps_info + assert "setuptools" in _deps_info + assert "sharp" in _deps_info + assert "numpy" in _deps_info + assert "pandas" in _deps_info + assert "scikit-learn" in _deps_info + assert "matplotlib" in _deps_info + assert "tqdm" in _deps_info + assert "Cython" in _deps_info + assert "scipy" in _deps_info + assert "keras" in _deps_info + assert "tensorflow" in _deps_info + assert "joblib" in _deps_info + + +def test_show_versions_default(capsys): + show_versions() + out, err = capsys.readouterr() + assert "python" in out + assert "executable" in out + assert "machine" in out + assert "pip" in out + assert "setuptools" in out + assert "sharp" in out + assert "numpy" in out + assert "pandas" in out + assert "scikit-learn" in out + assert "matplotlib" in out + assert "tqdm" in out + assert "Cython" in out + assert "scipy" in out + assert "keras" in out + assert "tensorflow" in out + assert "joblib" in out + + +def test_show_versions_github(capsys): + show_versions(github=True) + out, err = capsys.readouterr() + assert "
System, Dependency Information" in out + assert "**System Information**" in out + assert "* python" in out + assert "* executable" in out + assert "* machine" in out + assert "**Python Dependencies**" in out + assert "* pip" in out + assert "* setuptools" in out + assert "* sharp" in out + assert "* numpy" in out + assert "* pandas" in out + assert "* scikit-learn" in out + assert "* matplotlib" in out + assert "* tqdm" in out + assert "* Cython" in out + assert "* scipy" in out + assert "* keras" in out + assert "* tensorflow" in out + assert "* joblib" in out + assert "
" in out