From e056fb1b2d8416085e56e085dad271d13f82e9c3 Mon Sep 17 00:00:00 2001 From: sronilsson Date: Wed, 6 Mar 2024 18:19:52 +0000 Subject: [PATCH] cleaned --- simba/mixins/geometry_mixin.py | 18 ++++++++++++++---- simba/mixins/image_mixin.py | 33 +++++++++++++++++++++++++++------ simba/utils/read_write.py | 24 +++++++++++++++++++----- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/simba/mixins/geometry_mixin.py b/simba/mixins/geometry_mixin.py index 130456421..08117e35d 100644 --- a/simba/mixins/geometry_mixin.py +++ b/simba/mixins/geometry_mixin.py @@ -2698,7 +2698,9 @@ def rank_shapes( return [shapes[idx] for idx in ranked] @staticmethod - def contours_to_geometries(contours: List[np.ndarray], force_rectangles: Optional[bool] = True) -> List[Polygon]: + def contours_to_geometries( + contours: List[np.ndarray], force_rectangles: Optional[bool] = True + ) -> List[Polygon]: """ Convert a list of contours to a list of geometries. @@ -2715,9 +2717,17 @@ def contours_to_geometries(contours: List[np.ndarray], force_rectangles: Optiona >>> GeometryMixin.contours_to_geometries(contours=contours) """ - check_instance(source=GeometryMixin.contours_to_geometries.__name__, instance=contours, accepted_types=(list,)) - for i in contours: check_instance(source=f'{GeometryMixin.contours_to_geometries.__name__} {i}', instance=i, - accepted_types=(np.ndarray,)) + check_instance( + source=GeometryMixin.contours_to_geometries.__name__, + instance=contours, + accepted_types=(list,), + ) + for i in contours: + check_instance( + source=f"{GeometryMixin.contours_to_geometries.__name__} {i}", + instance=i, + accepted_types=(np.ndarray,), + ) results = [] for contour in contours: if contour.ndim == 3: diff --git a/simba/mixins/image_mixin.py b/simba/mixins/image_mixin.py index 1d4fb025e..a13be2401 100644 --- a/simba/mixins/image_mixin.py +++ b/simba/mixins/image_mixin.py @@ -89,19 +89,40 @@ def brightness_intensity( @staticmethod def gaussian_blur(img: np.ndarray, kernel_size: Optional[Tuple] = (9, 9)): check_if_valid_img(data=img, source=ImageMixin.gaussian_blur.__name__) - check_instance(source=ImageMixin.gaussian_blur.__name__, instance=kernel_size, accepted_types=(tuple,)) - check_valid_lst(data=list(kernel_size), source=ImageMixin.gaussian_blur.__name__, valid_dtypes=(int,), exact_len=2) + check_instance( + source=ImageMixin.gaussian_blur.__name__, + instance=kernel_size, + accepted_types=(tuple,), + ) + check_valid_lst( + data=list(kernel_size), + source=ImageMixin.gaussian_blur.__name__, + valid_dtypes=(int,), + exact_len=2, + ) return cv2.GaussianBlur(img, kernel_size, 0) @staticmethod - def erode(img: np.ndarray, kernel_size: Optional[Tuple] = (3, 3), iterations: Optional[int] = 3): + def erode( + img: np.ndarray, + kernel_size: Optional[Tuple] = (3, 3), + iterations: Optional[int] = 3, + ): check_if_valid_img(data=img, source=ImageMixin.gaussian_blur.__name__) - check_instance(source=ImageMixin.gaussian_blur.__name__, instance=kernel_size, accepted_types=(tuple,)) - check_valid_lst(data=list(kernel_size), source=ImageMixin.gaussian_blur.__name__, valid_dtypes=(int,), exact_len=2) + check_instance( + source=ImageMixin.gaussian_blur.__name__, + instance=kernel_size, + accepted_types=(tuple,), + ) + check_valid_lst( + data=list(kernel_size), + source=ImageMixin.gaussian_blur.__name__, + valid_dtypes=(int,), + exact_len=2, + ) check_int(name=ImageMixin.erode.__name__, value=iterations, min_value=1) return cv2.erode(img, np.ones((3, 3), np.uint8), iterations=3) - @staticmethod def get_histocomparison( img_1: np.ndarray, diff --git a/simba/utils/read_write.py b/simba/utils/read_write.py index 649b42639..d07ff6ce5 100644 --- a/simba/utils/read_write.py +++ b/simba/utils/read_write.py @@ -415,7 +415,9 @@ def read_config_file(config_path: Union[str, os.PathLike]) -> configparser.Confi return config -def get_video_meta_data(video_path: Union[str, os.PathLike], fps_as_int: bool = True) -> dict: +def get_video_meta_data( + video_path: Union[str, os.PathLike], fps_as_int: bool = True +) -> dict: """ Read video metadata (fps, resolution, frame cnt etc.) from video file (e.g., mp4). @@ -648,7 +650,14 @@ def find_all_videos_in_directory( return video_lst -def read_frm_of_video(video_path: Union[str, os.PathLike, cv2.VideoCapture], frame_index: int = 0, opacity: Optional[float] = None, size: Optional[Tuple[int, int]] = None, greyscale: Optional[bool] = False, clahe: Optional[bool] = False) -> np.ndarray: +def read_frm_of_video( + video_path: Union[str, os.PathLike, cv2.VideoCapture], + frame_index: int = 0, + opacity: Optional[float] = None, + size: Optional[Tuple[int, int]] = None, + greyscale: Optional[bool] = False, + clahe: Optional[bool] = False, +) -> np.ndarray: """ Reads single image from video file. @@ -710,14 +719,19 @@ def read_frm_of_video(video_path: Union[str, os.PathLike, cv2.VideoCapture], fra if size: img = cv2.resize(img, size, interpolation=cv2.INTER_LINEAR) if greyscale: - if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + if len(img.shape) > 2: + img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if clahe: - if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + if len(img.shape) > 2: + img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16)).apply(img) else: - NoDataFoundWarning(msg=f"Frame {frame_index} for video {video_path} could not be read.") + NoDataFoundWarning( + msg=f"Frame {frame_index} for video {video_path} could not be read." + ) return img + def find_video_of_file( video_dir: Union[str, os.PathLike], filename: str, raise_error: bool = False ) -> Union[str, os.PathLike]: