2

I have imported scipy.interpolate.Rbf from python to Julia 1.6 using PyCall. It works, but when I am trying to assign the radial function value to multiquadric it doesn't allow me to do so, due to syntax issue. For example:

using PyCall
interpolate = pyimport("scipy.interpolate")

x = [1,2,3,4,5]
y = [1,2,3,4,5]
z = [1,2,3,4,5]

rbf = interpolate.Rbf(x,y,z, function='multiquadric', smoothness=1.0)

for the above example I am getting this error:

LoadError: syntax: unexpected "="
Stacktrace:
 [1] top-level scope

This is due to the use of function variable. May I know what can be the way around this error, so that i can assign the multiquadric radial for the rbf.

Look forward to the suggestions.

Thanks!!

Mohammad Saad
  • 1,935
  • 10
  • 28

1 Answers1

2

Your problem is that function is a reserved keyword in Julia. Additionally, note that 'string' for strings is OK in Python but in Julia you have "string".

The easiest workaround in your case is py"" string macro:

julia> py"$interpolate.Rbf($x,$y,$z, function='multiquadric', smoothness=1.0)"
PyObject <scipy.interpolate.rbf.Rbf object at 0x000000006424F0A0>

Now suppose you actually need at some point to pass function= parameter? It is not easy because function is a reserved keyword in Julia. What you could do is to pack it into a named tuple using Symbol notation:

myparam = NamedTuple{(:function,)}(("multiquadric",))

Now if you do:

some_f(x,y; myparam...)

than myparam would get unpacked to a keyword parameter.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • thanks for the suggestion!! the first approach worked for me. But I noticed the results from the command `py"$interpolate.Rbf($x,$y,$z, function='multiquadric', smoothness=1.0)"` are not same in python and julia. Any idea why does this happen? – Mohammad Saad May 31 '21 at 06:27
  • the tuple idea is really amazing, I am still confused a bit on the syntax implementation will give it a try and update you on my findings! thanks for the suggestion. – Mohammad Saad May 31 '21 at 06:28
  • 1
    I have not used this library so this is hard me to say. Perhaps you need to check (in source code of this `Rbf` function) what is exactly expected as `x,y,z`. It might rely on some duck-typing rule that works less perfect when a Julia object is passed. – Przemyslaw Szufel May 31 '21 at 08:25
  • I tried the `Named tuple` approach, I would like to suggest a syntax correction in the unpacking function :-> `some_f(x,y; myparam...)` Thanks again for this amazing suggestion!! – Mohammad Saad Jun 02 '21 at 06:10
  • 1
    Thanks - changed comma to semicolon. – Przemyslaw Szufel Jun 02 '21 at 11:20