Skip to content

ValueError when using numpy>2.0 related to copy=False keyword usage #32

Closed

Description

@rbgdet

see https://numpy.org/doc/2.4/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword

Code:

import pymangle
import numpy as np
LOWZ_S_path = '/Users/raulteixeira/Downloads/mask_DR11v1_LOWZ_South.ply'

lowz_S = pymangle.Mangle(LOWZ_S_path)

good = lowz_S.contains(200, 0)

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 7
      3 LOWZ_S_path = '[/Users/raulteixeira/Downloads/mask_DR11v1_LOWZ_South.ply](http://localhost:8889/mask_DR11v1_LOWZ_South.ply)'
      5 lowz_S = pymangle.Mangle(LOWZ_S_path)
----> 7 good = lowz_S.contains(200, 0)

File [~/miniforge3/envs/mangletest/lib/python3.14/site-packages/pymangle/mangle.py:152](http://localhost:8889/~/miniforge3/envs/mangletest/lib/python3.14/site-packages/pymangle/mangle.py#line=151), in Mangle.contains(self, ra, dec)
    137 """
    138 Check points against mask, returning 1 if contained 0 if not
    139 
   (...)    149 Array of zeros or ones
    150 """
    151 # we specify order to force contiguous
--> 152 ra = array(ra, ndmin=1, dtype=longdouble, copy=False, order='C')
    153 dec = array(dec, ndmin=1, dtype=longdouble, copy=False, order='C')
    154 return super(Mangle, self).contains(ra, dec)

ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

I think the fix is to turn all copy=False keywords into copy=None in https://github.com/esheldon/pymangle/blob/master/pymangle/mangle.py

The .ply file used can be obtained by

wget https://data.sdss.org/sas/dr11/boss/lss/mask_DR11v1_LOWZ_South.ply

I am happy to share details of my environment setup upon request.

Activity

  1. esheldon commented on Apr 3, 2026

    @esheldon
    Owner

    Thanks for the report.

    I've got a PR here that fixes this: #33

    Want to give it a try?

  2. rbgdet commented on Apr 3, 2026

    @rbgdet
    Author

    Hi, thank you for opening the copy_if_needed branch.

    I've gotten an import error

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    Cell In[2], line 1
    ----> 1 import pymangle
    
    File ~/Downloads/pymangle/pymangle/__init__.py:3
          1 # flake8: noqa
    ----> 3 from . import mangle
          4 from .mangle import Mangle, genrand_cap
          5 from .version import __version__
    
    File ~/Downloads/pymangle/pymangle/mangle.py:23
         21 import numpy as np
         22 from numpy import array, longdouble
    ---> 23 from . import _mangle
         26 if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
         27     copy_if_needed = None
    
    ImportError: cannot import name '_mangle' from partially initialized module 'pymangle' (most likely due to a circular import) (/Users/raulteixeira/Downloads/pymangle/pymangle/__init__.py)
    
  3. esheldon commented on Apr 3, 2026

    @esheldon
    Owner

    Did you install the code using pip install . from the code directory, and then try importing from another directory?

  4. rbgdet commented on Apr 3, 2026

    @rbgdet
    Author

    Yes, that's how I installed it.

    If I open the code from pathtorepo/pymangle/pymangle it works. Issue happened when I was in pathtorepo/pymangle/. I am in the copy_if_needed branch.

  5. esheldon commented on Apr 3, 2026

    @esheldon
    Owner

    I think it is a path issue.

    In any case, from the unit tests I think this fixed the problem. I'm going to do a new release today.

  6. rbgdet commented on Apr 3, 2026

    @rbgdet
    Author

    Yes it did! Thank you very much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions