Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More type hinting for the codebase #265

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
docs: add type hints for contour
  • Loading branch information
Eoghan O'Connell committed Nov 26, 2024
commit 6f92494077aea36054ea8419779f1f273058fcc3
7 changes: 6 additions & 1 deletion dclab/features/bright.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,12 @@

def get_bright(mask: npt.NDArray[bool] | list[npt.NDArray[bool]],
image: npt.NDArray | list[npt.NDArray],
ret_data: str = "avg,sd") -> float | npt.NDArray:
ret_data: str = "avg,sd") -> (
float |
npt.NDArray |
tuple[float, float] |
tuple[npt.NDArray, npt.NDArray]
):
"""Compute avg and/or std of the event brightness

The event brightness is defined by the gray-scale values of the
7 changes: 6 additions & 1 deletion dclab/features/bright_bc.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,12 @@ def get_bright_bc(mask: npt.NDArray[bool] | list[npt.NDArray[bool]],
image: npt.NDArray | list[npt.NDArray],
image_bg: npt.NDArray | list[npt.NDArray],
bg_off: float | npt.NDArray = None,
ret_data: str = "avg,sd"):
ret_data: str = "avg,sd") -> (
float |
npt.NDArray |
tuple[float, float] |
tuple[npt.NDArray, npt.NDArray]
):
"""Compute avg and/or std of the background-corrected event brightness

The background-corrected event brightness is defined by the
6 changes: 4 additions & 2 deletions dclab/features/bright_perc.py
Original file line number Diff line number Diff line change
@@ -11,8 +11,10 @@
def get_bright_perc(mask: npt.NDArray[bool] | list[npt.NDArray[bool]],
image: npt.NDArray | list[npt.NDArray],
image_bg: npt.NDArray | list[npt.NDArray],
bg_off: float | npt.NDArray = None) -> \
tuple[float, float] | tuple[npt.NDArray, npt.NDArray]:
bg_off: float | npt.NDArray = None) -> (
tuple[float, float] |
tuple[npt.NDArray, npt.NDArray]
):
"""Compute 10th and 90th percentile of the bg-corrected event brightness

The background-corrected event brightness is defined by the
11 changes: 6 additions & 5 deletions dclab/features/contour.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ class NoValidContourFoundError(BaseException):


class LazyContourList(object):
def __init__(self, masks: npt.ArrayLike, max_events: int = 1000):
def __init__(self, masks: npt.ArrayLike, max_events: int = 1000) -> None:
"""A list-like object that computes contours upon indexing

Parameters
@@ -43,7 +43,7 @@ def __init__(self, masks: npt.ArrayLike, max_events: int = 1000):
self.identifier = str(masks[0][:].tobytes())
self.shape = len(masks), np.nan, 2

def __getitem__(self, idx):
def __getitem__(self, idx) -> npt.NDArray:
"""Compute contour(s) if not already in self.contours"""
if not isinstance(idx, numbers.Integral):
# slicing!
@@ -77,7 +77,7 @@ def __len__(self):
return len(self.masks)


def get_contour(mask):
def get_contour(mask: npt.NDArray[bool]) -> npt.NDArray | list[npt.NDArray]:
"""Compute the image contour from a mask

The contour is computed in a very inefficient way using scikit-image
@@ -130,7 +130,8 @@ def get_contour(mask):
return contours[0]


def get_contour_lazily(mask):
def get_contour_lazily(mask: npt.NDArray[bool]) -> \
npt.NDArray | LazyContourList:
"""Like :func:`get_contour`, but computes contours on demand

Parameters
@@ -156,7 +157,7 @@ def get_contour_lazily(mask):
return cont


def remove_duplicates(cont):
def remove_duplicates(cont: npt.NDArray) -> npt.NDArray:
"""Remove duplicates in a circular contour"""
x = np.resize(cont, (len(cont) + 1, 2))
selection = np.ones(len(x), dtype=bool)
Loading