JuliaPy / JuliaPy/pyjulia

Conversion issue using Interpolations

Open
#334 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
893
Forks
101
PR merge metrics
No merged PRs in 30d

Description

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.

```julia
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)")
#

type(Main.h)
#

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)")
#

type(Main.h2)
#

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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.