def iahistogram(f): """ o Purpose Image histogram. o Synopsis h = iahistogram(f) o Input f: o Output h: o Description o Examples f = iaread('woodlog.pgm') iashow(f) h = iahistogram(f) g,d = iaplot(h) g('set data style boxes') g.plot(d) showfig(h) """ from Numeric import asarray,searchsorted,sort,ravel,concatenate,product f = asarray(f) n = searchsorted(sort(ravel(f)), range(max(ravel(f))+1)) n = concatenate([n, [product(f.shape)]]) h = n[1:]-n[:-1] return h
def write(fieldname, input, timestep): "Transforms mm5 input to GIF file" field = input.get_field(fieldname, timestep) if (field == -1): return -1 outdat = fieldname + `timestep` dim_ns = field['dim_ns'] dim_ew = field['dim_ew'] dim_z = field['dim_z'] dim_k = dim_z arrfield = field['values'] for k in xrange(dim_k): maxval = max(ravel(arrfield[k])) minval = min(ravel(arrfield[k])) rprec = (maxval - minval) / 255.0 arrfield[k] = (arrfield[k] - minval) / rprec arrfield = arrfield.astype(Int32) try: palette = ImagePalette.load('rgb.txt') except: print "Using System color palette." print "To use your own color palette, copy the file /disk1/utenti/giuliani/python_mm5/pillo/etc/rgb.txt" print "in this directory and modify as needed." palette = ImagePalette.load('/disk1/utenti/giuliani/python_mm5/pillo/etc/rgb.txt') for k in xrange(dim_k): outname = outdat + '_' + `k` + '.gif' try: img = Image.fromstring("I", (dim_ew, dim_ns), arrfield[k].tostring()) except (ValueError, IOError), e: print "Cannot convert to GIF: ", e return -1 img = img.transpose(Image.FLIP_LEFT_RIGHT) img = img.transpose(Image.ROTATE_180) img = img.convert("L") img = img.resize((dim_ew*8, dim_ns*8)) img = img.filter(ImageFilter.SMOOTH_MORE) img.putpalette(palette) try: img.save(outname, "GIF") except IOError, e: print "Cannot write GIF file: ", e return -1
def safe_sum_p_log_p(a, base=None): """Calculates p * log(p) safely for an array that may contain zeros.""" flat = ravel(a) nz = take(flat, nonzero(flat)) logs = log(nz) if base: logs /= log(base) return sum(nz * logs)
def pairs_to_array(pairs, num_items=None, transform=None): """Returns array with same data as pairs (list of tuples). pairs can contain (first, second, weight) or (first, second) tuples. If 2 items in the tuple, weight will be assumed to be 1. num_items should contain the number of items that the pairs are chosen from. If None, will calculate based on the largest item in the actual list. transform contains a array that maps indices in the pairs coordinates to other indices, i.e. transform[old_index] = new_index. It is anticipated that transform will be the result of calling ungapped_to_gapped on the original, gapped sequence before the sequence is passed into something that strips out the gaps (e.g. for motif finding or RNA folding). WARNING: all tuples must be the same length! (i.e. if weight is supplied for any, it must be supplied for all. WARNING: if num_items is actually smaller than the biggest index in the list (+ 1, because the indices start with 0), you'll get an exception when trying to place the object. Don't do it. """ #handle easy case if not pairs: return array([]) data = array(pairs) #figure out if we're mapping the indices to gapped coordinates if transform: #pairs of indices idx_pairs = take(transform, data[:,0:2].astype(Int32)) else: idx_pairs = data[:,0:2].astype(Int32) #figure out biggest item if not supplied if num_items is None: num_items = int(max(ravel(idx_pairs))) + 1 #make result array result = zeros((num_items,num_items), Float64) if len(data[0]) == 2: values = 1 else: values = data[:,2] put(ravel(result), idx_pairs[:,0]*num_items+idx_pairs[:,1], values) return result
def find_smallest_index(matrix): """returns the index of the smallest element in a Numeric array for UPGMA clustering elements on the diagonal should first be substituted with a very large number so that they are always larger than the rest if the values in the array""" #get the shape of the array as a tuple (e.g. (3,3)) shape = matrix.shape #turn into a 1 by x array and get the index of the lowest number matrix1D = ravel(matrix) lowest_index = argmin(matrix1D) #convert the lowest_index derived from matrix1D to one for the original #square matrix and return row_len = shape[0] return divmod(lowest_index, row_len)
def G_ind(m, williams=False): """Returns G test for independence in an r x c table. Requires input data as a Numeric array. From Sokal and Rohlf p 738. """ f_ln_f_elements = safe_sum_p_log_p(m) f_ln_f_rows = safe_sum_p_log_p(sum(m)) f_ln_f_cols = safe_sum_p_log_p(sum(m,1)) tot = sum(ravel(m)) f_ln_f_table = tot * log(tot) df = (len(m)-1) * (len(m[0])-1) G = 2*(f_ln_f_elements-f_ln_f_rows-f_ln_f_cols+f_ln_f_table) if williams: q = 1+((tot*sum(1.0/sum(m,1))-1)*(tot*sum(1.0/sum(m))-1)/ \ (6*tot*df)) G = G/q return G, chi_high(max(G,0), df)
def safe_log(a): """Returns the log (base 2) of each nonzero item in a. a: Numeric array WARNING: log2 is only defined on positive numbers, so make sure there are no negative numbers in the array. Always returns an array with floats in there to avoid unexpected results when applying it to an array with just integers. """ c = array(a.copy(),Float64) flat = ravel(c) nz_i = nonzero(flat) nz_e = take(flat,nz_i) log_nz = log2(nz_e) put(flat,nz_i,log_nz) return c
def iagaussian(s,mu,sigma): """ o Purpose Generate a 2D Gaussian image. o Synopsis g = iagaussian(s,mu,sigma) o Input s: [rows columns] mu: Mean vector. 2D point (x;y). Point of maximum value. sigma: covariance matrix (square). [ Sx^2 Sxy; Syx Sy^2] o Output g: o Description A 2D Gaussian image is an image with a Gaussian distribution. It can be used to generate test patterns or Gaussian filters both for spatial and frequency domain. The integral of the gaussian function is 1.0. o Examples import Numeric f = iagaussian([8,4], [3,1], [[1,0],[0,1]]) print Numeric.array2string(f, precision=4, suppress_small=1) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) print g f = iagaussian(100, 50, 10*10) g = ianormalize(f, [0,1]) g,d = iaplot(g) showfig(g) f = iagaussian([50,50], [25,10], [[10*10,0],[0,20*20]]) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) iashow(g) """ from Numeric import asarray,product,arange,NewAxis,transpose,matrixmultiply,reshape,concatenate,resize,sum,zeros,Float,ravel,pi,sqrt,exp from LinearAlgebra import inverse,determinant if type(sigma).__name__ in ['int', 'float', 'complex']: sigma = [sigma] s, mu, sigma = asarray(s), asarray(mu), asarray(sigma) if (product(s) == max(s)): x = arange(product(s)) d = x - mu if len(d.shape) == 1: tmp1 = d[:,NewAxis] tmp3 = d else: tmp1 = transpose(d) tmp3 = tmp1 if len(sigma) == 1: tmp2 = 1./sigma else: tmp2 = inverse(sigma) k = matrixmultiply(tmp1, tmp2) * tmp3 else: aux = arange(product(s)) x, y = iaind2sub(s, aux) xx = reshape(concatenate((x,y)), (2, product(x.shape))) d = transpose(xx) - resize(reshape(mu,(len(mu),1)), (s[0]*s[1],len(mu))) if len(sigma) == 1: tmp = 1./sigma else: tmp = inverse(sigma) k = matrixmultiply(d, tmp) * d k = sum(transpose(k)) g = zeros(s, Float) aux = ravel(g) if len(sigma) == 1: tmp = sigma else: tmp = determinant(sigma) aux[:] = 1./(2*pi*sqrt(tmp)) * exp(-1./2 * k) return g
def any(a, axis=None): if axis is None: return sometrue(ravel(a)) else: return sometrue(a, axis)
def all(a, axis=None): '''Numpy-compatible version of all()''' if axis is None: return alltrue(ravel(a)) else: return alltrue(a, axis)
def isnan(a): """y = isnan(x) returns True where x is Not-A-Number""" return reshape(array([_isnan(i) for i in ravel(a)],'b'), shape(a))
def isnan(a): """y = isnan(x) returns True where x is Not-A-Number""" return reshape(array([_isnan(i) for i in ravel(a)], 'b'), shape(a))