Skip to content

Commit

Permalink
fix: update function type hints from np.array to np.ndarray for consi…
Browse files Browse the repository at this point in the history
…stency
  • Loading branch information
dummyindex committed Oct 10, 2024
1 parent 7a2a356 commit 9ee3107
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
4 changes: 2 additions & 2 deletions livecellx/core/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def get_img_by_idx(self, idx):
img = self.read_img_url_func(url)
return img

def get_img_by_time(self, time) -> np.array:
def get_img_by_time(self, time) -> np.ndarray:
"""Get an image by time"""
return self.read_img_url_func(self.time2url[time])

Expand Down Expand Up @@ -473,7 +473,7 @@ def __init__(self, img, name=None, ext=".png", in_memory=True):
def read_single_img_from_mem(self, url):
return self.img.copy()

def get_img_by_time(self, time=None) -> np.array:
def get_img_by_time(self, time=None) -> np.ndarray:
return self.read_single_img_from_mem(self.url)

def get_img_by_idx(self, idx=None):
Expand Down
22 changes: 11 additions & 11 deletions livecellx/core/single_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(
----------
timeframe : int
_description_
bbox : np.array, optional
bbox : np.ndarray, optional
[x1, y1, x2, y2], by default None
follwoing skimage convention: "Bounding box (min_row, min_col, max_row, max_col). Pixels belonging to the bounding box are in the half-open interval [min_row; max_row) and [min_col; max_col)."
regionprops : RegionProperties, optional
Expand Down Expand Up @@ -255,7 +255,7 @@ def compute_iomin(self, other_cell: "SingleCellStatic", bbox=None):
def update_regionprops(self):
self.regionprops = self.compute_regionprops()

def get_contour(self) -> np.array:
def get_contour(self) -> np.ndarray:
return np.copy(self.contour)

def get_img(self):
Expand All @@ -278,7 +278,7 @@ def get_label_mask(self, dtype=int):
mask = self.get_mask(dtype=dtype)
return mask

def get_bbox(self, padding=None) -> np.array:
def get_bbox(self, padding=None) -> np.ndarray:
# TODO: add unit test for this function
if self.bbox is None:
self.update_bbox()
Expand Down Expand Up @@ -390,7 +390,7 @@ def update_contour(self, contour, update_bbox=True, update_mask_dataset=True, dt
if update_bbox:
self.bbox = self.get_bbox_from_contour(self.contour)

def update_sc_mask_by_crop(self, mask, padding_pixels=np.zeros(2, dtype=int), bbox: np.array = None):
def update_sc_mask_by_crop(self, mask, padding_pixels=np.zeros(2, dtype=int), bbox: np.ndarray = None):
"""
Updates the single cell mask by cropping the input mask to the bounding box of the single cell and updating the contour.
Expand Down Expand Up @@ -684,7 +684,7 @@ def get_bbox_on_crop(self, bbox=None, padding=0):
contours = self.get_contour_coords_on_crop(bbox=bbox, padding=padding)
return self.get_bbox_from_contour(contours)

def get_contour_coords_on_img_crop(self, padding=0) -> np.array:
def get_contour_coords_on_img_crop(self, padding=0) -> np.ndarray:
"""
A utility function to calculate pixel coord in image crop's coordinate system
to draw contours on an image crop.
Expand All @@ -703,7 +703,7 @@ def get_contour_coords_on_img_crop(self, padding=0) -> np.array:
ys = self.contour[:, 1] - max(0, self.bbox[1] - padding)
return np.array([xs, ys]).T

def get_contour_mask_closed_form(self, padding=0, crop=True) -> np.array:
def get_contour_mask_closed_form(self, padding=0, crop=True) -> np.ndarray:
"""If contour points are pixel-wise closed, use this function to fill the contour."""
import scipy.ndimage as ndimage

Expand All @@ -729,7 +729,7 @@ def get_bbox_from_contour(contour, dtype=int):
@staticmethod
def gen_contour_mask(
contour, img=None, shape=None, bbox=None, padding=0, crop=True, mask_val=255, dtype=bool
) -> np.array: #
) -> np.ndarray: #
# TODO: optimize: we do not need img here but shape of img.
from PIL import Image, ImageDraw
import numpy as np
Expand Down Expand Up @@ -768,7 +768,7 @@ def gen_contour_mask(
@staticmethod
def gen_contour_mask_skimage_deprecated(
contour, img=None, shape=None, bbox=None, padding=0, crop=True, mask_val=255, dtype=bool
) -> np.array: #
) -> np.ndarray: #
# TODO: optimize: we do not need img here but shape of img.
from skimage.draw import line, polygon

Expand All @@ -790,7 +790,7 @@ def gen_contour_mask_skimage_deprecated(
res_mask = SingleCellStatic.gen_skimage_bbox_img_crop(bbox, res_mask, padding=padding)
return res_mask

def get_contour_mask(self, padding=0, crop=True, bbox=None, dtype=bool) -> np.array:
def get_contour_mask(self, padding=0, crop=True, bbox=None, dtype=bool) -> np.ndarray:
hash_key: Tuple = ("contour_mask", padding, crop, tuple(bbox if bbox is not None else [-1]))
if self.enable_cache_contour_mask and hash_key in self.cache:
return self.cache[hash_key]
Expand All @@ -802,7 +802,7 @@ def get_contour_mask(self, padding=0, crop=True, bbox=None, dtype=bool) -> np.ar
self.cache[hash_key] = res
return res

def get_contour_label_mask(self, padding=0, crop=True, bbox=None, dtype=int) -> np.array:
def get_contour_label_mask(self, padding=0, crop=True, bbox=None, dtype=int) -> np.ndarray:
hash_key: Tuple = ("contour_mask", padding, crop, tuple(bbox if bbox is not None else [-1]))
if self.enable_cache_contour_mask and hash_key in self.cache:
return self.cache[hash_key]
Expand All @@ -814,7 +814,7 @@ def get_contour_label_mask(self, padding=0, crop=True, bbox=None, dtype=int) ->
self.cache[hash_key] = res
return res

def get_contour_img(self, crop=True, bg_val=0, **kwargs) -> np.array:
def get_contour_img(self, crop=True, bg_val=0, **kwargs) -> np.ndarray:
"""return a contour image with out of self cell region set to background_val"""

# TODO: filter kwargs for contour mask case. (currently using the same kwargs as self.gen_skimage_bbox_img_crop)
Expand Down
2 changes: 1 addition & 1 deletion livecellx/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def rgb_img_to_gray(img):
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)


def get_cv2_bbox(label_mask: np.array):
def get_cv2_bbox(label_mask: np.ndarray):
"""generate cv2 style bounding box from a label mask
Parameters
Expand Down
7 changes: 4 additions & 3 deletions livecellx/model_zoo/segmentation/sc_correction_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(

self.force_no_edt_aug = force_no_edt_aug

def get_raw_seg(self, idx) -> np.array:
def get_raw_seg(self, idx) -> np.ndarray:
return np.array(Image.open(self.raw_seg_paths[idx]))

def get_scale(self, idx):
Expand All @@ -145,7 +145,8 @@ def get_subdir(self, idx):
return self.subdirs.iloc[idx]
return self.subdirs[idx]

def label_mask_to_edt(label_mask: np.array, bg_val=0):
@staticmethod
def label_mask_to_edt(label_mask: np.ndarray, bg_val=0):
label_mask = label_mask.astype(np.uint8)
labels = set(np.unique(label_mask))
labels.remove(bg_val)
Expand Down Expand Up @@ -331,7 +332,7 @@ def get_paths(self, idx):
"aug_diff_img": self.aug_diff_img_paths[idx] if self.aug_diff_img_paths else None,
}

def get_gt_label_mask(self, idx) -> np.array:
def get_gt_label_mask(self, idx) -> np.ndarray:
return np.array(Image.open(self.gt_label_mask_paths[idx]))

def __len__(self):
Expand Down
6 changes: 3 additions & 3 deletions livecellx/preprocess/augmentor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import numpy as np


def augment_images_by_augmentor(images: np.array, masks: np.array, crop_image_size: int, sampling_amount: int):
def augment_images_by_augmentor(images: np.ndarray, masks: np.ndarray, crop_image_size: int, sampling_amount: int):
"""Augments images and masks using Augmentor library.
Parameters
----------
images : np.array
images : np.ndarray
N x w x h x channel
masks : np.array
masks : np.ndarray
N x w x h x channel
crop_image_size :
size of the cropped image (outputs)
Expand Down
18 changes: 9 additions & 9 deletions livecellx/preprocess/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def normalize_edt(edt_img, edt_max=5):
return res_img


def normalize_features_zscore(features: np.array) -> np.array:
def normalize_features_zscore(features: np.ndarray) -> np.ndarray:
"""normalize features to z score
Parameters
----------
features : np.array
features : np.ndarray
_description_
Returns
Expand All @@ -53,12 +53,12 @@ def normalize_features_zscore(features: np.array) -> np.array:
return features


def normalize_img_to_uint8(img: np.array, dtype=np.uint8) -> np.array:
def normalize_img_to_uint8(img: np.ndarray, dtype=np.uint8) -> np.ndarray:
"""calculate z score of img and normalize to range [0, 255]
Parameters
----------
img : np.array
img : np.ndarray
_description_
Returns
Expand Down Expand Up @@ -138,11 +138,11 @@ def overlay_by_color(image, mask, color=(100, 0, 0), alpha=0.5):


# TODO: add tests and note if scale * raw_image exceeds type boundaries such as 255
def reserve_img_by_pixel_percentile(raw_img: np.array, percentile: float, target_val: float = None, scale: float = 1):
def reserve_img_by_pixel_percentile(raw_img: np.ndarray, percentile: float, target_val: float = None, scale: float = 1):
"""
Parameters
----------
raw_img : np.array
raw_img : np.ndarray
_description_
percentile : float
_description_
Expand Down Expand Up @@ -174,14 +174,14 @@ def reserve_img_by_pixel_percentile(raw_img: np.array, percentile: float, target
return flattened_img.reshape(raw_img.shape)


def enhance_contrast(img: np.array, factor=5):
def enhance_contrast(img: np.ndarray, factor=5):
im = Image.fromarray(img)
enhancer = ImageEnhance.Contrast(im)
im_output = enhancer.enhance(factor)
return np.array(im_output)


def dilate_or_erode_mask(cropped_mask: np.array, scale_factor):
def dilate_or_erode_mask(cropped_mask: np.ndarray, scale_factor):
"""
# TODO ensure reproducibility with different values of padding
cv's erode and dilation definition: https://docs.opencv.org/3.4/db/df6/tutorial_erosion_dilatation.html
Expand All @@ -203,7 +203,7 @@ def dilate_or_erode_mask(cropped_mask: np.array, scale_factor):
return s_cropped_mask


def dilate_or_erode_label_mask(label_mask: np.array, scale_factor, bg_val=0):
def dilate_or_erode_label_mask(label_mask: np.ndarray, scale_factor, bg_val=0):
"""Erode label mask to make each labeled region smaller and thus separated."""
labels = np.unique(label_mask)
# remove bg label
Expand Down
2 changes: 1 addition & 1 deletion livecellx/segment/cellpose_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from livecellx.preprocess.utils import normalize_img_to_uint8


def segment_single_image_by_cellpose(image, model, channels=[[0, 0]], diameter=150) -> np.array:
def segment_single_image_by_cellpose(image, model, channels=[[0, 0]], diameter=150) -> np.ndarray:
result_tuple = model.eval([image], diameter=diameter, channels=channels)
masks = result_tuple[0]
return np.array(masks[0])
Expand Down
6 changes: 3 additions & 3 deletions livecellx/segment/ou_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def underseg_overlay_scs(
return (img_crop, seg_crop, combined_gt_mask)


def gen_aug_diff_mask(aug_mask: np.array, combined_gt_mask: np.array, dtype=np.int32) -> np.array:
def gen_aug_diff_mask(aug_mask: np.ndarray, combined_gt_mask: np.ndarray, dtype=np.int32) -> np.ndarray:
"""generate a mask based on the difference between the augmented mask and the combined gt mask
0: no difference
-1: augmented mask is 0, combined gt mask is 1 -> over-segmentation
Expand All @@ -237,9 +237,9 @@ def gen_aug_diff_mask(aug_mask: np.array, combined_gt_mask: np.array, dtype=np.i
Parameters
----------
aug_mask : np.array
aug_mask : np.ndarray
_description_
combined_gt_mask : np.array
combined_gt_mask : np.ndarray
_description_
Returns
Expand Down
2 changes: 1 addition & 1 deletion livecellx/track/sort_tracker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_SORT_tracker_result_empty(sort_tracker):
return sort_tracker.time_since_update is not None


def get_bbox_from_contour(contour: list) -> np.array:
def get_bbox_from_contour(contour: list) -> np.ndarray:
"""get bboxes from a contour
Parameters
Expand Down

0 comments on commit 9ee3107

Please sign in to comment.