Best Python code snippet using pandera_python
mlarray.py
Source:mlarray.py
1# Copyright 2014-2015 MathWorks, Inc.2"""3Type-specific multidimensional arrays for4use when working with MATLAB.5This module defines type-specific multidimensional arrays to use when6evaluating functions using MATLAB. They are different from Python7sequences in the following ways:8 * They use strong typing. They can only contain values of the9 specified type. Attempting to insert values that cannot be represented10 in the specified type raises an exception.11 * They are multidimensional. The size of an empty array is (0,0).12 All arrays created using these classes are rectangular.13 * They use MATLAB style indexing.14 * They slice using views and not shallow copies.15Classes16-------17 * double - array of Python float seen as MATLAB double18 * single - array of Python float seen as MATLAB single19 * uint8 - array of Python int seen as MATLAB uint820 * int8 - array of Python int seen as MATLAB int821 * uint16 - array of Python int seen as MATLAB uint1622 * int16 - array of Python int seen as MATLAB int1623 * uint32 - array of Python int or long seen as MATLAB uint3224 * int32 - array of Python int seen as MATLAB int3225 * uint64 - array of Python int or long seen as MATLAB uint6426 * int64 - array of Python int or long seen as MATLAB int6427 * logical - array of Python bool seen as MATLAB logical28"""29from _internal.mlarray_sequence import _MLArrayMetaClass30class double(_MLArrayMetaClass):31 def __init__(self, initializer=None, size=None, is_complex=False):32 """33 A new matlab array whose items are initialized from the optional34 "initializer" value which must be a sequence. Initializer will be35 marshaled as an array of doubles,if possible, inside of MATLAB.36 "is_complex" can be set to True if the elements should be marshaled37 in as complex values.38 :param initializer: sequence39 :param size: sequence40 :param is_complex: bool41 """42 try:43 super(double, self).__init__('d', initializer, size, is_complex)44 except Exception as ex:45 raise ex46class single(_MLArrayMetaClass):47 def __init__(self, initializer=None, size=None, is_complex=False):48 """49 A new matlab array whose items are initialized from the optional50 "initializer" value which must be a sequence. Initializer will be51 marshaled as an array of singles,if possible, inside of MATLAB.52 "is_complex" can be set to True if the elements should be marshaled53 in as complex values.54 :param initializer: sequence55 :param size: sequence56 :param is_complex: bool57 """58 try:59 super(single, self).__init__('f', initializer, size, is_complex)60 except Exception as ex:61 raise ex62class uint8(_MLArrayMetaClass):63 def __init__(self, initializer=None, size=None, is_complex=False):64 """65 A new matlab array whose items are initialized from the optional66 "initializer" value which must be a sequence. Initializer will be67 marshaled as an array of uint8,if possible, inside of MATLAB.68 "is_complex" can be set to True if the elements should be marshaled69 in as complex values.70 :param initializer: sequence71 :param size: sequence72 :param is_complex: bool73 """74 try:75 super(uint8, self).__init__('B', initializer, size, is_complex)76 except Exception as ex:77 raise ex78class int8(_MLArrayMetaClass):79 def __init__(self, initializer=None, size=None, is_complex=False):80 """81 A new matlab array whose items are initialized from the optional82 "initializer" value which must be a sequence. Initializer will be83 marshaled as an array of int8,if possible, inside of MATLAB.84 "is_complex" can be set to True if the elements should be marshaled85 in as complex values.86 :param initializer: sequence87 :param size: sequence88 :param is_complex: bool89 """90 try:91 super(int8, self).__init__('b', initializer, size, is_complex)92 except Exception as ex:93 raise ex94class uint16(_MLArrayMetaClass):95 def __init__(self, initializer=None, size=None, is_complex=False):96 """97 A new matlab array whose items are initialized from the optional98 "initializer" value which must be a sequence. Initializer will be99 marshaled as an array of uint16,if possible, inside of MATLAB.100 "is_complex" can be set to True if the elements should be marshaled101 in as complex values.102 :param initializer: sequence103 :param size: sequence104 :param is_complex: bool105 """106 try:107 super(uint16, self).__init__('H', initializer, size, is_complex)108 except Exception as ex:109 raise ex110class int16(_MLArrayMetaClass):111 def __init__(self, initializer=None, size=None, is_complex=False):112 """113 A new matlab array whose items are initialized from the optional114 "initializer" value which must be a sequence. Initializer will be115 marshaled as an array of int16,if possible, inside of MATLAB.116 "is_complex" can be set to True if the elements should be marshaled117 in as complex values.118 :param initializer: sequence119 :param size: sequence120 :param is_complex: bool121 """122 try:123 super(int16, self).__init__('h', initializer, size, is_complex)124 except Exception as ex:125 raise ex126class uint32(_MLArrayMetaClass):127 def __init__(self, initializer=None, size=None, is_complex=False):128 """129 A new matlab array whose items are initialized from the optional130 "initializer" value which must be a sequence. Initializer will be131 marshaled as an array of unit32,if possible, inside of MATLAB.132 "is_complex" can be set to True if the elements should be marshaled133 in as complex values.134 :param initializer: sequence135 :param size: sequence136 :param is_complex: bool137 """138 try:139 super(uint32, self).__init__('I', initializer, size, is_complex)140 except Exception as ex:141 raise ex142class int32(_MLArrayMetaClass):143 def __init__(self, initializer=None, size=None, is_complex=False):144 """145 A new matlab array whose items are initialized from the optional146 "initializer" value which must be a sequence. Initializer will be147 marshaled as an array of int32,if possible, inside of MATLAB.148 "is_complex" can be set to True if the elements should be marshaled149 in as complex values.150 :param initializer: sequence151 :param size: sequence152 :param is_complex: bool153 """154 try:155 super(int32, self).__init__('i', initializer, size, is_complex)156 except Exception as ex:157 raise ex158class uint64(_MLArrayMetaClass):159 def __init__(self, initializer=None, size=None, is_complex=False):160 """161 A new matlab array whose items are initialized from the optional162 "initializer" value which must be a sequence. Initializer will be163 marshaled as an array of uint64,if possible, inside of MATLAB.164 "is_complex" can be set to True if the elements should be marshaled165 in as complex values.166 :param initializer: sequence167 :param size: sequence168 :param is_complex: bool169 """170 try:171 super(uint64, self).__init__('L', initializer, size, is_complex)172 except Exception as ex:173 raise ex174class int64(_MLArrayMetaClass):175 def __init__(self, initializer=None, size=None, is_complex=False):176 """177 A new matlab array whose items are initialized from the optional178 "initializer" value which must be a sequence. Initializer will be179 marshaled as an array of int64,if possible, inside of MATLAB.180 "is_complex" can be set to True if the elements should be marshaled181 in as complex values.182 :param initializer: sequence183 :param size: sequence184 :param is_complex: bool185 """186 try:187 super(int64, self).__init__('l', initializer, size, is_complex)188 except Exception as ex:189 raise ex190class logical(_MLArrayMetaClass):191 def __init__(self, initializer=None, size=None):192 """193 A new matlab array whose items are initialized from the optional194 "initializer" value which must be a sequence. Initializer will be195 marshaled as an array of logicals,if possible, inside of MATLAB.196 :param initializer: sequence197 :param size: sequence198 """199 try:200 super(logical, self).__init__('B', initializer, size)201 except Exception as ex:202 raise ex203 def __getitem__(self, index):204 value = super(logical, self).__getitem__(index)205 if isinstance(value, type(self)):206 return value207 else:...
helper_functions.py
Source:helper_functions.py
1import numpy as np2import pytest3from integration_test_functions import Polynomial, Exponential, Sinusoid4from utils.set_up_backend import set_up_backend5from utils.set_log_level import set_log_level6def get_test_functions(dim, backend):7 """Here we define a bunch of functions that will be used for testing.8 Args:9 dim (int): Dimensionality of test functions to use.10 backend (string): Numerical backend used for the integration11 """12 if dim == 1:13 return [14 # Real numbers15 Polynomial(4.0, [2.0], is_complex=False, backend=backend), # y = 216 Polynomial(0, [0, 1], is_complex=False, backend=backend), # y = x17 Polynomial(18 2 / 3, [0, 0, 2], domain=[[0, 1]], is_complex=False, backend=backend19 ), # y = 2x^220 # y = -3x^3+2x^2-x+321 Polynomial(22 27.75,23 [3, -1, 2, -3],24 domain=[[-2, 1]],25 is_complex=False,26 backend=backend,27 ),28 # y = 7x^4-3x^3+2x^2-x+329 Polynomial(30 44648.0 / 15.0,31 [3, -1, 2, -3, 7],32 domain=[[-4, 4]],33 is_complex=False,34 backend=backend,35 ),36 # # y = -x^5+7x^4-3x^3+2x^2-x+337 Polynomial(38 8939.0 / 60.0,39 [3, -1, 2, -3, 7, -1],40 domain=[[2, 3]],41 is_complex=False,42 backend=backend,43 ),44 Exponential(45 np.exp(1) - np.exp(-2),46 domain=[[-2, 1]],47 is_complex=False,48 backend=backend,49 ),50 Exponential(51 (np.exp(2) - 1.0) / np.exp(3),52 domain=[[-3, -1]],53 is_complex=False,54 backend=backend,55 ),56 Sinusoid(57 2 * np.sin(1) * np.sin(1),58 domain=[[0, 2]],59 is_complex=False,60 backend=backend,61 ),62 #63 # Complex numbers64 Polynomial(4.0j, [2.0j], is_complex=True, backend=backend), # y = 2j65 Polynomial(0, [0, 1j], is_complex=True, backend=backend), # y = xj66 # y=7x^4-3jx^3+2x^2-jx+367 Polynomial(68 44648.0 / 15.0,69 [3, -1j, 2, -3j, 7],70 domain=[[-4, 4]],71 is_complex=True,72 backend=backend,73 ),74 ]75 elif dim == 3:76 return [77 # Real numbers78 Polynomial(79 48.0, [2.0], dim=3, is_complex=False, backend=backend80 ), # f(x,y,z) = 281 Polynomial(82 0, [0, 1], dim=3, is_complex=False, backend=backend83 ), # f(x,y,z) = x + y + z84 # f(x,y,z) = x^2+y^2+z^285 Polynomial(8.0, coeffs=[0, 0, 1], dim=3, is_complex=False, backend=backend),86 # e^x+e^y+e^z87 Exponential(88 27 * (np.exp(3) - 1) / np.exp(2),89 dim=3,90 domain=[[-2, 1], [-2, 1], [-2, 1]],91 is_complex=False,92 backend=backend,93 ),94 Sinusoid(95 24 * np.sin(1) ** 2,96 dim=3,97 domain=[[0, 2], [0, 2], [0, 2]],98 is_complex=False,99 backend=backend,100 ),101 # e^x+e^y+e^z102 Exponential(103 1.756,104 dim=3,105 domain=[[-0.05, 0.1], [-0.25, 0.2], [-np.exp(1), np.exp(1)]],106 is_complex=False,107 backend=backend,108 ),109 #110 # Complex numbers111 Polynomial(112 48.0j, [2.0j], dim=3, is_complex=True, backend=backend113 ), # f(x,y,z) = 2j114 Polynomial(115 0, [0, 1.0j], dim=3, is_complex=True, backend=backend116 ), # f(x,y,z) = xj117 Polynomial(118 8.0j, coeffs=[0, 0, 1.0j], dim=3, is_complex=True, backend=backend119 ), # j*x^2+j*y^2+j*z^2120 ]121 elif dim == 10:122 return [123 # Real numbers124 # f(x_1, ..., x_10) = x_1^2+x_2^2+...125 Polynomial(126 3413.33333333,127 coeffs=[0, 0, 1],128 dim=10,129 is_complex=False,130 backend=backend,131 ),132 # Complex numbers133 # f(x_1, ..., x_10) = j*x_1^2+j*x_2^2+...134 Polynomial(135 3413.33333333j,136 coeffs=[0, 0, 1.0j],137 dim=10,138 is_complex=True,139 backend=backend,140 ),141 ]142 else:143 raise ValueError("Not testing functions implemented for dim " + str(dim))144def compute_integration_test_errors(145 integrator,146 integrator_args,147 dim,148 use_complex,149 backend,150):151 """Computes errors on all test functions for given dimension and integrator.152 Args:153 integrator (torchquad.base_integrator): Integrator to use.154 integrator_args (dict): Arguments for the integrator.155 dim (int): Dimensionality of the example functions to choose.156 use_complex (Boolean): If True, skip complex example functions.157 backend (string): Numerical backend for the example functions.158 Returns:159 (list, list): Absolute errors on all example functions and the chosen160 example functions161 """162 errors = []163 chosen_functions = []164 # Compute integration errors on the chosen functions and remember those165 # functions166 for test_function in get_test_functions(dim, backend):167 if not use_complex and test_function.is_complex:168 continue169 if backend == "torch":170 errors.append(171 np.abs(172 test_function.evaluate(integrator, integrator_args)173 .cpu()174 .detach()175 .numpy()176 - test_function.expected_result177 )178 )179 else:180 errors.append(181 np.abs(182 test_function.evaluate(integrator, integrator_args)183 - test_function.expected_result184 )185 )186 chosen_functions.append(test_function)187 return errors, chosen_functions188def setup_test_for_backend(test_func, backend, dtype_name):189 """190 Create a function to execute a test function with the given numerical backend.191 If the backend is not installed, skip the test.192 Args:193 test_func (function(backend, dtype_name)): The function which runs tests194 backend (string): The numerical backend195 dtype_name ("float32", "float64" or None): Floating point precision. If None, the global precision is not changed.196 Returns:197 function: A test function for Pytest198 """199 def func():200 pytest.importorskip(backend)201 set_log_level("INFO")202 set_up_backend(backend, dtype_name)203 if dtype_name is None:204 return test_func(backend)205 return test_func(backend, dtype_name)...
common.py
Source:common.py
1#2# Some common useful functions3#4import os5import numpy as np6import gdal7def get_basename(filepath):8 """9 Get filename basename without extension10 For example:11 >>> get_basename("/path/to/a/file.123sdufg.sdfs.tiff")12 'file.123sdufg.sdfs'13 """14 bfn = os.path.basename(filepath)15 splt = bfn.split(os.extsep)16 return os.extsep.join(splt[:-1]) if len(splt) > 1 else splt[0]17def get_dtype(depth, is_complex, signed=True):18 """19 Method to convert the pair (depth={1,2,4,8}, is_complex={True,False})20 into numpy dtype21 For example,22 >>> get_type(4, False)23 <type 'numpy.float32'>24 """25 if depth == 1 and not is_complex:26 return np.uint827 elif depth == 2 and not is_complex:28 return np.uint16 if not signed else np.int1629 elif depth == 4 and not is_complex:30 return np.float3231 elif depth == 8 and not is_complex:32 return np.float6433 elif depth == 8 and is_complex:34 return np.complex6435 elif depth == 16 and is_complex:36 return np.complex12837 else:38 raise AssertionError("Data type is not recognized")39def get_gdal_dtype(depth, is_complex, signed=True):40 """41 Method to convert the pair (depth={1,2,4,8}, is_complex={True,False})42 If is_complex == True, depth corresponds real and imaginary parts43 to GDAL data type : gdal.GDT_Byte, ...44 >>> get_gdal_dtype(4, False) == gdal.GDT_Float3245 True46 >>> get_gdal_dtype(8, True) == gdal.GDT_CFloat3247 True48 """49 if depth == 1 and not is_complex:50 return gdal.GDT_Byte51 elif depth == 2 and not is_complex:52 return gdal.GDT_UInt16 if not signed else gdal.GDT_Int1653 elif depth == 4 and not is_complex:54 return gdal.GDT_Float3255 elif depth == 8 and not is_complex:56 return gdal.GDT_Float6457 elif depth == 8 and is_complex:58 return gdal.GDT_CFloat3259 elif depth == 16 and is_complex:60 return gdal.GDT_CFloat6461 else:62 raise AssertionError("Data type is not recognized")63def gdal_to_numpy_datatype(gdal_datatype):64 """65 Method to convert gdal data type to numpy dtype66 >>> gdal_to_numpy_datatype(gdal.GDT_Float32) == np.float3267 True68 """69 if gdal_datatype == gdal.GDT_Byte:70 return np.uint871 elif gdal_datatype == gdal.GDT_Int16:72 return np.int1673 elif gdal_datatype == gdal.GDT_Int32:74 return np.int3275 elif gdal_datatype == gdal.GDT_UInt16:76 return np.uint1677 elif gdal_datatype == gdal.GDT_UInt32:78 return np.uint3279 elif gdal_datatype == gdal.GDT_Float32:80 return np.float3281 elif gdal_datatype == gdal.GDT_Float64:82 return np.float6483 elif gdal_datatype == gdal.GDT_CInt16:84 # No associated type -> cast to complex6485 return np.complex6486 elif gdal_datatype == gdal.GDT_CInt32:87 # No associated type -> cast to complex6488 return np.complex6489 elif gdal_datatype == gdal.GDT_CFloat32:90 return np.complex6491 elif gdal_datatype == gdal.GDT_CFloat64:92 return np.complex12893 else:94 raise AssertionError("Data type '%i' is not recognized" % gdal_datatype)95def numpy_to_gdal_datatype(dtype):96 """97 Method to convert numpy data type to gdal dtype98 """99 if dtype == np.uint8:100 return gdal.GDT_Byte101 elif dtype == np.int16:102 return gdal.GDT_Int16103 elif dtype == np.int32:104 return gdal.GDT_Int32105 elif dtype == np.uint16:106 return gdal.GDT_UInt16107 elif dtype == np.uint32:108 return gdal.GDT_UInt32109 elif dtype == np.float32:110 return gdal.GDT_Float32111 elif dtype == np.float64:112 return gdal.GDT_Float64113 elif dtype == np.complex64:114 return gdal.GDT_CFloat32115 elif dtype == np.complex128:116 return gdal.GDT_CFloat64117 else:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!