Skip to content

Commit

Permalink
ci: update typing, pre-commit, and add all kwargs to decon func (#48)
Browse files Browse the repository at this point in the history
* update typing

* add all args

* style(pre-commit.ci): auto fixes [...]

* more typing

* fix mypy

* style(pre-commit.ci): auto fixes [...]

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
tlambert03 and pre-commit-ci[bot] committed Sep 13, 2023
1 parent a2e6cbb commit cf9bbf0
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 72 deletions.
24 changes: 12 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ default_install_hook_types: [pre-commit, commit-msg]

repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.262
rev: v0.0.289
hooks:
- id: ruff
args: [--fix]

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.9.1
hooks:
- id: black

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.12.2
rev: v0.14
hooks:
- id: validate-pyproject

# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.2.0
# hooks:
# - id: mypy
# files: "^src/"
# # # you have to add the things you want to type check against here
# additional_dependencies:
# - numpy
# - typing_extensions
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
files: "^src/"
# # you have to add the things you want to type check against here
additional_dependencies:
- numpy
- typing_extensions
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
requires-python = ">=3.7"
license = { text = "MIT" }
keywords = ["deconvolution", "microscopy", "CUDA"]
authors = [{ email = "talley.lambert@gmail.com" }, { name = "Talley Lambert" }]
authors = [{ email = "talley.lambert@gmail.com", name = "Talley Lambert" }]
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: GPU :: NVIDIA CUDA",
Expand Down
6 changes: 3 additions & 3 deletions src/pycudadecon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
from importlib.metadata import PackageNotFoundError, version
except ImportError:
from importlib_metadata import PackageNotFoundError, version
from importlib_metadata import PackageNotFoundError, version # type: ignore

try:
__version__ = version("pycudadecon")
Expand All @@ -15,7 +15,7 @@


try:
from . import _libwrap as lib # noqa: F811
from . import _libwrap as lib
except FileNotFoundError as e:
import warnings

Expand All @@ -26,7 +26,7 @@ class _stub:
def __getattr__(self, name):
raise t

lib = _stub()
lib = _stub() # type: ignore


from .affine import affineGPU, deskewGPU, rotateGPU
Expand Down
15 changes: 12 additions & 3 deletions src/pycudadecon/_ctyped.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import os
from ctypes.util import find_library
from inspect import Parameter, signature
from typing import Callable, Optional, Tuple, Type
from typing import TYPE_CHECKING, Callable, Optional, Tuple, Type

import numpy as np
from typing_extensions import Annotated, get_args, get_origin

if TYPE_CHECKING:
from typing import TypeVar

from typing_extensions import ParamSpec

P = ParamSpec("P")
R = TypeVar("R")


class Library:
def __init__(self, name: str, version: Tuple[int, ...] = (0, 0, 0)):
Expand All @@ -24,7 +32,7 @@ def __init__(self, name: str, version: Tuple[int, ...] = (0, 0, 0)):
if not self.lib._name:
raise FileNotFoundError(f"Unable to find library: {self.name}")

def function(self, func: Callable) -> Callable:
def function(self, func: "Callable[P, R]") -> "Callable[P, R]":
func_c = getattr(self.lib, func.__name__)

sig = signature(func)
Expand All @@ -40,7 +48,7 @@ def __init__(self, func):
def __signature__(self):
return sig

def __call__(self, *args, **kw):
def __call__(self, *args: "P.args", **kw: "P.kwargs") -> "R":
return self._func(*args, **kw)

def __repr__(_self):
Expand Down Expand Up @@ -68,5 +76,6 @@ def cast_type(hint: Type) -> Optional[Type]:
float: ctypes.c_float,
int: ctypes.c_int,
str: ctypes.c_char_p,
bytes: ctypes.c_char_p,
np.ndarray: np.ctypeslib.ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
}[hint]
35 changes: 19 additions & 16 deletions src/pycudadecon/_libwrap.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from pathlib import Path
from typing import Tuple

import numpy as np
from typing_extensions import Annotated

from ._ctyped import Library
from typing import Tuple

# FIXME: ugly... we should export version better from cudadecon
_cudadecon_version: Tuple[int, ...] = (0, 0, 0)
_conda_prefix = os.getenv("CONDA_PREFIX")
Expand All @@ -32,12 +33,14 @@


@lib.function
def camcor_interface_init(nx: int, ny: int, nz: int, camparam: np.ndarray) -> int:
def camcor_interface_init( # type: ignore [empty-body]
nx: int, ny: int, nz: int, camparam: np.ndarray
) -> int:
"""Setup for camera corrections."""


@lib.function
def camcor_interface(
def camcor_interface( # type: ignore [empty-body]
raw_data: ndarray_uint16, nx: int, ny: int, nz: int, result: ndarray_uint16
) -> int:
"""Execute camera corrections."""
Expand All @@ -46,7 +49,7 @@ def camcor_interface(
if _cudadecon_version < (0, 6):

@lib.function
def RL_interface_init(
def RL_interface_init( # type: ignore [empty-body]
nx: int,
ny: int,
nz: int,
Expand Down Expand Up @@ -75,10 +78,10 @@ def RL_interface_init(
OTF_file_name: file name of OTF
"""

else: # include "bSkewDecon" arguement
else: # include "bSkewDecon" arguement

@lib.function
def RL_interface_init(
def RL_interface_init( # type: ignore [empty-body]
nx: int,
ny: int,
nz: int,
Expand Down Expand Up @@ -113,7 +116,7 @@ def RL_interface_init(
if _cudadecon_version < (0, 6):

@lib.function
def RL_interface(
def RL_interface( # type: ignore [empty-body]
raw_data: ndarray_uint16,
nx: int,
ny: int,
Expand All @@ -135,7 +138,7 @@ def RL_interface(
else:

@lib.function
def RL_interface(
def RL_interface( # type: ignore [empty-body]
raw_data: ndarray_uint16,
nx: int,
ny: int,
Expand All @@ -160,17 +163,17 @@ def RL_interface(
# retrieve the post-deskewed image dimensions
# can be used to allocate result buffer before calling RL_interface()
@lib.function
def get_output_nx() -> int:
def get_output_nx() -> int: # type: ignore [empty-body]
...


@lib.function
def get_output_ny() -> int:
def get_output_ny() -> int: # type: ignore [empty-body]
...


@lib.function
def get_output_nz() -> int:
def get_output_nz() -> int: # type: ignore [empty-body]
...


Expand All @@ -197,7 +200,7 @@ def cuda_reset() -> None:


@lib.function
def Deskew_interface(
def Deskew_interface( # type: ignore [empty-body]
raw_data: np.ndarray,
nx: int,
ny: int,
Expand All @@ -214,7 +217,7 @@ def Deskew_interface(


@lib.function
def Affine_interface(
def Affine_interface( # type: ignore [empty-body]
raw_data: np.ndarray,
nx: int,
ny: int,
Expand All @@ -226,7 +229,7 @@ def Affine_interface(


@lib.function
def Affine_interface_RA(
def Affine_interface_RA( # type: ignore [empty-body]
raw_data: np.ndarray,
nx: int,
ny: int,
Expand All @@ -251,8 +254,8 @@ def Affine_interface_RA(

@otf_lib.function
def makeOTF(
ifiles: str,
ofiles: str,
ifiles: bytes,
ofiles: bytes,
lambdanm: int = 520,
dz: float = 0.102,
interpkr: int = 10,
Expand Down
4 changes: 2 additions & 2 deletions src/pycudadecon/affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ def affineGPU(
# have to calculate this here to know the size of the return array
result = np.empty((nz, ny, nx), dtype=np.float32)
if isinstance(dzyx, (tuple, list)) and len(dzyx) == 3:
_dzyx = tuple(float(i) for i in dzyx[::-1])
dx, dy, dz = (float(i) for i in dzyx[::-1])
# note, dzyx coordinate order is flipped when handing to Affine_interface_RA
lib.Affine_interface_RA(im, nx, ny, nz, *_dzyx, result, tmat)
lib.Affine_interface_RA(im, nx, ny, nz, dx, dy, dz, result, tmat)
else:
lib.Affine_interface(im, nx, ny, nz, result, tmat)
return result
Expand Down
Loading

0 comments on commit cf9bbf0

Please sign in to comment.