def testDictIndex(self): """ test that an index using a dictionary works correctly """ index = {'a':0,'b':0,'c':0} index2 = {'a':1,'b':0,'c':0} assert(na.all(self.a.distribution[0,0,0,:] == self.a.distribution[index]) and \ na.all(self.a.distribution[1,0,0,:] == self.a.distribution[index2])), \ "Error getting with dict index"
def testGetCPT(self): """ Violate abstraction and check that setCPT actually worked correctly, by getting things out of the matrix """ assert(na.all(self.a.distribution[0, 0, 0, :] == \ self.a.distribution.cpt[0, 0, 0, :]) and \ na.all(self.a.distribution[1, 0, 0, :] == \ self.a.distribution.cpt[1, 0, 0, :])), \ "Error getting raw cpt"
def areArraysEqual(arr1, arr2): """ Are both `arr1` and `arr2` equal arrays? Arguments can be regular NumPy arrays, chararray arrays or record arrays (including nested record arrays). They are checked for type and value equality. """ t1 = type(arr1) t2 = type(arr2) if not ((hasattr(arr1, 'dtype') and arr1.dtype == arr2.dtype) or issubclass(t1, t2) or issubclass(t2, t1)): return False if numarray_imported: if isinstance(arr1, nra.NestedRecArray): arr1 = arr1.asRecArray() if isinstance(arr2, nra.NestedRecArray): arr2 = arr2.asRecArray() if isinstance(arr1, nra.NestedRecord): row = arr1.row arr1 = arr1.array[row:row+1] if isinstance(arr2, nra.NestedRecord): row = arr2.row arr2 = arr2.array[row:row+1] if numarray_imported and isinstance(arr1, numarray.records.RecArray): arr1Names = arr1._names arr2Names = arr2._names if arr1Names != arr2Names: return False for fieldName in arr1Names: if not areArraysEqual(arr1.field(fieldName), arr2.field(fieldName)): return False return True if numarray_imported and isinstance(arr1, numarray.NumArray): if arr1.shape != arr2.shape: return False if arr1.type() != arr2.type(): return False # The lines below are equivalent #return numarray.alltrue(arr1.flat == arr2.flat) return numarray.all(arr1 == arr2) if numarray_imported and isinstance(arr1, numarray.strings.CharArray): if arr1.shape != arr2.shape: return False if arr1._type != arr2._type: return False return numarray.all(arr1 == arr2) return numpy.all(arr1 == arr2)
def testNumSet(self): """ test that a raw index of numbers can access and set a position in the """ self.a.distribution[0,0,0,:] = -1 self.a.distribution[1,0,0,:] = 100 self.a.distribution[1,1,0,:] = na.array([-2, -3]) assert(na.all(self.a.distribution[0,0,0,:] == na.array([-1, -1])) and \ na.all(self.a.distribution[1,0,0,:] == na.array([100, 100])) and \ na.all(self.a.distribution[1,1,0,:] == na.array([-2, -3]))), \ "Error Setting cpt with num indices"
def testDictSet(self): """ test that an index using a dictionary can set a value within the cpt """ index = {'a':0,'b':0,'c':0} index2 = {'a':1,'b':0,'c':0} index3 = {'a':1,'b':1,'c':0} self.a.distribution[index] = -1 self.a.distribution[index2] = 100 self.a.distribution[index3] = na.array([-2, -3]) assert(na.all(self.a.distribution[0,0,0,:] == na.array([-1, -1])) and \ na.all(self.a.distribution[1,0,0,:] == na.array([100, 100])) and \ na.all(self.a.distribution[1,1,0,:] == na.array([-2, -3]))), \ "Error setting cpt with dict"
def testBalancedDefault(self): a = GenTestK(9) b = GenTestK_Balanced(9) t = numarray.all(a == b) self.assert_(not t)
def testHorizontalLinesARGB32(self): colors = (Qt.red, Qt.green, Qt.blue, Qt.white, Qt.black) alpha = 0xff000000 red = 0x00ff0000 green = 0x0000ff00 blue = 0x000000ff white = 0x00ffffff black = 0x00000000 image = QImage(3, len(colors), QImage.Format_ARGB32) painter = QPainter(image) for i, color in enumerate(colors): painter.setPen(color) painter.drawLine(0, i, image.width(), i) del painter array = np.zeros((image.height(), image.width()), dtype=np.UInt32) array[0, :] = alpha | red array[1, :] = alpha | green array[2, :] = alpha | blue array[3, :] = alpha | white array[4, :] = alpha | black self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def testHorizontalLinesARGB32(self): colors = (Qt.red, Qt.green, Qt.blue, Qt.white, Qt.black) alpha = 0xff000000 red = 0x00ff0000 green = 0x0000ff00 blue = 0x000000ff white = 0x00ffffff black = 0x00000000 image = QImage(3, len(colors), QImage.Format_ARGB32) painter = QPainter(image) for i, color in enumerate(colors): painter.setPen(color) painter.drawLine(0, i, image.width(), i) del painter array = np.zeros((image.height(), image.width()), dtype=np.UInt32) array[0,:] = alpha|red array[1,:] = alpha|green array[2,:] = alpha|blue array[3,:] = alpha|white array[4,:] = alpha|black self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def testBalancedChange(self): a = pickle.load(open(self.testbalancedname)) x = GenTestK_Balanced(9) t = numarray.all(a == x) self.assert_(t)
def testCubeChange(self): a = pickle.load(open(self.testdataname)) x = GenTestK(9) t = numarray.all(a == x) self.assert_(t)
def testUnion(self): """ test Union between two Tables """ a = Table(['a','b','c','d'],[2,3,4,5],range(2*3*4*5)) b = Table(['c','b','e'],[4,3,6],range(12*6)) ab,bb = a.union(b) assert(ab.names_list == ['a','b','c','d','e'] and \ ab.shape == tuple([2,3,4,5,1]) and \ na.all(bb == na.transpose(b.cpt[...,na.NewAxis,na.NewAxis],axes=[3,1,0,4,2]))), \ """ union does not work ..."""
def testIMul(self): """ test inplace multiplication """ b = Table(['c','b','e'],[4,3,6],range(12*6)) c = Table(['a','b','c','d','e'],[2,3,4,5,6],range(2*3*4*5*6)) bcpt = b.cpt[...,na.NewAxis,na.NewAxis] bcpt.transpose([3,1,0,4,2]) res = bcpt*c.cpt c *= b assert (na.all(c.cpt == res)), \ " InPlace Multiplication does not work"
def testVerticalLinesIndexed8(self): image = QImage(5, 3, QImage.Format_Indexed8) image.setColorTable([qRgb(i, i, i) for i in range(256)]) for i in range(image.width()): for j in range(image.height()): image.setPixel(i, j, i) array = np.zeros((image.height(), image.width()), dtype=np.UInt8) for i in range(image.width()): array[:, i] = i self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def testVerticalLinesIndexed8(self): image = QImage(5, 3, QImage.Format_Indexed8) image.setColorTable([qRgb(i, i, i) for i in range(256)]) for i in range(image.width()): for j in range(image.height()): image.setPixel(i, j, i) array = np.zeros((image.height(), image.width()), dtype=np.UInt8) for i in range(image.width()): array[:,i] = i self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def testIDiv(self): """ test inplace division """ b = Table(['c','b','e'],[4,3,6],range(12*6)) c = Table(['a','b','c','d','e'],[2,3,4,5,6],range(2*3*4*5*6)) bcpt = b.cpt[...,na.NewAxis,na.NewAxis] bcpt.transpose([3,1,0,4,2]) res = c.cpt/bcpt res[getnan(res)] = 0.0 c /= b assert (na.all(c.cpt == res)), \ " InPlace Division does not work"
def testPrepareOther(self): c = Table(['e','b'],[2,3],range(6)) d = Table(['a','b','c','d','e'],[2,3,2,2,2],range(3*2**4)) e = Table(['e','b','f'],[2,3,4],range(6*4)) src = Table(['s','r','c'],[2,3,4],range(24)) cr = Table(['c','r'],[4,3],range(12)) dc = d.prepareOther(c) try: d.prepareOther(e) assert(0),""" this should produce an error...""" except: pass cr_ = src.prepareOther(cr) assert(dc.shape == tuple([1,3,1,1,2]) and \ na.all(dc[0,:,0,0,:] == na.transpose(c.cpt, axes=[1,0])) and \ cr_.shape == (1,3,4)), \ """ problem with prepareOther"""
def testVerticalLinesARGB32(self): colors = (Qt.red, Qt.green, Qt.blue) alpha = 0xff000000 red = 0x00ff0000 green = 0x0000ff00 blue = 0x000000ff image = QImage(len(colors), 5, QImage.Format_ARGB32) painter = QPainter(image) for i, color in enumerate(colors): painter.setPen(color) painter.drawLine(i, 0, i, image.height()) del painter array = np.zeros((image.height(), image.width()), dtype=np.UInt32) array[:,0] = alpha|red array[:,1] = alpha|green array[:,2] = alpha|blue self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def testVerticalLinesARGB32(self): colors = (Qt.red, Qt.green, Qt.blue) alpha = 0xff000000 red = 0x00ff0000 green = 0x0000ff00 blue = 0x000000ff image = QImage(len(colors), 5, QImage.Format_ARGB32) painter = QPainter(image) for i, color in enumerate(colors): painter.setPen(color) painter.drawLine(i, 0, i, image.height()) del painter array = np.zeros((image.height(), image.width()), dtype=np.UInt32) array[:, 0] = alpha | red array[:, 1] = alpha | green array[:, 2] = alpha | blue self.assertEqual(np.all(array == toNumpy(image)), True) self.assertEqual(image == toQImage(array), True)
def test_one(self): sound_start = 400 sound_stop = sound_start + len(self.sound) self.sched.add(sound_start) stream_out = [] target = numarray.array([0] * self.periodsize, numarray.Int16) for i in range(100): self.clock.set(self.periodsize * i, self.periodsize) target *= 0 self.sched.render(target) stream_out.extend(target) stream_out = numarray.array(stream_out, type=numarray.Int16) pre_write = stream_out[:sound_start] self.assertEqual(numarray.any(pre_write), False) written = stream_out[sound_start:sound_stop] self.assertEqual(numarray.all(written), True) post_write = stream_out[sound_stop:] self.assertEqual(bool(numarray.any(post_write)), False)
def fit(x, y, sig=None): #Given a set of data points x, y, #with individual standard deviations sig, #fit them to a straight line y = a + bx by minimizing chi-sq. #Returned are a, b and their respective probable uncertainties siga and sigb, #the chi-square chi2, and the scatter sigdat. #If mwt=0 on input, then the standard deviations are assumed to be unavailable: #the normalization of chi2 is to unit standard deviation on all points. if sig is None or N.all(sig == 1.0): mwt=False else: mwt=True sx=0.0 sy=0.0 st2=0.0 b=0.0 ndata = len(x) if mwt: #Accumulate sums ... ss=0.0 for i in range(ndata): #...with weights wt=1.0/sig[i]**2 ss += wt sx += x[i]*wt sy += y[i]*wt else: for i in range(ndata): #...or without weights. sx += x[i] sy += y[i] ss=ndata sxoss=sx/ss if mwt: for i in range(ndata): t=(x[i]-sxoss)/sig[i] st2 += t*t b += t*y[i]/sig[i] else: for i in range(ndata): t=x[i]-sxoss st2 += t*t b += t*y[i] b /= st2 #Solve for a, b, siga, and sigb. a=(sy-sx*b)/ss siga=sqrt((1.0+sx*sx/(ss*st2))/ss) sigb=sqrt(1.0/st2) #Calculate chi2. chi2=0.0 if mwt: for i in range(ndata): chi2 += ((y[i] - a - b*x[i]) / sig[i])**2 else: #For unweighted data evaluate typical sig using chi2, #and adjust the standard deviations. for i in range(ndata): chi2 += (y[i] - a - b*x[i])**2 sigdat=sqrt(chi2/(ndata-2)) siga *= sigdat sigb *= sigdat return a, b, siga, sigb, chi2
def test_write(self): scheduler.RESAMPLE = self._scheduler_RESAMPLE stream_out = buffering.Sound(1000) self.writer.write(stream_out) self.assertEqual(numarray.all(self.sound[:100]), True) self.assertEqual(numarray.any(self.sound[100:]), False)
def testSetCPT(self): """ Violate abstraction and check that we can actually set elements. """ self.a.distribution.cpt[0, 1, 0, :] = na.array([4, 5]) assert(na.all(self.a.distribution[0, 1, 0, :] == na.array([4, 5]))), \ "Error setting the array when violating abstraction"
def testNumIndex(self): """ test that a raw index of numbers works correctly """ assert(na.all(self.a.distribution[0, :, 0, :] == self.a.distribution[0, :, 0, :]) and \ na.all(self.a.distribution[1, 0, 0, :] == self.a.distribution[1, 0, 0, :])), \ "Error getting item with num indices"