Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rules to auto convert Awkward types #57

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AwkwardArray"
uuid = "7d259134-7f60-4bf1-aa00-7452e11bde56"
authors = ["Jim Pivarski <pivarski@princeton.edu>", "Jerry Ling <jerry.ling@cern.ch>", "and contributors"]
version = "0.1.2"
version = "0.1.3"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand Down
47 changes: 47 additions & 0 deletions ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,51 @@ function AwkwardArray.convert(array::Py)::AwkwardArray.Content
)
end

function AwkwardArray.convert(array::PyIterable{Any})::AwkwardArray.Content
form, len, _containers = pyimport("awkward").to_buffers(array)
containers = pyconvert(Dict, _containers)

julia_buffers = Dict{String,AbstractVector{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


function __init__()
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.PrimitiveArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.EmptyArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ListOffsetArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ListArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.RegularArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.RecordArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.TupleArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.IndexedArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.IndexedOptionArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteMaskedArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.BitMaskedArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.UnmaskedArray}, x::Py) = AwkwardArray.convert(x)
PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.UnionArray}, x::Py) = AwkwardArray.convert(x)

PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.PrimitiveArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.EmptyArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ListOffsetArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ListArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.RegularArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.RecordArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.TupleArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.IndexedArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.IndexedOptionArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteMaskedArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.BitMaskedArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.UnmaskedArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.UnionArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY)
end

end # module
Loading