diff --git a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl index 38bfde2..fed937f 100644 --- a/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl +++ b/ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl @@ -3,38 +3,33 @@ using PythonCall using JSON import AwkwardArray -function _as_numpy(array::AbstractVector{UInt8}) - np = pyimport("numpy") - np.asarray(array, dtype = np.uint8) -end - -function AwkwardArray.julia_array_to_python(array) - form, len, containers = AwkwardArray.to_buffers(array) +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] = _as_numpy(buffer) + py_buffers[key] = pyimport("numpy").asarray(buffer, dtype = pyimport("numpy").uint8) end - return pyimport("awkward").from_buffers(form, len, py_buffers) + pyimport("awkward").from_buffers(form, len, py_buffers) end -function _as_julia(py_buffer) - uint8_buffer = reinterpret(UInt8, py_buffer) - return uint8_buffer -end - -function AwkwardArray.python_array_to_julia(py_array) - form, len, _containers = pyimport("awkward").to_buffers(py_array) +function AwkwardArray.convert(array::Py)::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] = _as_julia(buffer) + julia_buffers[key] = reinterpret(UInt8, buffer) end - return AwkwardArray.from_buffers(pyconvert(String, form.to_json()), pyconvert(Int, len), julia_buffers) + AwkwardArray.from_buffers( + pyconvert(String, form.to_json()), + pyconvert(Int, len), + julia_buffers, + ) end end # module diff --git a/ext/AwkwardPythonCallExt/README.md b/ext/AwkwardPythonCallExt/README.md index 1f2f743..554bca3 100644 --- a/ext/AwkwardPythonCallExt/README.md +++ b/ext/AwkwardPythonCallExt/README.md @@ -1,22 +1,17 @@ -[PyCall](https://github.com/JuliaPy/PyCall.jl) is currently configured to use the Julia-specific Python distribution -installed by the [Conda.jl](https://github.com/JuliaPy/Conda.jl) package. - - conda create --name ak-julia - conda activate ak-julia - conda install -c conda-forge awkward +[PythonCall](https://github.com/JuliaPy/PythonCall.jl) is currently configured to use the Julia-specific Python distribution +installed by the [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) package. ```julia -using Conda -Conda.add("awkward") +using CondaPkg +CondaPkg.add("numpy") +CondaPkg.add("awkward") ``` ```julia -using PyCall +using PythonCall const ak = pyimport("awkward") -test() = println(ak.__version__) - -test() -2.4.6 +println(ak.__version__) +2.5.0 ``` diff --git a/src/AwkwardArray.jl b/src/AwkwardArray.jl index 4ebb220..e5eedd9 100644 --- a/src/AwkwardArray.jl +++ b/src/AwkwardArray.jl @@ -6,9 +6,7 @@ import Tables include("./all_implementations.jl") include("./tables.jl") -# stub for PyCall Extention -function julia_array_to_python end -function python_array_to_julia end - +# stub for PythonCall Extention +function convert end end # module AwkwardArray diff --git a/src/all_implementations.jl b/src/all_implementations.jl index 2b428e0..f5c9a57 100644 --- a/src/all_implementations.jl +++ b/src/all_implementations.jl @@ -3606,4 +3606,4 @@ function _to_buffers_index(IndexType::DataType) else error("unexpected INDEX type in to_buffers: $IndexType") end -end \ No newline at end of file +end diff --git a/test/runpytests.jl b/test/runpytests.jl index d503250..e0ab499 100644 --- a/test/runpytests.jl +++ b/test/runpytests.jl @@ -1,32 +1,31 @@ -using PythonCall -using AwkwardArray: julia_array_to_python +using PythonCall +using AwkwardArray: convert -# Test julia_array_to_python function -@testset "julia_array_to_python tests" begin +# Test convert Julia array to Python function +@testset "convert Julia array to Python tests" begin array = AwkwardArray.ListOffsetArray( [0, 3, 3, 5], AwkwardArray.PrimitiveArray([1.1, 2.2, 3.3, 4.4, 5.5]), ) + py_array = convert(array) + # Test case 1: Check if the function returns an awkward array - @test isa(julia_array_to_python(array), Py) + @test py_array isa Py # Test case 2: Check if the awkward array has the correct layout - py_array = julia_array_to_python(array) @test typeof(py_array) == Py + ak_array = pyconvert(Vector, pyimport("awkward").to_list(py_array)) @test ak_array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] - end -using AwkwardArray: python_array_to_julia - -# Test python_array_to_julia function -@testset "python_array_to_julia tests" begin +# Test convert Python array to Julia function +@testset "convert Python array to Julia tests" begin py_array = pyimport("awkward").Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]]) - array = python_array_to_julia(py_array) # Test case 1: Check if the function returns an awkward array + array = convert(py_array) @test array isa AwkwardArray.ListOffsetArray @test array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] diff --git a/test/runtests.jl b/test/runtests.jl index 626412c..25b2d87 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3170,4 +3170,4 @@ end end # @testset "AwkwardArray.jl" -include("./runpytests.jl") \ No newline at end of file +include("./runpytests.jl")