Skip to content
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

BUG: Restore numpy alias such as np.bool, np.int etc #26333

Closed
JosephKarpinski opened this issue Apr 23, 2024 · 6 comments
Closed

BUG: Restore numpy alias such as np.bool, np.int etc #26333

JosephKarpinski opened this issue Apr 23, 2024 · 6 comments
Labels
33 - Question Question about NumPy usage or development

Comments

@JosephKarpinski
Copy link

Describe the issue:

There are so many Python packages that haven't replaced np.bool, np.int, etc.
So many useless errors with no work around, except the developer making the changes.
Installing an older version of numpy breaks all types of dependencies.
Adding the aliases back to numpy fixes so many issues.

Screenshot 2024-04-22 at 9 49 07 PM

Reproduce the code example:

from astroML.plotting import hist

Error message:

AttributeError                            Traceback (most recent call last)
Cell In[20], line 1
----> 1 from astroML.plotting import hist 
      3 rng = np.random.RandomState(10)  # deterministic random data                    #
      4 a = np.hstack((rng.normal(size=1000), rng.normal(loc=5, scale=2, size=1000)))   #

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/astroML/plotting/__init__.py:7
      5 from .multiaxes import MultiAxes
      6 from .settings import setup_text_plots
----> 7 from .regression import plot_regressions, plot_regression_from_trace

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/astroML/plotting/regression.py:5
      2 import matplotlib.pyplot as plt
      4 from scipy import optimize
----> 5 from astroML.linear_model import TLS_logL, LinearRegression
      8 # TLS:
      9 def get_m_b(beta):

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/astroML/linear_model/__init__.py:2
      1 from .linear_regression import LinearRegression, PolynomialRegression, BasisFunctionRegression
----> 2 from .linear_regression_errors import LinearRegressionwithErrors
      4 from .kernel_regression import NadarayaWatson
      5 from .TLS import TLS_logL

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/astroML/linear_model/linear_regression_errors.py:5
      2 import warnings
      4 try:
----> 5     import pymc3 as pm
      6     import theano.tensor as tt
      7     from packaging.version import Version

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/pymc3/__init__.py:23
     20 import platform
     22 import semver
---> 23 import theano
     25 _log = logging.getLogger("pymc3")
     27 if not logging.root.handlers:

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/__init__.py:124
    120 from theano.misc.safe_asarray import _asarray
    122 from theano.printing import pprint, pp
--> 124 from theano.scan_module import (scan, map, reduce, foldl, foldr, clone,
    125                                 scan_checkpoints)
    127 from theano.updates import OrderedUpdates
    129 # scan_module import above initializes tensor and scalar making these imports
    130 # redundant
    131 
   (...)
    136 
    137 # import sparse

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/scan_module/__init__.py:41
     38 __copyright__ = "(c) 2010, Universite de Montreal"
     39 __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
---> 41 from theano.scan_module import scan_opt
     42 from theano.scan_module.scan import scan
     43 from theano.scan_module.scan_checkpoints import scan_checkpoints

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/scan_module/scan_opt.py:60
     57 import numpy as np
     59 import theano
---> 60 from theano import tensor, scalar
     61 from theano.tensor import opt, get_scalar_constant_value, Alloc, AllocEmpty
     62 from theano import gof

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/tensor/__init__.py:8
      4 __docformat__ = "restructuredtext en"
      6 import warnings
----> 8 from theano.tensor.basic import *
      9 from theano.tensor.subtensor import *
     10 from theano.tensor.type_other import *

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/tensor/basic.py:20
     17 from theano.gof import Apply, Constant, Op, Variable, ParamsType
     18 from theano.gof.type import Generic
---> 20 from theano.scalar import int32 as int32_t
     21 from theano.tensor import elemwise
     22 from theano.tensor.var import (AsTensorError, TensorVariable,
     23                                TensorConstant, TensorConstantSignature,
     24                                _tensor_py_operators)

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/scalar/__init__.py:3
      1 from __future__ import absolute_import, print_function, division
----> 3 from .basic import *
      5 from .basic_scipy import *

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/scalar/basic.py:2370
   2367         else:
   2368             return s
-> 2370 convert_to_bool = Cast(bool, name='convert_to_bool')
   2371 convert_to_int8 = Cast(int8, name='convert_to_int8')
   2372 convert_to_int16 = Cast(int16, name='convert_to_int16')

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/theano/scalar/basic.py:2323, in Cast.__init__(self, o_type, name)
   2321 super(Cast, self).__init__(specific_out(o_type), name=name)
   2322 self.o_type = o_type
-> 2323 self.ctor = getattr(np, o_type.dtype)

File /opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/numpy/__init__.py:324, in __getattr__(attr)
    319     warnings.warn(
    320         f"In the future `np.{attr}` will be defined as the "
    321         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    323 if attr in __former_attrs__:
--> 324     raise AttributeError(__former_attrs__[attr])
    326 if attr == 'testing':
    327     import numpy.testing as testing

AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

Python and NumPy Versions:

import sys, numpy
print(numpy.version)
print(sys.version)

1.26.4
3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:49:36) [Clang 16.0.6 ]

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:49:36) '
'[Clang 16.0.6 ]',
'uname': uname_result(system='Darwin', node='Josephs-iMac-2.local', release='23.2.0', version='Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:34 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8103', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}},
{'architecture': 'armv8',
'filepath': '/opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/numpy/.dylibs/libopenblas64_.0.dylib',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'},
{'architecture': 'neoversen1',
'filepath': '/opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/scipy/.dylibs/libopenblas.0.dylib',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.26.dev'},
{'filepath': '/opt/anaconda3/envs/envM1arm64/lib/python3.11/site-packages/sklearn/.dylibs/libomp.dylib',
'internal_api': 'openmp',
'num_threads': 8,
'prefix': 'libomp',
'user_api': 'openmp',
'version': None}]
None

Context for the issue:

there are so many packages that haven't replaced np.boole even under the latest arm64 environment from Anaconda:

Anaconda3-2024.02-1-MacOSX-arm64.pkg

@ngoldbaum
Copy link
Member

For what it's worth, according to the commit log, Theano has not been actively maintained since 2020, seeing only sporadic commits since then. I don't think we should roll back numpy API changes to keep unmaintained libraries working without any need for downstream updates.

Are there other libraries besides theano that you're seeing issues from?

@ngoldbaum ngoldbaum added Numpy 2.0 API Changes 33 - Question Question about NumPy usage or development and removed 00 - Bug labels Apr 23, 2024
@ngoldbaum
Copy link
Member

It also looks like the theano fork, pytensor, is getting some attention for numpy 2.0 support: pymc-devs/pytensor#688

@JosephKarpinski
Copy link
Author

JosephKarpinski commented Apr 23, 2024 via email

@ngoldbaum
Copy link
Member

ngoldbaum commented Apr 23, 2024

For more rationale, see the NEP describing this work: https://numpy.org/neps/nep-0052-python-api-cleanup.html

I'll also note that np.bool has been deprecated since numpy 1.20, released Jan 2021, and accessing the attribute produced a warning that it would be removed in the future.

It's true that this change broke code like theano that hasn't seen updates for the past few years.

That said, there were probably also other changes in numpy that would lead to other breakage besides this, this is just the most visible change for your usage. There might also be other changes in numpy 2.0 that would break theano, particularly changes to the numpy C API. Judging from the issue I linked earlier, it seems the pytensor folks are trying to fix changes related to our wrappers for the C complex types.

@ngoldbaum
Copy link
Member

Just to clarify the timelines because I confused myself a little and got things a little wrong linking to NEP 52 above, the removal of np.bool predates numpy 2.0. As the error message says, it was originally an alias for the bool builtin, but people were using it as if it was an alias for the numpy boolean scalar.

In numpy 1.20, we deprecated it, and then removed it in numpy 1.24.

In numpy 2.0, we actually re-added it, but as the an alias for the numpy boolean scalar type.

Anyway, for bool in particular, this is all kind of old news. There were other API changes for numpy 2.0 and the NEP discusses the rationale there.

@JosephKarpinski
Copy link
Author

JosephKarpinski commented Apr 24, 2024 via email

@mattip mattip closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development
Projects
None yet
Development

No branches or pull requests

3 participants