Skip to content

Conversation

@ns-rse
Copy link
Contributor

@ns-rse ns-rse commented May 15, 2025

Further to #5 I've found in using numpyencoder with numpy==2.0.2 that np.complex_ has also been deprecated and it is recommended to use np.complex128. As this is already one of the items in the tuple I've simply removed np.complex_ from the tuple of possible types.

=========================================================================== FAILURES ===========================================================================
______________________________________________________________________ test_array_of_int _______________________________________________________________________

    def test_array_of_int():
        import json
        import numpy as np
        from numpyencoder import NumpyEncoder
    
        numpy_data = np.array([np.int64(0), np.int32(1), 2, 3])
        baseline_data = [0, 1, 2, 3]
    
>       j_np = json.dumps(
            numpy_data,
            sort_keys=True,
            separators=(", ", ": "),
            ensure_ascii=False,
            cls=NumpyEncoder,
        )

tests/test_example.py:9: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.11/json/__init__.py:238: in dumps
    **kw).encode(obj)
/usr/lib/python3.11/json/encoder.py:200: in encode
    chunks = self.iterencode(o, _one_shot=True)
/usr/lib/python3.11/json/encoder.py:258: in iterencode
    return _iterencode(o, 0)
/home/neil/.virtualenvs/topostats-afmreader/lib/python3.11/site-packages/numpyencoder/numpyencoder.py:31: in default
    elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

attr = 'complex_'

    def __getattr__(attr):
        # Warn for expired attributes
        import warnings
    
        if attr == "linalg":
            import numpy.linalg as linalg
            return linalg
        elif attr == "fft":
            import numpy.fft as fft
            return fft
        elif attr == "dtypes":
            import numpy.dtypes as dtypes
            return dtypes
        elif attr == "random":
            import numpy.random as random
            return random
        elif attr == "polynomial":
            import numpy.polynomial as polynomial
            return polynomial
        elif attr == "ma":
            import numpy.ma as ma
            return ma
        elif attr == "ctypeslib":
            import numpy.ctypeslib as ctypeslib
            return ctypeslib
        elif attr == "exceptions":
            import numpy.exceptions as exceptions
            return exceptions
        elif attr == "testing":
            import numpy.testing as testing
            return testing
        elif attr == "matlib":
            import numpy.matlib as matlib
            return matlib
        elif attr == "f2py":
            import numpy.f2py as f2py
            return f2py
        elif attr == "typing":
            import numpy.typing as typing
            return typing
        elif attr == "rec":
            import numpy.rec as rec
            return rec
        elif attr == "char":
            import numpy.char as char
            return char
        elif attr == "array_api":
            raise AttributeError("`numpy.array_api` is not available from "
                                 "numpy 2.0 onwards")
        elif attr == "core":
            import numpy.core as core
            return core
        elif attr == "strings":
            import numpy.strings as strings
            return strings
        elif attr == "distutils":
            if 'distutils' in __numpy_submodules__:
                import numpy.distutils as distutils
                return distutils
            else:
                raise AttributeError("`numpy.distutils` is not available from "
                                     "Python 3.12 onwards")
    
        if attr in __future_scalars__:
            # And future warnings for those that will change, but also give
            # the AttributeError
            warnings.warn(
                f"In the future `np.{attr}` will be defined as the "
                "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    
        if attr in __former_attrs__:
            raise AttributeError(__former_attrs__[attr])
    
        if attr in __expired_attributes__:
>           raise AttributeError(
                f"`np.{attr}` was removed in the NumPy 2.0 release. "
                f"{__expired_attributes__[attr]}"
            )
E           AttributeError: `np.complex_` was removed in the NumPy 2.0 release. Use `np.complex128` instead.

/home/neil/.virtualenvs/topostats-afmreader/lib/python3.11/site-packages/numpy/__init__.py:397: AttributeError

Would be useful if this and the np.float_ issue I reported in #5 which has already been addressed could be released to PyPI as v0.3.1 🙏

ns-rse added a commit to AFM-SPM/TopoStats that referenced this pull request May 15, 2025
With updates to [topoly-1.1.0]() newer versions of Numpy are supported. However TensorFlow still requires <2.1.0.

At the same time I discovered there are problems with some of the data types in [numpyencoder](https://github.com/hmallen/numpyencoder).

Last week I reported [Support for np.float_ removed](hmallen/numpyencoder#5) only to find that this had already been addressed in the `master` branch but that the tag applied `v0.30. - NumPy 2.0 Compatibility` whilst building a tar-ball on GitHub hadn't triggered or resulted in a version bump on PyPI.

Today I discovered that `np.complex_` is also deprecated in Numpy 2.0 and whilst I have made a [pull
request](hmallen/numpyencoder#6) to have that addressed in `numpyencoder`, rather than waiting on that I have forked the repository to the AFM-SPM organisation and fixed it there, setting TopoStats to install from the `master` branch of that fork.

If/when a new release of `numpyencoder` is made I will revert to using the PyPI version.
@hmallen hmallen self-assigned this May 19, 2025
@hmallen
Copy link
Owner

hmallen commented May 21, 2025

Removing this entirely would break backwards compatibility. I hadn't considered this before the previous change. I'll create a new release with existing changes then look into fully removing deprecated types in a subsequent release.

@hmallen hmallen closed this May 21, 2025
ns-rse added a commit to AFM-SPM/TopoStats that referenced this pull request Jun 3, 2025
With updates to [topoly-1.1.0]() newer versions of Numpy are supported. However TensorFlow still requires <2.1.0.

At the same time I discovered there are problems with some of the data types in [numpyencoder](https://github.com/hmallen/numpyencoder).

Last week I reported [Support for np.float_ removed](hmallen/numpyencoder#5) only to find that this had already been addressed in the `master` branch but that the tag applied `v0.30. - NumPy 2.0 Compatibility` whilst building a tar-ball on GitHub hadn't triggered or resulted in a version bump on PyPI.

Today I discovered that `np.complex_` is also deprecated in Numpy 2.0 and whilst I have made a [pull
request](hmallen/numpyencoder#6) to have that addressed in `numpyencoder`, rather than waiting on that I have forked the repository to the AFM-SPM organisation and fixed it there, setting TopoStats to install from the `master` branch of that fork.

If/when a new release of `numpyencoder` is made I will revert to using the PyPI version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants