-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from LilithHafner/lh/PyCall-extension
Add PyCall extension
- Loading branch information
Showing
6 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ jobs: | |
group: | ||
- Core | ||
- Downstream | ||
- Python | ||
version: | ||
- '1' | ||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module PyCallExt | ||
|
||
using PyCall: PyCall, PyObject, PyAny, pyfunctionret, pyimport, hasproperty | ||
using SciMLBase: SciMLBase, solve | ||
|
||
# SciML uses a function's arity (number of arguments) to determine if it operates in place. | ||
# PyCall does not preserve arity, so we inspect Python functions to find their arity. | ||
function SciMLBase.numargs(f::PyObject) | ||
inspect = pyimport("inspect") | ||
f2 = hasproperty(f, :py_func) ? f.py_func : f | ||
# if `f` is a bound method (i.e., `self.f`), `getfullargspec` includes | ||
# `self` in the `args` list. So, we subtract 1 in that case: | ||
length(first(inspect.getfullargspec(f2))) - inspect.ismethod(f2) | ||
end | ||
|
||
# differential equation solutions can be converted to lists, this tells PyCall not | ||
# to perform that conversion automatically when a solution is returned from `solve` | ||
PyCall.PyObject(::typeof(solve)) = pyfunctionret(solve, Any, Vararg{PyAny}) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[deps] | ||
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" | ||
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" | ||
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" | ||
|
||
[compat] | ||
OrdinaryDiffEq = "6.33" | ||
PyCall = "1.96" | ||
SciMLBase = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using PyCall, SciMLBase, OrdinaryDiffEq | ||
|
||
py""" # This is a mess because normal site-packages is not writeable in CI | ||
import subprocess, sys, site | ||
subprocess.run([sys.executable, '-m', 'pip', 'install', '--user', 'julia']) | ||
sys.path.append(site.getusersitepackages()) | ||
""" | ||
|
||
@testset "numargs" begin | ||
py""" | ||
def three_arg(a, b, c): | ||
return a + b + c | ||
def four_arg(a, b, c, d): | ||
return a + b + c + d | ||
class MyClass: | ||
def three_arg_method(self, a, b, c): | ||
return a + b + c | ||
def four_arg_method(self, a, b, c, d): | ||
return a + b + c + d | ||
""" | ||
|
||
@test SciMLBase.numargs(py"three_arg") === 3 | ||
@test SciMLBase.numargs(py"four_arg") === 4 | ||
x = py"MyClass()" | ||
@test SciMLBase.numargs(x.three_arg_method) === 3 | ||
@test SciMLBase.numargs(x.four_arg_method) === 4 | ||
end | ||
|
||
@testset "solution handling" begin | ||
py""" | ||
from julia import OrdinaryDiffEq as ode | ||
def f(u,p,t): | ||
return -u | ||
u0 = 0.5 | ||
tspan = (0., 1.) | ||
prob = ode.ODEProblem(f, u0, tspan) | ||
sol = ode.solve(prob, ode.Tsit5()) | ||
""" | ||
@test py"sol" isa ODESolution | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters