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

Conversion issue using Interpolations #334

Open
mattborghi opened this issue Oct 28, 2019 · 1 comment
Open

Conversion issue using Interpolations #334

mattborghi opened this issue Oct 28, 2019 · 1 comment

Comments

@mattborghi
Copy link

Hello, I have been trying to use Interpolations.jl from Python but I noticed an issue when pyjulia tries to convert the Julia objects back to Python.

I will show you a toy example with what I have been trying to implement:

i ) First I load Interpolations.jl and create the data to be interpolated.

from julia import Main

Main.using('Interpolations')

Main.h_x = [1, 2, 3, 4]
Main.h_f = [1, 2, 3, 4]

# string to be evaluated along with the extrapolation
interpolation = "interpolate( (h_x, ), h_f, Gridded(Linear()))"

ii) Then, there are two ways of running the same code, first like this, creating interpolation object inside the evaluated string:

Main.eval(f"h = extrapolate( {interpolation}, Flat())")

Main.eval("typeof(h)")
# <PyCall.jlwrap Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{Int64,1}}},Gridded{Linear},Flat{Nothing}}>

type(Main.h)
#<class 'list'>

Main.eval("h(5)")
# 4.0 

iii) Or, like this, which which creates the object separately from the actual instruction:

Main.h2 = Main.eval(f"extrapolate({interpolation}, Flat())")

Main.eval("typeof(h2)")
# <PyCall.jlwrap Array{Float64,1}>

type(Main.h2)
# <class 'numpy.ndarray'>

Main.eval("h2(1.0)")
# Exception 'MethodError: objects of type Array{Float64,1} are not callable
# Use square brackets [] for indexing an Array.' occurred while calling julia code:
# h2(1.0)

Step ii works, whereas step iii doesn't. So my question is, if this behavior is some kind of bug in pyjulia, although the latter step is used in the pyjulia documentation.

As a corollary, I tried, without luck, to set a parameter to the eval("str to eval", get_results=[True|False]) method to avoid pyjulia from returning Python Objects. The idea behind this is to avoid surplus Jl/Py communications given that I need to run a set of commands in Jl but only use in Py the last result, i.e.,

Run a set of commands in Julia and set get_results=False

  • create an interpolation obj
  • make some calculations
  • make more evaluations

Finally the last command will return a useful value to be used again in Python (get_results=True)

  • Get a result and send it back to python

Do you have any suggestion?

@tkf
Copy link
Member

tkf commented Oct 28, 2019

If you want to invoke a Julia callable, the safest way to do it is to just define a Julia function. Maybe something like this:

h = Main.eval(f"""
let h = extrapolate( {interpolation}, Flat())
    x -> h(x)
end
""")

h(5)

if this behavior is some kind of bug in pyjulia, although the latter step is used in the pyjulia documentation

I wouldn't call it a bug, but I agree it can be improved. The issue is tracked by JuliaPy/PyCall.jl#617

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants