JuliaPy / JuliaPy/PyCall.jl

Most numpy scalar types are not automatically converted to Julia types outside of arrays

Aperta
#744 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Julia
Stelle
1.5k
Fork
186
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Numpy arrays of numeric types are automatically converted to Julia types, but scalar values of the same numeric types are not usually converted.

Here is a demonstration. I skipped float128 and complex256 (and longfloat and clongfloat) because of Issue #743
```Julia
using PyCall
py"""
import numpy as np
# The numerical scalar types
types = [ # many of these are aliases of each other
# Bool
np.bool_, np.bool8,
# Integers
np.byte, np.short, np.intc, np.int_, np.longlong, np.int8, np.int16, np.int32, np.int64,
# Unsigned integers
np.ubyte, np.ushort, np.uintc, np.uint, np.ulonglong, np.uint8, np.uint16, np.uint32, np.uint64,
# Floating-point numbers
np.half, np.single, np.double, np.float_, np.float16, np.float32, np.float64,
# Complex floating-point numbers
np.csingle, np.complex_, np.complex64, np.complex128
]
# The aliased dtypes disappear in the dictionary
d_arrays = {t:np.array([1,2,3,4], dtype=t) for t in types}
d_scalars = {t:d_arrays[t][0] for t in types}
"""
```
The result is that all the arrays convert correctly, but only np.float64 and np.complex128 scalars convert correctly. The rest just become PyObjects and have to converted manually.
```Julia
julia> py"d_arrays"
Dict{Any,Any} with 16 entries:
PyObject => Complex{Float32}[1.0+0.0im, 2.0+0.0im, 3.0+0.0im, 4.0+0.0im]
PyObject => Int32[1, 2, 3, 4]
PyObject => UInt8[0x01, 0x02, 0x03, 0x04]
PyObject => Int16[1, 2, 3, 4]
PyObject => [1, 2, 3, 4]
PyObject => Float16[1.0, 2.0, 3.0, 4.0]
PyObject => UInt64[0x0000000000000001, 0x0000000000000002, 0x0000000000000003, 0x0000000000000004]
PyObject => Float32[1.0, 2.0, 3.0, 4.0]
PyObject => Bool[1, 1, 1, 1]
PyObject => UInt16[0x0001, 0x0002, 0x0003, 0x0004]
PyObject => UInt32[0x00000001, 0x00000002, 0x00000003, 0x00000004]
PyObject => UInt64[0x0000000000000001, 0x0000000000000002, 0x0000000000000003, 0x0000000000000004]
PyObject => Int8[1, 2, 3, 4]
PyObject => [1, 2, 3, 4]
PyObject => [1.0, 2.0, 3.0, 4.0]
PyObject => Complex{Float64}[1.0+0.0im, 2.0+0.0im, 3.0+0.0im, 4.0+0.0im]
```
```Julia
julia> py"d_elements"
Dict{Any,Any} with 16 entries:
PyObject => PyObject (1+0j)
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1.0
PyObject => PyObject 1
PyObject => PyObject 1.0
PyObject => PyObject True
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => PyObject 1
PyObject => 1.0
PyObject => 1.0+0.0im

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.