# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.""".. Data Type Conversion Algorithms (see parent package :mod:`cdl.algorithms`)"""# pylint: disable=invalid-name # Allows short reference names like x, y, ...from__future__importannotationsimportnumpyasnp
[docs]defis_integer_dtype(dtype:np.dtype)->bool:"""Return True if data type is an integer type Args: dtype: Data type to check Returns: True if data type is an integer type """returnissubclass(np.dtype(dtype).type,np.integer)
[docs]defis_complex_dtype(dtype:np.dtype)->bool:"""Return True if data type is a complex type Args: dtype: Data type to check Returns: True if data type is a complex type """returnissubclass(np.dtype(dtype).type,complex)
[docs]defclip_astype(data:np.ndarray,dtype:np.dtype)->np.ndarray:"""Convert array to a new data type, after having clipped values to the new data type's range if it is an integer type. If data type is not integer, this is equivalent to ``data.astype(dtype)``. Args: data: Array to convert dtype: Data type to convert to Returns: Array converted to new data type """ifis_integer_dtype(dtype):returnnp.clip(data,np.iinfo(dtype).min,np.iinfo(dtype).max).astype(dtype)returndata.astype(dtype)