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

Array conversion error? #177

Closed
jd-foster opened this issue May 25, 2022 · 7 comments · Fixed by JuliaPy/PythonPlot.jl#10
Closed

Array conversion error? #177

jd-foster opened this issue May 25, 2022 · 7 comments · Fixed by JuliaPy/PythonPlot.jl#10

Comments

@jd-foster
Copy link

MWE

import Pkg
Pkg.activate(@__DIR__)
Pkg.add("CondaPkg")
Pkg.add("PythonCall")

using CondaPkg
CondaPkg.add("python")
CondaPkg.add("matplotlib")

using PythonCall
plt = pyimport("matplotlib.pyplot")
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])

results in

ERROR: Python: TypeError: expecting 1 indices, got 2
Python stacktrace:
 [1] __getitem__
   @ /Users/xxx/.julia/packages/PythonCall/XgP8G/src/jlwrap/array.jl:293:16
 [2] _plot_args
   @ matplotlib.axes._base ~/julia/Examples/PythonCall/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_base.py:504
 [3] __call__
   @ matplotlib.axes._base ~/julia/Examples/PythonCall/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_base.py:312
 [4] plot
   @ matplotlib.axes._axes ~/julia/Examples/PythonCall/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_axes.py:1632
 [5] plot
   @ matplotlib.pyplot ~/julia/Examples/PythonCall/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py:2769
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/XgP8G/src/err.jl:94
...

with

julia> versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.5.0)
@cjdoris
Copy link
Collaborator

cjdoris commented May 25, 2022

The offending line of Matplotlib is

x = x[:, np.newaxis]

so it's assuming that the input is a numpy array.

Seems like this is a bug (or undocumented feature) of Matplotlib? It should really do

x = np.asarray(x)

on its inputs to make sure. I guess you will have to do this yourself.

@icweaver
Copy link

icweaver commented May 25, 2022

I could be remembering wrong, but I feel like this used to work here, at least for single arguments. Trying it again seems to throw the same error now (on main):

using PythonCall

@py import matplotlib.pyplot as plt

plt.plot(rand(10))
ERROR: Python: TypeError: expecting 1 indices, got 2
Python stacktrace:
 [1] __getitem__
   @ ~/.julia/packages/PythonCall/7vZkL/src/jlwrap/array.jl:309
 [2] _plot_args
   @ matplotlib.axes._base ~/Projects/pythoncall_main/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_base.py:506
 [3] __call__
   @ matplotlib.axes._base ~/Projects/pythoncall_main/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_base.py:312
 [4] plot
   @ matplotlib.axes._axes ~/Projects/pythoncall_main/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/axes/_axes.py:1632
 [5] plot
   @ matplotlib.pyplot ~/Projects/pythoncall_main/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py:2769
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/7vZkL/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/7vZkL/src/err.jl:10 [inlined]
 [3] pycallargs
   @ ~/.julia/packages/PythonCall/7vZkL/src/abstract/object.jl:210 [inlined]
 [4] pycall(f::Py, args::Vector{Float64}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ PythonCall ~/.julia/packages/PythonCall/7vZkL/src/abstract/object.jl:228
 [5] pycall
   @ ~/.julia/packages/PythonCall/7vZkL/src/abstract/object.jl:218 [inlined]
 [6] #_#13
   @ ~/.julia/packages/PythonCall/7vZkL/src/Py.jl:358 [inlined]
 [7] (::Py)(args::Vector{Float64})
   @ PythonCall ~/.julia/packages/PythonCall/7vZkL/src/Py.jl:358
 [8] top-level scope
   @ REPL[11]:1

I guess we could just wrap everything in pylists?

@cjdoris
Copy link
Collaborator

cjdoris commented May 25, 2022

Yeah it used to work.

The issue is that this function has changed.

The old version (in the link) eventually does np.atleast_1d(x) when x is a Julia array, converting it to a numpy array.

Unfortunately a Julia array looks enough like a numpy array that it passes through the current version of this function unchanged, which is a bug because the output really does need to be a numpy array.

Should be an easy fix, I'll make an issue on Matplotlib.

@cjdoris
Copy link
Collaborator

cjdoris commented May 25, 2022

For now you can call np.array on any Julia arrays manually, or pin Matplotlib to an older version.

@icweaver
Copy link

Ah, gotcha, thanks for following up!

@jd-foster
Copy link
Author

Thank you for verifying the issue, I was not entirely sure it was just my setup.

@cjdoris
Copy link
Collaborator

cjdoris commented May 27, 2022

I changed something in PythonCall (wrapped Julia arrays now have a to_numpy() method) which is enough to get matplotlib to do the desired conversion, so this works again. It's released.

@cjdoris cjdoris closed this as completed May 27, 2022
jd-foster added a commit to jd-foster/PythonPlot.jl that referenced this issue May 27, 2022
Support the 0.9 release (fixes JuliaPy/PythonCall.jl#177, for example)
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 a pull request may close this issue.

3 participants