Skip to content

Commit

Permalink
Switch to session-info2 (scverse#3384)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored and Ubuntu committed Feb 4, 2025
1 parent 120db93 commit a99d365
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 69 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
rapids_singlecell=("https://rapids-singlecell.readthedocs.io/en/latest/", None),
scipy=("https://docs.scipy.org/doc/scipy/", None),
seaborn=("https://seaborn.pydata.org/", None),
session_info2=("https://session-info2.readthedocs.io/en/stable/", None),
sklearn=("https://scikit-learn.org/stable/", None),
)

Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/3384.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch {func}`~scanpy.logging.print_header` and {func}`~scanpy.logging.print_versions` to {mod}`session_info2` {smaller}`P Angerer`
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies = [
"umap-learn>=0.5,!=0.5.0",
"pynndescent>=0.5",
"packaging>=21.3",
"session-info",
"session-info2",
"legacy-api-wrap>=1.4", # for positional API deprecations
"typing-extensions; python_version < '3.13'",
]
Expand Down
92 changes: 25 additions & 67 deletions src/scanpy/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

import logging
import sys
import warnings
from datetime import datetime, timedelta, timezone
from functools import partial, update_wrapper
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, overload

import anndata.logging

from ._compat import deprecated

if TYPE_CHECKING:
from typing import IO

from session_info2 import SessionInfo

from ._settings import ScanpyConfig


Expand Down Expand Up @@ -127,33 +130,11 @@ def format(self, record: logging.LogRecord):
get_memory_usage = anndata.logging.get_memory_usage


_DEPENDENCIES_NUMERICS = [
"anndata", # anndata actually shouldn't, but as long as it's in development
"umap",
"numpy",
"scipy",
"pandas",
("sklearn", "scikit-learn"),
"statsmodels",
"igraph",
"louvain",
"leidenalg",
"pynndescent",
]


def _versions_dependencies(dependencies):
# this is not the same as the requirements!
for mod in dependencies:
mod_name, dist_name = mod if isinstance(mod, tuple) else (mod, mod)
try:
imp = __import__(mod_name)
yield dist_name, imp.__version__
except (ImportError, AttributeError):
pass


def print_header(*, file=None):
@overload
def print_header(*, file: None = None) -> SessionInfo: ...
@overload
def print_header(*, file: IO[str]) -> None: ...
def print_header(*, file: IO[str] | None = None):
"""\
Versions that might influence the numerical results.
Matplotlib and Seaborn are excluded from this.
Expand All @@ -163,50 +144,27 @@ def print_header(*, file=None):
file
Optional path for dependency output.
"""
from session_info2 import session_info

modules = ["scanpy"] + _DEPENDENCIES_NUMERICS
print(
" ".join(f"{mod}=={ver}" for mod, ver in _versions_dependencies(modules)),
file=file or sys.stdout,
)
sinfo = session_info(os=True, cpu=True, gpu=True, dependencies=True)

if file is not None:
print(sinfo, file=file)
return

return sinfo


def print_versions(*, file: IO[str] | None = None):
@deprecated("Use `print_header` instead")
def print_versions() -> SessionInfo:
"""\
Print versions of imported packages, OS, and jupyter environment.
Alias for `print_header`.
For more options (including rich output) use `session_info.show` directly.
.. deprecated:: 1.11.0
Parameters
----------
file
Optional path for output.
Use :func:`print_header` instead.
"""
import session_info

if file is not None:
from contextlib import redirect_stdout

warnings.warn(
"Passing argument 'file' to print_versions is deprecated, and will be "
"removed in a future version.",
FutureWarning,
)
with redirect_stdout(file):
print_versions()
else:
session_info.show(
dependencies=True,
html=False,
excludes=[
"builtins",
"stdlib_list",
"importlib_metadata",
# Special module present if test coverage being calculated
# https://gitlab.com/joelostblom/session_info/-/issues/10
"$coverage",
],
)
return print_header()


def print_version_and_date(*, file=None):
Expand Down Expand Up @@ -235,7 +193,7 @@ def _copy_docs_and_signature(fn):
def error(
msg: str,
*,
time: datetime = None,
time: datetime | None = None,
deep: str | None = None,
extra: dict | None = None,
) -> datetime:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_call_outputs(func):
"""
output_io = StringIO()
with redirect_stdout(output_io):
func()
out = func()
if out is not None:
print(out)
output = output_io.getvalue()
assert output != ""

0 comments on commit a99d365

Please sign in to comment.