class Array2D: def __init__(self,m,n): self._nrow=m self._ncol=n self._RowArr=Array(m) for i in range(self._nrow): workArr=Array(n) workArr.clear(0) self._RowArr.setitem(workArr,i) pass pass def nrow(self): return self._nrow def ncol(self): return self._ncol def getitem2D(self,r,c): #print r arr=self._RowArr.getitem(r) return arr.getitem(c) def setitem2D(self,r,c,val): arr=self._RowArr.getitem(r) arr.setitem(val,c) pass def clear(self,val): for i in range(self._nrow): arr=self._RowArr.getitem(i) for j in range(self._ncol): arr.setitem(0,j)
def __init__(self, N = 0, names = None): Array.__init__(self, N, N) if names is None: self.names = np.array(['%d' % n for n in range(self.N)]) else: self.names = names self.rnames = self.names self.cnames = self.names self.node_covariates = {}
def __init__(self,m,n): self._nrow=m self._ncol=n self._RowArr=Array(m) for i in range(self._nrow): workArr=Array(n) workArr.clear(0) self._RowArr.setitem(workArr,i) pass pass
def readChild(self, element): child = None if element.tag == 'Array': child = Array() child.read(element) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) child.setValuesAreIntegers(integers) self._array = child return child
def test_execute_valid_array(self): from VM import VM vm = VM() a = Array() a.length = 9876 x = ldlen('') vm.stack.push(a) x.execute(vm) self.assertEqual(vm.stack.pop().value, 9876)
def test_execute_get_array_element_i4(self): from VM import VM vm = VM() a = Array(100) a.values[5] = Variable(1122) vm.stack.push(a) vm.stack.push(Variable(5)) x = ldelem('i4') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(a.values[5].value, 1122)
def setArray(self, dimension, values, floatEncoding="string"): dimensions = [len(values) / dimension, dimension] integers = bitDepthIsInteger(self.getAttribute("outBitDepth")) rawHalfs = not (self.getAttribute("rawHalfs") in [None, False]) self._array = Array(dimensions, values, rawHalfs=rawHalfs, integers=integers, floatEncoding=floatEncoding) self.addElement(self._array)
def setArray(self, dimension, values): dimensions = dimension dimensions.append(3) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) self._array = Array(dimensions, values, integers) self.addElement( self._array )
def readChild(self, element): child = None if element.tag == 'Array': child = Array() child.read(element) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) child.setValuesAreIntegers(integers) self._array = child elif element.tag == 'IndexMap': child = IndexMap() child.read(element) self._indexMaps.append( child ) return child
def readChild(self, element): child = None if element.tag == "Array": rawHalfs = not (self.getAttribute("rawHalfs") in [None, False]) child = Array(rawHalfs=rawHalfs) child.read(element) integers = bitDepthIsInteger(self.getAttribute("outBitDepth")) child.setValuesAreIntegers(integers) self._array = child elif element.tag == "IndexMap": child = IndexMap() child.read(element) self._indexMaps.append(child) return child
def readChild(self, element): child = None elementType = self.getElementType(element.tag) if elementType == 'Array': rawHalfs = not (self.getAttribute('rawHalfs') in [None, False]) child = Array(rawHalfs=rawHalfs) child.read(element) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) child.setValuesAreIntegers(integers) self._array = child elif elementType == 'IndexMap': child = IndexMap() child.read(element) self._indexMaps.append( child ) return child
def __init__(self, window, tag, parser): self.tagName = tag self.__parser = parser self.__dict__['__window'] = window if tag == 'body': self.__dict__['__window'].document.body = self if tag == 'select': self.options = Array() if tag == 'input': self.value = '' if tag == 'option': self.value = 0 self.children = [] self.childNodes = Array() self.style = CSSStyleDeclaration() self.__dict__['__window'].document.all.append(self) # assign an initial id to every dom node varname = 'domnode' + str(int(time.time()*10000000)) self.__setattr__('id', varname)
T_fit = 20 T_grid = 1000 min_error = 0.1 theta_grid_min = 0.0 theta_grid_max = 3.0 theta_grid_G = 121 def cond_a_nll(X, w): return cond_a_nll_b(X, w, sort_by_wopt_var = True) def cond_a_sample(r, c, w, T = 0): return cond_a_sample_b(r, c, w, T, sort_by_wopt_var = True) while True: a = Array(M, N) alpha_norm(a, 1.0) a.new_edge_covariate('x')[:,:] = np.random.normal(0, 1, (M, N)) d = NonstationaryLogistic() d.beta['x'] = theta d.match_kappa(a, kappa_target) a.generate(d) f = NonstationaryLogistic() f.beta['x'] = None f.fit_conditional(a, T = T_fit, verbose = True) abs_err = abs(f.beta['x'] - d.beta['x']) if abs_err > min_error:
def test_mul_element(): assert testArray.__mul__(5) == Array((4, ), 5, 10, 15, 20) assert testArray.__mul__(3) == Array((4, ), 3, 6, 9, 12)
def test_sub_array(): assert testArray.__sub__(testArray2) == Array((4, ), -1, -2, -3, -4) assert testArray.__sub__(failarray) == NotImplemented
def test_add_2d(): assert test2d + test2d2 == Array((2, 3), 6, 10, -9, 10, 6, 11) assert test2d + 5 == Array((2, 3), 8, 10, 6, 10, 11, 11)
def test_add_element(): assert testArray.__add__(5) == Array((4, ), 6, 7, 8, 9) assert testArray.__add__(3) == Array((4, ), 4, 5, 6, 7) assert 5 + testArray == Array((4, ), 6, 7, 8, 9) assert testArray + 5 == Array((4, ), 6, 7, 8, 9)
class LUT3D(ProcessNode): "A Common LUT Format LUT 3D ProcessNode element" def __init__(self, inBitDepth=bitDepths["FLOAT16"], outBitDepth=bitDepths["FLOAT16"], id="", name="", interpolation='trilinear'): "%s - Initialize the standard class variables" % 'LUT3D' ProcessNode.__init__(self, 'LUT3D', inBitDepth, outBitDepth, id, name) if interpolation != '': self._attributes['interpolation'] = interpolation self._array = None self._indexMaps = [] # __init__ def setIndexMaps(self, valuesR, valuesG = None, valuesB = None): indexMapR = IndexMap(len(valuesR[0]), valuesR) self._indexMaps.append( indexMapR ) self.addElement( indexMapR ) # Either one or three indexMaps if( valuesG != None and valuesB != None ): indexMapG = IndexMap(len(valuesG[0]), valuesG) self._indexMaps.append( indexMapG ) self.addElement( indexMapG ) indexMapB = IndexMap(len(valuesB[0]), valuesB) self._indexMaps.append( indexMapB ) self.addElement( indexMapB ) # setIndexMaps def setArray(self, dimension, values): dimensions = dimension dimensions.append(3) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) self._array = Array(dimensions, values, integers) self.addElement( self._array ) # setArray def getLUTDimensions(self): return self._array.getDimensions() def getLUTValues(self): return self._array.getValues() def getIndexMapDimensions(self, channel): return self._indexMaps[channel].getDimensions() def getIndexMapValues(self, channel): return self._indexMaps[channel].getValues() def readChild(self, element): child = None if element.tag == 'Array': child = Array() child.read(element) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) child.setValuesAreIntegers(integers) self._array = child elif element.tag == 'IndexMap': child = IndexMap() child.read(element) self._indexMaps.append( child ) return child # readChild def process(self, value, verbose=False): # Base attributes inBitDepth = self._attributes['inBitDepth'] outBitDepth = self._attributes['outBitDepth'] # Node attributes interpolation = '' if 'interpolation' in self._attributes: interpolation = self._attributes['interpolation'] ''' print( "interpolation : %s" % interpolation ) ''' # Get LUT dimensions dimensions = self.getLUTDimensions() # Actually process a value or two outValue = value # Run each channel through the index map, or base normalization for i in range(min(3, len(value))): # Run through single Index Map then normalize if len(self._indexMaps) > 1: outValue[i] = self._indexMaps[i].process(outValue[i]) outValue[i] /= float(dimensions[i]-1) # Run through per-channel Index Map then normalize elif len(self._indexMaps) > 0: outValue[i] = self._indexMaps[0].process(outValue[i]) outValue[i] /= float(dimensions[i]-1) # Normalize from bit-depth else: # Convert input bit depth outValue[i] = bitDepthToNormalized(outValue[i], inBitDepth) # Run color through LUT # trilinear interpolation if interpolation == 'trilinear': outValue = self._array.lookup3DTrilinear(outValue) # tetrahedral interpolation elif interpolation == 'tetrahedral': outValue = self._array.lookup3DTetrahedral(outValue) # Bit Depth conversion for output is ignored for LUTs # as LUT values are assumed to target a specific bit depth #for i in range(min(3, len(value))): # outValue[i] = normalizedToBitDepth(outValue[i], outBitDepth) return outValue
def __init__(self, capacity: int = 10): self.array = Array(capacity)
ShockwaveFlashPlugin = Plugin({'name' : 'Shockwave Flash', 'filename' : 'C:\\WINDOWS\\system32\\Macromed\\Flash\\NPSWF32.dll', 'description' : 'Shockwave Flash 10.0 r42'}) AdobeAcrobatPlugin = Plugin({'name' : 'Adobe Acrobat', 'filename' : 'C:\\Program Files\\Internet Explorer\\PLUGINS\\nppdf32.dll', 'description' : 'Adobe Acrobat Plug-In'}) AdobePDFPlugin = Plugin({'name' : 'Adobe PDF', 'filename' : 'C:\\Program Files\\Internet Explorer\\PLUGINS\\nppdf32.dll', 'description' : 'Adobe PDF Plug-In'}) Plugins = Array() Plugins.append(ShockwaveFlashPlugin) Plugins.append(AdobeAcrobatPlugin) Plugins.append(AdobePDFPlugin) class Navigator(object): appCodeName = config.appCodeName # The internal "code" name of the current browser appName = config.appName # The official name of the browser appVersion = config.appVersion # The version of the browser as a string buildID = "" # The build identifier of the browser (e.g. "2006090803") cookieEnabled = True # A boolean indicating whether cookies are enabled language = "" # A string representing the language version of the browser mimeTypes = [] # A list of the MIME types supported by the browser onLine = True # A boolean indicating whether the browser is working online oscpu = "" # A string that represents the current operating system
import matplotlib.pyplot as plt import sklearn.datasets as dsets import sklearn.linear_model as slm from sklearn.metrics import mean_squared_error, r2_score from Array import Array a = Array() class Neuro: def __init__(self, dXTrain, dYTrain, dXTest, dYTest): self.dXTrain = dXTrain self.dYTrain = dYTrain self.dXTest = dXTest self.dYTest = dYTest def trainFit(self, inputX, inputY): regr = slm.LinearRegression() regr.fit(inputX, inputY) dYPred = regr.predict(self.dXTest) print('Coefficients: \n', regr.coef_) print('Mean squared error: %.2f' % mean_squared_error(self.dYTest, dYPred)) print('Coefficient of determination: %.2f' % r2_score(self.dYTest, dYPred)) #Получаем изменённые параметры X,Y dX_finalTrain, dY_finalTrain = a.correctArr(regr.coef_, inputX, inputY) #Насколько я понял тут я тренирую и тестирую модель на новых значениях
def testArgmax(self): a1 = Array.fromList([1, 2, 3, 4, 5]) ans = argmax(a1) self.assertTrue(ans == 5)
def testSoftmax(self): a1 = Array.fromList([1, 2, 3, 4, 5]) a2 = softmax(a1) self.assertTrue(sum(a2.rows) == 1) self.assertTrue(a1.shape() == a2.shape())
class DOMObject(object): def __init__(self, window, tag, parser): self.tagName = tag self.__parser = parser self.__dict__['__window'] = window if tag == 'body': self.__dict__['__window'].document.body = self if tag == 'select': self.options = Array() if tag == 'input': self.value = '' if tag == 'option': self.value = 0 self.children = [] self.childNodes = Array() self.style = CSSStyleDeclaration() self.__dict__['__window'].document.all.append(self) # assign an initial id to every dom node varname = 'domnode' + str(int(time.time()*10000000)) self.__setattr__('id', varname) def handle_src(self, name, val): url = self.__dict__['__window'].document.location.fix_url(val) if config.retrieval_all: hc.get(url, self.__dict__['__window'].document.location.href) scheme, netloc, path, query, fragment = urlparse.urlsplit(url) if scheme not in ('http','file','https','ftp'): config.VERBOSE(config.VERBOSE_WARNING, "[WARNING] Got unknown scheme: %s in %s.%s ."%(url,self.tagName, name)); if 'onerror' in self.__dict__: config.VERBOSE(config.VERBOSE_DEBUG, "[DEBUG] Calling onerror of %s."%(self.tagName)); self.onerror() if self.tagName == "iframe": from Window import Window from PageParser import PageParser window = Window(self.__dict__['__window'].__dict__['__root'], self.__dict__['__window'].document.location.fix_url(val), self.__dict__['__window'].document.location.href) parser = PageParser(window, window.document, window.__dict__['__html']) parser.close() def handle_id(self, name, val): self.__dict__[name] = val val = val.replace(':','_').replace('-','_') try: #if self.__dict__['__window'].__dict__['__cx'].execute('typeof ' + val + ' == "undefined"'): self.__dict__['__window'].__dict__['__cx'].add_global(val, self) except: #traceback.print_exc() pass self.__dict__['__window'].__dict__['__fl'][-1].__setattr__(val, self) def handle_name(self, name, val): self.handle_id(name, val) def handle_innerHTML(self, name, val): val = str(val) if self.__parser: self.__parser.html = self.__parser.html[:self.begin] + val + self.__parser.html[self.end:] dev = self.end - self.begin - len(val) for i in self.__dict__['__window'].document.all: if i.begin: if i.begin > self.end: i.begin -= dev if i.end: if i.end >= self.end: i.end -= dev self.__parser.current -= dev return from PageParser import PageParser self.__parser = PageParser(self.__dict__['__window'], self.__dict__['__window'].document, val) def __setattr__(self, name, val): try: handler = getattr(self, "handle_%s" % (name, )) handler(name, val) except: pass #if it's an event, let it be a function if dataetc.isevent(name, self.tagName): # using 'this' in methods may cause additional problems. # i think i find a way to handle this, but there could # be some cases it cannot cover. val = str(val) + ';' cx = self.__dict__['__window'].__dict__['__cx'] val = cx.patch_script(val) try: if 'id' in self.__dict__: vals = re.split('(?<=[^a-zA-Z0-9_])this(?=[^a-zA-Z0-9_])', val) valstmp = re.split('^this(?=[^a-zA-Z0-9_])', vals[0]) if len(vals) > 1: vals = valstmp + vals[1:] valstmp = re.split('(?<=[^a-zA-Z0-9_])this$', vals[-1]) if len(vals) > 1: vals = vals[:-1] + valstmp val = self.id.join(vals) self.__dict__[name] = cx.execute('function(){' + val + '}') except: try: p = val.decode('string-escape') self.__dict__[name] = cx.execute('function(){' + p + '}') except: print val #traceback.print_exc() self.__dict__[name] = val def focus(self): if 'onfocus' in self.__dict__: self.onfocus() self.__dict__['__window'].document.activeElement = self def blur(self): if 'onblur' in self.__dict__: self.onblur() def __getattr__(self, name): if name == 'innerHTML': return self.__parser.html[self.begin:self.end] return unknown() def appendChild(self, dom): if self.childNodes.length == 0: self.firstChild = dom self.childNodes.append(dom) self.lastChild = dom dom.__dict__['parentNode'] = self self.children.append(dom) def removeChild(self, dom): self.childNodes.remove(dom) if self.childNodes.length == 0: self.firstChild = None self.lastChild = None else: self.firstChild = self.childNodes[0] self.lastChild = self.childNodes[self.childNodes.length - 1] def setAttribute(self, attname, attval): self.__setattr__(attname, attval) def removeAttribute(self, attname): if self.__dict__[attname]: del self.__dict__[attname]
def generate_data(case, theta, seed): # Advance random seed for parameter and covariate construction seed.next() case = params['case'] alpha = beta = kappa = offset = 0 conditional_sample = False if 'fixed_example' in case: # Load parameters and covariates with open(case['fixed_example'], 'r') as example_file: example = json.load(example_file) v = np.array(example['nu']) M, N = v.shape if 'alpha' in example: alpha = np.array(example['alpha']).reshape((M,1)) if 'beta' in example: beta = np.array(example['beta']).reshape((1,N)) if 'kappa' in example: kappa = example['kappa'] if 'offset' in example: offset = example['offset'] if ('r' in example) and ('c' in example): conditional_sample = True r = example['r'] c = example['c'] else: # Generate parameters and covariates M, N = case['M'], case['N'] if 'alpha_min' in case: alpha = np.random.uniform(size = (M,1)) + case['alpha_min'] if 'beta_min' in case: beta = np.random.uniform(size = (1,N)) + case['beta_min'] if 'kappa' in case: kappa = case['kappa'] if case['v_discrete']: v = np.sign(np.random.random(size = (M,N)) - 0.5) elif case['v_uniform']: v = np.random.uniform(size = (M,N)) elif case['v_normal']: v = np.random.normal(size = (M,N)) if 'v_scale' in case: v *= case['v_scale'] if 'v_loc' in case: v += case['v_loc'] if ('r' in case) and ('c' in case): conditional_sample = True r = case['r'] c = case['c'] # Generate Bernoulli probabilities from logistic regression model logit_P = np.zeros((M,N)) + kappa logit_P += alpha logit_P += beta logit_P += theta * v logit_P += offset if conditional_sample: arr = Array(M, N) arr.new_edge_covariate('x_0')[:] = logit_P arr.new_row_covariate('r', dtype = np.int)[:] = r arr.new_col_covariate('c', dtype = np.int)[:] = c base_model = StationaryLogistic() base_model.beta['x_0'] = 1.0 data_model = FixedMargins(base_model) while True: # Advance random seed for data generation seed.next() # Generate data for this trial if conditional_sample: X = data_model.generate(arr, coverage = 100.0) else: P = 1.0 / (1.0 + np.exp(-logit_P)) X = np.random.random((M,N)) < P yield X, v
def __init__(self, maxsize): self.maxsize = maxsize self.array = Array(maxsize) self.head = 0 self.tail = 0
min_error = 0.1 theta_grid_min = 0.0 theta_grid_max = 3.0 theta_grid_G = 121 def cond_a_nll(X, w): return cond_a_nll_b(X, w, sort_by_wopt_var=True) def cond_a_sample(r, c, w, T=0): return cond_a_sample_b(r, c, w, T, sort_by_wopt_var=True) while True: a = Array(M, N) alpha_norm(a, 1.0) a.new_edge_covariate('x')[:, :] = np.random.normal(0, 1, (M, N)) d = NonstationaryLogistic() d.beta['x'] = theta d.match_kappa(a, kappa_target) a.generate(d) f = NonstationaryLogistic() f.beta['x'] = None f.fit_conditional(a, T=T_fit, verbose=True) abs_err = abs(f.beta['x'] - d.beta['x']) if abs_err > min_error:
class MaxHeap(object): def __init__(self, arr=None): if arr is None: self.__data = Array() elif isinstance(arr, int): self.__data = Array(arr) else: # 如果 arr 不为空,使用 heapify 操作将 arr 整理成堆 self.__data = Array(arr) i = (self.get_size() - 2) // 2 while i >= 0: self.__sift_down(i) i -= 1 # 堆大小 def get_size(self): return self.__data.get_size() # 堆是否为空 def is_empty(self): return self.__data.is_empty() # 起始索引为0的完全二叉树下,返回给定索引的父结点索引 def get_parent(self, index: int): if index == 0: raise IndexError('index-0 has no parent') return (index - 1) // 2 # 起始索引为0的完全二叉树下,返回给定索引的左孩子索引 def get_left_child(self, index: int): return 2 * index + 1 # 起始索引为0的完全二叉树下,返回给定索引的右孩子索引 def get_right_child(self, index: int): return 2 * index + 2 # 向堆内添加新元素 e def add(self, e): self.__data.add_to_last(e) self.__sift_up(self.__data.get_size() - 1) # 上浮操作 def __sift_up(self, index: int): while index > 0 and self.__data.get(self.get_parent(index)) < self.__data.get(index): self.__data.swap(index, self.get_parent(index)) index = self.get_parent(index) # 获取堆内最大元素 def get_max(self): if self.get_size() == 0: raise IndexError('heap is empty') return self.__data.get(0) # 获取并从堆内删除最大元素 def extract_max(self): ret = self.get_max() self.__data.set(0, ret) self.__data.remove_last() self.__sift_down(0) return ret # 下沉操作 def __sift_down(self, index: int): while self.get_left_child(index) < self.get_size(): j = self.get_left_child(index) if j + 1 < self.get_size() and self.__data.get(j + 1) > self.__data.get(j): j += 1 if self.__data.get(index) > self.__data.get(j): break else: self.__data.swap(index, j) index = j # 取出堆内元素最大值,并替换为新值 def replace(self, ele): ret = self.get_max() self.__data.set(0, ele) self.__sift_down(0) return ret
def in_range(x, minx, maxx): ret = Array(x.m, x.n, False) ret.rows = [min(max(minx, item[0]), maxx) for item in zip(x.rows)] return ret
def generate_data(case, theta, seed): # Advance random seed for parameter and covariate construction seed.next() case = params["case"] alpha = beta = kappa = offset = 0 conditional_sample = False if "fixed_example" in case: # Load parameters and covariates with open(case["fixed_example"], "r") as example_file: example = json.load(example_file) v = np.array(example["nu"]) M, N = v.shape if "alpha" in example: alpha = np.array(example["alpha"]).reshape((M, 1)) if "beta" in example: beta = np.array(example["beta"]).reshape((1, N)) if "kappa" in example: kappa = example["kappa"] if "offset" in example: offset = example["offset"] if ("r" in example) and ("c" in example): conditional_sample = True r = example["r"] c = example["c"] else: # Generate parameters and covariates M, N = case["M"], case["N"] if "alpha_min" in case: alpha = np.random.uniform(size=(M, 1)) + case["alpha_min"] if "beta_min" in case: beta = np.random.uniform(size=(1, N)) + case["beta_min"] if "kappa" in case: kappa = case["kappa"] if case["v_discrete"]: v = np.random.random(size=(M, N)) < 0.5 else: v = np.random.uniform(size=(M, N)) if "v_min" in case: v += case["v_min"] if ("r" in case) and ("c" in case): conditional_sample = True r = case["r"] c = case["c"] # Generate Bernoulli probabilities from logistic regression model logit_P = np.zeros((M, N)) + kappa logit_P += alpha logit_P += beta logit_P += theta * v logit_P += offset if conditional_sample: arr = Array(M, N) arr.new_edge_covariate("x_0")[:] = logit_P arr.new_row_covariate("r", dtype=np.int)[:] = r arr.new_col_covariate("c", dtype=np.int)[:] = c base_model = StationaryLogistic() base_model.beta["x_0"] = 1.0 data_model = FixedMargins(base_model) while True: # Advance random seed for data generation seed.next() # Generate data for this trial if conditional_sample: X = data_model.generate(arr, coverage=2.0) else: P = 1.0 / (1.0 + np.exp(-logit_P)) X = np.random.random((M, N)) < P yield X, v
def testCrossEntropyError(self): a1 = Array.fromList([1, 2, 3, 4, 5]) t = 2 e = cross_entropy_error(a1, t) print(e)
class LUT1D(ProcessNode): "A Common LUT Format LUT 1D ProcessNode element" def __init__( self, inBitDepth=bitDepths["FLOAT16"], outBitDepth=bitDepths["FLOAT16"], id="", name="", interpolation="linear", rawHalfs="", halfDomain="", ): "%s - Initialize the standard class variables" % "LUT1D" ProcessNode.__init__(self, "LUT1D", inBitDepth, outBitDepth, id, name) if interpolation != "": self._attributes["interpolation"] = interpolation if rawHalfs != "": self._attributes["rawHalfs"] = rawHalfs if halfDomain != "": self._attributes["halfDomain"] = halfDomain self._array = None self._indexMaps = [] # __init__ def setIndexMaps(self, valuesR, valuesG=None, valuesB=None): indexMapR = IndexMap(len(valuesR[0]), valuesR) self._indexMaps.append(indexMapR) self.addElement(indexMapR) # Either one or three indexMaps if valuesG != None and valuesB != None: indexMapG = IndexMap(len(valuesG[0]), valuesG) self._indexMaps.append(indexMapG) self.addElement(indexMapG) indexMapB = IndexMap(len(valuesB[0]), valuesB) self._indexMaps.append(indexMapB) self.addElement(indexMapB) # setIndexMaps def setArray(self, dimension, values, floatEncoding="string"): dimensions = [len(values) / dimension, dimension] integers = bitDepthIsInteger(self.getAttribute("outBitDepth")) rawHalfs = not (self.getAttribute("rawHalfs") in [None, False]) self._array = Array(dimensions, values, rawHalfs=rawHalfs, integers=integers, floatEncoding=floatEncoding) self.addElement(self._array) # setArray def getLUTDimensions(self): return self._array.getDimensions() def getLUTValues(self): return self._array.getValues() def getIndexMapDimensions(self, channel): return self._indexMaps[channel].getDimensions() def getIndexMapValues(self, channel): return self._indexMaps[channel].getValues() def readChild(self, element): child = None if element.tag == "Array": rawHalfs = not (self.getAttribute("rawHalfs") in [None, False]) child = Array(rawHalfs=rawHalfs) child.read(element) integers = bitDepthIsInteger(self.getAttribute("outBitDepth")) child.setValuesAreIntegers(integers) self._array = child elif element.tag == "IndexMap": child = IndexMap() child.read(element) self._indexMaps.append(child) return child # readChild def process(self, values, stride=0, verbose=False): # Base attributes inBitDepth = self._attributes["inBitDepth"] outBitDepth = self._attributes["outBitDepth"] # Node attributes interpolation = "" if "interpolation" in self._attributes: interpolation = self._attributes["interpolation"] rawHalfs = not (self.getAttribute("rawHalfs") in [None, False]) halfDomain = not (self.getAttribute("halfDomain") in [None, False]) """ print( "interpolation : %s" % interpolation ) print( "raw halfs : %s" % rawHalfs ) print( "halfs domain : %s" % halfDomain ) """ # Get LUT dimensions dimensions = self.getLUTDimensions() # Handle processing of single values if stride == 0: stride = len(values) # Initialize the output value outValues = np.zeros(len(values), dtype=np.float32) for p in range(len(values) / stride): value = values[p * stride : (p + 1) * stride] outValue = values[p * stride : (p + 1) * stride] for i in range(min(3, stride)): # Run through single Index Map then normalize if len(self._indexMaps) > 1: outValue[i] = self._indexMaps[i].process(outValue[i]) outValue[i] /= float(dimensions[0] - 1) # Run through per-channel Index Map then normalize elif len(self._indexMaps) > 0: outValue[i] = self._indexMaps[0].process(outValue[i]) outValue[i] /= float(dimensions[0] - 1) # Normalize from bit-depth else: # Convert input bit depth outValue[i] = bitDepthToNormalized(outValue[i], inBitDepth) # Run through LUT # Use Cubic interpolation if interpolation == "cubic": outValue[i] = self._array.lookup1DCubic(outValue[i], i) # Use halfDomain lookup and interpolation elif halfDomain: outValue[i] = self._array.lookup1DHalfDomain(outValue[i], i, interpolate=True) # Linear interpolation is the default # elif interpolation == 'linear': else: outValue[i] = self._array.lookup1DLinear(outValue[i], i) # Bit Depth conversion for output is ignored for LUTs # as LUT values are assumed to target a specific bit depth # outValue[i] = normalizedToBitDepth(outValue[i], outBitDepth) # Copy the extra channels for i in range(min(3, stride), stride): outValue[i] = value[i] # Copy to the output array outValues[p * stride : (p + 1) * stride] = outValue return outValues
import os os.chdir(r'E:\Python') from Array import Array from Node import Node from Array2Link import Array2Link from replaceLinkList import replaceLinkList a1 = Array(20, 5) a1[3] = 20 a1[6] = 55 a1n = Array2Link(a1) a1h = a1n.convert() print('data ', a1h.data) a1h = replaceLinkList(a1h, 5, -33) while (a1h != None): print(a1h.data) a1h = a1h.nexti
def clear(self): """重置一个空的栈""" self._size = 0 self._items = Array(ArrayStack.DEFAULT_CAPACITY)
def execute(self, vm): length = vm.stack.pop().value a = Array(length) a.arrayType = self.type vm.stack.push(a)
def __init__(self, maxSize): self._count = 0 self._front = 0 self._back = maxSize - 1 self._qArray = Array(maxSize)
def __init__(self, levels): self.queue_levels = Array(levels) for num_level in range(levels): self.queue_levels[num_level] = linkedqueue() self.size = 0
class IndexMap: "A Common LUT Format IndexMap element" def __init__(self, dimension=[], values=[], elementType='IndexMap', useCachedProcess=False): "%s - Initialize the standard class variables" % elementType self._dimension = dimension self._values = values self._elementType = elementType self._useCachedProcess = useCachedProcess self._processCached = None if self._useCachedProcess and self._values != [] and self._dimension != []: self._createCachedProcess() # __init__ def setDimension(self, dimension): self._dimension = dimension def getDimension(self): return self._dimension def setValues(self, values): self._values = values if self._useCachedProcess and self._values != [] and self._dimension != []: self._createCachedEval() def getValues(self): return self._values def setuseCachedProcess(self, useCachedProcess): self._useCachedProcess = useCachedProcess def getUseCachedProcessue(self): return self._useCachedProcess # evaluation and caching def _processRaw(self, value, verbose=False): inputValues = self._values[0] outputValues = self._values[1] # NaNs if np.isnan(value): result = value # Infs elif np.isinf(value): result = value # Normal numbers # Below the input range elif value <= inputValues[0]: result = outputValues[0] # Above the input range elif value >= inputValues[-1]: result = outputValues[-1] # Within the input range else: for i in range(len(inputValues)): if value <= inputValues[i + 1]: inputLow = inputValues[i] inputHigh = inputValues[i + 1] interp = (value - inputLow) / (inputHigh - inputLow) outputLow = outputValues[i] outputHigh = outputValues[i + 1] result = interp * (outputHigh - outputLow) + outputLow break return result # process # _createCachedEval def _createCachedProcess(self): channels = 1 resolution = 65536 cacheValues = [0.0] * resolution for i in range(resolution): # Figure out which half value corresponds to the specific 16 bit integer value sample = uint16ToHalf(i) # Apply the function to the sample value # Should take the channel as input, or process an RGB triple fvalue = self._processRaw(sample) # Store the values for c in range(channels): cacheIndex = i * channels + c cacheValues[cacheIndex] = fvalue #print( "%d, %d, %d: %f -> %f" % (i, c, lutValueIndex, sample, fvalue)) dimensions = [len(cacheValues), channels] self._processCached = Array(dimensions, cacheValues) # _createCachedEval # Read / Write def write(self, tree): element = etree.SubElement(tree, self._elementType) element.set('dim', str(self._dimension)) # XXX # Make this pretty at some point element.text = " ".join( map(lambda a, b: "%s@%s" % (float(a), int(b)), self._values[0], self._values[1])) return element # write def read(self, element): # Store attributes for key, value in element.attrib.iteritems(): if key == 'dim': self._dimension = int(value) self._values = [] self._values.append( map(lambda p: float(p.split('@')[0]), element.text.split())) self._values.append( map(lambda p: float(p.split('@')[1]), element.text.split())) # read # Process values def process(self, value, verbose=False): # Pull results from cache if self._useCachedProcess: if self._processCached == None: self._createCachedProcess() result = self._processCached.lookup1DHalfDomainInterpolated( value, 0) # Evaluate with base IndexMap values else: result = self._processRaw(value, verbose) return result # process def printInfo(self): print("%20s" % "IndexMap") length = len(self._values[0]) print("%20s : %s" % ("Length", length)) #print( "\t\tvalues : %s" % self._values ) print("%20s" % "Values") if length < 15: print("\t\tmap : %s" % " ".join( map(lambda a, b: "%s,%s" % (a, b), self._values[0], self._values[1]))) else: pairs = map(lambda a, b: "%6.9f, %6.9f" % (a, b), self._values[0], self._values[1]) for n in (range(3)): print(" " * 30 + pairs[n]) print(" " * 30 + " ... ") for n in (range(length - 3, length)): print(" " * 30 + pairs[n])
class Binary_translator: def __init__(self): self.register_quantity = 32 self.rows = 3 self.cols = 3 self.mults = 1 self.mem = 1 self.level_amount = 1 self.array = Array(self.rows, self.cols, self.mults, self.mem, self.level_amount) #self.insert_fault(0, 'alu', (0,0)) def prepare_line(self, line): if line == '': return True words = line.rstrip('\n').split(' ') operation = words[0].upper() if operation in ALU_operations: if self.array.set_alus(words): return True else: print("error in " + operation) return False elif operation in MULT_operations: if self.array.set_mult(words): return True else: print("error in " + operation) return False elif operation in MEM_operations: if self.array.set_memory(words): return True else: print("error in " + operation) return False else: raise Exception def decode_assembly(self, text, line): while (line < len(text)): if not self.prepare_line(text[line]): break line += 1 return line def get_mux4_selector(self, string): return '{:02b}'.format(string) def get_mux32_selector(self, string): string = ''.join(filter(lambda x: x.isdigit(), string)) if string == '': return '11111' reg_number = "{:05b}".format(int(string)) return reg_number def get_operation_code(self, op_string): op_string = op_string.upper() if op_string == 'ADD': return '000' elif op_string == 'SUB': return '001' elif op_string == 'AND': return '010' elif op_string == 'OR': return '011' elif op_string == 'XOR': return '100' elif op_string == 'NOT': return '101' elif op_string == 'X': return '111' else: raise Exception def translate_alu_input_muxes(self, level): bitstream = '' for read_alu in level.alu_source: for (in1, in2) in read_alu: bitstream += self.get_mux32_selector( in1)[::-1] + self.get_mux32_selector(in2)[::-1] return bitstream def translate_alu_op(self, level): bitstream = '' for op_line in level.alu_op: for op in op_line: if op: bitstream += self.get_operation_code(op)[::-1] else: bitstream += '111' return bitstream def translate_output_alus(self, level): bitstream = '' registers = [ "R{0:02d}".format(i) for i in range(self.register_quantity) ] for row in range(self.rows): for reg in registers: line = level.alu_target[row] if reg in line: col = line.index(reg) bitstream += self.get_mux4_selector(col)[::-1] else: bitstream += '11' return bitstream def translate_final_muxes(self, level): bitstream = '' registers = [ "R{0:02d}".format(i) for i in range(self.register_quantity) ] for reg in registers: if level.register_in_mult(reg): bitstream += '00' elif level.register_in_memory(reg) and level.memory_op[ level.memory_target.index(reg)] == "LW": bitstream += '10' elif level.register_in_ALUs(reg): bitstream += '01' else: bitstream += '11' return bitstream def translate_mult_sources(self, level): bitstream = '' for (in1, in2) in level.mult_source: bitstream += self.get_mux32_selector( in1)[::-1] + self.get_mux32_selector(in2)[::-1] return bitstream def translate_memory_data(self, level): bitstream = '' for addr in level.memory_addr: if addr: bitstream += addr[::-1] else: bitstream += '00000' for pos in level.memory_pos: if addr: bitstream += pos[::-1] else: bitstream += '0000000000000000' for write in level.memory_op: if write == 'SW': bitstream += '1' else: bitstream += '0' for in1 in level.memory_target: bitstream += self.get_mux32_selector(in1)[::-1] return bitstream def translate_levels(self): bitstream = '' for level in self.array.levels: bitstream += self.translate_alu_input_muxes(level) bitstream += self.translate_alu_op(level) bitstream += self.translate_output_alus(level) bitstream += self.translate_final_muxes(level) bitstream += self.translate_mult_sources(level) bitstream += self.translate_memory_data(level) return bitstream def insert_fault(self, level, component, pos): fault = (component, pos) self.array.levels[level].insert_fault(fault) def clear(self): for level in self.array.levels: level.clear()
os.chdir(r'E:\Python') from Array import Array from Node import Node from Array2Link import Array2Link from replaceLinkList import replaceLinkList def insertAtend(head, data): shead = head if (head == None): head = Node(data) head.nexti = None else: while (head.nexti != None): head = head.nexti head.nexti = Node(data) return (shead) a1 = Array(10, 5) a1[3] = 20 a1[6] = 55 a1n = Array2Link(a1) a1h = a1n.convert() print('data ', a1h.data) a1h = insertAtend(a1h, 222) while (a1h != None): print(a1h.data) a1h = a1h.nexti
#!/usr/bin/env python3 from Array import Array import pytest shape = (4, ) shape2 = (5, ) shape2d = (2, 3) testArray = Array(shape, 1, 2, 3, 4) testArray2 = Array(shape, 2, 4, 6, 8) failarray = Array(shape2, 4, 4, 5, 3, 9) test2d = Array(shape2d, 3, 5, 1, 5, 6, 6) test2d2 = Array(shape2d, 3, 5, -10, 5, 0, 5) #print-test def test_print(): assert testArray.__str__() == "[1, 2, 3, 4]" assert testArray2.__str__() == "[2, 4, 6, 8]" def test_print_2d(): assert test2d.__str__() == "[[3, 5, 1][5, 6, 6]]" assert test2d2.__str__() == "[[3, 5, -10][5, 0, 5]]" #tests for adding element to array def test_add_element(): assert testArray.__add__(5) == Array((4, ), 6, 7, 8, 9) assert testArray.__add__(3) == Array((4, ), 4, 5, 6, 7) assert 5 + testArray == Array((4, ), 6, 7, 8, 9)
class ArrayQueue(Queue): def __init__(self, capacity: int = 10): self.__data = Array(capacity) def get_size(self): return self.__data.get_size() def enqueue(self, ele): self.__data.add_to_last(ele) def is_empty(self): return self.__data.is_empty() def dequeue(self): return self.__data.remove_first() def get_front(self): return self.__data.get_first() def get_capacity(self): return self.__data.get_capacity() def __str__(self): bstr = 'ArrayQueue size: %d, capacity: %d\n' % (self.__data.get_size(), self.__data.get_capacity()) bstr += 'front [' for i in range(self.__data.get_size()): bstr += '' + str(self.__data.get(i)) if i != self.__data.get_size() - 1: bstr += ', ' bstr += '] tail' return bstr
def test_add_array(): assert testArray.__add__(testArray2) == Array((4, ), 3, 6, 9, 12) assert testArray.__add__(failarray) == NotImplemented
def __init__(self, capacity: int = 10): self.__data = Array(capacity)
def test_sub_element(): assert testArray.__sub__(5) == Array((4, ), -4, -3, -2, -1) assert testArray.__sub__(3) == Array((4, ), -2, -1, 0, 1)
def test_sub_2d(): assert test2d - test2d2 == Array((2, 3), 0, 0, 11, 0, 6, 1) assert test2d - 5 == Array((2, 3), -2, 0, -4, 0, 1, 1)
class LUT3D(ProcessNode): "A Common LUT Format LUT 3D ProcessNode element" def __init__(self, inBitDepth=bitDepths["FLOAT16"], outBitDepth=bitDepths["FLOAT16"], id="", name="", interpolation='trilinear'): "%s - Initialize the standard class variables" % 'LUT3D' ProcessNode.__init__(self, 'LUT3D', inBitDepth, outBitDepth, id, name) if interpolation != '': self._attributes['interpolation'] = interpolation self._array = None self._indexMaps = [] # __init__ def setIndexMaps(self, valuesR, valuesG=None, valuesB=None): indexMapR = IndexMap(len(valuesR[0]), valuesR) self._indexMaps.append(indexMapR) self.addElement(indexMapR) # Either one or three indexMaps if (valuesG != None and valuesB != None): indexMapG = IndexMap(len(valuesG[0]), valuesG) self._indexMaps.append(indexMapG) self.addElement(indexMapG) indexMapB = IndexMap(len(valuesB[0]), valuesB) self._indexMaps.append(indexMapB) self.addElement(indexMapB) # setIndexMaps def setArray(self, dimension, values, floatEncoding='string'): dimensions = dimension dimensions.append(3) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) self._array = Array(dimensions, values, integers, floatEncoding=floatEncoding) self.addElement(self._array) # setArray def getLUTDimensions(self): return self._array.getDimensions() def getLUTValues(self): return self._array.getValues() def getIndexMapDimensions(self, channel): return self._indexMaps[channel].getDimensions() def getIndexMapValues(self, channel): return self._indexMaps[channel].getValues() def readChild(self, element): child = None elementType = self.getElementType(element.tag) if elementType == 'Array': child = Array() child.read(element) integers = bitDepthIsInteger(self.getAttribute('outBitDepth')) child.setValuesAreIntegers(integers) self._array = child elif elementType == 'IndexMap': child = IndexMap() child.read(element) self._indexMaps.append(child) return child # readChild def process(self, values, stride=0, verbose=False): # Base attributes inBitDepth = self._attributes['inBitDepth'] outBitDepth = self._attributes['outBitDepth'] # Node attributes interpolation = '' if 'interpolation' in self._attributes: interpolation = self._attributes['interpolation'] ''' print( "interpolation : %s" % interpolation ) ''' # Get LUT dimensions dimensions = self.getLUTDimensions() # Handle processing of single values if stride == 0: stride = len(values) # Initialize the output value outValues = np.zeros(len(values), dtype=np.float32) for p in range(len(values) / stride): value = values[p * stride:(p + 1) * stride] outValue = values[p * stride:(p + 1) * stride] # Run each channel through the index map, or base normalization for i in range(min(3, len(value))): # Run through per-channel Index Map then normalize if len(self._indexMaps) > 1: outValue[i] = self._indexMaps[i].process(outValue[i]) outValue[i] /= float(dimensions[i] - 1) # Run through single Index Map then normalize elif len(self._indexMaps) > 0: outValue[i] = self._indexMaps[0].process(outValue[i]) outValue[i] /= float(dimensions[i] - 1) # Normalize from bit-depth else: # Convert input bit depth outValue[i] = bitDepthToNormalized(outValue[i], inBitDepth) # Run color through LUT # trilinear interpolation if interpolation == 'trilinear': outValue[0:3] = self._array.lookup3DTrilinear(outValue) # tetrahedral interpolation elif interpolation == 'tetrahedral': outValue[0:3] = self._array.lookup3DTetrahedral(outValue) # Bit Depth conversion for output is ignored for LUTs # as LUT values are assumed to target a specific bit depth #for i in range(min(3, len(value))): # outValue[i] = normalizedToBitDepth(outValue[i], outBitDepth) # Copy the extra channels for i in range(min(3, stride), stride): outValue[i] = value[i] # Copy to the output array outValues[p * stride:(p + 1) * stride] = outValue return outValues
def test_mul_array(): assert testArray.__mul__(testArray2) == Array((4, ), 2, 8, 18, 32) assert testArray.__mul__(failarray) == NotImplemented