def triangularPut(m1d, upper=1, lower=0): """Returns 2D masked array with elements of the given 1D array in the strictly upper (lower) triangle. Elements of the 1D array should be ordered according to the upper triangular part of the 2D matrix. The lower triangular part (if requested) equals to the transposed upper triangular part. If upper == lower == 1 a symetric matrix is returned. """ assert upper in [0,1] and lower in [0,1], "[0|1] expected for upper / lower" m1d = MA.asarray(m1d) assert MA.rank(m1d) == 1, "1D masked array expected" m2dShape0 = math.ceil(math.sqrt(2*m1d.shape[0])) assert m1d.shape[0] == m2dShape0*(m2dShape0-1)/2, "the length of m1d does not correspond to n(n-1)/2" if upper: if lower: mask = Numeric.fromfunction(lambda i,j: i==j, (m2dShape0, m2dShape0)) else: mask = Numeric.fromfunction(lambda i,j: i>=j, (m2dShape0, m2dShape0)) else: if lower: mask = Numeric.fromfunction(lambda i,j: i<=j, (m2dShape0, m2dShape0)) else: mask = Numeric.ones((m2dShape0, m2dShape0)) m2d = MA.ravel(MA.zeros((m2dShape0, m2dShape0), m1d.dtype.char)) condUpperTriang = Numeric.fromfunction(lambda i,j: i<j, (m2dShape0, m2dShape0)) putIndices = Numeric.compress(Numeric.ravel(condUpperTriang), Numeric.arange(0, m2dShape0**2, typecode=Numeric.Int)) MA.put(m2d, putIndices, m1d) m2d = MA.reshape(m2d, (m2dShape0, m2dShape0)) m2d = MA.where(condUpperTriang, m2d, MA.transpose(m2d)) return MA.array(m2d, mask=Numeric.logical_or(mask, MA.getmaskarray(m2d)))
def arc_by_radian(x, y, height, radian_range, thickness, gaussian_width): """ Radial arc with Gaussian fall-off after the solid ring-shaped region with the given thickness, with shape specified by the (start,end) radian_range. """ # Create a circular ring (copied from the ring function) radius = height/2.0 half_thickness = thickness/2.0 distance_from_origin = sqrt(x**2+y**2) distance_outside_outer_disk = distance_from_origin - radius - half_thickness distance_inside_inner_disk = radius - half_thickness - distance_from_origin ring = 1.0-bitwise_xor(greater_equal(distance_inside_inner_disk,0.0),greater_equal(distance_outside_outer_disk,0.0)) sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: inner_falloff = x*0.0 outer_falloff = x*0.0 else: with float_error_ignore(): inner_falloff = exp(divide(-distance_inside_inner_disk*distance_inside_inner_disk, 2.0*sigmasq)) outer_falloff = exp(divide(-distance_outside_outer_disk*distance_outside_outer_disk, 2.0*sigmasq)) output_ring = maximum(inner_falloff,maximum(outer_falloff,ring)) # Calculate radians (in 4 phases) and cut according to the set range) # RZHACKALERT: # Function float_error_ignore() cannot catch the exception when # both dividend and divisor are 0.0, and when only divisor is 0.0 # it returns 'Inf' rather than 0.0. In x, y and # distance_from_origin, only one point in distance_from_origin can # be 0.0 (circle center) and in this point x and y must be 0.0 as # well. So here is a hack to avoid the 'invalid value encountered # in divide' error by turning 0.0 to 1e-5 in distance_from_origin. distance_from_origin += where(distance_from_origin == 0.0, 1e-5, 0) with float_error_ignore(): sines = divide(y, distance_from_origin) cosines = divide(x, distance_from_origin) arcsines = arcsin(sines) phase_1 = where(logical_and(sines >= 0, cosines >= 0), 2*pi-arcsines, 0) phase_2 = where(logical_and(sines >= 0, cosines < 0), pi+arcsines, 0) phase_3 = where(logical_and(sines < 0, cosines < 0), pi+arcsines, 0) phase_4 = where(logical_and(sines < 0, cosines >= 0), -arcsines, 0) arcsines = phase_1 + phase_2 + phase_3 + phase_4 if radian_range[0] <= radian_range[1]: return where(logical_and(arcsines >= radian_range[0], arcsines <= radian_range[1]), output_ring, 0.0) else: return where(logical_or(arcsines >= radian_range[0], arcsines <= radian_range[1]), output_ring, 0.0)
def contactsDiff(self, ref, cutoff=None): """ Number of different B{residue-residue} contacts in this and reference complex. @param ref: to compare this one with @type ref: Complex @param cutoff: maximal atom-atom distance, None .. previous setting @type cutoff: float @return: number of contacts different in this and refererence complex. @rtype: int """ both = N.logical_or( self.resContacts(cutoff), ref.resContacts(cutoff)) return N.sum(N.sum(both)) - self.contactsShared( ref, cutoff )
def contactsOverlap(self, ref, cutoff=None): """ Fraction of overlapping B{residue-residue} contacts between this and reference complex. @param ref: reference complex @type ref: Complex @param cutoff: maximal atom-atom distance, None .. previous setting @type cutoff: float @return: fraction of contacts shared between this and ref (normalized to number of all contacts) @rtype: float """ equal = N.logical_and(self.resContacts( cutoff=cutoff ), ref.resContacts( cutoff=cutoff ) ) total = N.logical_or( self.resContacts(cutoff), ref.resContacts(cutoff) ) return N.sum(N.sum( equal )) * 1.0 / N.sum(N.sum( total ))
def ttest_rsmplA(self, ma3d, callback): """conducts related samples t-test on individual examples wrt factor A (variables, ma3d axis 1); returns Numeric array of p-values in shape (1, numExamples). """ ps = -1*Numeric.ones((ma3d.shape[0],), Numeric.Float) for eIdx in range(ma3d.shape[0]): a = ma3d[eIdx][0] b = ma3d[eIdx][1] cond = Numeric.logical_not(Numeric.logical_or(MA.getmaskarray(a), MA.getmaskarray(b))) a = Numeric.asarray(MA.compress(cond, a)) b = Numeric.asarray(MA.compress(cond, b)) if len(a) >= 2: try: ps[eIdx] = scipy.stats.ttest_rel(a,b)[1] except Exception, inst: print "Warning: %s" % str(inst) print "Example %i:\n%s\n%s\n" % (eIdx, str(a), str(b)) ps[eIdx] = 1.0 else: print "Warning: removing example %i:\n%s\n%s\n" % (eIdx, str(a), str(b)) ps[eIdx] = 1.0 callback()
assert Numeric.allclose(X, Y) print "Raw reduce using pypar.LAND OK" pypar.raw_reduce(testArray, X, pypar.BAND, 0, 0) if myid == 0: Y = Numeric.ones(N)*255 #Neutral element for & for i in range(numproc): Y = Numeric.bitwise_and(Y, Numeric.array(range(N))*(i+1)) assert Numeric.allclose(X, Y) print "Raw reduce using pypar.BAND OK" pypar.raw_reduce(testArray, X, pypar.LOR, 0, 0) if myid == 0: Y = Numeric.zeros(N) for i in range(numproc): Y = Numeric.logical_or(Y, Numeric.array(range(N))*(i+1)) assert Numeric.allclose(X, Y) print "Raw reduce using pypar.LOR OK" pypar.raw_reduce(testArray, X, pypar.BOR, 0, 0) if myid == 0: Y = Numeric.zeros(N) #Neutral element for | for i in range(numproc): Y = Numeric.bitwise_or(Y, Numeric.array(range(N))*(i+1)) assert Numeric.allclose(X, Y) print "Raw reduce using pypar.BOR OK" pypar.raw_reduce(testArray, X, pypar.LXOR, 0, 0) if myid == 0: Y = Numeric.zeros(N) for i in range(numproc):
def logical_unary_or(m1, m2): el = Numeric.logical_or(m1.filled(0), m2.filled(0)) mask = Numeric.logical_and(MA.getmaskarray(m1), MA.getmaskarray(m2)) return MA.array(el, mask=mask)
def __init__(self, centers, radii, gridSpacing, gridOrigin, gridSize): import numpy.oldnumeric as Numeric self.grid = Numeric.zeros(gridSize, Numeric.UnsignedInt8) self.origin = gridOrigin self.spacing = gridSpacing self.minSpacing = min(gridSpacing) self.positions = [] self.sphereMasks = {} #i=0 for p in centers: self.positions.append([ int(round((p[0] - gridOrigin[0]) / gridSpacing[0])), int(round((p[1] - gridOrigin[1]) / gridSpacing[1])), int(round((p[2] - gridOrigin[2]) / gridSpacing[2])) ]) #print i, p, self.positions[-1] #i += 1 for r in radii: dr = round(r / self.minSpacing) if not self.sphereMasks.has_key(dr): self.sphereMasks[dr] = SphereMask(dr) largeMap = self.grid lx, ly, lz = largeMap.shape from numpy.oldnumeric import logical_or i = 0 for p, r in zip(self.positions, radii): #print i smallMap = self.sphereMasks[round(r / self.minSpacing)].grid offx = smallMap.shape[0] / 2 offy = smallMap.shape[0] / 2 offz = smallMap.shape[0] / 2 #print 'off', offx, offy, offz px1 = p[0] - offx px2 = p[0] + offx + 1 py1 = p[1] - offy py2 = p[1] + offy + 1 pz1 = p[2] - offz pz2 = p[2] + offz + 1 ox = oy = oz = 0 ex, ey, ez = smallMap.shape lx, ly, lz = largeMap.shape #print 'p1', px1, py1, pz1 #print 'p2', px2, py2, pz2 #print 'dims', lx, ly, lz if px2 < 0 or py2 < 0 or py2 < 0 or px1 > lx or py1 > ly or py1 > lz: print 'discreteSpheres: WARNING: sphere %d outside grid, it is skipped', i continue if px1 < 0: ox = -px1 px1 = 0 if py1 < 0: oy = -py1 py1 = 0 if pz1 < 0: oz = -pz1 pz1 = 0 if px2 > lx: ex = lx - px2 px2 = lx if py2 > ly: ey = ly - py2 py2 = ly if pz2 > lz: ez = lz - pz2 pz2 = lx #print 'o', ox, oy, oz #print 'e', ex, ey, ez submap = largeMap[px1:px2, py1:py2, pz1:pz2] #print 'submap shape', submap.shape largeMap[px1:px2, py1:py2, pz1:pz2] = logical_or(submap, smallMap[ox:ex, oy:ey, oz:ez]) i += 1
def __init__(self, centers, radii, gridSpacing, gridOrigin, gridSize): import numpy.oldnumeric as Numeric self.grid = Numeric.zeros(gridSize, Numeric.UnsignedInt8) self.origin = gridOrigin self.spacing = gridSpacing self.minSpacing = min(gridSpacing) self.positions = [] self.sphereMasks = {} #i=0 for p in centers: self.positions.append( [ int(round((p[0]-gridOrigin[0])/gridSpacing[0])), int(round((p[1]-gridOrigin[1])/gridSpacing[1])), int(round((p[2]-gridOrigin[2])/gridSpacing[2])) ] ) #print i, p, self.positions[-1] #i += 1 for r in radii: dr = round(r/self.minSpacing) if not self.sphereMasks.has_key(dr): self.sphereMasks[dr] = SphereMask( dr ) largeMap = self.grid lx, ly, lz = largeMap.shape from numpy.oldnumeric import logical_or i=0 for p, r in zip(self.positions, radii): #print i smallMap = self.sphereMasks[round(r/self.minSpacing)].grid offx = smallMap.shape[0]/2 offy = smallMap.shape[0]/2 offz = smallMap.shape[0]/2 #print 'off', offx, offy, offz px1 = p[0]-offx px2 = p[0]+offx+1 py1 = p[1]-offy py2 = p[1]+offy+1 pz1 = p[2]-offz pz2 = p[2]+offz+1 ox = oy = oz = 0 ex,ey,ez = smallMap.shape lx,ly,lz = largeMap.shape #print 'p1', px1, py1, pz1 #print 'p2', px2, py2, pz2 #print 'dims', lx, ly, lz if px2<0 or py2<0 or py2<0 or px1>lx or py1>ly or py1>lz: print 'discreteSpheres: WARNING: sphere %d outside grid, it is skipped', i continue if px1<0: ox = -px1 px1 = 0 if py1<0: oy = -py1 py1 = 0 if pz1<0: oz = -pz1 pz1 = 0 if px2>lx: ex = lx-px2 px2 = lx if py2>ly: ey = ly-py2 py2 = ly if pz2>lz: ez = lz-pz2 pz2 = lx #print 'o', ox, oy, oz #print 'e', ex, ey, ez submap = largeMap[px1:px2, py1:py2, pz1:pz2] #print 'submap shape', submap.shape largeMap[px1:px2, py1:py2, pz1:pz2] = logical_or( submap, smallMap[ox:ex, oy:ey, oz:ez]) i += 1