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

Add error hint when a numpy array is expected? #280

Closed
IanButterworth opened this issue Mar 8, 2023 · 3 comments
Closed

Add error hint when a numpy array is expected? #280

IanButterworth opened this issue Mar 8, 2023 · 3 comments

Comments

@IanButterworth
Copy link

I was bitten by

julia> labelled_frame = zeros(Int, 10, 10); labelled_frame[5:6, 5:6] .= 1;

julia> using PythonCall

julia> skimage = pyimport("skimage")
Python module: <module 'skimage' from '/Users/ian/Documents/GitHub/Foo.jl/.CondaPkg/env/lib/python3.11/site-packages/skimage/__init__.py'>

julia> skimage.measure.regionprops(labelled_frame)
ERROR: Python: AttributeError: Julia: type Array has no field dtype
Python stacktrace:
 [1] __getattr__
   @ ~/.julia/packages/PythonCall/dsECZ/src/jlwrap/any.jl:189
 [2] regionprops
   @ skimage.measure._regionprops ~/Documents/GitHub/Foo.jl/.CondaPkg/env/lib/python3.11/site-packages/skimage/measure/_regionprops.py:1253
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/dsECZ/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/dsECZ/src/err.jl:10 [inlined]
 [3] pycallargs(f::Py, args::Py)
   @ PythonCall ~/.julia/packages/PythonCall/dsECZ/src/abstract/object.jl:210
 [4] pycall(f::Py, args::Matrix{Int64}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ PythonCall ~/.julia/packages/PythonCall/dsECZ/src/abstract/object.jl:228
 [5] pycall
   @ ~/.julia/packages/PythonCall/dsECZ/src/abstract/object.jl:218 [inlined]
 [6] #_#11
   @ ~/.julia/packages/PythonCall/dsECZ/src/Py.jl:352 [inlined]
 [7] (::Py)(args::Matrix{Int64})
   @ PythonCall ~/.julia/packages/PythonCall/dsECZ/src/Py.jl:352
 [8] top-level scope
   @ REPL[35]:1

which after help on slack was fixed by converting to a numpy array

julia> skimage.measure.regionprops(pyjl(labelled_frame).to_numpy())
Python list: [<skimage.measure._regionprops.RegionProperties object at 0x1194fee60>]

The error itself was not helpful in pointing me in that direction

ERROR: Python: AttributeError: Julia: type Array has no field dtype

Is it possible to guide the user better here? Or at least add this to a troubleshooting section of the docs?

@cjdoris
Copy link
Collaborator

cjdoris commented Mar 10, 2023

I've added an entry to the FAQ: https://cjdoris.github.io/PythonCall.jl/dev/faq/#Issues-when-Numpy-arrays-are-expected (will appear in the stable docs in the next release)

Really this is an issue with skimage - it is assuming the input is an actual numpy array (hence it is attempting to access the dtype attribute), instead of coercing its input to one with numpy.asarray. You could consider raising an issue (or PR) over there.

I don't think there's much I can do from PythonCall to assist with this.

@IanButterworth
Copy link
Author

That's great, thanks.

Tried a report here scikit-image/scikit-image#6812
Please add detail if I missed anything

@cjdoris
Copy link
Collaborator

cjdoris commented Mar 10, 2023

Looks good!

@cjdoris cjdoris closed this as completed Mar 10, 2023
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

No branches or pull requests

2 participants