コード例 #1
0
 def printResults(self,Jt):
     nn = 0 ;
     yt0 = [] ;
     for k in range(self.ncurve):
         if self.dim==2:
             A = self.affB.getTransforms(self.Afft[k])
             (xt,yt) = evol.landmarkDirectEvolutionEuler(self.x0[k], self.at[k], self.param.KparDiff, affine=A, withPointSet=self.gridxy[k])
             yt0.append(yt)
             #print xt.shape, yt.shape
         n1 = self.xt[k].shape[1] ;
         for kk in range(self.Tsize+1):
             self.fvDef[k].updateVertices(np.squeeze(self.xt[-1][kk, nn:nn+n1, :]))
             self.fvDef[k].saveVTK(self.outputDir +'/'+ self.saveFile+str(k)+'Out'+str(kk)+'.vtk', scalars = Jt[-1][kk, nn:nn+n1], scal_name='Jacobian')
             self.fvDef[k].updateVertices(np.squeeze(self.xt[k][kk, :, :]))
             self.fvDef[k].saveVTK(self.outputDir +'/'+self.saveFile+str(k)+'In'+str(kk)+'.vtk', scalars = Jt[k][kk, :], scal_name='Jacobian')
             if self.dim == 2:
                 self.gridDef[k].vertices = np.copy(yt[kk, :, :])
                 self.gridDef[k].saveVTK(self.outputDir +'/grid'+str(k)+'In'+str(kk)+'.vtk')
         nn += n1
         #print xt.shape, yt.shape
         I1 = np.nonzero(self.inGrid[-1])
     if self.dim==2:
         (xt,yt) = evol.landmarkDirectEvolutionEuler(self.x0[self.ncurve], self.at[self.ncurve], self.param.KparDiffOut, withPointSet=self.gridxy[-1])
         yt0.append(yt)
         for kk in range(self.Tsize+1):
             self.gridDef[-1].vertices = np.copy(yt[kk, :, :])
             self.gridDef[-1].saveVTK(self.outputDir +'/gridOut'+str(kk)+'.vtk')
             for k in range(self.ncurve+1):
                 I1 = np.nonzero(self.inGrid[k])
                 self.gridAll.vertices[I1] = np.copy(yt0[k][kk, ...])
             self.gridAll.saveVTK(self.outputDir +'/gridAll'+str(kk)+'.vtk')
コード例 #2
0
 def  objectiveFunDef(self, at, Afft, withTrajectory = False, withJacobian=False, x0 = None):
     if x0 == None:
         x0 = self.fv0.vertices
     param = self.param
     timeStep = 1.0/self.Tsize
     #dim2 = self.dim**2
     A = self.affB.getTransforms(Afft)
     # A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
     # if self.affineDim > 0:
     #     for t in range(self.Tsize):
     #         AB = np.dot(self.affineBasis, Afft[t])
     #         A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
     #         A[1][t] = AB[dim2:dim2+self.dim]
     if withJacobian:
         (xt,Jt)  = evol.landmarkDirectEvolutionEuler(x0, at, param.KparDiff, affine=A, withJacobian=True)
     else:
         xt  = evol.landmarkDirectEvolutionEuler(x0, at, param.KparDiff, affine=A)
     #print xt[-1, :, :]
     #print obj
     obj=0
     for t in range(self.Tsize):
         z = np.squeeze(xt[t, :, :])
         a = np.squeeze(at[t, :, :])
         #rzz = kfun.kernelMatrix(param.KparDiff, z)
         ra = param.KparDiff.applyK(z, a)
         obj = obj + self.regweight*timeStep*np.multiply(a, (ra)).sum()
         if self.affineDim > 0:
             obj +=  timeStep * np.multiply(self.affineWeight.reshape(Afft[t].shape), Afft[t]**2).sum()
         #print xt.sum(), at.sum(), obj
     if withJacobian:
         return obj, xt, Jt
     elif withTrajectory:
         return obj, xt
     else:
         return obj
コード例 #3
0
 def endOptim(self):
     if self.saveRate==0 or self.iter%self.saveRate > 0:
         if self.dim==2:
             A = self.affB.getTransforms(self.Afft)
             (xt,yt) = evol.landmarkDirectEvolutionEuler(self.fv0.vertices, self.at, self.param.KparDiff, affine=A, withPointSet=self.gridxy)
         for kk in range(self.Tsize+1):
             self.fvDef.updateVertices(np.squeeze(self.xt[kk, :, :]))
             #self.fvDef.saveVTK(self.outputDir +'/'+ self.saveFile+str(kk)+'.vtk', scalars = Jt[kk, :], scal_name='Jacobian')
             self.fvDef.saveVTK(self.outputDir +'/'+ self.saveFile+str(kk)+'.vtk')
     self.defCost = self.obj - self.obj0 - self.dataTerm(self.fvDef)   
コード例 #4
0
    def  objectiveFunDef(self, at, Afft, withTrajectory = False, withJacobian = False):
        param = self.param
        timeStep = 1.0/at.shape[0]
        dim2 = self.dim**2
        A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
        if self.affineDim > 0:
            for t in range(self.Tsize):
                AB = np.dot(self.affineBasis, Afft[t]) 
                A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                A[1][t] = AB[dim2:dim2+self.dim]
        if withJacobian:
            xJ  = evol.landmarkDirectEvolutionEuler(self.fv0.vertices, at, param.KparDiff, withJacobian = True, affine=A)
            xt = xJ[0]
            Jt = xJ[1]
        else:
            xt  = evol.landmarkDirectEvolutionEuler(self.fv0.vertices, at, param.KparDiff, affine=A)

        obj=0
        for t in range(at.shape[0]):
            z = np.squeeze(xt[t, :, :]) 
            a = np.squeeze(at[t, :, :]) 
            ra = self.param.KparDiff.applyK(z, a)
            obj += self.regweight*timeStep*np.multiply(a, ra).sum()
            if self.affineDim > 0:
                obj +=  timeStep * np.multiply(self.affineWeight.reshape(Afft[t].shape), Afft[t]**2).sum()

        cstr = [[], []]
        if self.nconstr > 0:
            cstr = self.constraintTerm(xt)
            obj += cstr[0]
        
            #print xt.sum(), at.sum(), obj
        if withJacobian:
            return obj, xt, Jt, cstr[1]
        elif withTrajectory:
            return obj, xt, cstr[1]
        else:
            return obj
コード例 #5
0
    def endOfIteration(self):
        (obj1, self.xt, Jt) = self.objectiveFunDef(self.at, self.Afft, withTrajectory=True, withJacobian=True)
        self.iter += 1

        if self.saveRate > 0 and self.iter%self.saveRate==0:
            if self.dim==2:
                A = self.affB.getTransforms(self.Afft)
                (xt,yt) = evol.landmarkDirectEvolutionEuler(self.fv0.vertices, self.at, self.param.KparDiff, affine=A, withPointSet=self.gridxy)
            for kk in range(self.Tsize+1):
                self.fvDef.updateVertices(np.squeeze(self.xt[kk, :, :]))
                #self.fvDef.saveVTK(self.outputDir +'/'+ self.saveFile+str(kk)+'.vtk', scalars = Jt[kk, :], scal_name='Jacobian')
                self.fvDef.saveVTK(self.outputDir +'/'+ self.saveFile+str(kk)+'.vtk')
                if self.dim == 2:
                    self.gridDef.vertices = np.copy(yt[kk, :, :])
                    self.gridDef.saveVTK(self.outputDir +'/grid'+str(kk)+'.vtk')
        else:
            self.fvDef.updateVertices(np.squeeze(self.xt[self.Tsize, :, :]))
コード例 #6
0
    def covectorEvolution(self, at, Afft, px1):
        N = self.npt
        dim = self.dim
        M = self.Tsize
        timeStep = 1.0/M
        dim2 = self.dim**2

        A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
        if self.affineDim > 0:
            for t in range(self.Tsize):
                AB = np.dot(self.affineBasis, Afft[t]) 
                A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                A[1][t] = AB[dim2:dim2+self.dim]
        xt = evol.landmarkDirectEvolutionEuler(self.x0, at, self.param.KparDiff, affine=A)

        #### Restart here
        pxt = np.zeros([M, N, dim])
        if self.nconstr > 0:
            (lmb, dxcval) = self.constraintTermGrad(xt)
            pxt[M-1, :, :] = px1 + dxcval[M] *timeStep
        else:
            pxt[M-1, :, :] = px1
        # print c
        for t in range(M-1):
            px = np.squeeze(pxt[M-t-1, :, :])
            z = np.squeeze(xt[M-t-1, :, :])
            a = np.squeeze(at[M-t-1, :, :])
            if self.nconstr > 0:
                zpx = np.copy(dxcval[M-t-1])
            else:
                zpx = np.zeros(px1.shape)

            a1 = [px, a, -2*self.regweight*a]
            a2 = [a, px, a]
            zpx += self.param.KparDiff.applyDiffKT(z, a1, a2)
            if self.affineDim > 0:
                zpx += np.dot(px, A[0][M-t-1])
            pxt[M-t-2, :, :] = np.squeeze(pxt[M-t-1, :, :]) + timeStep * zpx

        return pxt, xt
コード例 #7
0
    def covectorEvolution(self, at, Afft, px1):
        M = self.Tsize
        timeStep = 1.0/M
        dim2 = self.dim**2
        A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
        if self.affineDim > 0:
            for t in range(self.Tsize):
                AB = np.dot(self.affineBasis, Afft[t]) 
                A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                A[1][t] = AB[dim2:dim2+self.dim]
        xt = evol.landmarkDirectEvolutionEuler(self.x0, at, self.param.KparDiff, affine = A)
        #xt = xJ
        pxt = np.zeros([M+1, self.npt, self.dim])
        pxt[M, :, :] = px1
        
        foo = self.constraintTermGrad(xt, at, Afft)
        lmb = foo[0]
        dxcval = foo[1]
        dacval = foo[2]
        dAffcval = foo[3]
        
        pxt[M, :, :] += timeStep * dxcval[M]
        
        for t in range(M):
            px = np.squeeze(pxt[M-t, :, :])
            z = np.squeeze(xt[M-t-1, :, :])
            a = np.squeeze(at[M-t-1, :, :])
            zpx = np.copy(dxcval[M-t-1])
            a1 = np.concatenate((px[np.newaxis,...], a[np.newaxis,...], -2*self.regweight*a[np.newaxis,...]))
            a2 = np.concatenate((a[np.newaxis,...], px[np.newaxis,...], a[np.newaxis,...]))
            zpx += self.param.KparDiff.applyDiffKT(z, a1, a2)
            if self.affineDim > 0:
                zpx += np.dot(px, A[0][M-t-1])
            pxt[M-t-1, :, :] = px + timeStep * zpx
            

        return pxt, xt, dacval, dAffcval
コード例 #8
0
    def endOfIteration(self):
        self.iter += 1
        if self.iter >= self.affBurnIn:
            self.coeffAff = self.coeffAff2
        if (self.iter % self.saveRate == 0):
            logging.info('Saving surfaces...')
            (obj1, self.xt) = self.objectiveFunDef(self.at, self.Afft, withTrajectory=True)
            for k in range(self.nTarg):
                self.fvDef[k].updateVertices(np.squeeze(self.xt[(k+1)*self.Tsize1, :, :]))
            dim2 = self.dim**2
            A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
            if self.affineDim > 0:
                for t in range(self.Tsize):
                    AB = np.dot(self.affineBasis, self.Afft[t])
                    A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                    A[1][t] = AB[dim2:dim2+self.dim]
                    
            (xt, ft, Jt)  = evol.landmarkDirectEvolutionEuler(self.x0, self.at, self.param.KparDiff, affine=A,
                                                           withPointSet = self.fv0Fine.vertices, withJacobian=True)

            if self.affine=='euclidean' or self.affine=='translation':
                f = surfaces.Surface(surf=self.fv0Fine)
                X = self.affB.integrateFlow(self.Afft)
                displ = np.zeros(self.x0.shape[0])
                dt = 1.0 /self.Tsize ;
                for t in range(self.Tsize+1):
                    U = la.inv(X[0][t])
                    yyt = np.dot(xt[t,...] - X[1][t, ...], U.T)
                    zt = np.dot(ft[t,...] - X[1][t, ...], U.T)
                    if t < self.Tsize:
                        at = np.dot(self.at[t,...], U.T)
                        vt = self.param.KparDiff.applyK(yyt, at, firstVar=zt)
                    f.updateVertices(zt)
                    vf = surfaces.vtkFields() ;
                    vf.scalars.append('Jacobian') ;
                    vf.scalars.append(np.exp(Jt[t, :])-1)
                    vf.scalars.append('displacement')
                    vf.scalars.append(displ)
                    vf.vectors.append('velocity') ;
                    vf.vectors.append(vt)
                    nu = self.fv0ori*f.computeVertexNormals()
                    displ += dt * (vt*nu).sum(axis=1)
                    f.saveVTK2(self.outputDir +'/'+self.saveFile+'Corrected'+str(t)+'.vtk', vf)

                for k,fv in enumerate(self.fv1):
                    f = surfaces.Surface(surf=fv)
                    U = la.inv(X[0][(k+1)*self.Tsize1])
                    yyt = np.dot(f.vertices - X[1][(k+1)*self.Tsize1, ...], U.T)
                    f.updateVertices(yyt)
                    f.saveVTK(self.outputDir +'/Target'+str(k)+'Corrected.vtk')
            
            fvDef = surfaces.Surface(surf=self.fv0Fine)
            AV0 = fvDef.computeVertexArea()
            nu = self.fv0ori*self.fv0Fine.computeVertexNormals()
            #v = self.v[0,...]
            displ = np.zeros(self.npt)
            dt = 1.0 /self.Tsize ;
            v = self.param.KparDiff.applyK(ft[0,...], self.at[0,...], firstVar=self.xt[0,...])
            for kk in range(self.Tsize+1):
                fvDef.updateVertices(np.squeeze(ft[kk, :, :]))
                AV = fvDef.computeVertexArea()
                AV = (AV[0]/AV0[0])-1
                vf = surfaces.vtkFields() ;
                vf.scalars.append('Jacobian') ;
                vf.scalars.append(np.exp(Jt[kk, :])-1)
                vf.scalars.append('Jacobian_T') ;
                vf.scalars.append(AV[:,0])
                vf.scalars.append('Jacobian_N') ;
                vf.scalars.append(np.exp(Jt[kk, :])/(AV[:,0]+1)-1)
                vf.scalars.append('displacement')
                vf.scalars.append(displ)
                displ += dt * (v*nu).sum(axis=1)
                if kk < self.Tsize:
                    nu = self.fv0ori*fvDef.computeVertexNormals()
                    v = self.param.KparDiff.applyK(ft[kk,...], self.at[kk,...], firstVar=self.xt[kk,...])
                    #v = self.v[kk,...]
                    kkm = kk
                else:
                    kkm = kk-1
                vf.vectors.append('velocity') ;
                vf.vectors.append(self.v[kkm,:])
                fvDef.saveVTK2(self.outputDir +'/'+ self.saveFile+str(kk)+'.vtk', vf)
        else:
            (obj1, self.xt) = self.objectiveFunDef(self.at, self.Afft, withTrajectory=True)
            for k in range(self.nTarg):
                self.fvDef[k].updateVertices(np.squeeze(self.xt[(k+1)*self.Tsize1, :, :]))
コード例 #9
0
    def covectorEvolution(self, at, Afft, px1, pnu1):
        M = self.Tsize
        timeStep = 1.0/M
        xt = []
        nut = []
        pxt = []
        pnut = []
        A = []
        dim2 = self.dim**2
        for k in range(self.ncurve):
            A.append([np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])])
            if self.affineDim > 0:
                for t in range(self.Tsize):
                    AB = np.dot(self.affineBasis, Afft[k][t]) 
                    A[k][0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                    A[k][1][t] = AB[dim2:dim2+self.dim]
            xJ = evol.landmarkDirectEvolutionEuler(self.x0[k], at[k], self.param.KparDiff, withNormals = self.nu0[k], affine = A[k])
            xt.append(xJ[0])
            nut.append(xJ[1])
            pxt.append(np.zeros([M, self.npt[k], self.dim]))
            pnut.append(np.zeros([M, self.npt[k], self.dim]))
            #print pnu1[k].shape
            pxt[k][M-1, :, :] = px1[k]
            pnut[k][M-1] = pnu1[k]
        
        xt.append(evol.landmarkDirectEvolutionEuler(self.x0[self.ncurve], at[self.ncurve], self.param.KparDiffOut))
        pxt.append(np.zeros([M, self.npoints, self.dim]))
        pxt[self.ncurve][M-1, :, :] = px1[self.ncurve]
        #lmb = np.zeros([self.npoints, self.dim])
        foo = self.constraintTermGrad(xt, nut, at, Afft)
        lmb = foo[0]
        dxcval = foo[1]
        dacval = foo[2]
        dAffcval = foo[3]
        
        for k in range(self.ncurve):
            pxt[k][M-1, :, :] += timeStep * dxcval[k][M]
        pxt[self.ncurve][M-1, :, :] += timeStep * dxcval[self.ncurve][M]
        
        for t in range(M-1):
            npt = 0
            for k in range(self.ncurve):
                npt1 = npt + self.npt[k]
                px = np.squeeze(pxt[k][M-t-1, :, :])
                z = np.squeeze(xt[k][M-t-1, :, :])
                a = np.squeeze(at[k][M-t-1, :, :])
                zpx = np.copy(dxcval[k][M-t-1])
                a1 = np.concatenate((px[np.newaxis,...], a[np.newaxis,...], -2*self.regweight*a[np.newaxis,...]))
                a2 = np.concatenate((a[np.newaxis,...], px[np.newaxis,...], a[np.newaxis,...]))
                zpx += self.param.KparDiff.applyDiffKT(z, a1, a2)
                if self.affineDim > 0:
                    zpx += np.dot(px, A[k][0][M-t-1])
                    
                pxt[k][M-t-2, :, :] = np.squeeze(pxt[k][M-t-1, :, :]) + timeStep * zpx
                npt = npt1
            px = np.squeeze(pxt[self.ncurve][M-t-1, :, :])
            z = np.squeeze(xt[self.ncurve][M-t-1, :, :])
            a = np.squeeze(at[self.ncurve][M-t-1, :, :])
            zpx = np.copy(dxcval[self.ncurve][M-t-1])
            a1 = np.concatenate((px[np.newaxis,...], a[np.newaxis,...], -2*self.regweightOut*a[np.newaxis,...]))
            a2 = np.concatenate((a[np.newaxis,...], px[np.newaxis,...], a[np.newaxis,...]))
            zpx += self.param.KparDiffOut.applyDiffKT(z, a1, a2)
            pxt[self.ncurve][M-t-2, :, :] = np.squeeze(pxt[self.ncurve][M-t-1, :, :]) + timeStep * zpx
            

        return pxt, pnut, xt, nut, dacval, dAffcval
コード例 #10
0
    def  objectiveFunDef(self, at, Afft, withTrajectory = False, withJacobian = False):
        param = self.param
        timeStep = 1.0/self.Tsize
        xt = []
        nut = []
        dim2 = self.dim**2
        if withJacobian:
            Jt = []
            for k in range(self.ncurve):
                A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
                if self.affineDim > 0:
                    for t in range(self.Tsize):
                        AB = np.dot(self.affineBasis, Afft[k][t]) 
                        A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                        A[1][t] = AB[dim2:dim2+self.dim]
                xJ = evol.landmarkDirectEvolutionEuler(self.x0[k], at[k], param.KparDiff, affine = A, withNormals = self.nu0[k], withJacobian = True)
                xt.append(xJ[0])
                nut.append(xJ[1])
                Jt.append(xJ[2])
            xJ = evol.landmarkDirectEvolutionEuler(self.x0[self.ncurve], at[self.ncurve], param.KparDiffOut, withJacobian=True)
            xt.append(xJ[0])
            Jt.append(xJ[1])
        else:
            for k in range(self.ncurve):
                A = [np.zeros([self.Tsize, self.dim, self.dim]), np.zeros([self.Tsize, self.dim])]
                if self.affineDim > 0:
                    for t in range(self.Tsize):
                        AB = np.dot(self.affineBasis, Afft[k][t]) 
                        A[0][t] = AB[0:dim2].reshape([self.dim, self.dim])
                        A[1][t] = AB[dim2:dim2+self.dim]
                xJ = evol.landmarkDirectEvolutionEuler(self.x0[k], at[k], param.KparDiff, withNormals = self.nu0[k], affine = A)
                xt.append(xJ[0])
                nut.append(xJ[1])
            xt.append(evol.landmarkDirectEvolutionEuler(self.x0[self.ncurve], at[self.ncurve], param.KparDiffOut))
        #print xt[-1, :, :]
        #print obj
        obj=0
        for t in range(self.Tsize):
            zB = np.squeeze(xt[self.ncurve][t, :, :])
            for k in range(self.ncurve):
                z = np.squeeze(xt[k][t, :, :]) 
                a = np.squeeze(at[k][t, :, :])
                ra = param.KparDiff.applyK(z,a)
                obj += self.regweight*timeStep*np.multiply(a, ra).sum()
                if self.affineDim > 0:
                    obj +=  timeStep * np.multiply(self.affineWeight.reshape(Afft[k][t].shape), Afft[k][t]**2).sum()
                #print t,k,obj

            a = np.squeeze(at[self.ncurve][t, :, :])
            ra = param.KparDiffOut.applyK(zB,a)
            obj += self.regweightOut*timeStep*np.multiply(a, ra).sum()

            #print 'obj before constraints:', obj
        cstr = self.constraintTerm(xt, nut, at, Afft)
        obj += cstr[0]

        if withJacobian:
            return obj, xt, Jt, cstr[1]
        elif withTrajectory:
            return obj, xt, cstr[1]
        else:
            return obj