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

numpy 1.25 warning on 1-element array input #1307

Closed
greglucas opened this issue Jun 22, 2023 · 7 comments
Closed

numpy 1.25 warning on 1-element array input #1307

greglucas opened this issue Jun 22, 2023 · 7 comments
Labels

Comments

@greglucas
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import time

import numpy as np
from pyproj import Transformer


transformer = Transformer.from_crs(2263, 4326)

# Repeat the calculation a bunch of times
t0 = time.time()
for i in range(2000000):
    transformer.transform(np.array([0]), np.array([1]))
    # transformer.transform(0, 1)

t1 = time.time()
print(t1 - t0)

Problem description

Numpy 1.25 is now issuing a warning on auto-conversion of 1-element arrays to double. We have recently added a try-except for a fast-path on the transformer that tries the conversion and then catches the exception. However, this now also throws a warning, which we actually don't care about, but if running with python -Werror script.py it will raise.

We can add the following filter to ignore these warnings within that block, but this adds a significant slowdown of ~5x for the single-point case.
single-point: ~10s

warnings.filterwarnings("ignore", message="Conversion of an array with ndim > 0")

Another option is to catch all exceptions and then do the comparison test and re-raise within the exception block.
single-point: ~2s

        except (TypeError, Exception) as e:
            if not (isinstance(e, TypeError) or
                    str(e).startswith("Conversion of an array with ndim > 0")):
                raise e

I'm raising this issue to get thoughts and ideas on what would be the best way to handle this (maybe something other than those two ideas above). The patch should go in geod and transformer, I haven't looked in detail at other locations. This was originally raised over in Cartopy: SciTools/cartopy#2194 but seems like it should probably be addressed in Pyproj

cc @neutrinoceros

Expected Output

No warnings using Numpy 1.25

Installation method

pip install -e .

@greglucas greglucas added the bug label Jun 22, 2023
@greglucas
Copy link
Contributor Author

Thinking more about the extra except: conditional. That actually won't help hide the plain warnings, it only helps the case when we've converted the warnings to errors with -Werror. I am not entirely sure there is a great solution here, other than to tell users to ignore the warning for this single version of numpy if they pass in a 1-element array... The 5x slowdown seems like a big price to pay to minimize a downstream warning.

@snowman2
Copy link
Member

I agree that no code change and recommend users apply a warning filter sounds like the best course of action.

Side note: Mind making an issue to remove the hacks we put in place due to the current numpy conversion to double in a future version of pyproj?

@greglucas
Copy link
Contributor Author

Opened the new issue.

Lets close this issue then and move the conversation back to Cartopy @neutrinoceros. Sorry for pointing you all over the place here!

@neutrinoceros
Copy link

no worries ! I understand this issue is more subtle than most :-)

@felixdivo
Copy link

For others: The new issue is #1309

@raphaelquast
Copy link

Quick question just to be absolutely sure how to deal with this warning since I'm still a bit confused...

Am I right that the recommendation here is to simply ignore this warning since upcoming releases of pyproj will properly handle 1-element arrays?

...Or in other words:
Will transformer.transform(np.array([0]), np.array([1])) work (without warnings) with with numpy 2.x or should I implement a workaround to avoid future errors?

@snowman2
Copy link
Member

snowman2 commented Feb 5, 2024

python -m pip install numpy --index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple --pre
python -m pip install pyproj
>>> import numpy
>>> import pyproj
>>> numpy.__version__
'2.0.0.dev0+git20240203.5c34c87'
>>> pyproj.__version__
'3.6.1'
>>> transformer = pyproj.Transformer.from_crs(4326, 3857, always_xy=True)
>>> transformer.transform(numpy.array([0]), numpy.array([0]))
(array([0.]), array([0.]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants