From ee0d3e10938b7323ac5c3e81e96ed3645c319582 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 11 Dec 2023 10:22:08 +0100 Subject: [PATCH 1/6] feat: add a method to convert `PyIterable` --- ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index fed937f..1bfd1ec 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -32,4 +32,20 @@ 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 + end # module From e3c09a5803a3121d2d5792db04ee93467f35a9ff Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 12 Dec 2023 15:44:34 +0100 Subject: [PATCH 2/6] feat: add auto conversion rules --- .../AwkwardPythonCallExt.jl | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index 1bfd1ec..68ca778 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -48,4 +48,45 @@ function AwkwardArray.convert(array::PyIterable{Any})::AwkwardArray.Content ) end + +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.StringOffsetArray}, x::Py) = AwkwardArray.convert(x) +PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.StringArray}, x::Py) = AwkwardArray.convert(x) +PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.StringRegularArray}, x::Py) = AwkwardArray.convert(x) +PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringOffsetArray}, x::Py) = AwkwardArray.convert(x) +PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringArray}, x::Py) = AwkwardArray.convert(x) +PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringRegularArray}, 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) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringOffsetArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringRegularArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringOffsetArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) +# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringRegularArray, 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 # module From 5ab43352bd424998cbfc60c652aafbf0ac95cc97 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 14 Dec 2023 16:59:10 +0100 Subject: [PATCH 3/6] fix: update conversion rules --- ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index 68ca778..f3c0450 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -49,17 +49,12 @@ function AwkwardArray.convert(array::PyIterable{Any})::AwkwardArray.Content end + 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.StringOffsetArray}, x::Py) = AwkwardArray.convert(x) -PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.StringArray}, x::Py) = AwkwardArray.convert(x) -PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.StringRegularArray}, x::Py) = AwkwardArray.convert(x) -PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringOffsetArray}, x::Py) = AwkwardArray.convert(x) -PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringArray}, x::Py) = AwkwardArray.convert(x) -PythonCall.pyconvert_rule_array_nocopy(::Type{AwkwardArray.ByteStringRegularArray}, 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) @@ -74,12 +69,6 @@ PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.EmptyArray 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) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringOffsetArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.StringRegularArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringOffsetArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringArray, PythonCall.pyconvert_rule_array_nocopy, PythonCall.PYCONVERT_PRIORITY_ARRAY) -# FIXME: PythonCall.pyconvert_add_rule("awkward.highlevel:Array", AwkwardArray.ByteStringRegularArray, 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) From 7af724dc3fdda09dace4545f184881ba91c9a6e9 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Wed, 17 Jan 2024 14:14:10 +0100 Subject: [PATCH 4/6] feat: define __init__ function --- Project.toml | 2 +- .../AwkwardPythonCallExt.jl | 57 ++++++++++--------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/Project.toml b/Project.toml index c78d433..6f79a23 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AwkwardArray" uuid = "7d259134-7f60-4bf1-aa00-7452e11bde56" authors = ["Jim Pivarski ", "Jerry Ling ", "and contributors"] -version = "0.1.2" +version = "0.1.3" [deps] JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index f3c0450..fe4b91d 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -49,33 +49,34 @@ function AwkwardArray.convert(array::PyIterable{Any})::AwkwardArray.Content end - -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) +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 From 009bcafae0bad9da9a07d466b16f0a0e2dd9ae83 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Wed, 17 Jan 2024 14:23:36 +0100 Subject: [PATCH 5/6] fix: move convert out of the __init__ function --- .../AwkwardPythonCallExt.jl | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index fe4b91d..eab0a4f 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -49,21 +49,22 @@ function AwkwardArray.convert(array::PyIterable{Any})::AwkwardArray.Content 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_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) + +function __init__() 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) From d6000ab09887874857c000211649492c361a6822 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Wed, 17 Jan 2024 14:31:22 +0100 Subject: [PATCH 6/6] test: add a test passing a Python array to a Julia function --- Project.toml | 2 +- test/runpytests.jl | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6f79a23..c78d433 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AwkwardArray" uuid = "7d259134-7f60-4bf1-aa00-7452e11bde56" authors = ["Jim Pivarski ", "Jerry Ling ", "and contributors"] -version = "0.1.3" +version = "0.1.2" [deps] JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" diff --git a/test/runpytests.jl b/test/runpytests.jl index e0ab499..fd65e29 100644 --- a/test/runpytests.jl +++ b/test/runpytests.jl @@ -30,3 +30,15 @@ end @test array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] end + +# Test passing Python array to Julia function +@testset "pass Python array to Julia test" begin + function f1(x) + x + end + + py_array = pyimport("awkward").Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]]) + + array = f1(py_array) + @test array isa AwkwardArray.ListOffsetArray +end