-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add a method to convert `PyIterable` * feat: add auto conversion rules * fix: update conversion rules * feat: define __init__ function * fix: move convert out of the __init__ function * test: add a test passing a Python array to a Julia function * feat: define conversion rules * fix: remove convertion of an iterable * feat: add rules and register * fix: leave it to a user to invoke pyconvert * test: test pyconvert of an awkward array * tests: add tests * fix: use Vector in convert to avoid problems with BitMasked buffers * feat: convert to an awkward type * fix: add dtypes * fix: more dtypes
- Loading branch information
Showing
5 changed files
with
292 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
module AwkwardPythonCallExt | ||
using PythonCall | ||
using JSON | ||
import AwkwardArray | ||
|
||
function AwkwardArray.convert(layout::AwkwardArray.Content)::Py | ||
form, len, containers = AwkwardArray.to_buffers(layout) | ||
|
||
py_buffers = Dict{String,Any}() | ||
|
||
for (key, buffer) in containers | ||
py_buffers[key] = pyimport("numpy").asarray(buffer, dtype = pyimport("numpy").uint8) | ||
end | ||
|
||
pyimport("awkward").from_buffers(form, len, py_buffers) | ||
end | ||
|
||
function AwkwardArray.convert(array::Py)::AwkwardArray.Content | ||
form, len, _containers = pyimport("awkward").to_buffers(array) | ||
containers = pyconvert(Dict, _containers) | ||
|
||
julia_buffers = Dict{String,Vector{UInt8}}() | ||
|
||
for (key, buffer) in containers | ||
julia_buffers[key] = reinterpret(UInt8, buffer) | ||
end | ||
|
||
AwkwardArray.from_buffers( | ||
pyconvert(String, form.to_json()), | ||
pyconvert(Int, len), | ||
julia_buffers, | ||
) | ||
end | ||
|
||
# rule functions | ||
function pyconvert_rule_awkward_array_primitive(::Type{AwkwardArray.PrimitiveArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_empty(::Type{AwkwardArray.EmptyArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_listoffset(::Type{AwkwardArray.ListOffsetArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_list(::Type{AwkwardArray.ListArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
function pyconvert_rule_awkward_array_regular(::Type{AwkwardArray.RegularArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_record(::Type{AwkwardArray.RecordArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_tuple(::Type{AwkwardArray.TupleArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_indexed(::Type{AwkwardArray.IndexedArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_indexedoption(::Type{AwkwardArray.IndexedOptionArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_bytemasked(::Type{AwkwardArray.ByteMaskedArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_bitmasked(::Type{AwkwardArray.BitMaskedArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_unmasked(::Type{AwkwardArray.UnmaskedArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function pyconvert_rule_awkward_array_union(::Type{AwkwardArray.UnionArray}, x::Py) | ||
array = AwkwardArray.convert(x) | ||
return PythonCall.pyconvert_return(array) | ||
end | ||
|
||
function __init__() | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.PrimitiveArray, pyconvert_rule_awkward_array_primitive, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.EmptyArray, pyconvert_rule_awkward_array_empty, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ListOffsetArray, pyconvert_rule_awkward_array_listoffset, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ListArray, pyconvert_rule_awkward_array_list, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.RegularArray, pyconvert_rule_awkward_array_regular, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.RecordArray, pyconvert_rule_awkward_array_record, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.TupleArray, pyconvert_rule_awkward_array_tuple, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.IndexedArray, pyconvert_rule_awkward_array_indexed, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.IndexedOptionArray, pyconvert_rule_awkward_array_indexedoption, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteMaskedArray, pyconvert_rule_awkward_array_bytemasked, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.BitMaskedArray, pyconvert_rule_awkward_array_bitmasked, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.UnmaskedArray, pyconvert_rule_awkward_array_unmasked, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.UnionArray, pyconvert_rule_awkward_array_union, PythonCall.PYCONVERT_PRIORITY_ARRAY) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters