-
-
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.
Merge pull request #519 from LilithHafner/lh/PythonCall-extension
Add PythonCall Extension
- Loading branch information
Showing
19 changed files
with
267 additions
and
74 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 |
---|---|---|
|
@@ -4,3 +4,6 @@ Manifest.toml | |
# vscode stuff | ||
.vscode | ||
.vscode/* | ||
|
||
# python extensions | ||
.CondaPkg |
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,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
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
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
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
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
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.