JuliaPy / JuliaPy/PyCall.jl

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

Ouverte
#744 2 commentaires 2 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Julia
Étoiles
1.5k
Forks
186
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez par reproduire la démonstration Julia et Python de cette issue, puis suivez le chemin de conversion de PyCall pour les valeurs scalaires NumPy et comparez-le à la conversion des tableaux. C’est terminé lorsque les types scalaires numériques pris en charge et listés sont convertis en valeurs Julia correspondantes, comme leurs tableaux ; float128 et complex256 restent exclus en raison de Issue #743.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
julia, python
Domaine
backend-api-design
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.