ICB-DCM / pyABC

distributed, likelihood-free inference
https://pyabc.rtfd.io
BSD 3-Clause "New" or "Revised" License
206 stars 44 forks source link

Unable to evaluate ODEProblems by pyabc's julia interface #638

Open MG-dkfz opened 6 months ago

MG-dkfz commented 6 months ago

Bug description I was testing pyabc for models defined in Julia, and I was able to reproduce the example of the SIR model, i.e., using Gillespie's algorithm. That is, I could pre-compile the Julia code, evaluate the model from python, and then follow the remaining steps in the notebook to perform parameter inference. As a next step, I modified the SIR example into an ODE problem (see minimal example below). While I can pre-compile the Julia code and import the model function into python, I get a RuntimeError when I call the model from python (if I call the model function from Julia, everything is fine). This error has something to do with PyCall, and might therefore not be directly linked to pyabc. But I was wondering if there is some way to evaluate ODE models in Julia when using pyabc.

Expected behavior Being able to work with ODE models in Julia when using pyabc.

To reproduce In Julia:

# SIR.jl

module SIR

using DifferentialEquations

function sir_model!(du, u, p, t)
    r1, r2 = p
    du[1] = -r1 * u[1] * u[2]
    du[2] = (r1 * u[1] - r2) * u[2]
    du[3] = r2 * u[2]
end

# ground truth parameter
p = (0.0001, 0.01)
# initial state
u0 = [999, 1, 0]
# time span
tspan = (0.0, 250.0)

"""
Simulate model for parameters `10.0.^par`.
"""
function model(par)
    p = 10.0.^((par["p1"], par["p2"]))
    prob = ODEProblem(sir_model!, u0, tspan, p)
    sol = solve(prob, saveat=2.5)
    return Dict("t"=>sol.t, "u"=>sol.u)
end

end  # module

In Python:

from julia.api import Julia
jl = Julia(compiled_modules=False)
from pyabc.external.julia import Julia

jl = Julia(module_name="SIR", source_file="SIR.jl")

model = jl.model()
model({"p1": -4.0, "p2": -2.0}) # <- this throws a RuntimeError 

Environment Python: python 3.12, pyabc 0.12.13, julia 0.6.2

Julia: Julia 1.9.3 DifferentialEquations 7.11.0 PyCall 1.96.4

stephanmg commented 6 months ago

@MG-dkfz could you please provide the actual error message?

Update: Fails for me withTypeError: __init__() got an unexpected keyword argument 'compiled_modules'