-
Notifications
You must be signed in to change notification settings - Fork 11
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 #17 from tlambert03/fix-lib-discovery
General cleanup around library wrap
- Loading branch information
Showing
18 changed files
with
819 additions
and
847 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,78 @@ | ||
name: test and deploy | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
tags: | ||
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10 | ||
pull_request: | ||
branches: | ||
- master | ||
workflow_dispatch: | ||
|
||
jobs: | ||
# Won't work without GPU | ||
|
||
# test: | ||
# name: ${{ matrix.platform }} (${{ matrix.python-version }}) | ||
# runs-on: ${{ matrix.platform }} | ||
# defaults: | ||
# run: | ||
# shell: bash -l {0} | ||
# strategy: | ||
# fail-fast: false | ||
# matrix: | ||
# python-version: [3.7, 3.8, 3.9] | ||
# platform: [ubuntu-latest, windows-latest] | ||
# steps: | ||
# - uses: actions/checkout@v2 | ||
|
||
# - uses: conda-incubator/setup-miniconda@v2 | ||
# with: | ||
# mamba-version: "*" | ||
# channels: conda-forge,defaults | ||
# channel-priority: true | ||
# python-version: ${{ matrix.python-version }} | ||
|
||
# - name: Print info | ||
# run: | | ||
# conda info | ||
# conda list | ||
# conda config --show-sources | ||
# conda config --show | ||
# printenv | sort | ||
|
||
# - name: Install | ||
# run: | | ||
# mamba install cudadecon>=0.0.11 pytest | ||
# pip install -e . | ||
|
||
# - name: Test | ||
# run: pytest | ||
|
||
# - name: Coverage | ||
# uses: codecov/codecov-action@v1 | ||
|
||
deploy: | ||
# needs: test | ||
runs-on: ubuntu-latest | ||
if: ${{ github.repository == 'tlambert03/pycudadecon' && contains(github.ref, 'tags') }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: "3.x" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -U setuptools setuptools_scm wheel twine | ||
- name: Build and publish | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.TWINE_API_KEY }} | ||
run: | | ||
git tag | ||
python setup.py sdist bdist_wheel | ||
twine upload dist/* |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
from .version import __version__ | ||
|
||
from .deconvolution import decon | ||
from .otf import make_otf, TemporaryOTF | ||
from .libcudawrapper import RLContext, rl_decon, rl_init, rl_cleanup | ||
from ._libwrap import RL_cleanup as rl_cleanup | ||
from .affine import affineGPU, deskewGPU, rotateGPU | ||
from .deconvolution import RLContext, decon, rl_decon, rl_init | ||
from .otf import TemporaryOTF, make_otf | ||
|
||
__version__ = "0.1.2" | ||
|
||
__all__ = ( | ||
decon, | ||
TemporaryOTF, | ||
RLContext, | ||
rl_decon, | ||
rl_init, | ||
rl_cleanup, | ||
affineGPU, | ||
deskewGPU, | ||
rotateGPU, | ||
make_otf, | ||
TemporaryOTF, | ||
) | ||
__all__ = [ | ||
"decon", | ||
"TemporaryOTF", | ||
"RLContext", | ||
"rl_decon", | ||
"rl_init", | ||
"rl_cleanup", | ||
"affineGPU", | ||
"deskewGPU", | ||
"rotateGPU", | ||
"make_otf", | ||
"TemporaryOTF", | ||
] |
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,72 @@ | ||
import ctypes | ||
import functools | ||
import os | ||
from ctypes.util import find_library | ||
from inspect import Parameter, signature | ||
from typing import Callable, Optional, Type | ||
|
||
import numpy as np | ||
from typing_extensions import Annotated, get_args, get_origin | ||
|
||
|
||
class Library: | ||
def __init__(self, name: str): | ||
self.name = name | ||
|
||
_file = name | ||
if not _file or not os.path.exists(_file): | ||
_file = find_library(name.replace("lib", "", 1)) | ||
if not _file or not os.path.exists(_file): | ||
_file = find_library(name) | ||
|
||
self.lib = ctypes.CDLL(_file) | ||
if not self.lib._name: | ||
raise FileNotFoundError(f"Unable to find library: {self.name}") | ||
|
||
def function(self, func: Callable) -> Callable: | ||
func_c = getattr(self.lib, func.__name__) | ||
|
||
sig = signature(func) | ||
func_c.restype = cast_type(sig.return_annotation) | ||
func_c.argtypes = [cast_type(p.annotation) for p in sig.parameters.values()] | ||
|
||
class CTypesFunction: | ||
def __init__(self, func): | ||
self._func = func | ||
functools.update_wrapper(self, func) | ||
|
||
@property | ||
def __signature__(self): | ||
return sig | ||
|
||
def __call__(self, *args, **kw): | ||
return self._func(*args, **kw) | ||
|
||
def __repr__(_self): | ||
return ( | ||
f"<CTypesFunction: {os.path.basename(self.name)}.{func.__name__}>" | ||
) | ||
|
||
return CTypesFunction(func_c) | ||
|
||
|
||
def cast_type(hint: Type) -> Optional[Type]: | ||
|
||
if isinstance(hint, str): | ||
raise ValueError("forward ref typehints not supported") | ||
|
||
if get_origin(hint) is Annotated: | ||
args = get_args(hint) | ||
if args and args[0] is np.ndarray: | ||
c_type = np.ctypeslib.as_ctypes_type(np.dtype(args[1])) | ||
return np.ctypeslib.ndpointer(c_type, flags="C_CONTIGUOUS") | ||
|
||
return { | ||
None: None, | ||
Parameter.empty: None, | ||
bool: ctypes.c_bool, | ||
float: ctypes.c_float, | ||
int: ctypes.c_int, | ||
str: ctypes.c_char_p, | ||
np.ndarray: np.ctypeslib.ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"), | ||
}[hint] |
Oops, something went wrong.