|
1 | 1 |
|
2 | | -# # See https://github.com/cjdoris/PythonCall.jl/issues/172. |
3 | | -# function _pyconvert(x::Py) |
4 | | -# @show x |
5 | | -# if pyisinstance(x, datasets.Dataset) |
6 | | -# return Dataset(x) |
7 | | -# elseif pyisinstance(x, datasets.DatasetDict) |
8 | | -# return DatasetDict(x) |
9 | | -# elseif pyisinstance(x, PIL.PngImagePlugin.PngImageFile) || pyisinstance(x, PIL.JpegImagePlugin.JpegImageFile) |
10 | | -# @show x |
11 | | -# a = numpy2jl(np.array(x)) |
12 | | -# if ndims(a) == 3 && size(a, 1) == 3 |
13 | | -# return colorview(RGB{N0f8}, a) |
14 | | -# elseif ndims(a) == 2 |
15 | | -# return reinterpret(Gray{N0f8}, a) |
16 | | -# else |
17 | | -# error("Unknown image format") |
18 | | -# end |
19 | | -# elseif pyisinstance(x, np.ndarray) |
20 | | -# return numpy2jl(x) |
21 | | -# else |
22 | | -# return pyconvert(Any, x) |
23 | | -# end |
24 | | -# end |
25 | | - |
26 | | -# # # Do nothing on a non-Py object. |
27 | | -# # _pyconvert(x) = x |
28 | | - |
29 | 2 | """ |
30 | 3 | py2jl(x) |
31 | 4 |
|
32 | | -Convert Python types to Julia types applying `pyconvert` recursively. |
| 5 | +Convert Python types to Julia types. It will recursively traverse built-in python |
| 6 | +containers such as lists, tuples, dicts, and sets, and convert all nested objects. |
| 7 | +On the leaves, it will call either `pyconvert(Any, x)` or [`numpy2jl`](@ref). |
33 | 8 | """ |
34 | | -py2jl |
35 | | - |
36 | | -# py2jl recurses through pycanonicalize and converts through _pyconvert |
37 | 9 | py2jl(x) = pyconvert(Any, x) |
38 | 10 |
|
39 | 11 | function py2jl(x::Py) |
|
74 | 46 | """ |
75 | 47 | numpy2jl(x) |
76 | 48 |
|
77 | | -Convert a numpy array to a Julia array using DLPack. |
| 49 | +Convert a numpy array to a Julia array using DLPack.jl. |
78 | 50 | The conversion is copyless, and mutations to the Julia array are reflected in the numpy array. |
| 51 | +For row major python arrays, the returned Julia array has permuted dimensions. |
| 52 | +
|
| 53 | +This function is called by [`py2jl`](@ref). |
| 54 | +See also [`jl2numpy`](@ref). |
79 | 55 | """ |
80 | 56 | function numpy2jl(x::Py) |
81 | | - # pyconvert(Any, x) |
82 | | - # PyArray(x, copy=false) |
83 | | - if Bool(x.dtype.type == np.str_) |
84 | | - return PyArray(x, copy=false) |
85 | | - else |
86 | | - return DLPack.wrap(x, x -> x.__dlpack__()) |
87 | | - end |
| 57 | + return DLPack.from_dlpack(x) |
88 | 58 | end |
89 | 59 |
|
90 | | -## TODO this doesn't work yet. |
91 | | -## https://github.com/pabloferz/DLPack.jl/issues/32 |
92 | | -# function jl2numpy(x::AbstractArray) |
93 | | -# return DLPack.share(x, np.from_dlpack) |
94 | | -# end |
| 60 | +""" |
| 61 | + jl2numpy(x) |
| 62 | +
|
| 63 | +Convert a Julia array to a numpy array using DLPack.jl. |
| 64 | +The conversion is copyless, and mutations to the numpy array are reflected in the Julia array. |
| 65 | +The returned numpy array has permuted dimensions with respect to the input Julia array. |
| 66 | +
|
| 67 | +See also [`numpy2jl`](@ref). |
| 68 | +""" |
| 69 | +function jl2numpy(x::AbstractArray) |
| 70 | + return DLPack.share(x, np.from_dlpack) |
| 71 | +end |
0 commit comments