Skip to content

Commit

Permalink
Revert numpy warnings workaround (#6025)
Browse files Browse the repository at this point in the history
* Revert "Workaround to suporess (some) import warnings from NumPy"

This has been fixed upstream in Aesara and is therefore no longer necessary.
  • Loading branch information
maresb authored Aug 4, 2022
1 parent 9f93b3e commit b506fec
Showing 1 changed file with 0 additions and 71 deletions.
71 changes: 0 additions & 71 deletions pymc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
__version__ = "4.1.4"

import logging
import sys

_log = logging.getLogger("pymc")

Expand All @@ -27,74 +26,6 @@
_log.addHandler(handler)


def __suppress_aesara_import_warnings():
"""This is a workaround to suppress nonlethal NumPy warnings.
Some more printouts remain. See https://github.com/numpy/numpy/issues/21942
Remove this once https://github.com/aesara-devs/aesara/pull/980 is merged.
"""
# We need to catch warnings as in some cases NumPy prints
# stuff that we don't want the user to see.
import io
import warnings

from contextlib import redirect_stderr, redirect_stdout

import numpy.distutils.system_info

class NumpyCompatibleStdoutStringIO(io.StringIO):
"""Used as a temporary replacement of sys.stdout to capture Numpy's output.
We want to simply use io.StringIO, but this doesn't work because
Numpy expects the .encoding attribute to be a string. For io.StringIO,
this attribute is set to None and cannot be modified, hence the need for
this subclass.
(See forward_bytes_to_stdout in numpy.distutils.exec_command.)
"""

encoding = sys.stdout.encoding

# Known executables which trigger false-positive warnings when not found.
# Ref: <https://github.com/conda-forge/aesara-feedstock/issues/54>
executables = ["g77", "f77", "ifort", "ifl", "f90", "DF", "efl"]

# The Numpy function which defines blas_info emits false-positive warnings.
# In what follows we capture these warnings and ignore them.
with warnings.catch_warnings(record=True):
# The warnings about missing executables don't use Python's "warnings"
# mechanism, and thus are not filtered by the catch_warnings context
# above. On Linux the warnings are printed to stderr, but on Windows
# they are printed to stdout. Thus we capture and filter both stdout
# and stderr.
stdout_sio, stderr_sio = NumpyCompatibleStdoutStringIO(), io.StringIO()
with redirect_stdout(stdout_sio), redirect_stderr(stderr_sio):
numpy.distutils.system_info.get_info("blas_opt")

# Print any unfiltered messages to stdout and stderr.
# (We hope that, beyond what we filter out, there should have been
# no messages printed to stdout or stderr. In case there were messages,
# we want to print them for the user to see. In what follows, we print
# the stdout messages followed by the stderr messages. This means that
# messages will be printed in the wrong order in the case that
# there is output to both stdout and stderr, and the stderr output
# doesn't come at the end.)
for captured_buffer, print_destination in (
(stdout_sio, sys.stdout),
(stderr_sio, sys.stderr),
):
raw_lines = captured_buffer.getvalue().splitlines()
filtered_lines = [
line
for line in raw_lines
# Keep a line when none of the warnings are found within
# (equiv. when all of the warnings are not found within).
if all(f"Could not locate executable {exe}" not in line for exe in executables)
]
for line in filtered_lines:
print(line, file=print_destination)
return


def __set_compiler_flags():
# Workarounds for Aesara compiler problems on various platforms
import aesara
Expand All @@ -114,8 +45,6 @@ def __set_compiler_flags():
aesara.config.gcc__cxxflags = augmented


if sys.platform == "win32":
__suppress_aesara_import_warnings()
__set_compiler_flags()

from pymc import gp, ode, sampling
Expand Down

0 comments on commit b506fec

Please sign in to comment.