How to use numpy_complex_dtypes method in pandera

Best Python code snippet using pandera_python

strategies.py

Source:strategies.py Github

copy

Full Screen

...212 return pd.Timestamp(value).value213 min_value = MIN_DT_VALUE if min_value is None else _to_unix(min_value)214 max_value = MAX_DT_VALUE if max_value is None else _to_unix(max_value)215 return _datetime_strategy(dtype, st.integers(min_value, max_value))216def numpy_complex_dtypes(217 dtype,218 min_value: complex = complex(0, 0),219 max_value: Optional[complex] = None,220 allow_infinity: bool = None,221 allow_nan: bool = None,222):223 """Create numpy strategy for complex numbers.224 :param dtype: numpy complex number datatype225 :param min_value: minimum value, must be complex number226 :param max_value: maximum value, must be complex number227 :returns: ``hypothesis`` strategy228 """229 max_real: Optional[float]230 max_imag: Optional[float]231 if max_value:232 max_real = max_value.real233 max_imag = max_value.imag234 else:235 max_real = max_imag = None236 if dtype.itemsize == 8:237 width = 32238 else:239 width = 64240 # switch min and max values for imaginary if min value > max value241 if max_imag is not None and min_value.imag > max_imag:242 min_imag = max_imag243 max_imag = min_value.imag244 else:245 min_imag = min_value.imag246 strategy = st.builds(247 complex,248 st.floats(249 min_value=min_value.real,250 max_value=max_real,251 width=width,252 allow_infinity=allow_infinity,253 allow_nan=allow_nan,254 ),255 st.floats(256 min_value=min_imag,257 max_value=max_imag,258 width=width,259 allow_infinity=allow_infinity,260 allow_nan=allow_nan,261 ),262 ).map(dtype.type)263 @st.composite264 def build_complex(draw):265 value = draw(strategy)266 hypothesis.assume(min_value <= value)267 if max_value is not None:268 hypothesis.assume(max_value >= value)269 return value270 return build_complex()271def to_numpy_dtype(pandera_dtype: DataType):272 """Convert a :class:`~pandera.dtypes.DataType` to numpy dtype compatible273 with hypothesis."""274 try:275 np_dtype = pandas_engine.Engine.numpy_dtype(pandera_dtype)276 except TypeError as err:277 if is_datetime(pandera_dtype):278 return np.dtype("datetime64[ns]")279 raise TypeError(280 f"Data generation for the '{pandera_dtype}' data type is "281 "currently unsupported."282 ) from err283 if np_dtype == np.dtype("object") or str(pandera_dtype) == "str":284 np_dtype = np.dtype(str)285 return np_dtype286def pandas_dtype_strategy(287 pandera_dtype: DataType,288 strategy: Optional[SearchStrategy] = None,289 **kwargs,290) -> SearchStrategy:291 # pylint: disable=line-too-long,no-else-raise292 """Strategy to generate data from a :class:`pandera.dtypes.DataType`.293 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.294 :param strategy: an optional hypothesis strategy. If specified, the295 pandas dtype strategy will be chained onto this strategy.296 :kwargs: key-word arguments passed into297 `hypothesis.extra.numpy.from_dtype <https://hypothesis.readthedocs.io/en/latest/numpy.html#hypothesis.extra.numpy.from_dtype>`_ .298 For datetime, timedelta, and complex number datatypes, these arguments299 are passed into :func:`~pandera.strategies.numpy_time_dtypes` and300 :func:`~pandera.strategies.numpy_complex_dtypes`.301 :returns: ``hypothesis`` strategy302 """303 def compat_kwargs(*args):304 return {k: v for k, v in kwargs.items() if k in args}305 # hypothesis doesn't support categoricals or objects, so we'll will need to306 # build a pandera-specific solution.307 if is_category(pandera_dtype):308 raise TypeError(309 "data generation for the Category dtype is currently "310 "unsupported. Consider using a string or int dtype and "311 "Check.isin(values) to ensure a finite set of values."312 )313 np_dtype = to_numpy_dtype(pandera_dtype)314 if strategy:315 if _is_datetime_tz(pandera_dtype):316 return _datetime_strategy(pandera_dtype.type, strategy) # type: ignore317 return strategy.map(np_dtype.type)318 elif is_datetime(pandera_dtype) or is_timedelta(pandera_dtype):319 return numpy_time_dtypes(320 pandera_dtype.type if _is_datetime_tz(pandera_dtype) else np_dtype, # type: ignore321 **compat_kwargs("min_value", "max_value"),322 )323 elif is_complex(pandera_dtype):324 return numpy_complex_dtypes(325 np_dtype,326 **compat_kwargs(327 "min_value", "max_value", "allow_infinity", "allow_nan"328 ),329 )330 return npst.from_dtype(331 np_dtype,332 **{ # type: ignore333 "allow_nan": False,334 "allow_infinity": False,335 **kwargs,336 },337 )338def eq_strategy(...

Full Screen

Full Screen

_common.py

Source:_common.py Github

copy

Full Screen

1from ._cfunctions import MKL, mkl_library_name2from ._constants import *3from ._structs import sparse_matrix_t, MKL_Complex8, MKL_Complex164import numpy as _np5import ctypes as _ctypes6from scipy import sparse as _spsparse7import warnings8import time9from numpy.ctypeslib import as_array10# Use mkl-service to check version if it's installed11# Since it's not on PyPi I don't want to make this an actual package dependency12# So without it just create mock functions and don't do version checking13try:14 from mkl import get_version, get_version_string, get_max_threads15except ImportError:16 def get_version():17 return None18 def get_version_string():19 return None20 21 def get_max_threads():22 return None23if get_version() is not None and get_version()["MajorVersion"] < 2020:24 _verr_msg = "Loaded version of MKL is out of date: {v}".format(v=get_version_string())25 warnings.warn(_verr_msg)26# Dict keyed by ('sparse_type_str', 'double_precision_bool', 'complex_bool')27_create_functions = {('csr', False, False): MKL._mkl_sparse_s_create_csr,28 ('csr', True, False): MKL._mkl_sparse_d_create_csr,29 ('csr', False, True): MKL._mkl_sparse_c_create_csr,30 ('csr', True, True): MKL._mkl_sparse_z_create_csr,31 ('csc', False, False): MKL._mkl_sparse_s_create_csc,32 ('csc', True, False): MKL._mkl_sparse_d_create_csc,33 ('csc', False, True): MKL._mkl_sparse_c_create_csc,34 ('csc', True, True): MKL._mkl_sparse_z_create_csc,35 ('bsr', False, False): MKL._mkl_sparse_s_create_bsr,36 ('bsr', True, False): MKL._mkl_sparse_d_create_bsr,37 ('bsr', False, True): MKL._mkl_sparse_c_create_bsr,38 ('bsr', True, True): MKL._mkl_sparse_z_create_bsr}39# Dict keyed by ('sparse_type_str', 'double_precision_bool', 'complex_bool')40_export_functions = {('csr', False, False): MKL._mkl_sparse_s_export_csr,41 ('csr', True, False): MKL._mkl_sparse_d_export_csr,42 ('csr', False, True): MKL._mkl_sparse_c_export_csr,43 ('csr', True, True): MKL._mkl_sparse_z_export_csr,44 ('csc', False, False): MKL._mkl_sparse_s_export_csc,45 ('csc', True, False): MKL._mkl_sparse_d_export_csc,46 ('csc', False, True): MKL._mkl_sparse_c_export_csc,47 ('csc', True, True): MKL._mkl_sparse_z_export_csc,48 ('bsr', False, False): MKL._mkl_sparse_s_export_bsr,49 ('bsr', True, False): MKL._mkl_sparse_d_export_bsr,50 ('bsr', False, True): MKL._mkl_sparse_c_export_bsr,51 ('bsr', True, True): MKL._mkl_sparse_z_export_bsr}52# Dict keyed by ('double_precision_bool', 'complex_bool')53_output_dtypes = {(False, False): _np.float32,54 (True, False): _np.float64,55 (False, True): _np.csingle,56 (True, True): _np.cdouble}57NUMPY_FLOAT_DTYPES = [_np.float32, _np.float64]58NUMPY_COMPLEX_DTYPES = [_np.csingle, _np.cdouble]59def set_debug_mode(debug_bool):60 """61 Activate or deactivate debug mode62 :param debug_bool: True to be printy. False to be quiet.63 :type debug_bool: bool64 """65 MKL.MKL_DEBUG = debug_bool66def print_mkl_debug():67 """68 Print the MKL interface status if debug mode is on69 """70 if not MKL.MKL_DEBUG:71 return72 if get_version_string() is None:73 print("mkl-service must be installed to get full debug messaging")74 else:75 print(get_version_string())76 print("MKL Number of Threads: {n}".format(n=get_max_threads()))77 print("MKL linked: {fn}".format(fn=mkl_library_name()))78 print("MKL interface {_np} | {c}".format(_np=MKL.MKL_INT_NUMPY, c=MKL.MKL_INT))79 print("Set int32 interface with env MKL_INTERFACE_LAYER=LP64")80 print("Set int64 interface with env MKL_INTERFACE_LAYER=ILP64")81def debug_print(msg):82 """83 Print a message if debug mode is on84 :param msg: Message85 :type msg: str86 """87 if not MKL.MKL_DEBUG:88 return89 else:90 print(msg)91def debug_timer(msg=None, old_time=None):92 """93 Print a message with timing information if debug mode is on94 :param msg: Message95 :type msg: str96 :param old_time: Time to calculate difference for97 :type old_time: float98 """99 if not MKL.MKL_DEBUG:100 return101 t0 = time.time()102 if old_time is not None and msg is not None:103 print(msg + ": {0:.6f} seconds".format(t0 - old_time))104 return t0105def _check_scipy_index_typing(sparse_matrix):106 """107 Ensure that the sparse matrix indicies are in the correct integer type108 :param sparse_matrix: Scipy matrix in CSC or CSR format109 :type sparse_matrix: scipy.sparse.spmatrix110 """111 int_max = _np.iinfo(MKL.MKL_INT_NUMPY).max112 if (sparse_matrix.nnz > int_max) or (max(sparse_matrix.shape) > int_max):113 msg = "MKL interface is {t} and cannot hold matrix {m}\n".format(m=repr(sparse_matrix), t=MKL.MKL_INT_NUMPY)114 msg += "Try changing MKL to int64 with the environment variable MKL_INTERFACE_LAYER=ILP64"115 raise ValueError(msg)116 # Cast indexes to MKL_INT type117 if sparse_matrix.indptr.dtype != MKL.MKL_INT_NUMPY:118 sparse_matrix.indptr = sparse_matrix.indptr.astype(MKL.MKL_INT_NUMPY)119 if sparse_matrix.indices.dtype != MKL.MKL_INT_NUMPY:120 sparse_matrix.indices = sparse_matrix.indices.astype(MKL.MKL_INT_NUMPY)121def _get_numpy_layout(numpy_arr, second_arr=None):122 """123 Get the array layout code for a dense array in C or F order.124 Raises a ValueError if the array is not contiguous.125 :param numpy_arr: Numpy dense array126 :type numpy_arr: _np.ndarray127 :param second_arr: Numpy dense array; if numpy_arr is 1d (and therefore both C and F order), use this order128 :type second_arr: _np.ndarray, None129 :return: The layout code for MKL and the leading dimension130 :rtype: int, int131 """132 # Return the second array order if the first is ambiguous133 if numpy_arr.flags.c_contiguous and numpy_arr.flags.f_contiguous and second_arr is not None:134 if second_arr.flags.c_contiguous:135 return LAYOUT_CODE_C, numpy_arr.shape[1]136 elif second_arr.flags.f_contiguous:137 return LAYOUT_CODE_F, numpy_arr.shape[0]138 # Return the first order array otherwise139 elif numpy_arr.flags.c_contiguous:140 return LAYOUT_CODE_C, numpy_arr.shape[1]141 elif numpy_arr.flags.f_contiguous:142 return LAYOUT_CODE_F, numpy_arr.shape[0]143 elif not numpy_arr.flags.contiguous:144 raise ValueError("Array is not contiguous")145 else:146 raise ValueError("Array layout check has failed for unknown reason")147def _create_mkl_sparse(matrix):148 """149 Create MKL internal representation150 :param matrix: Sparse data in CSR or CSC format151 :type matrix: scipy.sparse.spmatrix152 :return ref, double_precision: Handle for the MKL internal representation and boolean for 153 double precision and for complex dtype154 :rtype: sparse_matrix_t, bool, bool155 """156 double_precision, complex_type = _is_double(matrix)157 # Figure out which matrix creation function to use158 if _spsparse.isspmatrix_csr(matrix):159 _check_scipy_index_typing(matrix)160 assert matrix.data.shape[0] == matrix.indices.shape[0]161 assert matrix.indptr.shape[0] == matrix.shape[0] + 1162 handle_func = _create_functions[('csr', double_precision, complex_type)]163 elif _spsparse.isspmatrix_csc(matrix):164 _check_scipy_index_typing(matrix)165 assert matrix.data.shape[0] == matrix.indices.shape[0]166 assert matrix.indptr.shape[0] == matrix.shape[1] + 1167 handle_func = _create_functions[('csc', double_precision, complex_type)]168 elif _spsparse.isspmatrix_bsr(matrix):169 _check_scipy_index_typing(matrix)170 return _create_mkl_sparse_bsr(matrix), double_precision, complex_type171 else:172 raise ValueError("Matrix is not CSC, CSR, or BSR")173 return _pass_mkl_handle_csr_csc(matrix, handle_func), double_precision, complex_type174def _pass_mkl_handle_csr_csc(data, handle_func):175 """176 Create MKL internal representation for CSR or CSC matrix177 :param data: Sparse data178 :type data: scipy.sparse.spmatrix179 :return ref: Handle for the MKL internal representation180 :rtype: sparse_matrix_t181 """182 # Create a pointer for the output matrix183 ref = sparse_matrix_t()184 # Load into a MKL data structure and check return185 ret_val = handle_func(_ctypes.byref(ref),186 _ctypes.c_int(SPARSE_INDEX_BASE_ZERO),187 MKL.MKL_INT(data.shape[0]),188 MKL.MKL_INT(data.shape[1]),189 data.indptr[0:-1],190 data.indptr[1:],191 data.indices,192 data.data)193 # Check return194 _check_return_value(ret_val, handle_func.__name__)195 return ref196def _create_mkl_sparse_bsr(matrix):197 """198 Create MKL internal representation for BSR matrix199 :param matrix: Sparse data200 :type matrix: scipy.sparse.bsr_matrix201 :return ref: Handle for the MKL internal representation202 :rtype: sparse_matrix_t203 """204 double_precision, complex_type = _is_double(matrix)205 handle_func = _create_functions[('bsr', double_precision, complex_type)]206 # Get the blocksize and check that the blocks are square207 _blocksize = matrix.blocksize[0]208 if _blocksize != matrix.blocksize[1]:209 _err = "MKL BSR representation requires square blocks; {n} blocks provided".format(n=matrix.blocksize)210 raise ValueError(_err)211 if (matrix.shape[0] % _blocksize != 0) or (matrix.shape[1] % _blocksize != 0):212 _err = "BSR blocks {n} do not align with dims {m}".format(n=matrix.blocksize, m=matrix.shape)213 raise ValueError(_err)214 _block_rows = int(matrix.shape[0] / _blocksize)215 _block_cols = int(matrix.shape[1] / _blocksize)216 # Get the data block array structure217 _layout, _ = _get_numpy_layout(matrix.data)218 # Create a pointer for the output matrix219 ref = sparse_matrix_t()220 # Load into a MKL data structure and check return221 ret_val = handle_func(_ctypes.byref(ref),222 _ctypes.c_int(SPARSE_INDEX_BASE_ZERO),223 _ctypes.c_int(_layout),224 MKL.MKL_INT(_block_rows),225 MKL.MKL_INT(_block_cols),226 MKL.MKL_INT(_blocksize),227 matrix.indptr[0:-1],228 matrix.indptr[1:],229 matrix.indices,230 matrix.data)231 # Check return232 _check_return_value(ret_val, handle_func.__name__)233 return ref234def _export_mkl(csr_mkl_handle, double_precision, complex_type=False, output_type="csr"):235 """236 Export a MKL sparse handle of CSR or CSC type237 :param csr_mkl_handle: Handle for the MKL internal representation238 :type csr_mkl_handle: sparse_matrix_t239 :param double_precision: Use float64 if True, float32 if False. This MUST match the underlying float type - this240 defines a memory view, it does not cast.241 :type double_precision: bool242 :param output_type: The structure of the MKL handle (and therefore the type of scipy sparse to create)243 :type output_type: str244 :return: Sparse matrix in scipy format245 :rtype: scipy.spmatrix246 """247 output_type = output_type.lower()248 if output_type == "bsr":249 return _export_mkl_sparse_bsr(csr_mkl_handle, double_precision, complex_type=complex_type)250 elif output_type == "csr" or output_type == "csc":251 out_func = _export_functions[(output_type, double_precision, complex_type)]252 sp_matrix_constructor = _spsparse.csr_matrix if output_type == "csr" else _spsparse.csc_matrix253 else:254 raise ValueError("Only CSR, CSC, and BSR output types are supported")255 # Allocate for output256 ordering, nrows, ncols, indptrb, indptren, indices, data = _allocate_for_export(double_precision)257 final_dtype = _output_dtypes[(double_precision, complex_type)]258 ret_val = out_func(csr_mkl_handle,259 _ctypes.byref(ordering),260 _ctypes.byref(nrows),261 _ctypes.byref(ncols),262 _ctypes.byref(indptrb),263 _ctypes.byref(indptren),264 _ctypes.byref(indices),265 _ctypes.byref(data))266 # Check return267 _check_return_value(ret_val, out_func.__name__)268 # Check ordering269 if ordering.value != 0:270 raise ValueError("1-indexing (F-style) is not supported")271 # Get matrix dims272 ncols, nrows = ncols.value, nrows.value273 # If any axis is 0 return an empty matrix274 if nrows == 0 or ncols == 0:275 return sp_matrix_constructor((nrows, ncols), dtype=final_dtype)276 # Get the index dimension277 index_dim = nrows if output_type == "csr" else ncols278 # Construct a numpy array and add 0 to first position for scipy.sparse's 3-array indexing279 indptrb = as_array(indptrb, shape=(index_dim,))280 indptren = as_array(indptren, shape=(index_dim,))281 indptren = _np.insert(indptren, 0, indptrb[0])282 nnz = indptren[-1] - indptrb[0]283 # If there are no non-zeros, return an empty matrix284 # If the number of non-zeros is insane, raise a ValueError285 if nnz == 0:286 return sp_matrix_constructor((nrows, ncols), dtype=final_dtype)287 elif nnz < 0 or nnz > ncols * nrows:288 raise ValueError("Matrix ({m} x {n}) is attempting to index {z} elements".format(m=nrows, n=ncols, z=nnz))289 # Construct numpy arrays from data pointer and from indicies pointer290 data = _np.array(as_array(data, shape=(nnz * 2 if complex_type else nnz,)), copy=True)291 indices = _np.array(as_array(indices, shape=(nnz,)), copy=True)292 293 # Pack and return the matrix294 return sp_matrix_constructor((data.view(final_dtype) if complex_type else data, indices, indptren),295 shape=(nrows, ncols))296def _export_mkl_sparse_bsr(bsr_mkl_handle, double_precision, complex_type=False):297 """298 Export a BSR matrix from MKL's internal representation to scipy299 :param bsr_mkl_handle: MKL internal representation300 :type bsr_mkl_handle: sparse_matrix_t301 :param double_precision: Use float64 if True, float32 if False. This MUST match the underlying float type - this302 defines a memory view, it does not cast.303 :type double_precision: bool304 :return: Sparse BSR matrix305 :rtype:306 """307 # Allocate for output308 ordering, nrows, ncols, indptrb, indptren, indices, data = _allocate_for_export(double_precision)309 block_layout = _ctypes.c_int()310 block_size = MKL.MKL_INT()311 # Set output312 out_func = _export_functions[('bsr', double_precision, complex_type)]313 final_dtype = _output_dtypes[(double_precision, complex_type)]314 ret_val = out_func(bsr_mkl_handle,315 _ctypes.byref(ordering),316 _ctypes.byref(block_layout),317 _ctypes.byref(nrows),318 _ctypes.byref(ncols),319 _ctypes.byref(block_size),320 _ctypes.byref(indptrb),321 _ctypes.byref(indptren),322 _ctypes.byref(indices),323 _ctypes.byref(data))324 # Check return325 _check_return_value(ret_val, out_func.__name__)326 # Get matrix dims327 ncols, nrows, block_size = ncols.value, nrows.value, block_size.value328 index_dim, block_dims = nrows, (block_size, block_size)329 ncols, nrows = ncols * block_size, nrows * block_size330 # If any axis is 0 return an empty matrix331 if nrows == 0 or ncols == 0:332 return _spsparse.bsr_matrix((nrows, ncols), dtype=final_dtype, blocksize=block_dims)333 ordering = "F" if ordering.value == LAYOUT_CODE_F else "C"334 # Construct a numpy array and add 0 to first position for scipy.sparse's 3-array indexing335 indptrb = as_array(indptrb, shape=(index_dim,))336 indptren = as_array(indptren, shape=(index_dim,))337 indptren = _np.insert(indptren, 0, indptrb[0])338 nnz_blocks = (indptren[-1] - indptrb[0])339 nnz = nnz_blocks * (block_size ** 2)340 # If there's no non-zero data, return an empty matrix341 if nnz_blocks == 0:342 return _spsparse.bsr_matrix((nrows, ncols), dtype=final_dtype, blocksize=block_dims)343 344 elif nnz_blocks < 0 or nnz > ncols * nrows:345 _err = "Matrix ({m} x {n}) is attempting to index {z} elements as {b} {bs} blocks"346 _err = _err.format(m=nrows, n=ncols, z=nnz, b=nnz_blocks, bs=(block_size, block_size))347 raise ValueError(_err)348 nnz_row_block = block_size if not complex_type else block_size * 2349 data = _np.array(as_array(data, shape=(nnz_blocks, block_size, nnz_row_block)), copy=True, order=ordering)350 indices = _np.array(as_array(indices, shape=(nnz_blocks,)), copy=True)351 return _spsparse.bsr_matrix((data.view(final_dtype) if complex_type else data, indices, indptren),352 shape=(nrows, ncols), blocksize=block_dims)353def _allocate_for_export(double_precision):354 """355 Get pointers for output from MKL internal representation356 :param double_precision: Allocate an output pointer of doubles357 :type double_precision: bool358 :return: ordering, nrows, ncols, indptrb, indptren, indices, data359 :rtype: c_int, MKL_INT, MKL_INT, MKL_INT*, MKL_INT*, MKL_INT*, c_float|c_double*360 """361 # Create the pointers for the output data362 indptrb = _ctypes.POINTER(MKL.MKL_INT)()363 indptren = _ctypes.POINTER(MKL.MKL_INT)()364 indices = _ctypes.POINTER(MKL.MKL_INT)()365 ordering = _ctypes.c_int()366 nrows = MKL.MKL_INT()367 ncols = MKL.MKL_INT()368 data = _ctypes.POINTER(_ctypes.c_double)() if double_precision else _ctypes.POINTER(_ctypes.c_float)()369 return ordering, nrows, ncols, indptrb, indptren, indices, data370def _check_return_value(ret_val, func_name):371 """372 Check the return value from a sparse function373 :param ret_val:374 :param func_name:375 :return:376 """377 if ret_val != 0:378 err_msg = "{fn} returned {v} ({e})".format(fn=func_name, v=ret_val, e=RETURN_CODES[ret_val])379 if ret_val == 2:380 err_msg += "; " + ILP64_MSG381 raise ValueError(err_msg)382 elif MKL.MKL_DEBUG:383 print("{fn} returned {v} ({e})".format(fn=func_name, v=ret_val, e=RETURN_CODES[ret_val]))384 else:385 return386def _destroy_mkl_handle(ref_handle):387 """388 Deallocate a MKL sparse handle389 :param ref_handle:390 :type ref_handle: sparse_matrix_t391 """392 ret_val = MKL._mkl_sparse_destroy(ref_handle)393 _check_return_value(ret_val, "mkl_sparse_destroy")394def _order_mkl_handle(ref_handle):395 """396 Reorder indexes in a MKL sparse handle397 :param ref_handle:398 :type ref_handle: sparse_matrix_t399 """400 ret_val = MKL._mkl_sparse_order(ref_handle)401 _check_return_value(ret_val, "mkl_sparse_order")402def _convert_to_csr(ref_handle, destroy_original=False):403 """404 Convert a MKL sparse handle to CSR format405 :param ref_handle:406 :type ref_handle: sparse_matrix_t407 :return:408 """409 csr_ref = sparse_matrix_t()410 ret_val = MKL._mkl_sparse_convert_csr(ref_handle, _ctypes.c_int(10), _ctypes.byref(csr_ref))411 try:412 _check_return_value(ret_val, "mkl_sparse_convert_csr")413 except ValueError:414 try:415 _destroy_mkl_handle(csr_ref)416 except ValueError:417 pass418 raise419 if destroy_original:420 _destroy_mkl_handle(ref_handle)421 return csr_ref422def _sanity_check(matrix_a, matrix_b, allow_vector=False):423 """424 Check matrix dimensions425 :param matrix_a: sp.sparse or numpy array426 :param matrix_b: sp.sparse or numpy array427 """428 a_2d, b_2d = matrix_a.ndim == 2, matrix_b.ndim == 2429 a_vec, b_vec = _is_dense_vector(matrix_a), _is_dense_vector(matrix_b)430 # Check to make sure that both matrices are 2-d431 if not allow_vector and (not a_2d or not b_2d):432 err_msg = "Matrices must be 2d: {m1} * {m2} is not valid".format(m1=matrix_a.shape, m2=matrix_b.shape)433 raise ValueError(err_msg)434 invalid_ndims = not (a_2d or a_vec) or not (b_2d or b_vec)435 invalid_align = (matrix_a.shape[1] if not matrix_a.ndim == 1 else matrix_a.shape[0]) != matrix_b.shape[0]436 # Check to make sure that this multiplication can work437 if invalid_align or invalid_ndims:438 err_msg = "Matrix alignment error: {m1} * {m2} is not valid".format(m1=matrix_a.shape, m2=matrix_b.shape)439 raise ValueError(err_msg)440def _cast_to(matrix, dtype):441 """ Make a copy of the array as double precision floats or return the reference if it already is"""442 return matrix.astype(dtype) if matrix.dtype != dtype else matrix443def _is_valid_dtype(matrix, complex_dtype=False, all_dtype=False):444 """ Check to see if it's a usable float dtype """445 if all_dtype:446 return matrix.dtype in NUMPY_FLOAT_DTYPES + NUMPY_COMPLEX_DTYPES447 elif complex_dtype:448 return matrix.dtype in NUMPY_COMPLEX_DTYPES449 else:450 return matrix.dtype in NUMPY_FLOAT_DTYPES451def _type_check(452 matrix_a,453 matrix_b=None,454 cast=False,455 allow_complex=True456):457 """458 Make sure that both matrices are single precision floats or both are double precision floats459 If not, convert to double precision floats if cast is True, or raise an error if cast is False460 """461 _n_complex = _np.iscomplexobj(matrix_a) + _np.iscomplexobj(matrix_b)462 if not allow_complex and _n_complex > 0:463 raise ValueError(464 "Complex datatypes are not supported"465 ) 466 # If there's no matrix B and matrix A is valid dtype, return it467 if matrix_b is None and _is_valid_dtype(matrix_a, all_dtype=True):468 return matrix_a469 # If matrix A is complex but not csingle or cdouble, and cast is True, convert it to a cdouble470 elif matrix_b is None and cast and _n_complex == 1:471 return _cast_to(matrix_a, _np.cdouble)472 # If matrix A is real but not float32 or float64, and cast is True, convert it to a float64473 elif matrix_b is None and cast:474 return _cast_to(matrix_a, _np.float64)475 # Raise an error - the dtype is invalid and cast is False476 elif matrix_b is None:477 _err_msg = f"Matrix data type must be float32, float64, csingle, or cdouble; {matrix_a.dtype} provided"478 raise ValueError(_err_msg)479 # If Matrix A & B have the same valid dtype, return them480 if _is_valid_dtype(matrix_a, all_dtype=True) and matrix_a.dtype == matrix_b.dtype:481 return matrix_a, matrix_b482 # If neither matrix is complex and cast is True, convert to float64s and return them483 elif cast and _n_complex == 0:484 debug_print(f"Recasting matrix data types {matrix_a.dtype} and {matrix_b.dtype} to _np.float64")485 return _cast_to(matrix_a, _np.float64), _cast_to(matrix_b, _np.float64)486 # If both matrices are complex and cast is True, convert to cdoubles and return them487 elif cast and _n_complex == 2:488 debug_print(f"Recasting matrix data types {matrix_a.dtype} and {matrix_b.dtype} to _np.cdouble")489 return _cast_to(matrix_a, _np.cdouble), _cast_to(matrix_b, _np.cdouble)490 # Cast reals and complex matrices together491 elif cast and _n_complex == 1 and _is_valid_dtype(matrix_a, complex_dtype=True):492 debug_print(f"Recasting matrix data type {matrix_b.dtype} to {matrix_a.dtype}")493 return matrix_a, _cast_to(matrix_b, matrix_a.dtype)494 elif cast and _n_complex == 1 and _is_valid_dtype(matrix_b, complex_dtype=True):495 debug_print(f"Recasting matrix data type {matrix_a.dtype} to {matrix_b.dtype}")496 return _cast_to(matrix_a, matrix_b.dtype), matrix_b497 elif cast and _n_complex == 1:498 debug_print(f"Recasting matrix data type {matrix_a.dtype} and {matrix_b.dtype} to _np.cdouble")499 return _cast_to(matrix_a, _np.cdouble), _cast_to(matrix_b, _np.cdouble)500 # If cast is False, can't cast anything together501 elif not cast:502 raise ValueError(503 "Matrix data type must be float32, float64, csingle, or cdouble, " +504 "and must be the same if cast=False; " +505 f"{matrix_a.dtype} & {matrix_b.dtype} provided"506 )507def _mkl_scalar(scalar, complex_type, double_precision):508 """Turn complex scalars into appropriate precision MKL scalars or leave floats as floats"""509 scalar = 1. if scalar is None else scalar510 if complex_type and double_precision:511 return MKL_Complex16(complex(scalar))512 elif complex_type:513 return MKL_Complex8(complex(scalar))514 else:515 return float(scalar)516def _out_matrix(517 shape,518 dtype,519 order="C",520 out_arr=None,521 out_t=False522):523 """524 Create an all-zero matrix or check to make sure that525 the provided output array matches526 :param shape: Required output shape527 :type shape: tuple(int)528 :param dtype: Required output data type529 :type dtype: _np.dtype530 :param order: Array order (row or column-major)531 :type order: str532 :param out_arr: Provided output array533 :type out_arr: _np.ndarray534 :param out_t: Out array has been transposed535 :type out_t: bool536 :return: Array537 :rtype: _np.ndarray538 """539 out_t = False if out_t is None else out_t540 # If there's no output array allocate a new array and return it541 if out_arr is None:542 return _np.zeros(shape, dtype=dtype, order=order)543 # Check and make sure the order is correct544 # Note 1d arrays have both flags set545 if order == "C":546 _order_match = out_arr.flags['C_CONTIGUOUS']547 else:548 _order_match = out_arr.flags['F_CONTIGUOUS']549 # If there are any incompatible parameters, raise an error550 # with the provided and required array parameters551 # Flip them if out_T is set so that the original values and552 # the values which would have to be provided are correct553 if (shape != out_arr.shape or554 dtype != out_arr.dtype or555 not _order_match or556 not out_arr.data.contiguous557 ):558 _c_contig = out_arr.flags['C_CONTIGUOUS']559 _f_contig = out_arr.flags['F_CONTIGUOUS']560 if not out_t or out_arr.ndim == 1:561 _err_shape = out_arr.shape562 _req_shape = shape563 _err_order = "C" if _c_contig else "F"564 _req_order = order565 else:566 _err_shape = out_arr.shape[::-1]567 _req_shape = shape[::-1]568 _err_order = "F" if _c_contig and not _f_contig else "C"569 _req_order = "C" if order == "F" else "F"570 try:571 _req_dtype = dtype.__name__572 except AttributeError:573 _req_dtype = dtype.name574 raise ValueError(575 "Provided out array is "576 f"{_err_shape} {out_arr.dtype} [{_err_order}"577 f"_{'CONTIGUOUS' if out_arr.data.contiguous else 'NONCONTIGUOUS'}]"578 f" and product requires "579 f"{_req_shape} {_req_dtype} [{_req_order}_CONTIGUOUS]"580 )581 else:582 return out_arr583def _is_dense_vector(m_or_v):584 return not _spsparse.issparse(m_or_v) and ((m_or_v.ndim == 1) or ((m_or_v.ndim == 2) and min(m_or_v.shape) == 1))585def _is_double(arr):586 """587 Return bools corresponding to float precision & real/complex.588 :param arr: 2d array589 :type arr: np.ndarray, scipy.sparse.spmatrix590 :return: False(single) or True(double), False(real) or True(complex)591 :rtype: bool, bool592 """593 # Figure out which dtype for data594 if arr.dtype == _np.float32:595 return False, False596 elif arr.dtype == _np.float64:597 return True, False598 elif arr.dtype == _np.csingle:599 return False, True600 elif arr.dtype == _np.cdouble:601 return True, True602 else:603 raise ValueError("Only float32, float64, csingle, and cdouble dtypes are supported")604def _is_allowed_sparse_format(matrix):605 """606 Return True if the matrix is dense or a sparse format we can turn into an MKL object. False otherwise.607 :param matrix:608 :return:609 :rtype: bool610 """611 if _spsparse.isspmatrix(matrix):612 return _spsparse.isspmatrix_csr(matrix) or _spsparse.isspmatrix_csc(matrix) or _spsparse.isspmatrix_bsr(matrix)613 else:614 return True615def _empty_output_check(matrix_a, matrix_b):616 """Check for trivial cases where an empty array should be produced"""617 # One dimension is zero618 if min([*matrix_a.shape, *matrix_b.shape]) == 0:619 return True620 # The sparse array is empty621 elif _spsparse.issparse(matrix_a) and min(matrix_a.data.size, matrix_a.indices.size) == 0:622 return True623 elif _spsparse.issparse(matrix_b) and min(matrix_b.data.size, matrix_b.indices.size) == 0:624 return True625 # Neither trivial condition626 else:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful