Skip to content

Commit

Permalink
docs: add type hints for contour
Browse files Browse the repository at this point in the history
  • Loading branch information
Eoghan O'Connell committed Nov 26, 2024
1 parent 821b4b3 commit 6f92494
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
7 changes: 6 additions & 1 deletion dclab/features/bright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion dclab/features/bright_bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions dclab/features/bright_perc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions dclab/features/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 6f92494

Please sign in to comment.