def multiplication_table_n(n: int = 12, zero_out_multiples: int = None): """Returns a 2-dimensional multiplication table for size `n`. A multiplication table will contain all the multiples 1x1, 1x2, ..., 1xn on the first row; 2x1, 2x2, ..., 2xn on the seconds row; until nx1, nx2, ..., nxn on the last row. Certain multiples can be 'zeroed out' by giving the parameter `zero_out_multiples`. For example, giving the value '2' will make all even numbers zero on the output array. Parameters ---------- zero_out_multiples : int or None Multiples of this number are 'zeroed out' in the array. n : int Size of returned array 1...n Returns ------- numpy.ndarray nxn sized array of type int. """ ns = arange(n, dtype=int) + 1 # Counting starts from 1 arr = array(outerproduct(ns, ns)) if zero_out_multiples is not None: arr[arr % zero_out_multiples == 0] = 0 return arr
def compute_centroid(im,sigw=None,nb_iter=4): """ Computes centroid. #TODO: would be interesting to compare with Sam's moments based computation Calls: * gaussfitter.gaussfit """ if sigw is None: param=gaussfitter.gaussfit(im,returnfitimage=False) #print param sigw = (param[3]+param[4])/2 sigw = float(sigw) n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0,n1)) ry = array(range(0,n2)) Wc = ones((n1,n2)) centroid = zeros((1,2)) # Four iteration loop to compute the centroid i=0 for i in range(0,nb_iter): xx = npma.outerproduct(rx-centroid[0,0],ones(n2)) yy = npma.outerproduct(ones(n1),ry-centroid[0,1]) W = npma.exp(-(xx**2+yy**2)/(2*sigw**2)) centroid = zeros((1,2)) # Estimate Centroid Wc = copy(W) if i == 0:Wc = ones((n1,n2)) totx=0.0 toty=0.0 cx=0 cy=0 for cx in range(0,n1): centroid[0,0] += (im[cx,:]*Wc[cx,:]).sum()*(cx) totx += (im[cx,:]*Wc[cx,:]).sum() for cy in range(0,n2): centroid[0,1] += (im[:,cy]*Wc[:,cy]).sum()*(cy) toty += (im[:,cy]*Wc[:,cy]).sum() centroid = centroid*array([1/totx,1/toty]) return (centroid,Wc)
def compute_centroid(im, sigw=None, nb_iter=4): """ Computes centroid. #TODO: would be interesting to compare with Sam's moments based computation Calls: * gaussfitter.gaussfit """ if sigw is None: param = gaussfitter.gaussfit(im, returnfitimage=False) #print param sigw = (param[3] + param[4]) / 2 sigw = float(sigw) n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0, n1)) ry = array(range(0, n2)) Wc = ones((n1, n2)) centroid = zeros((1, 2)) # Four iteration loop to compute the centroid i = 0 for i in range(0, nb_iter): xx = npma.outerproduct(rx - centroid[0, 0], ones(n2)) yy = npma.outerproduct(ones(n1), ry - centroid[0, 1]) W = npma.exp(-(xx**2 + yy**2) / (2 * sigw**2)) centroid = zeros((1, 2)) # Estimate Centroid Wc = copy(W) if i == 0: Wc = ones((n1, n2)) totx = 0.0 toty = 0.0 cx = 0 cy = 0 for cx in range(0, n1): centroid[0, 0] += (im[cx, :] * Wc[cx, :]).sum() * (cx) totx += (im[cx, :] * Wc[cx, :]).sum() for cy in range(0, n2): centroid[0, 1] += (im[:, cy] * Wc[:, cy]).sum() * (cy) toty += (im[:, cy] * Wc[:, cy]).sum() centroid = centroid * array([1 / totx, 1 / toty]) return (centroid, Wc)
def proj_mat(siz, offset=0): n1, n2 = siz[0], siz[1] mat_proj = np.zeros((n1, n2, 6)) normv = np.zeros((6, )) rx = array(range(0, n1)) mat_proj[:, :, 0] = npma.outerproduct(rx + offset, ones(n2)) normv[0] = np.sqrt((mat_proj[:, :, 0]**2).sum()) ry = array(range(0, n2)) mat_proj[:, :, 1] = npma.outerproduct(np.ones(n1), ry + offset) normv[1] = np.sqrt((mat_proj[:, :, 1]**2).sum()) mat_proj[:, :, 2] = np.ones((n1, n2)) normv[2] = np.sqrt((mat_proj[:, :, 2]**2).sum()) mat_proj[:, :, 3] = mat_proj[:, :, 0]**2 + mat_proj[:, :, 1]**2 normv[3] = np.sqrt((mat_proj[:, :, 3]**2).sum()) mat_proj[:, :, 4] = mat_proj[:, :, 0]**2 - mat_proj[:, :, 1]**2 normv[4] = np.sqrt((mat_proj[:, :, 4]**2).sum()) mat_proj[:, :, 5] = mat_proj[:, :, 0] * mat_proj[:, :, 1] normv[5] = np.sqrt((mat_proj[:, :, 5]**2).sum()) return mat_proj, normv
def mk_ellipticity(im,sigw,niter_cent=4,cent_return=True): q = zeros((2,2)) ell = zeros((1,2)) centroid,Wc = compute_centroid(im,sigw,niter_cent) n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0,n1)) xx = npma.outerproduct(rx-centroid[0,0],ones(n2)) ry = array(range(0,n2)) yy = npma.outerproduct(ones(n1),ry-centroid[0,1]) q[0,0]=(im*(xx**2)*Wc).sum() q[1,1]=(im*(yy**2)*Wc).sum() q[0,1]=(im*xx*yy*Wc).sum() q[1,0]=q[0,1] q = q/((im*Wc).sum()) ell[0,0] = (q[0,0]-q[1,1])/(q[0,0]+q[1,1]) ell[0,1] = 2*q[0,1]/(q[0,0]+q[1,1]) if cent_return==True: return ell,centroid else: return ell
def compute_centroid(im,sigw,nb_iter=4): n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0,n1)) ry = array(range(0,n2)) Wc = ones((n1,n2)) centroid = zeros((1,2)) # Four iteration loop to compute the centroid i=0 for i in range(0,nb_iter): xx = npma.outerproduct(rx-centroid[0,0],ones(n2)) yy = npma.outerproduct(ones(n1),ry-centroid[0,1]) W = npma.exp(-(xx**2+yy**2)/(2*sigw**2)) centroid = zeros((1,2)) # Estimate Centroid Wc = copy(W) if i == 0:Wc = ones((n1,n2)) totx=0.0 toty=0.0 cx=0 cy=0 for cx in range(0,n1): centroid[0,0] += (im[cx,:]*Wc[cx,:]).sum()*(cx) totx += (im[cx,:]*Wc[cx,:]).sum() for cy in range(0,n2): centroid[0,1] += (im[:,cy]*Wc[:,cy]).sum()*(cy) toty += (im[:,cy]*Wc[:,cy]).sum() centroid = centroid*array([1/totx,1/toty]) return (centroid,Wc)
def make_P(ps, A, B, P0): ''' # Author Charles Doutriaux # Version 1.0 # email: [email protected] # Step 1 of conversion of a field from sigma levels to pressure levels # Create the Pressure field on sigma levels, from the surface pressure # Input # Ps : Surface pressure # A,B,Po: Coefficients, such as: p=B.ps+A.Po # Ps is 2D (lonxlat) # B,A are 1D (vertical sigma levels) # Output # Pressure field from TOP (level 0) to BOTTOM (last level) # 3D field (lon/lat/sigma) # External : Numeric # Compute the pressure for the sigma levels''' import numpy.ma as MA p = MA.outerproduct(B, ps) dim = B.shape[0], ps.shape[0], ps.shape[1] p = MA.reshape(p, dim) ## p=ps.filled()[Numeric.NewAxis,...]*B.filled()[:,Numeric.NewAxis,Numeric.NewAxis] ## Po=P0*MA.ones(p.shape,Numeric.Float) A = MA.outerproduct(A, P0 * MA.ones(p.shape[1:])) A = MA.reshape(A, p.shape) p = p + A # Now checking to make sure we return P[0] as the top a = MA.average(MA.average(p[0] - p[-1], axis=0)) if a > 0: # We got the wrong order ! p = p[::-1] return p
def make_P(ps,A,B,P0): ''' # Author Charles Doutriaux # Version 1.0 # email: [email protected] # Step 1 of conversion of a field from sigma levels to pressure levels # Create the Pressure field on sigma levels, from the surface pressure # Input # Ps : Surface pressure # A,B,Po: Coefficients, such as: p=B.ps+A.Po # Ps is 2D (lonxlat) # B,A are 1D (vertical sigma levels) # Output # Pressure field from TOP (level 0) to BOTTOM (last level) # 3D field (lon/lat/sigma) # External : Numeric # Compute the pressure for the sigma levels''' import numpy.ma as MA p=MA.outerproduct(B,ps) dim=B.shape[0],ps.shape[0],ps.shape[1] p=MA.reshape(p,dim) ## p=ps.filled()[Numeric.NewAxis,...]*B.filled()[:,Numeric.NewAxis,Numeric.NewAxis] ## Po=P0*MA.ones(p.shape,Numeric.Float) A=MA.outerproduct(A,P0*MA.ones(p.shape[1:])) A=MA.reshape(A,p.shape) p=p+A # Now checking to make sure we return P[0] as the top a=MA.average(MA.average(p[0]-p[-1], axis=0)) if a>0: # We got the wrong order ! p=p[::-1] return p