vkit.element.uniqueness
1# Copyright 2022 vkit-x Administrator. All Rights Reserved. 2# 3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses. 4# 5# The commercial license gives you the full rights to create and distribute software 6# on your own terms without any SSPL license obligations. For more information, 7# please see the "LICENSE_COMMERCIAL.txt" file. 8# 9# This project is also available under Server Side Public License (SSPL). 10# The SSPL licensing is ideal for use cases such as open source projects with 11# SSPL distribution, student/academic purposes, hobby projects, internal research 12# projects without external distribution, or other projects where all SSPL 13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file. 14from typing import Union, Tuple, Sequence, cast 15import math 16 17import numpy as np 18 19 20def check_element_uniqueness( 21 value0: Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float], 22 value1: Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float], 23): 24 if type(value0) is not type(value1): 25 return False 26 27 if isinstance(value0, Image): 28 value1 = cast(Image, value1) 29 if value0.shape != value1.shape: 30 return False 31 return (value0.mat == value1.mat).all() 32 33 elif isinstance(value0, ScoreMap): 34 value1 = cast(ScoreMap, value1) 35 if value0.shape != value1.shape: 36 return False 37 return np.isclose(value0.mat, value1.mat).all() 38 39 elif isinstance(value0, Mask): 40 value1 = cast(Mask, value1) 41 if value0.shape != value1.shape: 42 return False 43 return (value0.mat == value1.mat).all() 44 45 elif isinstance(value0, np.ndarray): 46 value1 = cast(np.ndarray, value1) 47 if value0.shape != value1.shape: 48 return False 49 if value0.dtype != value1.dtype: 50 return False 51 if np.issubdtype(value0.dtype, np.floating): 52 return np.isclose(value0, value1).all() 53 else: 54 return (value0 == value1).all() 55 56 elif isinstance(value0, tuple): 57 value1 = cast(tuple, value1) 58 assert len(value0) == len(value1) 59 return value0 == value1 60 61 elif isinstance(value0, int): 62 value1 = cast(int, value1) 63 return value0 == value1 64 65 elif isinstance(value0, float): 66 value1 = cast(float, value1) 67 return math.isclose(value0, value1) 68 69 else: 70 raise NotImplementedError() 71 72 73def check_elements_uniqueness( 74 values: Sequence[Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float]], 75): 76 unique = True 77 for idx, value in enumerate(values): 78 if idx == 0: 79 continue 80 if not check_element_uniqueness(values[0], value): 81 unique = False 82 break 83 return unique 84 85 86# Cyclic dependency, by design. 87from .mask import Mask # noqa: E402 88from .score_map import ScoreMap # noqa: E402 89from .image import Image # noqa: E402
def
check_element_uniqueness( value0: Union[vkit.element.image.Image, vkit.element.score_map.ScoreMap, vkit.element.mask.Mask, numpy.ndarray, Tuple[int, ...], int, float], value1: Union[vkit.element.image.Image, vkit.element.score_map.ScoreMap, vkit.element.mask.Mask, numpy.ndarray, Tuple[int, ...], int, float]):
21def check_element_uniqueness( 22 value0: Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float], 23 value1: Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float], 24): 25 if type(value0) is not type(value1): 26 return False 27 28 if isinstance(value0, Image): 29 value1 = cast(Image, value1) 30 if value0.shape != value1.shape: 31 return False 32 return (value0.mat == value1.mat).all() 33 34 elif isinstance(value0, ScoreMap): 35 value1 = cast(ScoreMap, value1) 36 if value0.shape != value1.shape: 37 return False 38 return np.isclose(value0.mat, value1.mat).all() 39 40 elif isinstance(value0, Mask): 41 value1 = cast(Mask, value1) 42 if value0.shape != value1.shape: 43 return False 44 return (value0.mat == value1.mat).all() 45 46 elif isinstance(value0, np.ndarray): 47 value1 = cast(np.ndarray, value1) 48 if value0.shape != value1.shape: 49 return False 50 if value0.dtype != value1.dtype: 51 return False 52 if np.issubdtype(value0.dtype, np.floating): 53 return np.isclose(value0, value1).all() 54 else: 55 return (value0 == value1).all() 56 57 elif isinstance(value0, tuple): 58 value1 = cast(tuple, value1) 59 assert len(value0) == len(value1) 60 return value0 == value1 61 62 elif isinstance(value0, int): 63 value1 = cast(int, value1) 64 return value0 == value1 65 66 elif isinstance(value0, float): 67 value1 = cast(float, value1) 68 return math.isclose(value0, value1) 69 70 else: 71 raise NotImplementedError()
def
check_elements_uniqueness( values: collections.abc.Sequence[typing.Union[vkit.element.image.Image, vkit.element.score_map.ScoreMap, vkit.element.mask.Mask, numpy.ndarray, typing.Tuple[int, ...], int, float]]):
74def check_elements_uniqueness( 75 values: Sequence[Union['Image', 'ScoreMap', 'Mask', np.ndarray, Tuple[int, ...], int, float]], 76): 77 unique = True 78 for idx, value in enumerate(values): 79 if idx == 0: 80 continue 81 if not check_element_uniqueness(values[0], value): 82 unique = False 83 break 84 return unique