[PyCall] Converting PyObject to Array

790 views
Skip to first unread message

Luke Stagner

unread,
Nov 12, 2014, 4:21:12 PM11/12/14
to julia...@googlegroups.com
Forgive me if this is a basic question. I have a matrix stored in an IDL save file. I have been trying to read it in using PyCall and scipy.io.readsav

julia> using PyCall

julia
> @pyimport scipy.io as io

julia
> a=io.readsav("weight_and_dist.sav")
/usr/lib/python2.7/dist-packages/scipy/io/idl.py:167: UserWarning: warning: empty strings are now set to '' instead of None
  warnings
.warn("warning: empty strings are now set to '' instead of None")
Dict{Any,Any} with 8 entries:
 
"w_matrix" => PyObject array([[  1.68552301e-101,   2.356978
 
"m02_fbm"  => PyObject rec.array([ (array([[  1.34499655e+10
 
"pitch"    => PyObject array([-0.89999998, -0.70000005, -0.5
 
"mean_fbm" => PyObject array([[  1.31980645e+10,   3.3166579
 
"v17_fbm"  => PyObject rec.array([ (array([[  1.30541168e+10
 
"energy"   => PyObject array([  5.,  15.,  25.,  35.,  45.,
 
"m10_fbm"  => PyObject rec.array([ (array([[  1.26698307e+10
 
"p2_fbm"   => PyObject rec.array([ (array([[  1.36183452e+10

julia
> w_matrix = a["w_matrix"]
PyObject array([[  1.68552301e-101,   2.35697831e-066,   3.61510788e-046, ...,
         
3.25287399e+003,   6.30708487e+003,   6.26874721e+003],
       
[  3.42516635e-103,   9.95975928e-068,   2.55145289e-047, ...,
         
2.85362314e+003,   5.95774610e+003,   6.24852521e+003],
       
[  6.73923741e-105,   4.07514829e-069,   1.74358145e-048, ...,
         
2.47492336e+003,   5.58385846e+003,   6.20714822e+003],
       
...,
       
[  0.00000000e+000,   0.00000000e+000,   0.00000000e+000, ...,
         
4.78256276e-049,   6.15881422e-073,   2.18231882e-100],
       
[  0.00000000e+000,   0.00000000e+000,   0.00000000e+000, ...,
         
2.07619108e-050,   1.37890846e-074,   2.53424699e-102],
       
[  0.00000000e+000,   0.00000000e+000,   0.00000000e+000, ...,
         
8.63614392e-052,   2.95848174e-076,   2.81982090e-104]])

julia
> w = convert(Array{Float64,2},w_matrix)
ERROR
: PyError (PyFloat_AsDouble) <type 'exceptions.TypeError'>
TypeError('only length-1 arrays can be converted to Python scalars',)

 
in pyerr_check at /home/lstagner/.julia/v0.4/PyCall/src/exception.jl:58
 
in convert at /home/lstagner/.julia/v0.4/PyCall/src/conversions.jl:75
 
in py2array at /home/lstagner/.julia/v0.4/PyCall/src/conversions.jl:339
 
in convert at /home/lstagner/.julia/v0.4/PyCall/src/numpy.jl:430

julia
> w = convert(Array{Float64,2},w_matrix)
ERROR
: PyError (PyFloat_AsDouble) <type 'exceptions.TypeError'>
TypeError('only length-1 arrays can be converted to Python scalars',)

 
in pyerr_check at /home/lstagner/.julia/v0.4/PyCall/src/exception.jl:58
 
in convert at /home/lstagner/.julia/v0.4/PyCall/src/conversions.jl:75
 
in py2array at /home/lstagner/.julia/v0.4/PyCall/src/conversions.jl:339
 
in convert at /home/lstagner/.julia/v0.4/PyCall/src/numpy.jl:430


Could someone direct me towards the correct way to convert PyObjects to Arrays.


Steven G. Johnson

unread,
Nov 12, 2014, 7:46:07 PM11/12/14
to julia...@googlegroups.com


On Wednesday, November 12, 2014 4:21:12 PM UTC-5, Luke Stagner wrote:
Forgive me if this is a basic question. I have a matrix stored in an IDL save file. I have been trying to read it in using PyCall and scipy.io.readsav

I can't reproduce your problem:

julia> o1 = PyObject(rand(2,3))
PyObject array([[ 0.33150974,  0.89636836,  0.68890329],
       [ 0.63132778,  0.36191286,  0.15337456]])

julia> convert(Matrix{Float64}, o1)
2x3 Array{Float64,2}:
 0.33151   0.896368  0.688903
 0.631328  0.361913  0.153375

julia> o2 = pyeval("[[  1.68552301e-101,   2.35697831e-066,   3.61510788e-04], [  3.42516635e-103,   9.95975928e-068,   2.55145289e-04]]", PyObject)
PyObject [[1.68552301e-101, 2.35697831e-66, 0.000361510788], [3.42516635e-103, 9.95975928e-68, 0.000255145289]]

julia> convert(Matrix{Float64}, o2)
2x3 Array{Float64,2}:
 1.68552e-101  2.35698e-66  0.000361511
 3.42517e-103  9.95976e-68  0.000255145
 
Do the above examples work for you?

Luke Stagner

unread,
Nov 12, 2014, 8:01:48 PM11/12/14
to julia...@googlegroups.com
Yes, the examples work for me.

Here is the IDL save file that I used. (700kB).

http://www.filedropper.com/weightanddist

Steven G. Johnson

unread,
Nov 12, 2014, 9:58:59 PM11/12/14
to julia...@googlegroups.com
Can you file an issue at PyCall.jl?

Luke Stagner

unread,
Nov 12, 2014, 11:55:27 PM11/12/14
to julia...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages