From 9f125f19b48b6f489efe0a869e94cbc3f9786a44 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 28 Nov 2023 13:20:16 +0100 Subject: [PATCH] fix: remove global constants (#46) * create julia_helpers.jl * a helper to convert Julia Awkward array to Python * type lock to a PrimitiveArray * fix: use np.asarray * convert only vectors of uint8 * use np.asarray instead * check if PyCall is installed * check PyCall installation * check for PyCall in Main? * move the import in * Update and rename julia_helpers.jl to AwkwardPyCall.jl * try reexport AwkwardArray * Update AwkwardPyCall.jl * test: add tests * fix: remove global constants * fix: cleanup * fix: add PyCall * fix: revert changes * fix: add AwkwardArray * fix: include AwkwardArray * fix: revert changes --- src/pycall/AwkwardPyCall.jl | 10 +++------- test/runpytests.jl | 8 ++++---- test/runtests.jl | 1 + 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/pycall/AwkwardPyCall.jl b/src/pycall/AwkwardPyCall.jl index a4e8f67..0654bec 100644 --- a/src/pycall/AwkwardPyCall.jl +++ b/src/pycall/AwkwardPyCall.jl @@ -1,15 +1,11 @@ module AwkwardPyCall - using PyCall using JSON using AwkwardArray -const ak = pyimport("awkward") -const np = pyimport("numpy") - function _as_numpy(array::AbstractVector{UInt8}) py_array = PyObject(array) - np.asarray(py_array, dtype = np.uint8) + pyimport("numpy").asarray(py_array, dtype = pyimport("numpy").uint8) end function julia_array_to_python(array) @@ -21,7 +17,7 @@ function julia_array_to_python(array) py_buffers[key] = _as_numpy(buffer) end - return ak.from_buffers(form, len, py_buffers) + return pyimport("awkward").from_buffers(form, len, py_buffers) end function _as_julia(py_buffer) @@ -30,7 +26,7 @@ function _as_julia(py_buffer) end function python_array_to_julia(py_array) - form, len, containers = ak.to_buffers(py_array) + form, len, containers = pyimport("awkward").to_buffers(py_array) julia_buffers = Dict{String,AbstractVector{UInt8}}() for (key, buffer) in containers diff --git a/test/runpytests.jl b/test/runpytests.jl index ad55339..8b21a3d 100644 --- a/test/runpytests.jl +++ b/test/runpytests.jl @@ -22,8 +22,6 @@ using JSON include("../src/pycall/AwkwardPyCall.jl") -import Main.AwkwardPyCall: ak - import Main.AwkwardPyCall: julia_array_to_python # Test julia_array_to_python function @@ -38,14 +36,16 @@ import Main.AwkwardPyCall: julia_array_to_python # Test case 2: Check if the awkward array has the correct layout py_array = julia_array_to_python(array) @test typeof(py_array) == PyObject - @test ak.to_list(py_array) == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] + @test pyimport("awkward").to_list(py_array) == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] + + end import Main.AwkwardPyCall: python_array_to_julia # Test python_array_to_julia function @testset "python_array_to_julia tests" begin - py_array = ak.Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]]) + py_array = pyimport("awkward").Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]]) # Test case 1: Check if the function returns an awkward array @test isa( diff --git a/test/runtests.jl b/test/runtests.jl index ff3dc20..7bfa93f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3166,4 +3166,5 @@ end @test df.x == AwkwardArray.to_vector(awt[:x]) @test df.y == AwkwardArray.to_vector(awt[:y]) end + end # @testset "AwkwardArray.jl"