Python Callbacks using pyjulia

167 views
Skip to first unread message

Frank Hellmann

unread,
Oct 19, 2016, 8:44:15 AM10/19/16
to julia-users
Hello,
I am evaluating using Julia for implementing some algorithms we want to use. As a first usecase we wanted to try using the DASSL solver in julia on a python function. Unfortunately we get the error below. I don't really know how to start debugging this problem, any pointers would be deeply appreciated.

Best,
Frank

======

$ python dassl-from-python.py

Traceback (most recent call last):
  File "dassl-from-python.py", line 8, in <module>
    r = j.dasslSolve(f, 1., [0., 10.])
RuntimeError: Julia exception: MethodError(DASSL.#dasslStep,(Any[:dy0,[0.0],:tstop,10.0],DASSL.dasslStep,PyObject <function <lambda> at 0x101a69500>,[1.0],0.0))
dassl-from-python.py

Frank Hellmann

unread,
Oct 19, 2016, 10:02:55 AM10/19/16
to julia-users
The python script looks like this (sorry, thought attachments would be inlined):


import julia
 
j = julia.Julia()
 
j.using("DASSL")
 
f = lambda t, x, dx: x - dx

Steven G. Johnson

unread,
Oct 19, 2016, 10:21:05 AM10/19/16
to julia-users
It looks like this is a problem in the DASSL.jl package.  They made the common mistake of over-specifying the types of their arguments, and in particular they require you to pass a Julia ::Function argument for the equations to be solved, rather than any callable object (here, the Python function is passed as a callable ::PyObject).

cdm

unread,
Oct 19, 2016, 10:27:11 AM10/19/16
to julia-users

any luck with replicating the example from the README at


cdm

unread,
Oct 19, 2016, 10:42:53 AM10/19/16
to julia-users

it seems a near term work around could be had through PyCall ... ?

Steven G. Johnson

unread,
Oct 19, 2016, 1:17:23 PM10/19/16
to julia-users
A workaround from pyjulia might be to create a Julia function that can wrap a Julia Function around a Python function

fwrap = j.eval('f -> (args...) -> f(args...)')

and then do

j.dasslSolve(fwrap(f), 1.0, [0.0, 10.0])

Not super elegant, but occasionally inter-language calling requires this kind of "type massaging".  (We occasionally have to do a similar thing in PyCall when passing Julia functions to Python, since some Python code only works with "native" Python functions.)

Frank Hellmann

unread,
Oct 20, 2016, 5:41:59 AM10/20/16
to julia-users
Thanks a lot for your help! The workaround works and more importantly I understand what happened now. Unfortunately now I get a massive stack trace and a segmentation fault... I guess I'll take that over to the DASSL developers.
julia-error.txt
Reply all
Reply to author
Forward
0 new messages