Skip to content

Commit

Permalink
add tests and extension boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed Oct 5, 2023
1 parent be6174a commit a2fdd5e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ Manifest.toml
# vscode stuff
.vscode
.vscode/*

# python extensions
.CondaPkg
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[weakdeps]
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[extensions]
PyCallExt = "PyCall"
PythonCallExt = "PythonCall"
ZygoteExt = "Zygote"

[compat]
Expand Down Expand Up @@ -72,11 +74,12 @@ DelayDiffEq = "bcd4f6db-9728-5f36-b5f7-82caef46ccdb"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Pkg", "PyCall", "SafeTestsets", "Test", "StaticArrays", "StochasticDiffEq", "Aqua", "Zygote"]
test = ["Pkg", "PyCall", "PythonCall", "SafeTestsets", "Test", "StaticArrays", "StochasticDiffEq", "Aqua", "Zygote"]
4 changes: 4 additions & 0 deletions test/python/Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
[deps]
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"

[compat]
DifferentialEquations = "7.11"
OrdinaryDiffEq = "6.33"
PyCall = "1.96"
PythonCall = "0.9.14"
SciMLBase = "2"
69 changes: 69 additions & 0 deletions test/python/pythoncall.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using DifferentialEquations, PythonCall

@testset "Use of DifferentialEquations through PythonCall with user code written in Python" begin
pyexec("""
from juliacall import Main
de = Main.seval("DifferentialEquations")
def f(u,p,t):
return -u
u0 = 0.5
tspan = (0., 1.)
prob = de.ODEProblem(f, u0, tspan)
sol = de.solve(prob)
""", @__MODULE__)
@test pyconvert(Any, pyeval("sol", @__MODULE__)) isa ODESolution

pyexec("""
def f(u,p,t):
x, y, z = u
sigma, rho, beta = p
return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]
u0 = [1.0,0.0,0.0]
tspan = (0., 100.)
p = [10.0,28.0,8/3]
prob = de.ODEProblem(f, u0, tspan, p)
sol = de.solve(prob,saveat=0.01)
""", @__MODULE__)
@test pyconvert(Any, pyeval("sol", @__MODULE__)) isa ODESolution

# TODO: test the types and shapes of sol.t and de.transpose(de.stack(sol.u)) but don't actually plot them in CI
# pyexec("""
# import matplotlib.pyplot as plt

# plt.plot(sol.t, de.transpose(de.stack(sol.u))) # :( fails without the conversion
# plt.show()
# """, @__MODULE__)

@pyexec """
jul_f = Main.seval(""\"
function f(du,u,p,t)
x, y, z = u
sigma, rho, beta = p
du[1] = sigma * (y - x)
du[2] = x * (rho - z) - y
du[3] = x * y - beta * z
end""\")
u0 = [1.0,0.0,0.0]
tspan = (0., 100.)
p = [10.0,28.0,2.66]
prob = de.ODEProblem(jul_f, u0, tspan, p)
sol = de.solve(prob)
"""
@test pyconvert(Any, pyeval("sol", @__MODULE__)) isa ODESolution

pyexec("""
def f(u,p,t):
return 1.01*u
def g(u,p,t):
return 0.87*u
u0 = 0.5
tspan = (0.0,1.0)
prob = de.SDEProblem(f,g,u0,tspan)
sol = de.solve(prob,reltol=1e-3,abstol=1e-3)
""", @__MODULE__)
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@ end
@time @safetestset "PyCall" begin
include("python/pycall.jl")
end
@time @safetestset "PythonCall" begin
include("python/pythoncall.jl")
end
end
end

0 comments on commit a2fdd5e

Please sign in to comment.