Skip to content

Commit 77cad85

Browse files
hayesallchkoar
authored andcommitted
ENH: Show versions and system info for debugging (#557)
1 parent 985238e commit 77cad85

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

doc/whats_new/v0.5.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Enhancement
2323
- Add Parallelisation for SMOTEENN and SMOTETomek.
2424
:issue:`547` by :user:`Michael Hsieh <Microsheep>`.
2525

26+
- Add :class:`imblearn.utils._show_versions`. Updated the contribution guide
27+
and issue template showing how to print system and dependency information
28+
from the command line. :issue:`557` by :user:`Alexander L. Hayes <batflyer>`.
29+
2630
Maintenance
2731
...........
2832

imblearn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
from .base import FunctionSampler
4646
from ._version import __version__
47+
from .utils._show_versions import show_versions
4748

4849
__all__ = ['combine', 'ensemble', 'exceptions', 'keras', 'metrics',
4950
'over_sampling', 'tensorflow', 'under_sampling',

imblearn/utils/_show_versions.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
Utility method which prints system info to help with debugging,
3+
and filing issues on GitHub.
4+
Adapted from :func:`sklearn.show_versions`,
5+
which was adapted from :func:`pandas.show_versions`
6+
"""
7+
8+
# Author: Alexander L. Hayes <hayesall@iu.edu>
9+
# License: MIT
10+
11+
import sys
12+
import importlib
13+
14+
15+
def _get_deps_info():
16+
"""Overview of the installed version of main dependencies
17+
Returns
18+
-------
19+
deps_info: dict
20+
version information on relevant Python libraries
21+
"""
22+
deps = [
23+
"pip",
24+
"setuptools",
25+
"imblearn",
26+
"sklearn",
27+
"numpy",
28+
"scipy",
29+
"Cython",
30+
"pandas",
31+
"keras",
32+
"tensorflow",
33+
]
34+
35+
def get_version(module):
36+
return module.__version__
37+
38+
deps_info = {}
39+
40+
for modname in deps:
41+
try:
42+
if modname in sys.modules:
43+
mod = sys.modules[modname]
44+
else:
45+
mod = importlib.import_module(modname)
46+
ver = get_version(mod)
47+
deps_info[modname] = ver
48+
except ImportError:
49+
deps_info[modname] = None
50+
51+
return deps_info
52+
53+
54+
def show_versions(github=False):
55+
"""Print debugging information.
56+
Parameters
57+
----------
58+
github : bool,
59+
If true, wrap system info with GitHub markup.
60+
"""
61+
62+
from sklearn.utils._show_versions import (
63+
_get_sys_info,
64+
_get_blas_info,
65+
)
66+
67+
_sys_info = _get_sys_info()
68+
_blas_info = _get_blas_info()
69+
_deps_info = _get_deps_info()
70+
_github_markup = (
71+
"<details>"
72+
"<summary>System, BLAS, and Dependencies</summary>\n\n"
73+
"**System Information**\n\n"
74+
"{0}\n"
75+
"**BLAS**\n\n"
76+
"{1}\n"
77+
"**Python Dependencies**\n\n"
78+
"{2}\n"
79+
"</details>"
80+
)
81+
82+
if github:
83+
84+
_sys_markup = ""
85+
_blas_markup = ""
86+
_deps_markup = ""
87+
88+
for k, stat in _sys_info.items():
89+
_sys_markup += "* {k:<10}: `{stat}`\n".format(k=k, stat=stat)
90+
for k, stat in _blas_info.items():
91+
_blas_markup += "* {k:<10}: `{stat}`\n".format(k=k, stat=stat)
92+
for k, stat in _deps_info.items():
93+
_deps_markup += "* {k:<10}: `{stat}`\n".format(k=k, stat=stat)
94+
95+
print(_github_markup.format(_sys_markup, _blas_markup, _deps_markup))
96+
97+
else:
98+
99+
print("\nSystem:")
100+
for k, stat in _sys_info.items():
101+
print("{k:>11}: {stat}".format(k=k, stat=stat))
102+
103+
print("\nBLAS:")
104+
for k, stat in _blas_info.items():
105+
print("{k:>11}: {stat}".format(k=k, stat=stat))
106+
107+
print("\nPython dependencies:")
108+
for k, stat in _deps_info.items():
109+
print("{k:>11}: {stat}".format(k=k, stat=stat))
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Test for the show_versions helper. Based on the sklearn tests."""
2+
# Author: Alexander L. Hayes <hayesall@iu.edu>
3+
# License: MIT
4+
5+
from imblearn.utils._show_versions import _get_deps_info
6+
from imblearn.utils._show_versions import show_versions
7+
8+
9+
def test_get_deps_info():
10+
_deps_info = _get_deps_info()
11+
assert "pip" in _deps_info
12+
assert "setuptools" in _deps_info
13+
assert "imblearn" in _deps_info
14+
assert "sklearn" in _deps_info
15+
assert "numpy" in _deps_info
16+
assert "scipy" in _deps_info
17+
assert "Cython" in _deps_info
18+
assert "pandas" in _deps_info
19+
20+
21+
def test_show_versions_default(capsys):
22+
show_versions()
23+
out, err = capsys.readouterr()
24+
assert "python" in out
25+
assert "executable" in out
26+
assert "machine" in out
27+
assert "macros" in out
28+
assert "lib_dirs" in out
29+
assert "cblas_libs" in out
30+
assert "pip" in out
31+
assert "setuptools" in out
32+
assert "imblearn" in out
33+
assert "sklearn" in out
34+
assert "numpy" in out
35+
assert "scipy" in out
36+
assert "Cython" in out
37+
assert "pandas" in out
38+
assert "keras" in out
39+
assert "tensorflow" in out
40+
41+
42+
def test_show_versions_github(capsys):
43+
show_versions(github=True)
44+
out, err = capsys.readouterr()
45+
assert "<details><summary>System, BLAS, and Dependencies</summary>" in out
46+
assert "**System Information**" in out
47+
assert "* python" in out
48+
assert "* executable" in out
49+
assert "* machine" in out
50+
assert "**BLAS**" in out
51+
assert "* macros" in out
52+
assert "* lib_dirs" in out
53+
assert "* cblas_libs" in out
54+
assert "**Python Dependencies**" in out
55+
assert "* pip" in out
56+
assert "* setuptools" in out
57+
assert "* imblearn" in out
58+
assert "* sklearn" in out
59+
assert "* numpy" in out
60+
assert "* scipy" in out
61+
assert "* Cython" in out
62+
assert "* pandas" in out
63+
assert "* keras" in out
64+
assert "* tensorflow" in out
65+
assert "</details>" in out

0 commit comments

Comments
 (0)