From 6badaa5ec9cce89dc46b5fcb7763bcbc94143741 Mon Sep 17 00:00:00 2001 From: Valentin Dury Date: Tue, 16 Aug 2022 08:53:05 +0200 Subject: [PATCH] Typing + Add type None in union in prototype when None is the default value. Signed-off-by: Valentin Dury --- aloscene/__init__.py | 4 ++- aloscene/bounding_boxes_2d.py | 18 ++++++++----- aloscene/bounding_boxes_3d.py | 4 +-- aloscene/camera_calib.py | 10 +++---- aloscene/depth.py | 22 ++++++++++----- aloscene/disparity.py | 17 ++++++------ aloscene/flow.py | 5 ++-- aloscene/frame.py | 40 ++++++++++++++-------------- aloscene/labels.py | 6 ++--- aloscene/mask.py | 21 ++++++++++----- aloscene/oriented_boxes_2d.py | 18 ++++++++++--- aloscene/points_2d.py | 6 ++--- aloscene/tensors/augmented_tensor.py | 2 +- 13 files changed, 106 insertions(+), 67 deletions(-) diff --git a/aloscene/__init__.py b/aloscene/__init__.py index 7fe48375..568be3e6 100644 --- a/aloscene/__init__.py +++ b/aloscene/__init__.py @@ -18,6 +18,8 @@ from .renderer import Renderer +from typing import Union + def batch_list(tensors): return SpatialAugmentedTensor.batch_list(tensors) @@ -30,7 +32,7 @@ def render( views: list, renderer: str = "cv", size=None, - record_file: str = None, + record_file: Union[str, None] = None, fps=30, grid_size=None, skip_views=False, diff --git a/aloscene/bounding_boxes_2d.py b/aloscene/bounding_boxes_2d.py index cfb5c073..5ec1a28a 100644 --- a/aloscene/bounding_boxes_2d.py +++ b/aloscene/bounding_boxes_2d.py @@ -71,7 +71,7 @@ def __new__( x, boxes_format: str, absolute: bool, - labels: Union[dict, Labels] = None, + labels: Union[dict, Labels, None] = None, frame_size=None, names=("N", None), *args, @@ -102,7 +102,7 @@ def __new__( def __init__(self, x, *args, **kwargs): super().__init__(x) - def append_labels(self, labels: Labels, name: str = None): + def append_labels(self, labels: Labels, name: Union[str, None] = None): """Attach a set of labels to the boxes. The attached set of labels are supposed to be equal to the number of points. In other words, the N dimensions must match in both tensor. Parameters @@ -342,7 +342,7 @@ def rel_pos(self): tensor.frame_size = None return tensor - def get_with_format(self, boxes_format: str) : + def get_with_format(self, boxes_format: str): """Set boxes into the desired format (Inplace operation) Parameters ---------- @@ -425,7 +425,13 @@ def area(self) -> torch.Tensor: _GLOBAL_COLOR_SET = np.random.uniform(0, 1, (300, 3)) - def get_view(self, frame: Tensor = None, size: tuple = None, labels_set: str = None, **kwargs): + def get_view( + self, + frame: Union[Tensor, None] = None, + size: Union[tuple, None] = None, + labels_set: Union[str, None] = None, + **kwargs, + ): """Create a view of the boxes a frame Parameters ---------- @@ -890,7 +896,7 @@ def _spatial_shift(self, shift_y: float, shift_x: float, **kwargs): return n_boxes - def as_boxes(self, boxes) : + def as_boxes(self, boxes): """Convert the current boxes state into the given `boxes` state, following the same `boxes_format`, the same `frame_size` (if any) and the same `padded_size` (if any). Parameters @@ -915,7 +921,7 @@ def as_boxes(self, boxes) : return n_boxes - def remove_padding(self) : + def remove_padding(self): """This method can be usefull when one use a padded Frame but only want to learn on the non-padded area. Thefore the target points will remain unpadded while keeping information about the real padded size. Thus, this method will simply remove the memorized padded information. diff --git a/aloscene/bounding_boxes_3d.py b/aloscene/bounding_boxes_3d.py index c6bb0886..1d10350c 100644 --- a/aloscene/bounding_boxes_3d.py +++ b/aloscene/bounding_boxes_3d.py @@ -40,7 +40,7 @@ class BoundingBoxes3D(aloscene.tensors.AugmentedTensor): """ @staticmethod - def __new__(cls, x, labels: Union[dict, Labels] = None, names=("N", None), *args, **kwargs): + def __new__(cls, x, labels: Union[dict, Labels, None] = None, names=("N", None), *args, **kwargs): tensor = super().__new__(cls, x, *args, names=names, **kwargs) assert tensor.shape[-1] == 7 tensor.add_child("labels", labels, align_dim=["N"], mergeable=True) @@ -441,7 +441,7 @@ def giou3d_with(self, boxes2, enclosing_type="smallest", ret_iou3d=False) -> Uni """ return self.giou3d(self, boxes2, enclosing_type, ret_iou3d) - def get_view(self, frame, size: tuple = None, mode: str = "3D", **kwargs) -> View: + def get_view(self, frame, size: Union[tuple, None] = None, mode: str = "3D", **kwargs) -> View: """Create a View instance from a Frame Parameters diff --git a/aloscene/camera_calib.py b/aloscene/camera_calib.py index 2c5f2e22..3f2a6db4 100644 --- a/aloscene/camera_calib.py +++ b/aloscene/camera_calib.py @@ -1,5 +1,5 @@ # from __future__ import annotations -from typing import Tuple +from typing import Tuple, Union from aloscene.tensors.augmented_tensor import AugmentedTensor import aloscene @@ -48,10 +48,10 @@ class CameraIntrinsic(AugmentedTensor): def __new__( cls, x=None, - focal_length: float = None, - plane_size: tuple = None, - principal_point: tuple = None, - skew: tuple = None, + focal_length: Union[float, None] = None, + plane_size: Union[tuple, None] = None, + principal_point: Union[tuple, None] = None, + skew: Union[float, None] = None, *args, names=(None, None), **kwargs, diff --git a/aloscene/depth.py b/aloscene/depth.py index fa068957..26563dc5 100644 --- a/aloscene/depth.py +++ b/aloscene/depth.py @@ -9,6 +9,7 @@ from aloscene.renderer import View from aloscene.utils.depth_utils import coords2rtheta, add_colorbar import numpy as np +from typing import Union from aloscene.io.depth import load_depth @@ -35,7 +36,7 @@ class Depth(aloscene.tensors.SpatialAugmentedTensor): def __new__( cls, x, - occlusion: Mask = None, + occlusion: Union[Mask, None] = None, is_absolute=True, is_planar=True, scale=None, @@ -166,7 +167,7 @@ def encode_absolute( return n_depth - def append_occlusion(self, occlusion: Mask, name: str = None): + def append_occlusion(self, occlusion: Mask, name: Union[str, None] = None): """Attach an occlusion mask to the depth tensor. Parameters @@ -207,7 +208,9 @@ def __get_view__( return View(depth_color, title=title) - def as_points3d(self, camera_intrinsic: aloscene.CameraIntrinsic = None, projection=None, distortion=None): + def as_points3d( + self, camera_intrinsic: Union[aloscene.CameraIntrinsic, None] = None, projection=None, distortion=None + ): """Compute the 3D coordinates of points 2D points based on their respective depth. Parameters @@ -292,7 +295,10 @@ def as_points3d(self, camera_intrinsic: aloscene.CameraIntrinsic = None, project return aloscene.Points3D(points_3d, names=target_names, device=self.device) def as_disp( - self, camera_side: str = None, baseline: float = None, camera_intrinsic: aloscene.CameraIntrinsic = None + self, + camera_side: Union[str, None] = None, + baseline: Union[float, None] = None, + camera_intrinsic: Union[aloscene.CameraIntrinsic, None] = None, ): """Create a disparity augmented tensor from the current Depth augmented tensor. To use this method, one must know the target `camera_side` ("left" or "right"). Also, if not set on the @@ -344,7 +350,9 @@ def as_disp( ) return disp - def as_euclidean(self, camera_intrinsic: aloscene.CameraIntrinsic = None, projection=None, distortion=None): + def as_euclidean( + self, camera_intrinsic: Union[aloscene.CameraIntrinsic, None] = None, projection=None, distortion=None + ): """Create a new Depth augmented tensor whose data is the euclidean depth (distance) from camera to world points. To use this method, we must know intrinsic matrix of camera, projection model and distortion coefficient (if exists). @@ -384,7 +392,9 @@ def as_euclidean(self, camera_intrinsic: aloscene.CameraIntrinsic = None, projec euclidean.is_planar = False return euclidean - def as_planar(self, camera_intrinsic: aloscene.CameraIntrinsic = None, projection=None, distortion=None): + def as_planar( + self, camera_intrinsic: Union[aloscene.CameraIntrinsic, None] = None, projection=None, distortion=None + ): """Create a new planar depth augmented tensor from the euclidean depth between camera to world points with corresponding depth. To use this method, we must know intrinsic matrix of camera, projection model and distortion coefficient (if exists). diff --git a/aloscene/disparity.py b/aloscene/disparity.py index 77622239..bf6a1062 100644 --- a/aloscene/disparity.py +++ b/aloscene/disparity.py @@ -1,6 +1,7 @@ import numpy as np import matplotlib import torch +from typing import Union import aloscene from aloscene import Mask @@ -31,9 +32,9 @@ class Disparity(aloscene.tensors.SpatialAugmentedTensor): def __new__( cls, x, - occlusion: Mask = None, + occlusion: Union[Mask, None] = None, disp_format="unsigned", - png_negate: bool = None, + png_negate: Union[bool, None] = None, *args, names=("C", "H", "W"), **kwargs, @@ -58,7 +59,7 @@ def __new__( def __init__(self, x, *args, **kwargs): super().__init__(x) - def append_occlusion(self, occlusion: Mask, name: str = None): + def append_occlusion(self, occlusion: Mask, name: Union[str, None] = None): """Attach an occlusion mask to the frame. Parameters @@ -136,7 +137,7 @@ def unsigned(self): disp = torch.absolute(disp) return disp - def signed(self, camera_side: str = None): + def signed(self, camera_side: Union[str, None] = None): """ Returns a copy of disparity map in signed disparity format """ @@ -154,10 +155,10 @@ def signed(self, camera_side: str = None): def as_depth( self, - baseline: float = None, - focal_length: float = None, - camera_side: float = None, - camera_intrinsic: aloscene.CameraIntrinsic = None, + baseline: Union[float, None] = None, + focal_length: Union[float, None] = None, + camera_side: Union[float, None] = None, + camera_intrinsic: Union[aloscene.CameraIntrinsic, None] = None, max_depth=np.inf, ): """Return a Depth augmented tensor based on the given `baseline` & `focal_length`. diff --git a/aloscene/flow.py b/aloscene/flow.py index 95198860..d3e24861 100644 --- a/aloscene/flow.py +++ b/aloscene/flow.py @@ -1,3 +1,4 @@ +from typing import Union import aloscene from aloscene import Mask from aloscene.io.flow import load_flow @@ -17,7 +18,7 @@ class Flow(aloscene.tensors.SpatialAugmentedTensor): """ @staticmethod - def __new__(cls, x, occlusion: Mask = None, *args, names=("C", "H", "W"), **kwargs): + def __new__(cls, x, occlusion: Union[Mask, None] = None, *args, names=("C", "H", "W"), **kwargs): if isinstance(x, str): # load flow from path x = load_flow(x) @@ -29,7 +30,7 @@ def __new__(cls, x, occlusion: Mask = None, *args, names=("C", "H", "W"), **kwar def __init__(self, x, *args, **kwargs): super().__init__(x) - def append_occlusion(self, occlusion: Mask, name: str = None): + def append_occlusion(self, occlusion: Mask, name: Union[str, None] = None): """Attach an occlusion mask to the frame. Parameters diff --git a/aloscene/frame.py b/aloscene/frame.py index 432ba2e6..304fc6fd 100644 --- a/aloscene/frame.py +++ b/aloscene/frame.py @@ -92,15 +92,15 @@ class Frame(aloscene.tensors.SpatialAugmentedTensor): def __new__( cls, x, - boxes2d: Union[dict, BoundingBoxes2D] = None, - boxes3d: Union[dict, BoundingBoxes3D] = None, - labels: Union[dict, Labels] = None, - flow: Union[dict, Flow] = None, - segmentation: Union[dict, Mask] = None, - disparity: Union[dict, Disparity] = None, - points2d: Union[dict, Points2D] = None, - points3d: Union[dict, Points3D] = None, - depth: Union[dict, Depth] = None, + boxes2d: Union[dict, BoundingBoxes2D, None] = None, + boxes3d: Union[dict, BoundingBoxes3D, None] = None, + labels: Union[dict, Labels, None] = None, + flow: Union[dict, Flow, None] = None, + segmentation: Union[dict, Mask, None] = None, + disparity: Union[dict, Disparity, None] = None, + points2d: Union[dict, Points2D, None] = None, + points3d: Union[dict, Points3D, None] = None, + depth: Union[dict, Depth, None] = None, normalization="255", mean_std=None, names=("C", "H", "W"), @@ -152,7 +152,7 @@ def save(self, tgt_path: str): """ torchvision.utils.save_image(self.cpu().norm01().as_tensor(), tgt_path) - def append_labels(self, labels: Labels, name: str = None): + def append_labels(self, labels: Labels, name: Union[str, None] = None): """Attach a set of labels to the frame. This can be usefull for classification or multi label classification. The rank of the label must be >= 1 @@ -172,7 +172,7 @@ def append_labels(self, labels: Labels, name: str = None): """ self._append_child("labels", labels, name) - def append_boxes2d(self, boxes: BoundingBoxes2D, name: str = None): + def append_boxes2d(self, boxes: BoundingBoxes2D, name: Union[str, None] = None): """Attach a set of BoundingBoxes2D to the frame. Parameters @@ -197,7 +197,7 @@ def append_boxes2d(self, boxes: BoundingBoxes2D, name: str = None): """ self._append_child("boxes2d", boxes, name) - def append_points2d(self, points: Points2D, name: str = None): + def append_points2d(self, points: Points2D, name: Union[str, None] = None): """Attach a set of points to the frame. Parameters @@ -210,7 +210,7 @@ def append_points2d(self, points: Points2D, name: str = None): """ self._append_child("points2d", points, name) - def append_points3d(self, points_3d: Points3D, name: str = None): + def append_points3d(self, points_3d: Points3D, name: Union[str, None] = None): """Attach a set of points to the frame. Parameters @@ -223,7 +223,7 @@ def append_points3d(self, points_3d: Points3D, name: str = None): """ self._append_child("points3d", points_3d, name) - def append_boxes3d(self, boxes_3d: BoundingBoxes3D, name: str = None): + def append_boxes3d(self, boxes_3d: BoundingBoxes3D, name: Union[str, None] = None): """Attach BoundingBoxes3D to the frame Parameters @@ -247,7 +247,7 @@ def append_boxes3d(self, boxes_3d: BoundingBoxes3D, name: str = None): """ self._append_child("boxes3d", boxes_3d, name) - def append_flow(self, flow, name: str = None): + def append_flow(self, flow, name: Union[str, None] = None): """Attach a flow to the frame. Parameters @@ -266,7 +266,7 @@ def append_flow(self, flow, name: str = None): """ self._append_child("flow", flow, name) - def append_disparity(self, disparity, name: str = None): + def append_disparity(self, disparity, name: Union[str, None] = None): """Attach a disparity map to the frame. Parameters @@ -285,7 +285,7 @@ def append_disparity(self, disparity, name: str = None): """ self._append_child("disparity", disparity, name) - def append_depth(self, depth, name: str = None): + def append_depth(self, depth, name: Union[str, None] = None): """Attach a depth map to the frame. Parameters @@ -304,7 +304,7 @@ def append_depth(self, depth, name: str = None): """ self._append_child("depth", depth, name) - def append_segmentation(self, segmentation: Mask, name: str = None): + def append_segmentation(self, segmentation: Mask, name: Union[str, None] = None): """Attach a segmentation to the frame. Parameters @@ -318,7 +318,7 @@ def append_segmentation(self, segmentation: Mask, name: str = None): """ self._append_child("segmentation", segmentation, name) - def append_pose(self, pose: Pose, name: str = None): + def append_pose(self, pose: Pose, name: Union[str, None] = None): """Attach a pose to the frame. Parameters @@ -331,7 +331,7 @@ def append_pose(self, pose: Pose, name: str = None): """ self._append_child("pose", pose, name) - def append_scene_flow(self, scene_flow: SceneFlow, name: str = None): + def append_scene_flow(self, scene_flow: SceneFlow, name: Union[str, None] = None): """Attach a scene flow to the frame. Parameters diff --git a/aloscene/labels.py b/aloscene/labels.py index 7ba26660..45cec4c9 100644 --- a/aloscene/labels.py +++ b/aloscene/labels.py @@ -18,8 +18,8 @@ def __new__( cls, x, encoding: str = "id", - labels_names: list = None, - scores: torch.Tensor = None, + labels_names: Union[list, None] = None, + scores: Union[torch.Tensor, None] = None, names=None, *args, **kwargs, @@ -72,7 +72,7 @@ def __new__( def __init__(self, x, *args, **kwargs): super().__init__(x) - def get_view(self, frame: Tensor = None, **kwargs): + def get_view(self, frame: Union[Tensor, None] = None, **kwargs): """Create a view of the boxes a frame Parameters diff --git a/aloscene/mask.py b/aloscene/mask.py index 82815c5f..54de8f89 100644 --- a/aloscene/mask.py +++ b/aloscene/mask.py @@ -23,7 +23,7 @@ class Mask(aloscene.tensors.SpatialAugmentedTensor): """ @staticmethod - def __new__(cls, x, labels: Union[dict, Labels] = None, *args, **kwargs): + def __new__(cls, x, labels: Union[dict, Labels, None] = None, *args, **kwargs): # Load frame from path if isinstance(x, str): x = load_mask(x) @@ -32,7 +32,7 @@ def __new__(cls, x, labels: Union[dict, Labels] = None, *args, **kwargs): tensor.add_child("labels", labels, align_dim=["N"], mergeable=True) return tensor - def append_labels(self, labels: Labels, name: str = None): + def append_labels(self, labels: Labels, name: Union[str, None] = None): """Attach a set of labels to the masks. Parameters @@ -46,7 +46,7 @@ def append_labels(self, labels: Labels, name: str = None): self._append_child("labels", labels, name) def iou_with(self, mask2) -> torch.Tensor: - """ IoU calculation between mask2 and itself + """IoU calculation between mask2 and itself Parameters ---------- @@ -79,7 +79,12 @@ def iou_with(self, mask2) -> torch.Tensor: return intersection / (union - intersection) def get_view( - self, frame: Tensor = None, size: tuple = None, labels_set: str = None, color_by_cat: bool = True, **kwargs + self, + frame: Union[Tensor, None] = None, + size: Union[tuple, None] = None, + labels_set: Union[str, None] = None, + color_by_cat: bool = True, + **kwargs, ): """Get view of segmentation mask and used it in a input :mod:`Frame ` @@ -126,7 +131,9 @@ def get_view( frame = 0.2 * frame + 0.8 * masks return View(frame, **kwargs) - def __get_view__(self, labels_set: str = None, title: str = None, color_by_cat: bool = True, **kwargs): + def __get_view__( + self, labels_set: Union[str, None] = None, title: Union[str, None] = None, color_by_cat: bool = True, **kwargs + ): """Create a view of the frame""" from alodataset.utils.panoptic_utils import id2rgb @@ -150,7 +157,7 @@ def __get_view__(self, labels_set: str = None, title: str = None, color_by_cat: ) return View(frame, title=title) - def mask2id(self, labels_set: str = None, return_ann: bool = False, return_cats: bool = False): + def mask2id(self, labels_set: Union[str, None] = None, return_ann: bool = False, return_cats: bool = False): """Create a panoptic view of the frame, where each pixel represent one class Parameters @@ -202,7 +209,7 @@ def mask2id(self, labels_set: str = None, return_ann: bool = False, return_cats: return frame, annotations return frame - def _get_set_children(self, labels_set: str = None): + def _get_set_children(self, labels_set: Union[str, None] = None): if not (labels_set is None or isinstance(self.labels, dict)): raise Exception( f"Trying to display a set of labels ({labels_set}) while masks do not have multiple set of labels" diff --git a/aloscene/oriented_boxes_2d.py b/aloscene/oriented_boxes_2d.py index d2f143cf..b4d8104d 100644 --- a/aloscene/oriented_boxes_2d.py +++ b/aloscene/oriented_boxes_2d.py @@ -30,7 +30,14 @@ class OrientedBoxes2D(aloscene.tensors.AugmentedTensor): @staticmethod def __new__( - cls, x, absolute=True, frame_size=None, labels: Union[dict, Labels] = None, names=("N", None), *args, **kwargs + cls, + x, + absolute=True, + frame_size=None, + labels: Union[dict, Labels, None] = None, + names=("N", None), + *args, + **kwargs, ): tensor = super().__new__(cls, x, *args, names=names, **kwargs) @@ -53,7 +60,7 @@ def __init__(self, x, *args, **kwargs): ) raise import_error - def append_labels(self, labels: Labels, name: str = None): + def append_labels(self, labels: Labels, name: Union[str, None] = None): """Attach a set of labels to the boxes. Parameters @@ -92,7 +99,12 @@ def corners(self) -> torch.Tensor: GLOBAL_COLOR_SET = np.random.uniform(0, 1, (300, 3)) def get_view( - self, frame: Tensor = None, size: tuple = None, color: tuple = None, labels_set: str = None, **kwargs + self, + frame: Union[Tensor, None] = None, + size: Union[tuple, None] = None, + color: Union[tuple, None] = None, + labels_set: Union[str, None] = None, + **kwargs, ): """Create a view of the boxes in a frame diff --git a/aloscene/points_2d.py b/aloscene/points_2d.py index aaf5bb85..865e90eb 100644 --- a/aloscene/points_2d.py +++ b/aloscene/points_2d.py @@ -84,7 +84,7 @@ def __new__( x: Union[list, np.array, torch.Tensor], points_format: str, absolute: bool, - labels: Union[dict, Labels] = None, + labels: Union[dict, Labels, None] = None, frame_size=None, names=("N", None), *args, @@ -115,7 +115,7 @@ def __new__( def __init__(self, x, *args, **kwargs): super().__init__(x) - def append_labels(self, labels: Labels, name: str = None): + def append_labels(self, labels: Labels, name: Union[str, None] = None): """Attach a set of labels to the points. The attached set of labels are supposed to be equal to the number of points. In other words, the N dimensions must match in both tensor. @@ -306,7 +306,7 @@ def get_with_format(self, points_format: str): _GLOBAL_COLOR_SET = np.random.uniform(0, 1, (300, 3)) - def get_view(self, frame=None, size: tuple = None, labels_set: str = None, **kwargs): + def get_view(self, frame=None, size: Union[tuple, None] = None, labels_set: Union[str, None] = None, **kwargs): """Create a view of the points on a frame Parameters diff --git a/aloscene/tensors/augmented_tensor.py b/aloscene/tensors/augmented_tensor.py index 385a640e..c06fd868 100644 --- a/aloscene/tensors/augmented_tensor.py +++ b/aloscene/tensors/augmented_tensor.py @@ -194,7 +194,7 @@ def add_child(self, child_name, child, align_dim=["B", "T"], mergeable=True, **k setattr(self, child_name, child) - def _append_child(self, child_name: str, child, set_name: str = None): + def _append_child(self, child_name: str, child, set_name: Union[str, None] = None): """ Attach a new value for a given child name.