-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: add prepare_u0 to all problems (currently just ODE and SDE) TODO: add tests TODO: add package extension boilerplate (e.g. update Project.toml)
- Loading branch information
Lilith Hafner
authored and
Lilith Hafner
committed
Oct 3, 2023
1 parent
75b1925
commit ff257cf
Showing
5 changed files
with
143 additions
and
53 deletions.
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module PythonCallExt | ||
|
||
using PythonCall: Py, PyList, pyimport, hasproperty, pyconvert, pyisinstance, pybuiltins | ||
using SciMLBase: SciMLBase | ||
|
||
# SciML uses a function's arity (number of arguments) to determine if it operates in place. | ||
# PythonCall does not preserve arity, so we inspect Python functions to find their arity. | ||
function SciMLBase.numargs(f::Py) | ||
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: | ||
pyconvert(Int, length(first(inspect.getfullargspec(f2))) - inspect.ismethod(f2)) | ||
end | ||
|
||
_pyconvert(x::Py) = pyisinstance(x, pybuiltins.list) ? [_pyconvert(x) for x in x] : pyconvert(Any, x) | ||
_pyconvert(x::PyList) = [_pyconvert(x) for x in x] | ||
_pyconvert(x) = x | ||
|
||
SciMLBase.prepare_initial_state(u0::Union{Py, PyList}) = _pyconvert(u0) | ||
SciMLBase.prepare_function(f::Py) = _pyconvert ∘ f | ||
|
||
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
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
Oops, something went wrong.