Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH: Added display modes for show_runtime #26255

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions 5 doc/release/upcoming_changes/26255.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`np.show_runtime` has display modes
-----------------------------------

`np.show_runtime` now has a new optional parameter ``mode`` to help
customize the output.
51 changes: 9 additions & 42 deletions 51 numpy/__config__.py.in
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
# This file is generated by numpy's build process
# It contains system_info results at the time of building this package.
from enum import Enum
from numpy._core._multiarray_umath import (
__cpu_features__,
__cpu_baseline__,
__cpu_dispatch__,
)
from numpy._utils._config_helpers import (
ConfigDisplayModes,
cleanup_empty_dict_values,
print_or_return_config,
)

__all__ = ["show"]
_built_with_meson = True


class DisplayModes(Enum):
stdout = "stdout"
dicts = "dicts"


def _cleanup(d):
"""
Removes empty values in a `dict` recursively
This ensures we remove values that Meson could not provide to CONFIG
"""
if isinstance(d, dict):
return {k: _cleanup(v) for k, v in d.items() if v and _cleanup(v)}
else:
return d


CONFIG = _cleanup(
CONFIG = cleanup_empty_dict_values(
{
"Compilers": {
"c": {
Expand Down Expand Up @@ -109,13 +97,7 @@ CONFIG = _cleanup(
)


def _check_pyyaml():
import yaml

return yaml


def show(mode=DisplayModes.stdout.value):
def show(mode=ConfigDisplayModes.stdout.value):
"""
Show libraries and system information on which NumPy was built
and is being used
Expand All @@ -136,27 +118,12 @@ def show(mode=DisplayModes.stdout.value):
--------
get_include : Returns the directory containing NumPy C
header files.
show_runtime : Print information about various resources in the system

Notes
-----
1. The `'stdout'` mode will give more readable
output if ``pyyaml`` is installed

"""
if mode == DisplayModes.stdout.value:
try: # Non-standard library, check import
yaml = _check_pyyaml()

print(yaml.dump(CONFIG))
except ModuleNotFoundError:
import warnings
import json

warnings.warn("Install `pyyaml` for better output", stacklevel=1)
print(json.dumps(CONFIG, indent=2))
elif mode == DisplayModes.dicts.value:
return CONFIG
else:
raise AttributeError(
f"Invalid `mode`, use one of: {', '.join([e.value for e in DisplayModes])}"
)
return print_or_return_config(mode, CONFIG)
3 changes: 2 additions & 1 deletion 3 numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ from contextlib import contextmanager
import numpy as np
from numpy._pytesttester import PytestTester
from numpy._core._internal import _ctypes
from numpy._utils._config_helpers import ConfigDisplayModes

from numpy._typing import (
# Arrays
Expand Down Expand Up @@ -637,7 +638,7 @@ test: PytestTester
#
# Placeholders for classes

def show_config() -> None: ...
def show_config(mode: ConfigDisplayModes) -> None | dict: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BvB93 / @mattip / @rgommers , just curious, how did the UT not catch this? I went over the typing UT but didn't quite get how it's not failing. Should we add some tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some tests?

The answer to that question is pretty much always "yes, if they are not slow and complicated"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take this up in a following PR. It's a bit hard to navigate the typing tests.


_NdArraySubClass = TypeVar("_NdArraySubClass", bound=NDArray[Any])
_DTypeScalar_co = TypeVar("_DTypeScalar_co", covariant=True, bound=generic)
Expand Down
48 changes: 48 additions & 0 deletions 48 numpy/_utils/_config_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from enum import Enum


class ConfigDisplayModes(Enum):
stdout = "stdout"
dicts = "dicts"


def cleanup_empty_dict_values(d):
"""
Removes empty values in a `dict` recursively
This ensures we remove values that Meson could not provide to CONFIG
"""
if isinstance(d, dict):
return {
k: cleanup_empty_dict_values(v)
for k, v in d.items()
if v and cleanup_empty_dict_values(v)
}
else:
return d


def check_pyyaml():
import yaml

return yaml


def print_or_return_config(mode, config):
if mode == ConfigDisplayModes.stdout.value:
try: # Non-standard library, check import
yaml = check_pyyaml()

print(yaml.dump(config))
except ModuleNotFoundError:
import warnings
import json

warnings.warn("Install `pyyaml` for better output", stacklevel=1)
print(json.dumps(config, indent=2))
elif mode == ConfigDisplayModes.dicts.value:
return config
else:
raise AttributeError(
"Invalid `mode`, use one of: "
f"{', '.join([e.value for e in ConfigDisplayModes])}"
)
31 changes: 25 additions & 6 deletions 31 numpy/lib/_utils_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import platform

from numpy._core import ndarray
from numpy._utils import set_module
from numpy._utils import set_module, _config_helpers
import numpy as np

__all__ = [
Expand All @@ -17,14 +17,26 @@


@set_module('numpy')
def show_runtime():
def show_runtime(mode=_config_helpers.ConfigDisplayModes.stdout.value):
"""
Print information about various resources in the system
including available intrinsic support and BLAS/LAPACK library
in use

.. versionadded:: 1.24.0

Parameters
----------
mode : {`'stdout'`, `'dicts'`}, optional.
Indicates how to display the config information.
`'stdout'` prints to console, `'dicts'` returns a dictionary
of the configuration.

Returns
-------
out : {`dict`, `None`}
If mode is `'dicts'`, a dict is returned, else None

See Also
--------
show_config : Show libraries in the system on which NumPy was built.
Expand All @@ -41,11 +53,19 @@ def show_runtime():
__cpu_features__, __cpu_baseline__, __cpu_dispatch__
)
from pprint import pprint
uname = platform.uname()
config_found = [{
"numpy_version": np.__version__,
"python": sys.version,
"uname": platform.uname(),
}]
"uname": {
"machine": uname.machine,
"node": uname.node,
"processor": uname.processor,
"release": uname.release,
"system": uname.system,
"version": uname.version,
},
}]
features_found, features_not_found = [], []
for feature in __cpu_dispatch__:
if __cpu_features__[feature]:
Expand All @@ -67,8 +87,7 @@ def show_runtime():
" Install it by `pip install threadpoolctl`."
" Once installed, try `np.show_runtime` again"
" for more detailed build information")
pprint(config_found)

return _config_helpers.print_or_return_config(mode, config_found)

@set_module('numpy')
def get_include():
Expand Down
3 changes: 2 additions & 1 deletion 3 numpy/lib/_utils_impl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from typing import (
from numpy._core.numerictypes import (
issubdtype as issubdtype,
)
from numpy._utils._config_helpers import ConfigDisplayModes

_T_contra = TypeVar("_T_contra", contravariant=True)

Expand All @@ -30,4 +31,4 @@ def source(
output: None | _SupportsWrite[str] = ...,
) -> None: ...

def show_runtime() -> None: ...
def show_runtime(mode: ConfigDisplayModes) -> None | dict: ...
4 changes: 2 additions & 2 deletions 4 numpy/tests/test_numpy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestNumPyConfigs:
"Python Information",
]

@patch("numpy.__config__._check_pyyaml")
@patch("numpy._utils._config_helpers.check_pyyaml")
def test_pyyaml_not_found(self, mock_yaml_importer):
mock_yaml_importer.side_effect = ModuleNotFoundError()
with pytest.warns(UserWarning):
Expand All @@ -38,7 +38,7 @@ def test_invalid_mode(self):
np.show_config(mode="foo")

def test_warn_to_add_tests(self):
assert len(np.__config__.DisplayModes) == 2, (
assert len(np._utils._config_helpers.ConfigDisplayModes) == 2, (
"New mode detected,"
" please add UT if applicable and increment this count"
)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.