How to use fakefun method in tavern

Best Python code snippet using tavern

two_dim.py

Source:two_dim.py Github

copy

Full Screen

1'''2 Two dimensional measured objects where the second abscissa variable is either3 * discrete (:class:`FunctionBundle`), or4 * continuous (:class:`MeasuredSurface`)5'''6import matplotlib.pyplot as plt7import numpy as np8from scipy import interpolate9import matplotlib.cm as cm10from functools import wraps11from itertools import repeat12from .one_dim import MeasuredFunction, Waveform13from lightlab.laboratory import Hashable14class FunctionBundle(Hashable): # pylint: disable=eq-without-hash15 ''' A bundle of :class:`~lightlab.util.data.one_dim.MeasuredFunction`'s: "z" vs. "x", "i"16 The key is that they have the same abscissa base.17 This class will take care of resampling in a common abscissa base.18 The bundle can be:19 * iterated to get the individual :class`~lightlab.util.data.one_dim.MeasuredFunction`'s20 * operated on with other ``FunctionBundles``21 * plotted with :meth`simplePlot` and :meth:`multiAxisPlot`22 Feeds through **callable** signal processing methods to its members (type MeasuredFunction),23 If the method is not found in the FunctionBundle, and it is in it's member,24 it will be mapped to every function in the bundle, returning a new bundle.25 Distinct from a :class:`MeasuredSurface` because26 the additional axis does not represent a continuous thing.27 It is discrete and sometimes unordered.28 Distinct from a :class:`FunctionalBasis` because29 it does not support most linear algebra-like stuff30 (e.g. decomposision, matrix multiplication, etc.).31 This is not a strict rule.32 '''33 def __init__(self, measFunList=None):34 ''' Can be initialized fully, or initialized with None to be built interactively.35 Args:36 measFunList (list[MeasuredFunction] or None): list of MeasuredFunctions that must have the same abscissa.37 '''38 self.absc = None39 self.ordiMat = None40 self.memberType = None41 self.nDims = 042 if measFunList is not None:43 if type(measFunList) is list:44 for m in measFunList:45 self.addDim(m)46 else:47 self.addDim(measFunList)48 def addDim(self, newMeasFun):49 if self.absc is None:50 self.absc = newMeasFun.absc51 self.ordiMat = np.matrix(newMeasFun.ordi)52 self.memberType = type(newMeasFun)53 else:54 y = self._putInTimebase(newMeasFun) # This does the checking for type and timebase55 self.ordiMat = np.append(self.ordiMat, [y], axis=0)56 self.nDims += 157 def __getitem__(self, index):58 ''' Gives out individual measured functions of the type used, or59 If it gets a slice, it returns a function bundle,60 just like a sliced numpy array returns a numpy array.61 Args:62 index (int or slice): which of the function(s) do you want to get63 Returns:64 (MeasuredFunction or FunctionBundle): depending on type of index65 '''66 if type(index) is int:67 theOrdi = self.ordiMat[index, :].A1 # A1 is a special numpy thing that converts from matrix to 1-d array68 return self.memberType(self.absc, theOrdi)69 elif type(index) is slice:70 newBundle = self.copy()71 newOrdiMat = self.ordiMat[index, :]72 newBundle.ordiMat = newOrdiMat73 newBundle.nDims = np.shape(newOrdiMat)[0]74 return newBundle75 def __len__(self):76 return self.ordiMat.shape[0]77 def __eq__(self, other):78 return (self.memberType is other.memberType and79 self.absc == other.absc and80 self.ordiMat == other.ordiMat)81 def __add__(self, other):82 ''' This works with scalars, vectors, MeasuredFunctions, and FunctionBundles83 '''84 if np.isscalar(other) or isinstance(other, MeasuredFunction):85 other = repeat(other, self.nDims)86 newBundle = type(self)()87 for selfItem, otherItem in zip(self, other):88 newItem = selfItem + otherItem89 newBundle.addDim(newItem)90 return newBundle91 def __radd__(self, other):92 return self.__add__(other)93 def __sub__(self, other):94 return self.__add__(-1 * other)95 def __mul__(self, other):96 ''' This works with scalars, vectors, MeasuredFunctions, and FunctionBundles97 '''98 if np.isscalar(other) or isinstance(other, MeasuredFunction):99 other = repeat(other, self.nDims)100 newBundle = type(self)()101 for selfItem, otherItem in zip(self, other):102 newItem = selfItem * otherItem103 newBundle.addDim(newItem)104 return newBundle105 def __rmul__(self, other):106 return self.__mul__(other)107 def __truediv__(self, other):108 return self * (1 / other)109 def __getattr__(self, attrName):110 ''' Feeds through **callable** methods to its members (type MeasuredFunction),111 if the attribute is not found in the FunctionBundle.112 If it finds it there, then applies it to every function in the bundle.113 With binary operator math, it only works with scalars.114 For example, starting with::115 .. code-block:: python116 spct1 = Spectrum([0, 1, 2], [2, 5, 3])117 spct2 = Spectrum([0, 1, 2], [4, 4, 1])118 funBun = FunctionBundle([spct1, spct2])119 You can do120 .. code-block:: python121 croppedFunBund = FunctionBundle([122 spct1.crop([0, 1]),123 spct2.crop([0, 1]))124 ])125 funBun.crop([0, 1]) == croppedFunBund126 Note:127 Be careful about "overloading" a MeasuredFunction method in FunctionBundle.128 It can have a different meaning than what would happen if not overloaded.129 For example ``MeasuredFunction.__mul__(nonscalar)`` is function multiplication,130 while ``FunctionBundle.__mul__(nonscalar)`` means vector dot product.131 Todo:132 This only works with :class:`~lightlab.util.data.one_dim.SignalProcessingMixin`133 methods that are MF-in/MF-out. How should we handle characteristic-type methods,134 such as ``centerOfMass``, or ``getRange``?135 '''136 if attrName == 'memberType':137 raise RuntimeError('Missed "memberType"')138 try:139 memberClassFunc = getattr(self.memberType, attrName)140 except AttributeError as err:141 betterErrStr = err.args[0] + ', neither does \'{}\' object'.format(type(self).__name__)142 err.args = tuple([betterErrStr, err.args[1:]])143 raise144 if not callable(memberClassFunc):145 raise TypeError(f'{memberClassFunc} in'146 ' {} of {}'.format(type(self).__name__, self.memberType.__name__) + ' '147 'is not callable')148 @wraps(memberClassFunc)149 def fakeFun(*args, **kwargs):150 newBundle = type(self)()151 for item in self:152 newItem = memberClassFunc(item, *args, **kwargs)153 newBundle.addDim(newItem)154 return newBundle155 return fakeFun156 def copy(self):157 newObj = type(self)()158 newObj.__dict__ = self.__dict__.copy()159 newObj.ordiMat = self.ordiMat.copy()160 return newObj161 def extend(self, otherFunctionBund):162 for func in otherFunctionBund:163 self.addDim(func)164 def max(self):165 ''' Returns a single MeasuredFunction(subclass) that is the maximum of all in this bundle166 '''167 return self.memberType(self.absc, np.max(self.ordiMat, axis=0).A1)168 def min(self):169 ''' Returns a single MeasuredFunction(subclass) that is the minimum of all in this bundle170 '''171 return self.memberType(self.absc, np.min(self.ordiMat, axis=0).A1)172 def mean(self):173 ''' Returns a single MeasuredFunction(subclass) that is the mean of all in this bundle174 '''175 return self.memberType(self.absc, np.mean(self.ordiMat, axis=0).A1)176 def _putInTimebase(self, testFun):177 ''' Makes sure signal type is correct and time basis is the same178 Args:179 testFun (MeasuredFunction): the new function, as encapsulated by an object180 Returns:181 array: the ordinate of the new function in the right timebase182 Raises:183 TypeError: if the type of ``testFun`` is different than the others in this FunctionalBasis184 '''185 if type(testFun).__name__ is not self.memberType.__name__:186 raise TypeError('This FunctionalBasis expects ' + str(self.memberType) +187 ', but was given ' + str(type(testFun)) + '.')188 # Check time base189 if np.any(testFun.absc != self.absc):190 # logger.warning('Warning: Basis signal time abscissa are different. Interpolating...')191 return testFun(self.absc)192 else:193 return testFun.ordi194 def simplePlot(self, *args, **kwargs):195 '''196 '''197 for f in self:198 f.simplePlot(*args, **kwargs)199 def multiAxisPlot(self, *args, axList=None, titleRoot=None, **kwargs):200 ''' titleRoot must take one argument in its format method, which is given the index201 Returns:202 (list(axis)): The axes that were plotted upon203 '''204 if axList is None:205 _, axList = plt.subplots(nrows=len(self), figsize=(14, 14))206 if len(axList) != len(self):207 raise ValueError('Wrong number of axes. Got {}, need {}.'.format(208 len(axList), len(self)))209 for i, ax in enumerate(axList):210 plt.sca(ax)211 self[i].simplePlot(*args, **kwargs)212 if titleRoot is not None:213 plt.title(titleRoot.format(i + 1))214 # plt.xlabel('Time (s)')215 if i < len(self) - 1:216 ax.xaxis.set_ticklabels([])217 ax.set_xlabel('')218 # plt.ylabel('Intensity (a.u.)')219 # plt.xlim(0,2e-8)220 return axList221 def histogram(self):222 ''' Gives a MeasuredFunction of counts vs. ordinate values (typically voltage)223 Does not maintain any abscissa information224 At this point, does not allow caller to set the arguments passed to np.histogram225 This is mainly just for plotting226 '''227 hist, bins = np.histogram(self.ordiMat, bins='auto', density=False)228 histFun = MeasuredFunction([], [])229 for iBin, thisOrdi in enumerate(hist):230 thisAbsc = np.mean(bins[iBin:iBin + 1])231 histFun.addPoint((thisAbsc, thisOrdi))232 return histFun233 def weightedAddition(self, weiVec):234 ''' Calculates the weighted addition of the basis signals235 Args:236 weiVec (array): weights to be applied to the basis functions237 Returns:238 (MeasuredFunction): weighted addition of basis signals239 '''240 assert(len(weiVec) == self.nDims)241 weiVec = np.reshape(weiVec, (1, self.nDims))242 outOrdi = np.dot(weiVec, self.ordiMat)243 return self.memberType(self.absc, outOrdi.A1)244 def moment(self, order=2, allDims=True, relativeGauss=False):245 ''' The order'th moment of all the points in the bundle.246 Args:247 order (integer): the polynomial moment of inertia. Don't trust the normalization of > 2'th order.248 order = 1: mean249 order = 2: variance250 order = 3: skew251 order = 4: kurtosis252 allDims (bool): if true, collapses all signals, returning a scalar253 Returns:254 (ndarray or float): the specified moment(s)255 '''256 byDim = np.zeros(len(self))257 for iDim in range(len(self)):258 byDim[iDim] = self[iDim].moment(order, relativeGauss=relativeGauss)259 if allDims:260 return np.mean(byDim)261 else:262 return byDim263 def componentAnalysis(self, *args, pcaIca=True, lNorm=2, expectedComponents=None, **kwargs):264 ''' Gives the waveform representing the principal component of the order265 Args:266 pcaIca (bool): if True, does PCA; if False, does ICA267 lNorm (int): how to normalize weight vectors. L1 norm uses the maximum abs weight, while L2 norm (default) is vector unit268 expectedComponents (FunctionBundle or subclass): Used for flipping signs269 args, kwargs: Feed through to sklearn.decomposition.[PCA(), FastICA()]270 Returns:271 (FunctionBundle or subclass): principal component waveforms272 '''273 from sklearn.decomposition import PCA, FastICA274 compBuiltIn = PCA if pcaIca else FastICA275 compAnal = compBuiltIn(*args, **kwargs)276 compAnal.fit(self.ordiMat.T)277 pcBundle = type(self)()278 for c in compAnal.components_:279 if lNorm == 1:280 w = c / np.sqrt(np.max(c ** 2))281 elif lNorm == 2:282 w = c / np.sqrt(np.sum(c ** 2))283 else:284 raise Exception('Are you serious? Use a valid L-norm')285 pcWfm = self.weightedAddition(w)286 pcBundle.addDim(pcWfm)287 if expectedComponents is not None:288 pcBundle = pcBundle.correctSigns(expectedComponents, maintainOrder=pcaIca)289 return pcBundle290 def correctSigns(self, otherBundle, maintainOrder=True):291 ''' Goes through each component and flips the sign if correlation is negative292 ICA also has a permutation indeterminism.293 '''294 if len(self) != len(otherBundle):295 raise ValueError('Self and other bundle must be the same length')296 newBundle = type(self)()297 usedPermInds = []298 for iDim, oSig in enumerate(otherBundle):299 if maintainOrder:300 sSig = self[iDim]301 if (sSig * oSig).getMean() > 0:302 newWfm = sSig303 else:304 newWfm = -1 * sSig305 else:306 permCorrs = np.zeros(len(self))307 for jDim, sSig in enumerate(self):308 if jDim not in usedPermInds:309 permCorrs[jDim] = (sSig * oSig).getMean()310 permInd = np.argmax(np.abs(permCorrs))311 permSign = int(np.sign(permCorrs[permInd]))312 newWfm = permSign * self[permInd]313 usedPermInds.append(permInd)314 newBundle.addDim(newWfm)315 return newBundle316class FunctionalBasis(FunctionBundle):317 ''' A FunctionBundle that supports additional linear algebra methods318 Created for weighted addition, decomposition, and component analysis319 '''320 @classmethod321 def independentDefault(cls, nDims):322 ''' Gives a basis of non-overlapping pulses. Waveforms only323 '''324 newAbsc = np.linspace(0, 1, 1000)325 pWid = .5 / nDims326 newObj = cls()327 for iDim in range(nDims):328 on = (iDim + .25) / nDims329 sig = Waveform.pulse(newAbsc, tOn=on, tOff=on + pWid)330 newObj.addDim(sig)331 return newObj332 def innerProds(self, trial):333 ''' takes the inner products of the trial function onto this basis.334 '''335 tvec = self._putInTimebase(trial)336 tmat = np.tile(tvec, (self.nDims, 1))337 products = np.sum(np.multiply(tmat, self.ordiMat), axis=1).A1338 return products339 def magnitudes(self):340 ''' The inner product of the basis with itself341 '''342 return np.sum(np.multiply(self.ordiMat, self.ordiMat), axis=1).A1343 def project(self, trial):344 ''' Projects onto normalized basis345 If the basis is orthogonal, this is equivalent to weight decomposition346 '''347 return self.innerProds(trial) / self.magnitudes()348 def decompose(self, trial, moment=1):349 ''' Uses the Moore-Penrose pseudoinverse to get weight decomposition without orthogonality350 Args:351 trial (MeasuredFunction): signal to be decomposed352 moment (float): polynomial moment of the basis to use when decomposing353 '''354 tvec = self._putInTimebase(trial)355 momBasis = np.power(self.ordiMat, moment)356 basisInverse = np.linalg.pinv(momBasis)357 return np.dot(tvec, basisInverse).A1358 def matrixMultiply(self, weiMat):359 assert(weiMat.shape[1] == self.nDims)360 outBasis = type(self)()361 for weiVec in weiMat:362 weightedDim = self.weightedAddition(weiVec)363 outBasis.addDim(weightedDim)364 return outBasis365 def getMoment(self, weiVecs=None, order=2, relativeGauss=False):366 ''' This is actually the projected moment. Named for compatibility with bss package367 Make sure weiVecs is two dimensional368 '''369 moms = np.zeros(len(weiVecs))370 for iv, v in enumerate(weiVecs):371 weighted = self.weightedAddition(v)372 calcMom = weighted.moment(order, relativeGauss=relativeGauss)373 moms[iv] = calcMom374 return moms375 def remainder(self, trial):376 ''' Gives the remaining parts of the signal that are not explained by the minimum-squared-error decomposition377 '''378 explainedWeights = self.decompose(trial)379 explainedSignal = self.weightedAddition(explainedWeights)380 return trial - explainedSignal381 def covariance(self):382 ''' Returns covariance matrix of the basis, which is nDims x nDims '''383 return np.cov(self.ordiMat)384class MeasuredSurface(object):385 ''' Basically a two dimensional measured function: "z" vs. "x", "y"386 Useful trick when gathering data: build incrementally using :meth:`FunctionBundle.addDim`,387 then convert that to this class using :meth:`MeasuredSurface.fromFunctionBundle`.388 '''389 def __init__(self, absc, ordi):390 '''391 Args:392 absc (ndarray): same meaning as measured function393 ordi (ndarray): two-dimensional array or matrix394 '''395 if type(absc) == np.ndarray and absc.ndim != 1:396 raise Exception(397 'absc should be a 2-element list of arrays or an array of objects (which are arrays)')398 if len(absc) != 2:399 raise Exception('Wrong number of abscissas. Need two')400 abscshape = np.zeros(2)401 for iDim in range(2):402 abscshape[iDim] = len(absc[iDim])403 if not np.all(ordi.shape == abscshape):404 raise Exception('Dimensions of ordinate and abscissa do not match')405 self.absc = absc406 self.ordi = ordi407 @classmethod408 def fromFunctionBundle(cls, otherBund, addedAbsc=None):409 ''' gives back a MeasuredSurface from a function Bundle410 Args:411 otherBund (FunctionBundle): The source. The ordering of functions matters412 addedAbsc (np.ndarray): the second dimension abscissa array (default, integers)413 Returns:414 (:class:`MeasuredSurface`) new object415 '''416 existingAbsc = otherBund.absc417 if addedAbsc is None:418 addedAbsc = np.arange(otherBund.nDims)419 return cls([addedAbsc, existingAbsc], otherBund.ordiMat)420 def __call__(self, testAbscissaVec=None):421 f = interpolate.interp2d(*self.absc, z=self.ordi, kind='cubic')422 return f(*testAbscissaVec)423 def item(self, index, dim=None):424 if np.isscalar(index):425 assert(dim is not None)426 newAbsc = self.absc[dim]427 if dim == 0:428 newOrdi = self.ordi[index, :]429 else:430 newOrdi = self.ordi[:, index]431 return MeasuredFunction(newAbsc, newOrdi)432 else:433 assert(len(index) == 2)434 firstDimMf = self.item(index[0], dim=0)435 return firstDimMf[index[1]]436 def shape(self):437 return self.ordi.shape438 def simplePlot(self, *args, **kwargs):439 if 'cmap' not in kwargs.keys():440 kwargs['cmap'] = cm.inferno # pylint: disable=no-member441 if 'shading' not in kwargs.keys():442 kwargs['shading'] = 'flat'443 YY, XX = np.meshgrid(self.absc[0], self.absc[1])444 plt.pcolormesh(XX, YY, np.array(self.ordi.T), *args, **kwargs)445 plt.autoscale(tight=True)446class Spectrogram(MeasuredSurface):447 pass448class MeasuredErrorField(object):449 ''' A field that hold two abscissa arrays and two ordinate matrices450 Error is the measuredGrid - nominalGrid, which is a vector field451 '''452 def __init__(self, nominalGrid, measuredGrid):453 assert(nominalGrid.ndim == 3)454 self.nomiGrid = nominalGrid455 if measuredGrid.ndim == 4:456 self.measGrid = np.mean(measuredGrid, axis=0)457 elif measuredGrid.ndim == 3:458 self.measGrid = measuredGrid459 else:460 raise Exception('measuredGrid must be dimension 3 (meaned) or 4 (trials)')461 def __call__(self, testVec=None):462 xVec = self.nomiGrid[:, :, 0]463 yVec = self.nomiGrid[:, :, 1]464 uVec = self.measGrid[:, :, 0]465 vVec = self.measGrid[:, :, 1]466 u = interpolate.interp2d(xVec, yVec, uVec, kind='linear')467 v = interpolate.interp2d(xVec, yVec, vVec, kind='linear')468 testU = u(*testVec)[0]469 testV = v(*testVec)[0]470 return np.array([testU, testV])471 def errorAt(self, testVec=None):472 return self(testVec) - testVec473 def invert(self, desiredVec):474 reflectedVec = desiredVec - self.errorAt(desiredVec)475 avgErr = (self.errorAt(desiredVec) + self.errorAt(reflectedVec)) / 2476 commandVec = desiredVec - avgErr477 return commandVec478 def zeroCenteredSquareSize(self):479 ''' Very stupid, just look at corner points480 Returns:481 (tuple(float)): square sides of nominal and measured grids482 '''483 def cornerInd(grid, minOrMax):484 score = np.sum(grid, axis=2)485 cornerFlatInd = np.argmin(score) if minOrMax else np.argmax(score)486 return np.unravel_index(cornerFlatInd, grid.shape[:2])487 def zcSz(grid, corner):488 cornerVec = grid[(corner + (slice(None), ))]489 sqSide = np.min(np.abs(cornerVec))490 return sqSide491 nomiCornerInds = [cornerInd(self.nomiGrid, m) for m in [True, False]]492 nomiSq = np.min([zcSz(self.nomiGrid, nomiCornerInds[i]) for i in range(2)])493 measSq = np.min([zcSz(self.measGrid, nomiCornerInds[i]) for i in range(2)])...

Full Screen

Full Screen

Guassianproblem.py

Source:Guassianproblem.py Github

copy

Full Screen

1import pandas as pd2import numpy as np3from sres import SRES, _lib4import ctypes as ct5import tellurium as te6from math import pi as PI7import unittest8sres = SRES(2)9def generateData(mu, sigma):10 return np.random.normal(mu, sigma, 10)11EXP_DATA = generateData(5, 0.1)12class Test(unittest.TestCase):13 def setUp(self) -> None:14 pass15 @sres.COST_FUNCTION_CALLBACK16 def test_ctypes_callback_fn_example(self):17 from ctypes import cdll18 libc = cdll.msvcrt19 IntArray5 = ct.c_int * 520 ia = IntArray5(5, 1, 7, 33, 99)21 qsort = libc.qsort22 qsort.restype = None23 CALLBACK_FN = ct.CFUNCTYPE(ct.c_int, ct.POINTER(ct.c_int), ct.POINTER(ct.c_int))24 def py_cb(a, b):25 print("Pycb", a[0], b[0])26 return 027 qsort(ia, len(ia), ct.sizeof(ct.c_int), CALLBACK_FN(py_cb))28 def testPassingDoubleArray(self):29 lb = ct.pointer(sres.DoubleArrayLen2(0.1, 0.1)) # double *lb,30 sres.fakeFun(lb)31 def testPassingDoubleArrayUsingWrapperFn(self):32 lb = sres._makeDoubleArrayPtr([0.1, 0.1]) # double *lb,33 sres.fakeFun(lb)34 def testFunctionPointerInIsolation(self):35 import ctypes as ct36 lib = ct.CDLL("SRES")37 F1_CALLBACK = ct.CFUNCTYPE(None, ct.POINTER(ct.c_double * 2), ct.POINTER(ct.c_double), ct.POINTER(ct.c_double))38 lib.function_that_takes_a_function.argtypes = [39 F1_CALLBACK, ct.POINTER(ct.c_double * 2),40 ct.POINTER(ct.c_double),41 ct.POINTER(ct.c_double)42 ]43 lib.function_that_takes_a_function.restype = None44 def func_to_pass_in(d1, d2, d3):45 print("hello from Python: ")46 # function_that_takes_a_function(func_to_pass_in, sres._makeDoubleArrayPtr([0.1, 0.1]))47 lib.function_that_takes_a_function(F1_CALLBACK(func_to_pass_in), sres._makeDoubleArrayPtr([0.1, 1.2]),48 ct.pointer(ct.c_double(4.0)), ct.pointer(ct.c_double(6.0)))49 def testFunctionPointerInIsolationAndUpdateAValue(self):50 import ctypes as ct51 lib = ct.CDLL("SRES")52 F1_FUNCTION_PTR = ct.CFUNCTYPE(None, ct.POINTER(ct.c_double), ct.POINTER(ct.c_double))53 lib.function_that_takes_a_function.argtypes = [54 F1_FUNCTION_PTR, ct.POINTER(ct.c_double), ct.POINTER(ct.c_double)55 ]56 lib.function_that_takes_a_function.restype = None57 def func_to_pass_in(x, y):58 print("From Python: hello from Python: ")59 print("From Python: x, y: ", x.contents, y.contents)60 new_value = x.contents.value + y.contents.value61 new_value_double_ptr = ct.pointer(ct.c_double(new_value))62 ct.memmove(ct.cast(y, ct.c_void_p).value,63 ct.cast(new_value_double_ptr, ct.c_void_p).value,64 ct.sizeof(ct.c_double))65 # function_that_takes_a_function(func_to_pass_in, sres._makeDoubleArrayPtr([0.1, 0.1]))66 input = ct.c_double(4.0)67 output = ct.c_double(1.0)68 input_ptr = ct.pointer(input)69 output_ptr = ct.pointer(output)70 lib.function_that_takes_a_function(F1_FUNCTION_PTR(func_to_pass_in), input_ptr, output_ptr)71 def testFunctionPointerInIsolationAndUpdateAValue2(self):72 import ctypes as ct73 lib = ct.CDLL("SRES")74 ESFcnFG_FUNCTION_PTR = ct.CFUNCTYPE(None, ct.POINTER(ct.c_double * 2), ct.POINTER(ct.c_double),75 ct.POINTER(ct.c_double))76 lib.function_that_takes_f2.argtypes = [77 ESFcnFG_FUNCTION_PTR, ct.POINTER(ct.c_double), ct.POINTER(ct.c_double), ct.POINTER(ct.c_double)78 ]79 lib.function_that_takes_f2.restype = None80 def func_to_pass_in(x, y, z):81 print("hello from Python: ")82 print("x, y: ", x.contents[0], y.contents)83 new_value = x.contents[0] + x.contents[1] + y.contents.value84 new_value_double_ptr = ct.pointer(ct.c_double(new_value))85 ct.memmove(86 ct.cast(y, ct.c_void_p).value,87 ct.cast(new_value_double_ptr, ct.c_void_p).value,88 ct.sizeof(ct.c_double))89 # function_that_takes_a_function(func_to_pass_in, sres._makeDoubleArrayPtr([0.1, 0.1]))90 input = ct.pointer(ct.c_double(4.0))91 output = ct.pointer(ct.c_double(1.0))92 ignored = ct.pointer(ct.c_double(2.0))93 lib.function_that_takes_f2(ESFcnFG_FUNCTION_PTR(func_to_pass_in), input, output, ignored)94 def test_use_the_problematic_function_pointer_outside_context_of_SRES(self):95 import ctypes as ct96 lib = ct.CDLL("SRES")97 ESfcnFG_TYPE = ct.CFUNCTYPE(None, ct.POINTER(ct.c_double * 2), ct.POINTER(ct.c_double), ct.POINTER(ct.c_double))98 lib.function_that_takes_ESfcnFG.argtypes = [99 ESfcnFG_TYPE100 ]101 lib.function_that_takes_ESfcnFG.restype = None102 def cost_fun(x, f, g):103 print("hello from cost_fun")104 sim = generateData(x.contents[0], x.contents[1])105 cost = 0106 for i in range(10):107 cost += (EXP_DATA[i] - sim[i]) ** 2108 cost_dbl_ptr = ct.pointer(ct.c_double(cost))109 # copy the value from Python to C. If we don't do this, the value gets deleted.110 ct.memmove(ct.cast(f, ct.c_void_p).value, ct.cast(cost_dbl_ptr, ct.c_void_p).value, ct.sizeof(ct.c_double))111 lib.function_that_takes_ESfcnFG(ESfcnFG_TYPE(cost_fun))112 def test_sres_alg_using_dereferencing_pointer_functions(self):113 """114 We need double pointers as input to ESInit but since pointers in ESStep.115 This version attempts to create double pointers in make* functions and then116 dereference them in using deref* function calls. The actual dereferencing117 happens on the C end118 :return:119 """120 sres = SRES()121 param = sres._makeESParameter()122 stats = sres._makeESStatistics()123 population = sres._makeESPopulation()124 trsfm = sres._getTransformFun(2)125 # https://stackoverflow.com/questions/51131433/how-to-pass-lists-into-a-ctypes-function-on-python/51132594126 seed = ct.c_int32(0)127 gamma = ct.c_double(0.85)128 alpha = ct.c_double(0.2)129 retry = ct.c_int32(10)130 es = ct.c_int32(1)131 constraint = ct.c_int32(0)132 dim = ct.c_int32(2)133 varphi = ct.c_double(1.0)134 ngen = ct.c_int32(50)135 miu = 5136 lambda_ = 5137 # How to verify that this works?138 ub = ct.pointer(sres.DoubleArrayLen2(10.0, 10.0)) # double *ub,139 lb = ct.pointer(sres.DoubleArrayLen2(0.1, 0.1)) # double *lb,140 ESfcnFG_TYPE = ct.CFUNCTYPE(141 None,142 ct.POINTER(ct.c_double * 2),143 ct.POINTER(ct.c_double),144 ct.POINTER(ct.c_double)145 )146 def cost_fun(x, f, g):147 print("From Python : Hello")148 sim = generateData(x.contents[0], x.contents[1])149 cost = 0150 for i in range(10):151 cost += (EXP_DATA[i] - sim[i]) ** 2152 cost_dbl_ptr = ct.pointer(ct.c_double(cost))153 # copy the value from Python to C. If we don't do this, the value gets deleted.154 ct.memmove(ct.cast(f, ct.c_void_p).value, ct.cast(cost_dbl_ptr, ct.c_void_p).value, ct.sizeof(ct.c_double))155 print("From Python: Call to sres.ESInitial")156 sres.ESInitial(157 seed,158 param,159 trsfm,160 ESfcnFG_TYPE(cost_fun),161 es,162 constraint,163 dim,164 ub,165 lb,166 miu,167 lambda_,168 ngen,169 gamma,170 alpha,171 varphi,172 retry,173 population,174 stats175 )176 print("From Python: Call to sres.ESInitial has finished")177 curgen = 0178 while curgen < 10:179 print("Current gen: ", curgen)180 print("From Python: Call to sres.ESStep")181 sres._ESStep(182 sres.derefESPopulation(population),183 sres.derefESParameter(param),184 sres.derefESStatistics(stats),185 ct.c_double(0.45)186 )187 print("From Python: Call to sres.ESStep has finished")188 curgen += 1189 # sres.ESDeInitial(190 # sres.derefESParameter(esparam),191 # sres.derefESPopulation(pop),192 # sres.derefESStatistics(stats)193 # )194 # sres.freeCostFunPtr(costFun)195 # sres.freeTransformFun(trsf)196 def test_sres_alg_using_version_of_ESStep_that_takes_double_pointers(self):197 """198 We need double pointers as input to ESInit but since pointers in ESStep. In attempt to debug the199 memory related issues in test_sres_alg_using_dereferencing_pointer_functions we rewrote the200 ESStep function in C so that it takes double pointers.201 :return:202 """203 sres = SRES()204 param = sres._makeESParameter()205 stats = sres._makeESStatistics()206 population = sres._makeESPopulation()207 trsfm = sres._getTransformFun(2)208 # https://stackoverflow.com/questions/51131433/how-to-pass-lists-into-a-ctypes-function-on-python/51132594209 seed = ct.c_int32(0)210 gamma = ct.c_double(0.85)211 alpha = ct.c_double(0.2)212 retry = ct.c_int32(10)213 es = ct.c_int32(1)214 constraint = ct.c_int32(0)215 dim = ct.c_int32(2)216 varphi = ct.c_double(1.0)217 ngen = ct.c_int32(50)218 miu = 5219 lambda_ = 5220 # How to verify that this works?221 ub = ct.pointer(sres.DoubleArrayLen2(10.0, 10.0)) # double *ub,222 lb = ct.pointer(sres.DoubleArrayLen2(0.01, 0.01)) # double *lb,223 ESfcnFG_TYPE = ct.CFUNCTYPE(224 None,225 ct.POINTER(ct.c_double * 2),226 ct.POINTER(ct.c_double),227 ct.POINTER(ct.c_double)228 )229 def cost_fun(x, f, g):230 sim = generateData(x.contents[0], x.contents[1])231 cost = 0232 for i in range(10):233 cost += (EXP_DATA[i] - sim[i]) ** 2234 cost_dbl_ptr = ct.pointer(ct.c_double(cost))235 # copy the value from Python to C. If we don't do this, the value gets deleted.236 ct.memmove(ct.cast(f, ct.c_void_p).value, ct.cast(cost_dbl_ptr, ct.c_void_p).value, ct.sizeof(ct.c_double))237 f = ESfcnFG_TYPE(cost_fun)238 sres.ESInitial(239 seed,240 param,241 trsfm,242 f,243 es,244 constraint,245 dim,246 ub,247 lb,248 miu,249 lambda_,250 ngen,251 gamma,252 alpha,253 varphi,254 retry,255 population,256 stats257 )258 curgen = 0259 while curgen < 1000:260 sres._ESStep(261 sres.derefESPopulation(population),262 sres.derefESParameter(param),263 sres.derefESStatistics(stats),264 0.45265 )266 curgen += 1267 # sres.ESDeInitial(268 # sres.derefESParameter(esparam),269 # sres.derefESPopulation(pop),270 # sres.derefESStatistics(stats)271 # )272 # sres.freeCostFunPtr(costFun)273 # sres.freeTransformFun(trsf)274 def test_sres(self):275 sres = SRES(dim=2)276 @sres.COST_FUNCTION_CALLBACK277 def cost_fun(x, f, g):278 sim = generateData(x.contents[0], x.contents[1])279 cost = 0280 for i in range(10):281 cost += (EXP_DATA[i] - sim[i]) ** 2282 f.contents.value = cost283 sres.cost_function = cost_fun284if __name__ == "__main__":285 pass286 # check that passing in double array works287 # testPassingDoubleArray()288 # y = ct.POINTER(ct.c_double)289 # print(y, y())290 # do_sres()291 # ct callback example from docs292 # Use qsort from python:...

Full Screen

Full Screen

item.py

Source:item.py Github

copy

Full Screen

...55 elif "id" in stage:56 name = stage["id"]57 stages.append("{:d}: {:s}".format(i + 1, name))58 # This needs to be a function or skipif breaks59 def fakefun():60 pass61 fakefun.__doc__ = self.name + ":\n" + "\n".join(stages)62 return fakefun63 @property64 def _obj(self):65 return self.obj66 def add_markers(self, pytest_marks):67 for pm in pytest_marks:68 if pm.name == "usefixtures":69 if (70 not isinstance(pm.mark.args, (list, tuple))71 or len(pm.mark.args) == 072 ):73 logger.error(...

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 tavern 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