コード例 #1
0
def create_image_and_mask(imagefilename, maskfilename):

    # import a clean input to be corrupted with the mask
    input = mpimg.imread(imagefilename)

    if input.ndim == 3:
        M, N, C = input.shape
    else:
        M, N = input.shape
        C = 1

    # import the mask of the inpainting domain
    # mask = 1 intact part
    # mask = 0 missing domain
    mask = scipy.float64((mpimg.imread(maskfilename) == 1))

    if (input.ndim == 3) & (mask.ndim < 3):
        mask = np.repeat(mask[:, :, np.newaxis], C, axis=2)

    if C == 1:
        input = scipy.expand_dims(input, axis=2)
        mask = scipy.expand_dims(mask, axis=2)

    # create the image with the missin domain:
    noise = scipy.rand(M, N, C)
    u = mask * input + (1 - mask) * noise

    return (u, mask)
コード例 #2
0
ファイル: mixture_model.py プロジェクト: Yevgnen/prml
    def fit(self, X):
        n_samples, n_features = X.shape
        n_classes = self.n_classes
        max_iter = self.max_iter
        tol = self.tol

        rand_center_idx = sprand.permutation(n_samples)[0:n_classes]
        center = X[rand_center_idx].T
        responsilibity = sp.zeros((n_samples, n_classes))

        for iter in range(max_iter):
            # E step
            dist = sp.expand_dims(X, axis=2) - sp.expand_dims(center, axis=0)
            dist = spla.norm(dist, axis=1)**2
            min_idx = sp.argmin(dist, axis=1)
            responsilibity.fill(0)
            responsilibity[sp.arange(n_samples), min_idx] = 1

            # M step
            center_new = sp.dot(X.T, responsilibity) / sp.sum(responsilibity, axis=0)
            diff = center_new - center
            print('K-Means: {0:5d} {1:4e}'.format(iter, spla.norm(diff) / spla.norm(center)))
            if (spla.norm(diff) < tol * spla.norm(center)):
                break

            center = center_new

        self.center = center.T
        self.responsibility = responsilibity

        return self
コード例 #3
0
def update():
    global i
    if i == tvec.shape[0]-1:
        i = 0
    else:
        i = i + 1
    
    if show_left:
        poi_left_scatter.setData(pos=sp.expand_dims(poi_left_pos[i],0))
        hand_left_scatter.setData(pos=sp.expand_dims(hand_left_pos[i],0))
        string_left_line.setData(pos=sp.vstack((hand_left_pos[i],poi_left_pos[i])))
#        arm_left.setData(pos=sp.vstack((hand_left_pos[i],[0,-1*shoulder_width/2,0])))
        arm_left.setData(pos=sp.vstack((hand_left_pos[i],[0,0,offset])))
    else:
        poi_left_scatter.hide()
        poi_left_line.hide()
        hand_left_scatter.hide()
        hand_left_line.hide()
        string_left_line.hide()
        arm_left.hide()
    
    if show_right:
        poi_right_scatter.setData(pos=sp.expand_dims(poi_right_pos[i],0))
        hand_right_scatter.setData(pos=sp.expand_dims(hand_right_pos[i],0))
        string_right_line.setData(pos=sp.vstack((hand_right_pos[i],poi_right_pos[i])))
#        arm_right.setData(pos=sp.vstack((hand_right_pos[i],[0,shoulder_width/2,0])))
        arm_right.setData(pos=sp.vstack((hand_right_pos[i],[0,0,offset])))
    else:
        poi_right_scatter.hide()
        poi_right_line.hide()
        hand_right_scatter.hide()
        hand_right_line.hide()
        string_right_line.hide()
        arm_right.hide()
コード例 #4
0
def ZYFF(Te, EIJ):
    """Computes `ZY` and `FF`, used in other functions.
    
    If `EIJ` is a scalar, the output has the same shape as `Te`. If `EIJ` is an
    array, the output has shape `EIJ.shape` + `Te.shape`. This should keep the
    output broadcastable with `Te`.
    
    Parameters
    ----------
    Te : array of float
        Electron temperature. Shape is arbitrary.
    EIJ : scalar float or array of float
        Energy difference.
    """
    # Expand the dimensions of EIJ to produce the desired output shape:
    Te = scipy.asarray(Te, dtype=float)
    EIJ = scipy.asarray(EIJ, dtype=float)
    for n in xrange(Te.ndim):
        EIJ = scipy.expand_dims(EIJ, axis=-1)
    
    ZY = EIJ / (1e3 * Te)
    
    FF = scipy.zeros_like(ZY)
    mask = (ZY >= 1.5)
    FF[mask] = scipy.log((ZY[mask] + 1) / ZY[mask]) - (0.36 + 0.03 * scipy.sqrt(ZY[mask] + 0.01)) / (ZY[mask] + 1)**2
    mask = ~mask
    FF[mask] = scipy.log((ZY[mask] + 1) / ZY[mask]) - (0.36 + 0.03 / scipy.sqrt(ZY[mask] + 0.01)) / (ZY[mask] + 1)**2
    
    return ZY, FF
コード例 #5
0
def plot_dist_grid(length=20,start=[(0,0)],n=1,metric='euclidean',interp=None,cmap=None,cbar=False,title='Distance to Closest Point',xlabel="X Coord",ylabel="Y Coord"): 

	# Create array of x and y coordinates
	x_array = sp.zeros((length,length)) + sp.arange(length)
	y_array = sp.zeros((length,length)) + sp.expand_dims(sp.arange(length),length)
	coords = zip(x_array.ravel(),y_array.ravel())

	# Iterate over coords to calculate distance from 'start'
	minima = []
	for i in range(len(start)):
		val = distance.cdist([start[i]], coords, metric).reshape(length,length)
		if i == 0:
			minima = sp.copy(val) # Assume all are minimums
		else: 
			minima = sp.minimum(minima,val) # Take smaller

	# Create plot from 'minima' array
	fig, ax = plt.subplots()
	cax = ax.imshow(minima,interpolation=interp,cmap=cmap)
	if cbar: 
		cbar = fig.colorbar(cax, ticks=[range(int(sp.amax(minima)))])
	ax.set_title(title)
	ax.set_xlabel(xlabel)
	ax.set_ylabel(ylabel)
	fig.savefig('figure' + str(n) + '.pdf')
コード例 #6
0
 def wrapper(self, *args, **kwargs):
     kernel = self.kernelcorrmu if kwargs.get('corrmu',
                                              False) else self.kernel
     integrand = scipy.expand_dims(func(self, self.kk, self.mumu, *args,
                                        **kwargs),
                                   axis=-3)
     return integrate.trapz(integrand * kernel, x=self.mu, axis=-1)
コード例 #7
0
def harmonic_inpainting(
        damaged_file, mask_file, output_file, fidelity, tol, maxiter,
        dt):  #damaged_file,mask_file,output_file,lambda,tol,maxiter,dt
    input = mpimg.imread(damaged_file)

    if input.ndim == 3:
        M, N, C = input.shape
    else:
        M, N = input.shape
        C = 1

    mask = scipy.float64((mpimg.imread(mask_file) == 1))
    if (input.ndim == 3) & (mask.ndim < 3):
        mask = np.repeat(mask[:, :, np.newaxis], C, axis=2)

    if C == 1:
        input = scipy.expand_dims(input, axis=2)
        mask = scipy.expand_dims(mask, axis=2)

    u = mask * input

    # u = input.copy()

    for c in range(0, C):

        for iter in range(0, maxiter):

            # COMPUTE NEW SOLUTION
            laplacian = cv.Laplacian(u[:, :, c], cv.CV_64F)
            unew = u[:, :, c] + dt * (laplacian + fidelity * mask[:, :, c] *
                                      (input[:, :, c] - u[:, :, c]))

            # exit condition
            diff_u = np.linalg.norm(
                unew.reshape(M * N, 1) - u[:, :, c].reshape(M * N, 1),
                2) / np.linalg.norm(unew.reshape(M * N, 1), 2)

            # update
            u[:, :, c] = unew

            # test exit condition
            if diff_u < tol:
                break

    mpimg.imsave(output_file, u)

    return u
コード例 #8
0
    def predict(self, hyperparams, Xstar_r, compute_cov = False, debugging = False):
        """
        predict on Xstar
        """
        self._update_inputs(hyperparams)
        KV = self.get_covariances(hyperparams,debugging=debugging)
        
        self.covar_r.Xcross = Xstar_r
        
        Kstar_r = self.covar_r.Kcross(hyperparams['covar_r'])
        Kstar_c = self.covar_c.K(hyperparams['covar_c'])

        KinvY = SP.dot(KV['U_r'],SP.dot(KV['Ytilde'],KV['U_c'].T))
        Ystar = SP.dot(Kstar_r.T,SP.dot(KinvY,Kstar_c))
        Ystar = unravel(Ystar,self.covar_r.n_cross,self.t)

        if debugging:
            Kstar = SP.kron(Kstar_c,Kstar_r)
            Ynaive = SP.dot(Kstar.T,KV['alpha'])
            Ynaive = unravel(Ynaive,self.covar_r.n_cross,self.t)
            assert SP.allclose(Ystar,Ynaive), 'ouch, prediction does not work out'
        
        Ystar_covar = []
        if compute_cov:
            
            CU = fast_dot(Kstar_c, KV['U_c'])     
            s_rev = 1./KV['S']
            Ystar_covar = SP.zeros([Xstar_r.shape[0], self.Y.shape[1]])
            printProgressBar(0, Xstar_r.shape[0], prefix = 'Computing perdiction varaince:', suffix = 'Complete', length = 20)
            for i in range(Xstar_r.shape[0]):
                R_star_star = self.covar_r.K(hyperparams['covar_r'], SP.expand_dims(Xstar_r[i,:],axis=0))
                self.covar_r.Xcross = SP.expand_dims(Xstar_r[i,:],axis=0)
                R_tr_star = self.covar_r.Kcross(hyperparams['covar_r'])
                RU = SP.dot(R_tr_star.T, KV['U_r'])
                q = SP.kron(SP.diag(Kstar_c), R_star_star)
                t = SP.zeros([self.t])                
                for j in range(self.t):
                    temp = SP.kron(CU[j,:], RU) 
                    t[j,] = SP.sum((s_rev * temp).T * temp.T, axis = 0)
                Ystar_covar[i,:] = q - t
                if (i + 1) % (Xstar_r.shape[0]/10) == 0:
                    printProgressBar(i+1, Xstar_r.shape[0], prefix = 'Computing perdiction varaince:', suffix = 'Complete', length = 20)
            self.covar_r.Xcross = Xstar_r
            
        return Ystar, Ystar_covar
コード例 #9
0
ファイル: utils.py プロジェクト: maxlem/AVRCpp
def concatenate(misalignmentsAndScales, biases):

    if sp.ndim(biases) > 1:
        biases = sp.reshape(biases, 3)

    modelParametersMatrix = sp.concatenate(
        (misalignmentsAndScales, sp.expand_dims(biases, axis=1)), axis=1)

    return sp.reshape(modelParametersMatrix, 12)
コード例 #10
0
    def sgrad(self, x, ndata=None):
        """Returns a stochastic gradient at x.
		Projects the gradient in a uniformly random direction."""
        ### Pick a random direction, then calculate (U.T-dot-gradfx)*u
        ### to project grad vector in random direction.
        # if ndata == None: ndata = 1
        if x.ndim == 1:
            x = x.reshape(1, x.size)
        grad = self.grad(x)
        u = scipy.randn(*x.shape)
        # u = u / scipy.sqrt( (u*u).sum(axis=1) )
        # u = u / scipy.linalg.norm(u,2,axis=1)
        u = u / scipy.expand_dims(scipy.linalg.norm(u, 2, axis=1), 1)
        gradfx = u * scipy.sum((self.grad(x) * u).sum(axis=1))
        return bound(gradfx)
コード例 #11
0
 def spectrum_multipoles(self, *args, **kwargs):
     qpar, qper = kwargs.get('qpar', 1.), kwargs.get('qper', 1.)
     kwargs['kobs'] = self.kk
     jacob, kap, muap = self.__class__.k_mu_ap(self.kk, self.mumu, qpar,
                                               qper)
     kernel = self.kernelcorrmu if kwargs.get('corrmu',
                                              False) else self.kernel
     integral = scipy.expand_dims(
         jacob * self.input_model_ref(kap, muap, *args, **kwargs), axis=-3)
     multi = integrate.trapz(integral * kernel, x=self.mu, axis=-1)
     if self.input_model_lin is not None:
         integral = jacob * self.input_model_lin(kap, *args, **kwargs)
         modellin = integrate.trapz(integral * kernel[0],
                                    x=self.mu,
                                    axis=-1)
         return multi, modellin
     return multi, None
コード例 #12
0
def readmattsdata(filename,datadir,outdir,keepspec=[0,1,2,6],angle=20.5):
    d2r=sp.pi/180.
    angr=d2r*angle
    lsp=7
    # Read in Data
    
    inst = sio.loadmat(os.path.join(datadir,filename))
    xg=inst['xg'][0,0]
    x1v = xg['xp']# added to avoid gratting lobes.
    x3v = xg['zp']
    
    [x1mat,x3mat] = sp.meshgrid(x1v,x3v);
    

    
    E = x1mat*sp.sin(angr)#x
    N = x1mat*sp.cos(angr)#y
    U = x3mat
    lxs=x3mat.size
    
    Time_Vector = sp.column_stack([inst['t'],inst['t']+15])
    ns =inst['ns']
    print('Loaded densities...');
    
    ns= sp.reshape(ns,[lxs,lsp])
    Ts =inst['Ts']
    print('Loaded temperatures...')
    
    Ts=sp.reshape(Ts,[lxs,lsp])
    vs = inst['vsx1']
    print('Loaded parallel velocities...\n');
    
    # derive velocity from ExB
    Ez,Ex=sp.gradient(-1*inst['Phi'])
    dx=sp.diff(xg['x'].flatten())[0]
    dEdx=Ex/dx
    vx1=-1*dEdx/xg['Bmag']
    # from looking at the data it seems that the velocity is off by a factor of
    # 10.
    vx1=vx1.flatten()/10.
    vs=sp.reshape(vs,[lxs,lsp])
    vs=sp.sum(ns[:,:(lsp-1)]*vs[:,:(lsp-1)],1)/ns[:,lsp-1]
    v_e= vx1*sp.sin(angr)
    v_n = vx1*sp.cos(angr)
    v_u = vs
    #change units of velocity to km/s
    Velocity = sp.reshape(sp.column_stack([v_e,v_n,v_u]),[lxs,1,3])
    # reduce the number of spcecies
#    if islogical(keepspec)
#        keepspec(end)=true;
#        keepspecnum = sum(keepspec);
#    elseif ~any(keepspec==numspec)
#        keepspec = [keepspec(:),numspec];
#        keepspecnum = length(keepspec);
#    else
#        keepspecnum = length(keepspec);
#    end
    nsout = sp.reshape(ns[:,keepspec],[lxs,1,len(keepspec)])
    Tsout = sp.reshape(Ts[:,keepspec],[lxs,1,len(keepspec)])
    
    
    
    # Put everything in to ionocontainer structure
    Cart_Coords = sp.column_stack([E.flatten(),N.flatten(),U.flatten()])*1e-3

    Param_List = sp.concatenate((sp.expand_dims(nsout,nsout.ndim),sp.expand_dims(Tsout,Tsout.ndim)),-1);
    Species = sp.array(['O+','NO+','N2+','O2+','N+', 'H+','e-'])
    Species = Species[keepspec]
    fpart=os.path.splitext(filename)[0]
    fout=os.path.join(outdir,fpart+'.h5')
    ionoout=IonoContainer(Cart_Coords,Param_List,Time_Vector,ver=0,species=Species,velocity=Velocity)
    
    ionoout.saveh5(fout)
コード例 #13
0
ファイル: gene.py プロジェクト: EricDeveaud/spladder
 def add_exon(self, exon, idx):
     if idx > (len(self.exons) - 1): 
         self.exons.append(sp.zeros((0, 2), dtype='int'))
     self.exons[idx] = sp.r_[self.exons[idx], sp.expand_dims(exon, axis=0)]
コード例 #14
0
ファイル: gp_sMTGPTR.py プロジェクト: smkia/MTNorm
    def predict(self, hyperparams, Xstar_r, compute_cov=False):
        """
        predicting
        """
        KV = self.get_covariances(hyperparams)

        self.covar_r[0].Xcross = Xstar_r
        Kstar_r = self.covar_r[0].Kcross(hyperparams['covar_r'][0])
        USUr = SP.dot(
            SP.sqrt(1. / KV['S_o'][0]) * KV['U_o'][0], KV['Utilde_r'][0])
        S = KV['Stilde_rc']
        RUSUrYhat = SP.dot(
            Kstar_r.T,
            SP.dot(
                USUr,
                unravel(
                    ravel(KV['UYtildeU_rc']) * 1. / S, self.n,
                    SP.prod(self.nbn))))
        usuc = list()
        for i in range(self.out_dims):
            usuc.append(
                SP.dot(
                    self.basis[i],
                    SP.dot(
                        KV['K_c'][i],
                        SP.dot(
                            self.basis[i].T,
                            SP.dot(self.nbasis[i],
                                   SP.dot(KV['USi_s'][i].T,
                                          KV['Utilde_c'][i]))))).T)
        if SP.prod(self.t) <= 10000:
            USUC = reduce(SP.kron, usuc[::-1])
            Ystar = SP.dot(RUSUrYhat, USUC)
        else:
            Ystar = SP.zeros([Xstar_r.shape[0], SP.prod(self.t)])
            if self.out_dims == 1:
                for j in range(self.t[0]):
                    USUC = usuc[0][:, j]
                    Ystar[:, j] = SP.dot(RUSUrYhat, USUC)
            elif self.out_dims == 2:
                for j in range(self.t[0]):
                    for k in range(self.t[1]):
                        USUC = reduce(SP.kron, [usuc[1][:, k], usuc[0][:, j]])
                        Ystar[:, k + j * self.t[1]] = SP.dot(RUSUrYhat, USUC)
            elif self.out_dims == 3:
                for j in range(self.t[0]):
                    for k in range(self.t[1]):
                        for l in range(self.t[2]):
                            USUC = reduce(
                                SP.kron,
                                [usuc[2][:, l], usuc[1][:, k], usuc[0][:, j]])
                            Ystar[:, l + k * self.t[2] + j *
                                  (self.t[1] * self.t[2])] = SP.dot(
                                      RUSUrYhat, USUC)
        Ystar_covar = []
        if compute_cov:
            if self.nt < 10000:
                B = reduce(SP.kron, self.basis[::-1])
                C = SP.dot(B, SP.dot(reduce(SP.kron, KV['K_c'][::-1]), B.T))
                USUC = reduce(SP.kron, usuc[::-1])
                R_star_star = self.covar_r[0].K(hyperparams['covar_r'][0],
                                                Xstar_r)
                temp = SP.kron(USUC.T, SP.dot(Kstar_r.T, USUr))
                Ystar_covar = SP.kron(SP.diag(C),
                                      SP.diag(R_star_star)) - SP.sum(
                                          (1. / S * temp).T * temp.T, axis=0)
                Ystar_covar = unravel(Ystar_covar, Xstar_r.shape[0],
                                      SP.prod(self.t))
            elif SP.prod(self.t) < 10000:
                Ystar_covar = SP.zeros([Xstar_r.shape[0], SP.prod(self.t)])
                B = reduce(SP.kron, self.basis[::-1])
                C = SP.dot(B, SP.dot(reduce(SP.kron, KV['K_c'][::-1]), B.T))
                USUC = reduce(SP.kron, usuc[::-1])
                printProgressBar(0,
                                 Xstar_r.shape[0],
                                 prefix='Computing perdiction varaince:',
                                 suffix='Complete',
                                 length=20)
                for i in range(Xstar_r.shape[0]):
                    R_star_star = self.covar_r[0].K(
                        hyperparams['covar_r'][0],
                        SP.expand_dims(Xstar_r[i, :], axis=0))
                    self.covar_r[0].Xcross = SP.expand_dims(Xstar_r[i, :],
                                                            axis=0)
                    R_tr_star = self.covar_r[0].Kcross(
                        hyperparams['covar_r'][0])
                    r = SP.dot(R_tr_star.T, USUr)
                    q = SP.diag(SP.kron(C, R_star_star))
                    t = SP.zeros([SP.prod(self.t)])
                    for j in range(SP.prod(self.t)):
                        temp = SP.kron(USUC[:, j], r)
                        t[j, ] = SP.sum((1. / S * temp).T * temp.T, axis=0)
                    Ystar_covar[i, :] = q - t
                    if (i + 1) % 50 == 0:
                        printProgressBar(
                            i + 1,
                            Xstar_r.shape[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)
                self.covar_r[0].Xcross = Xstar_r
            elif Xstar_r.shape[0] < 2000:
                Ystar_covar = SP.zeros([Xstar_r.shape[0], SP.prod(self.t)])
                c_diag = list()
                for j in range(self.out_dims):
                    temp = SP.dot(self.basis[j],
                                  SP.dot(KV['K_c'][j], self.basis[j].T))
                    c_diag.append(SP.diag(temp))
                C = reduce(SP.kron, c_diag[::-1])
                R_star_star = self.covar_r[0].K(hyperparams['covar_r'][0],
                                                Xstar_r)
                R_tr_star = self.covar_r[0].Kcross(hyperparams['covar_r'][0])
                r = SP.dot(R_tr_star.T, USUr)
                #q = SP.reshape(SP.kron(SP.diag(R_star_star),C),[Ystar_covar.shape[0],Ystar_covar.shape[1]])
                q = unravel(SP.kron(C, SP.diag(R_star_star)),
                            Ystar_covar.shape[0], Ystar_covar.shape[1])
                t = SP.zeros(Ystar_covar.shape)
                printProgressBar(0,
                                 Xstar_r.shape[0],
                                 prefix='Computing perdiction varaince:',
                                 suffix='Complete',
                                 length=20)
                if self.out_dims == 1:
                    for j in range(self.t[0]):
                        USUC = usuc[0][:, j]
                        temp = SP.kron(USUC, r)
                        t[j, ] = SP.sum((1. / S * temp).T * temp.T, axis=0)
                        printProgressBar(
                            j + 1,
                            self.t[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)
                elif self.out_dims == 2:
                    for j in range(self.t[0]):
                        for k in range(self.t[1]):
                            USUC = reduce(SP.kron,
                                          [usuc[1][:, k], usuc[0][:, j]])
                            temp = SP.kron(USUC, r)
                            t[k + (j * self.t[1]), ] = SP.sum(
                                (1. / S * temp).T * temp.T, axis=0)
                        printProgressBar(
                            j + 1,
                            self.t[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)
                elif self.out_dims == 3:
                    for j in range(self.t[0]):
                        for k in range(self.t[1]):
                            for l in range(self.t[2]):
                                USUC = reduce(SP.kron, [
                                    usuc[2][:, l], usuc[1][:, k], usuc[0][:, j]
                                ])
                                temp = SP.kron(USUC, r)
                                t[:, l + k * self.t[2] +
                                  j * self.t[1] * self.t[2], ] = SP.sum(
                                      (1. / S * temp).T * temp.T, axis=0)
                        if (j + 1) % 5 == 0:
                            printProgressBar(
                                j + 1,
                                self.t[0],
                                prefix='Computing perdiction varaince:',
                                suffix='Complete',
                                length=20)
                Ystar_covar = q - t
            else:
                Ystar_covar = SP.zeros([Xstar_r.shape[0], SP.prod(self.t)])
                c_diag = list()
                for j in range(self.out_dims):
                    temp = SP.dot(self.basis[j],
                                  SP.dot(KV['K_c'][j], self.basis[j].T))
                    c_diag.append(SP.diag(temp))
                C = reduce(SP.kron, c_diag[::-1])
                printProgressBar(0,
                                 Xstar_r.shape[0],
                                 prefix='Computing perdiction varaince:',
                                 suffix='Complete',
                                 length=20)
                for i in range(Xstar_r.shape[0]):
                    R_star_star = self.covar_r[0].K(
                        hyperparams['covar_r'][0],
                        SP.expand_dims(Xstar_r[i, :], axis=0))
                    self.covar_r[0].Xcross = SP.expand_dims(Xstar_r[i, :],
                                                            axis=0)
                    R_tr_star = self.covar_r[0].Kcross(
                        hyperparams['covar_r'][0])
                    r = SP.dot(R_tr_star.T, USUr)
                    q = C * R_star_star
                    t = SP.zeros([SP.prod(self.t)])
                    if self.out_dims == 1:
                        for j in range(self.t[0]):
                            USUC = usuc[0][:, j]
                            temp = SP.kron(USUC, r)
                            t[j, ] = SP.sum((1. / S * temp).T * temp.T, axis=0)
                    elif self.out_dims == 2:
                        for j in range(self.t[0]):
                            for k in range(self.t[1]):
                                USUC = reduce(SP.kron,
                                              [usuc[1][:, k], usuc[0][:, j]])
                                temp = SP.kron(USUC, r)
                                t[k + (j * self.t[1]), ] = SP.sum(
                                    (1. / S * temp).T * temp.T, axis=0)
                    elif self.out_dims == 3:
                        for j in range(self.t[0]):
                            for k in range(self.t[1]):
                                for l in range(self.t[2]):
                                    USUC = reduce(SP.kron, [
                                        usuc[2][:, l], usuc[1][:, k],
                                        usuc[0][:, j]
                                    ])
                                    temp = SP.kron(USUC, r)
                                    t[l + k * self.t[2] +
                                      j * self.t[1] * self.t[2], ] = SP.sum(
                                          (1. / S * temp).T * temp.T, axis=0)
                    Ystar_covar[i, :] = q - t
                    if (i + 1) % 10 == 0:
                        printProgressBar(
                            i + 1,
                            Xstar_r.shape[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)
                self.covar_r[0].Xcross = Xstar_r

        return Ystar, Ystar_covar
コード例 #15
0
 def add_exon(self, exon, idx):
     if idx > (len(self.exons) - 1):
         self.exons.append(sp.zeros((0, 2), dtype='int'))
     self.exons[idx] = sp.r_[self.exons[idx], sp.expand_dims(exon, axis=0)]
コード例 #16
0
ファイル: __funcs__.py プロジェクト: mstiegl/porespy
def snow_partitioning(im,
                      dt=None,
                      r_max=4,
                      sigma=0.4,
                      return_all=False,
                      mask=True,
                      randomize=True):
    r"""
    Partitions the void space into pore regions using a marker-based watershed
    algorithm, with specially filtered peaks as markers.

    The SNOW network extraction algorithm (Sub-Network of an Over-segmented
    Watershed) was designed to handle to perculiarities of high porosity
    materials, but it applies well to other materials as well.

    Parameters
    ----------
    im : array_like
        A boolean image of the domain, with ``True`` indicating the pore space
        and ``False`` elsewhere.
    dt : array_like, optional
        The distance transform of the pore space.  This is done automatically
        if not provided, but if the distance transform has already been
        computed then supplying it can save some time.
    r_max : int
        The radius of the spherical structuring element to use in the Maximum
        filter stage that is used to find peaks.  The default is 4
    sigma : float
        The standard deviation of the Gaussian filter used in step 1.  The
        default is 0.4.  If 0 is given then the filter is not applied, which is
        useful if a distance transform is supplied as the ``im`` argument that
        has already been processed.
    return_all : boolean
        If set to ``True`` a named tuple is returned containing the original
        image, the distance transform, the filtered peaks, and the final
        pore regions.  The default is ``False``
    mask : boolean
        Apply a mask to the regions where the solid phase is.  Default is
        ``True``
    randomize : boolean
        If ``True`` (default), then the region colors will be randomized before
        returning.  This is helpful for visualizing otherwise neighboring
        regions have simlar coloring are are hard to distinguish.

    Returns
    -------
    image : ND-array
        An image the same shape as ``im`` with the void space partitioned into
        pores using a marker based watershed with the peaks found by the
        SNOW algorithm [1].

    Notes
    -----
    If ``return_all`` is ``True`` then a **named tuple** is returned containing
    all of the images used during the process.  They can be access as
    attriutes with the following names:

        * ``im``: The binary image of the void space
        * ``dt``: The distance transform of the image
        * ``peaks``: The peaks of the distance transform after applying the
        steps of the SNOW algorithm
        * ``regions``: The void space partitioned into pores using a marker
        based watershed with the peaks found by the SNOW algorithm

    References
    ----------
    [1] Gostick, J. "A versatile and efficient network extraction algorithm
    using marker-based watershed segmenation".  Physical Review E. (2017)

    """
    tup = namedtuple('results', field_names=['im', 'dt', 'peaks', 'regions'])
    print('_' * 60)
    print("Beginning SNOW Algorithm")
    im_shape = sp.array(im.shape)
    if im.dtype is not bool:
        print('Converting supplied image (im) to boolean')
        im = im > 0
    if dt is None:
        print('Peforming Distance Transform')
        if sp.any(im_shape == 1):
            ax = sp.where(im_shape == 1)[0][0]
            dt = spim.distance_transform_edt(input=im.squeeze())
            dt = sp.expand_dims(dt, ax)
        else:
            dt = spim.distance_transform_edt(input=im)

    tup.im = im
    tup.dt = dt

    if sigma > 0:
        print('Applying Gaussian blur with sigma =', str(sigma))
        dt = spim.gaussian_filter(input=dt, sigma=sigma)

    peaks = find_peaks(dt=dt, r_max=r_max)
    print('Initial number of peaks: ', spim.label(peaks)[1])
    peaks = trim_saddle_points(peaks=peaks, dt=dt, max_iters=500)
    print('Peaks after trimming saddle points: ', spim.label(peaks)[1])
    peaks = trim_nearby_peaks(peaks=peaks, dt=dt)
    peaks, N = spim.label(peaks)
    print('Peaks after trimming nearby peaks: ', N)
    tup.peaks = peaks
    if mask:
        mask_solid = im > 0
    else:
        mask_solid = None
    regions = watershed(image=-dt, markers=peaks, mask=mask_solid)
    if randomize:
        regions = randomize_colors(regions)
    if return_all:
        tup.regions = regions
        return tup
    else:
        return regions
コード例 #17
0
ファイル: aum_main.py プロジェクト: ajoshiusc/eeg-meg
Q = Q[:, 0]
br_face_area = face_areas(brain)
Q = 1.0 + sp.zeros(brain.vertices.shape[0])
# Q=0.0*Q
# Q[1800]=1
# Q=smooth_surf_function(brain,Q,10,10)
area_v = (1.0 / 3.0) * Tri * br_face_area
v = sp.zeros(skull.vertices.shape[0])  # eq 8 from Sarvas
v_aaj = sp.zeros(skull.vertices.shape[0])  # joshi
view_patch(brain, attrib=Q, opacity=1)
for i in range(skull.vertices.shape[0]):
    r0 = brain.vertices
    r_r0 = skull.vertices[i, ] - r0
    norm_r_r0 = norm2(r_r0, ord=2, axis=1)
    den = norm_r_r0**3.0
    num = r_r0
    v1 = num / sp.expand_dims(den, axis=1)
    Qvec = sp.expand_dims(Q, axis=1) * brain.normals
    v1_aaj = (2.0 * m) / norm_r_r0
    v1 = sp.sum(v1 * Qvec, axis=1)
    #    v1_aaj=v1_aaj*sp.sign(v1)
    v[i] = sp.sum(v1 * area_v)
    v_aaj[i] = sp.sum(Q * v1_aaj * area_v)
    if sp.mod(i, 100) == 0:
        print 'i=' + str(i)

# In[15]:

view_patch(skull, attrib=v, show=1)
#view_patch(skull, attrib=v_aaj,show=1)
コード例 #18
0
 def isInSpace(self, p):
     """Return true if the point p is in the affine space, false otherwise."""
     p = scipy.array(p)
     close = scipy.isclose(self.getProjection(p),  p)
     inSpace = scipy.all(close, axis=1)
     return scipy.expand_dims(inSpace, 1)
コード例 #19
0
ファイル: sigp.py プロジェクト: Clark333/vibrationtesting
def crsd(x, y, t, windowname = "hanning", ave = bool(True)):
    """
    Calculate the cross spectrum (power spectrum) density between two signals.

    :Example:

    >>> from scipy import signal
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> import vibrationtesting as vt
    >>> from numpy import linalg

    Generate a 5 second test signal, a 10 V sine wave at 50 Hz, corrupted by
    0.001 V**2/Hz of white noise sampled at 1 kHz.

    >>> sample_freq = 1e3
    >>> tfinal = 5
    >>> sig_freq=50
    >>> A=10
    >>> noise_power = 0.0001 * sample_freq / 2
    >>> noise_power = A/1e12
    >>> time = np.arange(0,tfinal,1/sample_freq)
    >>> time = np.reshape(time, (1, -1))
    >>> x = A*np.sin(2*np.pi*sig_freq*time)
    >>> x = x + np.random.normal(scale=np.sqrt(noise_power), size=(1, time.shape[1]))
    >>> plt.subplot(2,1,1)
    <matplotlib...>
    >>> plt.plot(time[0,:],x[0,:])
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.title('Time history')
    <matplotlib...>
    >>> plt.xlabel('Time (sec)')
    <matplotlib...>
    >>> plt.ylabel('$x(t)$')
    <matplotlib...>

    Compute and plot the autospectrum density.

    >>> freq_vec, Pxx = vt.asd(x, time, window="hanning", ave=bool(False))
    >>> plt.subplot(2,1,2)
    <matplotlib...>
    >>> plt.plot(freq_vec[0,:], 20*np.log10(Pxx[0,:,:]))
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.ylim([-400, 100])
    (-400, 100)
    >>> plt.xlabel('frequency [Hz]')
    <matplotlib.text.Text object at ...>
    >>> plt.ylabel('PSD [V**2/Hz]')
    <matplotlib.text.Text object at ...>
    >>> plt.show()

    If we average the last half of the spectral density, to exclude the
    peak, we can recover the noise power on the signal.


    Now compute and plot the power spectrum.
    """
    #t=np.reshape(t,(1,-1))
    
    if t.shape[0]>1:
        dt=t[2]-t[1]
    elif t.shape[1]>1:
        dt=t[2]-t[1]
        print('t must be a scalar or size (n,1)')
    elif t.shape[1]==1 and t.shape[0]==1:
        dt=t[0]
    if dt <= 0:
        print('You sent in bad data. Delta t is negative. Please check your inputs.')

    if len(x.shape)==1:
        x = sp.expand_dims(x, axis = 0)
        x = sp.expand_dims(x, axis = 2)
        y = sp.expand_dims(y, axis = 0)
        y = sp.expand_dims(y, axis = 2)
    n=x.shape[1];

    # No clue what this does, and I wrote it. Comment your code, you fool!
    # What this "should" do is assure that the data is longer in 0 axis than the others. 
    # if len(x.shape)==2:
    #     # The issue fixed here is that the user put time along the 1 axis (instead of zero)
    #     if (x.shape).index(max(x.shape))==0:
    #         #x=x.reshape(max(x.shape),-1,1)
    #         print('I think you put time along the 0 axis instead of the 1 axis. Not even attempting to fix this.')
    #     else:
    #         # Here we are appending a 3rd dimension to simplify averaging command later. We could bypass at that point, and should. 
    #         x=x.reshape(max(x.shape),-1,1)

    # if len(y.shape)==2:
    #     if (y.shape).indey(may(y.shape))==0:
    #         #y=y.reshape(may(y.shape),-1,1)
    #         print('I think you put time along the 0 axis instead of the 1 axis. Not attempting to fix this.')            
    #     else:
    #         y=y.reshape(may(y.shape),-1,1)
    # # Should use scipy.signal windows. I need to figure this out. Problem is: They don't scale the ASDs by the windowing "weakening". 
    
    if window=="none":
        a=1
    else:
        #print('This doesn\'t work yet')
        win=1
        if window=="hanning":#BLACKWIN, BOXWIN, EXPWIN, HAMMWIN, FLATWIN and TRIWIN
            #print('shape of x')
            #print(x.shape)
            win=window(x, windowname = 'hanning')
        elif window=="blackwin":
            win=window(x, windowname = 'blackwin')
        elif window=="boxwin":
            win=window(x, windowname = 'boxwin')
        elif window=="expwin":
            win=window(x, windowname = 'expwin')
        elif window=="hammwin":
            win=window(x, windowname = 'hamming')
        elif window=="triwin":
            win=window(x, windowname = 'triwin')
        elif window=="flatwin":
            win=window(x, windowname = 'flatwin')
        
        y=y*win
        x=x*win
        del win

    
    ffty=np.fft.rfft(y,axis = 1)*dt

    fftx=np.fft.rfft(x,n,axis = 1)*dt

    Pxy=np.conj(fftx)*ffty/(n*dt)*2

    
    if len(Pxy.shape)==3 and Pxy.shape[2]>1 and ave:
        Pxy=np.mean(Pxy,2)
        
    
    nfreq=1/dt/2;
    f=np.linspace(0, nfreq, Pxy.shape[1])#/2./sp.pi
    
   
    return f, Pxy
コード例 #20
0
def crsd(x, y, t, windowname="hanning", ave=bool(True)):
    """
    Calculate the cross spectrum (power spectrum) density between two signals.

    :Example:

    >>> from scipy import signal
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> import vibrationtesting as vt
    >>> from numpy import linalg

    Generate a 5 second test signal, a 10 V sine wave at 50 Hz, corrupted by
    0.001 V**2/Hz of white noise sampled at 1 kHz.

    >>> sample_freq = 1e3
    >>> tfinal = 5
    >>> sig_freq=50
    >>> A=10
    >>> noise_power = 0.0001 * sample_freq / 2
    >>> noise_power = A/1e12
    >>> time = np.arange(0,tfinal,1/sample_freq)
    >>> time = np.reshape(time, (1, -1))
    >>> x = A*np.sin(2*np.pi*sig_freq*time)
    >>> x = x + np.random.normal(scale=np.sqrt(noise_power), size=(1, time.shape[1]))
    >>> plt.subplot(2,1,1)
    <matplotlib...>
    >>> plt.plot(time[0,:],x[0,:])
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.title('Time history')
    <matplotlib...>
    >>> plt.xlabel('Time (sec)')
    <matplotlib...>
    >>> plt.ylabel('$x(t)$')
    <matplotlib...>

    Compute and plot the autospectrum density.

    >>> freq_vec, Pxx = vt.asd(x, time, window="hanning", ave=bool(False))
    >>> plt.subplot(2,1,2)
    <matplotlib...>
    >>> plt.plot(freq_vec[0,:], 20*np.log10(Pxx[0,:,:]))
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.ylim([-400, 100])
    (-400, 100)
    >>> plt.xlabel('frequency [Hz]')
    <matplotlib.text.Text object at ...>
    >>> plt.ylabel('PSD [V**2/Hz]')
    <matplotlib.text.Text object at ...>
    >>> plt.show()

    If we average the last half of the spectral density, to exclude the
    peak, we can recover the noise power on the signal.


    Now compute and plot the power spectrum.
    """
    #t=np.reshape(t,(1,-1))

    if t.shape[0] > 1:
        dt = t[2] - t[1]
    elif t.shape[1] > 1:
        dt = t[2] - t[1]
        print('t must be a scalar or size (n,1)')
    elif t.shape[1] == 1 and t.shape[0] == 1:
        dt = t[0]
    if dt <= 0:
        print(
            'You sent in bad data. Delta t is negative. Please check your inputs.'
        )

    if len(x.shape) == 1:
        x = sp.expand_dims(x, axis=0)
        x = sp.expand_dims(x, axis=2)
        y = sp.expand_dims(y, axis=0)
        y = sp.expand_dims(y, axis=2)
    n = x.shape[1]

    # No clue what this does, and I wrote it. Comment your code, you fool!
    # What this "should" do is assure that the data is longer in 0 axis than the others.
    # if len(x.shape)==2:
    #     # The issue fixed here is that the user put time along the 1 axis (instead of zero)
    #     if (x.shape).index(max(x.shape))==0:
    #         #x=x.reshape(max(x.shape),-1,1)
    #         print('I think you put time along the 0 axis instead of the 1 axis. Not even attempting to fix this.')
    #     else:
    #         # Here we are appending a 3rd dimension to simplify averaging command later. We could bypass at that point, and should.
    #         x=x.reshape(max(x.shape),-1,1)

    # if len(y.shape)==2:
    #     if (y.shape).indey(may(y.shape))==0:
    #         #y=y.reshape(may(y.shape),-1,1)
    #         print('I think you put time along the 0 axis instead of the 1 axis. Not attempting to fix this.')
    #     else:
    #         y=y.reshape(may(y.shape),-1,1)
    # # Should use scipy.signal windows. I need to figure this out. Problem is: They don't scale the ASDs by the windowing "weakening".

    if window == "none":
        a = 1
    else:
        #print('This doesn\'t work yet')
        win = 1
        if window == "hanning":  #BLACKWIN, BOXWIN, EXPWIN, HAMMWIN, FLATWIN and TRIWIN
            #print('shape of x')
            #print(x.shape)
            win = window(x, windowname='hanning')
        elif window == "blackwin":
            win = window(x, windowname='blackwin')
        elif window == "boxwin":
            win = window(x, windowname='boxwin')
        elif window == "expwin":
            win = window(x, windowname='expwin')
        elif window == "hammwin":
            win = window(x, windowname='hamming')
        elif window == "triwin":
            win = window(x, windowname='triwin')
        elif window == "flatwin":
            win = window(x, windowname='flatwin')

        y = y * win
        x = x * win
        del win

    ffty = np.fft.rfft(y, axis=1) * dt

    fftx = np.fft.rfft(x, n, axis=1) * dt

    Pxy = np.conj(fftx) * ffty / (n * dt) * 2

    if len(Pxy.shape) == 3 and Pxy.shape[2] > 1 and ave:
        Pxy = np.mean(Pxy, 2)

    nfreq = 1 / dt / 2
    f = np.linspace(0, nfreq, Pxy.shape[1])  #/2./sp.pi

    return f, Pxy
コード例 #21
0
    def predict(self, hyperparams, Xstar_r, compute_cov=False):
        """
        predict on Xstar
        """
        KV = self.get_covariances(hyperparams)

        self.covar_r.Xcross = Xstar_r

        Kstar_r = self.covar_r.Kcross(hyperparams['covar_r'])
        Kstar_c = self.covar_c.K(hyperparams['covar_c'])
        KinvY = SP.dot(KV['U_r'], SP.dot(KV['Ytilde'], KV['U_c'].T))
        BD = SP.dot(self.basis, Kstar_c)
        Ystar = reduce(SP.dot, [Kstar_r.T, KinvY, BD.T])

        Ystar_covar = []
        if compute_cov:
            BDU = SP.dot(BD, KV['U_c'])
            BDB = (BD * self.basis).sum(-1)
            Ystar_covar = SP.zeros([Xstar_r.shape[0], self.Y.shape[1]])
            s_rev = 1. / KV['S']
            printProgressBar(0,
                             BDU.shape[0],
                             prefix='Computing perdiction varaince:',
                             suffix='Complete',
                             length=20)
            if Xstar_r.shape[0] < 500:
                R_star_star = self.covar_r.K(hyperparams['covar_r'], Xstar_r)
                self.covar_r.Xcross = Xstar_r
                R_tr_star = self.covar_r.Kcross(hyperparams['covar_r'])
                RU = SP.dot(R_tr_star.T, KV['U_r'])
                q = unravel(SP.kron(BDB, SP.diag(R_star_star)),
                            Ystar_covar.shape[0], Ystar_covar.shape[1])
                t = SP.zeros(Ystar_covar.shape)
                for j in range(BDU.shape[0]):
                    temp = SP.kron(BDU[j, :], RU)
                    t[:, j] = SP.sum((s_rev * temp).T * temp.T, axis=0)
                    if (j + 1) % 10000 == 0:
                        printProgressBar(
                            j + 1,
                            BDU.shape[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)

                Ystar_covar = q - t
            else:
                for i in range(Xstar_r.shape[0]):
                    R_star_star = self.covar_r.K(
                        hyperparams['covar_r'],
                        SP.expand_dims(Xstar_r[i, :], axis=0))
                    self.covar_r.Xcross = SP.expand_dims(Xstar_r[i, :], axis=0)
                    R_tr_star = self.covar_r.Kcross(hyperparams['covar_r'])
                    RU = SP.dot(R_tr_star.T, KV['U_r'])
                    q = SP.kron(BDB, R_star_star)
                    t = SP.zeros([self.basis.shape[0]])
                    for j in range(BDU.shape[0]):
                        temp = SP.kron(BDU[j, :], RU)
                        t[j, ] = SP.sum((s_rev * temp).T * temp.T, axis=0)
                    Ystar_covar[i, :] = q - t
                    if (i + 1) % (Xstar_r.shape[0] / 10) == 0:
                        printProgressBar(
                            i + 1,
                            Xstar_r.shape[0],
                            prefix='Computing perdiction varaince:',
                            suffix='Complete',
                            length=20)
                self.covar_r.Xcross = Xstar_r
        return Ystar, Ystar_covar