Esempio n. 1
0
    def svdBpmMatrix(self, plane):  # Computes SVD of the bpm_matrix
        global nturns

        bpm_matrix = self.peak_peak(plane)
        n_bpms = shape(bpm_matrix)[1]
        print "performing SVD"
        # ----svd for matrix with bpms >10
        if n_bpms > 10:
            A = singular_value_decomposition(bpm_matrix)
        else:
            sys.exit("Exit, # of bpms < 10")

        return A
Esempio n. 2
0
    def svd_hessian_compute_fit(self, damping=None):
        "take one Hessian fitting step  using singular-value-decomposition"

        try:
            n = self.pointcount
            self.set_weights()

            if not self.scalar_weights:
                raise exceptions.AssertionError, "SVD solutions require, for now, scalar weights"

            sigi = Numeric.sqrt(self.weights)
            if not operator.isSequenceType(sigi):
                design = self.derivs() * float(sigi)
            else:
                # w should be a column vector
                design = self.derivs() * sigi[:, Numeric.newaxis]

            if (self.firstpass):
                self.funcvals = self.compute_funcvals()
                self.firstpass = 0

            u, w, v = singular_value_decomposition(design)
            w = self.singular_value_edit(w)
            for i in range(self.param_count):
                if w[i] != 0:
                    w[i] = 1.0 / w[i]
            b = (self.yarray[:n] - self.funcvals) * sigi
            self.fitvector = Numeric.sum(
                dot(Numeric.transpose(u * w), b) * Numeric.transpose(v), -1)
            self.svd_u = u
            self.svd_v = v
            self.svd_winv = w
        except:
            import traceback
            traceback.print_exc()
            raise

        if damping is None:
            self.funcparams = self.funcparams + self.fitvector * (1 -
                                                                  self.frozen)
        else:
            self.funcparams = self.funcparams + \
                self.fitvector * (1 - self.frozen) * damping

        self.funcvals = self.compute_funcvals()
        self.compute_chi2()
Esempio n. 3
0
    def svdClean(self, plane):
        global nturns, tx, ty

        
        if plane == 'x':
            b = tx[turn:,:]  #truncate by the first 5 turns
            n_turns = shape(b)[0]

        elif plane == 'y':
            b = ty[turn:,:]  #truncate by the first 5 turns
            n_turns = shape(b)[0]
        else:
            print "no tbt data acquired"
        
        b_mean = mean(b)
        b = (b-b_mean)/sqrt(n_turns)
        n_bpms = shape(b)[1]
        #----svd for matrix with bpms >10
        if n_bpms > 10:
            A = singular_value_decomposition(b)
            #print "Singular values:",A[1]
        else:
            sys.exit('Exit, # of bpms < 10')
        
        #----SVD cut for noise floor
        if sing_val > n_bpms:
            svdcut = n_bpms
            print 'requested more singular values than available'
            print '# of sing_val used for', plane, '=', n_bpms
        else:
            svdcut = int(sing_val)
            print '# of sing_val used for', plane, '=', svdcut
        #print A[1][0]
        
        A[1][svdcut:] = 0.
	temp=matrixmultiply(identity(len(A[1]))*A[1], A[2])
        b = matrixmultiply(A[0],temp) ### check
        b = (b *sqrt(n_turns))+b_mean
        #b = b*sqrt(n_turns)
        if plane == 'x':
            tx[turn:,:] = b
        elif plane == 'y':
            ty[turn:,:] = b
        else:
            print "no tbt data to analyze"
        nturns = shape(tx)[0]
Esempio n. 4
0
  def svd_hessian_compute_fit(self, damping=None):
    "take one Hessian fitting step  using singular-value-decomposition"

    try:
      n = self.pointcount
      self.set_weights()

      if not self.scalar_weights:
        raise exceptions.AssertionError, "SVD solutions require, for now, scalar weights"

      sigi = Numeric.sqrt(self.weights)
      if not operator.isSequenceType(sigi):
        design = self.derivs() * float(sigi)
      else:
        # w should be a column vector
        design = self.derivs() * sigi[:, Numeric.newaxis]

      if(self.firstpass):
        self.funcvals = self.compute_funcvals()
        self.firstpass = 0

      u, w, v = singular_value_decomposition(design)
      w = self.singular_value_edit(w)
      for i in range(self.param_count):
        if w[i] != 0:
          w[i] = 1.0 / w[i]
      b = (self.yarray[:n] - self.funcvals) * sigi
      self.fitvector = Numeric.sum(
          dot(Numeric.transpose(u * w), b) * Numeric.transpose(v), -1)
      self.svd_u = u
      self.svd_v = v
      self.svd_winv = w
    except:
      import traceback
      traceback.print_exc()
      raise

    if damping is None:
      self.funcparams = self.funcparams + self.fitvector * (1 - self.frozen)
    else:
      self.funcparams = self.funcparams + \
          self.fitvector * (1 - self.frozen) * damping

    self.funcvals = self.compute_funcvals()
    self.compute_chi2()
Esempio n. 5
0
    def svdClean(self, plane):
        global nturns, tx, ty

        if plane == "x":
            b = tx[turn:, :]  # truncate by the first 5 turns
            n_turns = shape(b)[0]

        elif plane == "y":
            b = ty[turn:, :]  # truncate by the first 5 turns
            n_turns = shape(b)[0]
        else:
            print "no tbt data acquired"

        b_mean = mean(b)
        b = (b - b_mean) / sqrt(n_turns)
        n_bpms = shape(b)[1]
        # ----svd for matrix with bpms >10
        if n_bpms > 10:
            A = singular_value_decomposition(b)
        else:
            sys.exit("Exit, # of bpms < 10")

        # ----SVD cut for noise floor
        if sing_val > n_bpms:
            svdcut = n_bpms
            print "requested more singular values than available"
            print "# of sing_val used for", plane, "=", n_bpms
        else:
            svdcut = sing_val
            print "# of sing_val used for", plane, "=", svdcut
        # print A[1][:svdcut]
        A[1][svdcut:] = 0.0
        b = matrixmultiply(A[0], matrixmultiply(identity(len(A[1])) * A[1], A[2]))
        b = (b * sqrt(n_turns)) + b_mean
        # b = b*sqrt(n_turns)
        if plane == "x":
            tx[turn:, :] = b
        elif plane == "y":
            ty[turn:, :] = b
        else:
            print "no tbt data to analyze"
        nturns = shape(tx)[0]
 def run(self):
     "Superimpose the coordinate sets."
     if self.coords is None or self.reference_coords is None:
         raise Exception, "No coordinates set."
     coords=self.coords
     reference_coords=self.reference_coords
     # center on centroid
     av1=sum(coords)/self.n  
     av2=sum(reference_coords)/self.n    
     coords=coords-av1
     reference_coords=reference_coords-av2
     # correlation matrix
     a=matrixmultiply(transpose(coords), reference_coords)
     u, d, vt=singular_value_decomposition(a)
     self.rot=transpose(matrixmultiply(transpose(vt), transpose(u)))
     # check if we have found a reflection
     if determinant(self.rot)<0:
         vt[2]=-vt[2]
         self.rot=transpose(matrixmultiply(transpose(vt), transpose(u)))
     self.tran=av2-matrixmultiply(av1, self.rot)
Esempio n. 7
0
    def svdClean(self, plane):
        global nturns, tx, ty

        
        if plane == 'x':
            b = tx[turn:,:]  #truncate by the first 5 turns
            n_turns = shape(b)[0]

        elif plane == 'y':
            b = ty[turn:,:]  #truncate by the first 5 turns
            n_turns = shape(b)[0]
        else:
            print "no tbt data acquired"
        
        b_mean = mean(b)
        b = (b-b_mean)/sqrt(n_turns)
        n_bpms = shape(b)[1]
        print "number of bpms",n_bpms
      
        #----svd for matrix with bpms >10
        if n_bpms > 10:
            A = singular_value_decomposition(b)
            #print "Singular values:",A[1]
        else:
            sys.exit('Exit, # of bpms < 10')
        
        #----SVD cut for noise floor
        if sing_val > n_bpms:
            svdcut = n_bpms
            print 'requested more singular values than available'
            print '# of sing_val used for', plane, '=', n_bpms
        else:
            svdcut = int(sing_val)
            print '# of sing_val used for', plane, '=', svdcut


        print shape(b)[1]

        print A[1]
        sing=open(options.file+"_sing_val","w")
        for xx in A[1]:
            print >> sing,xx

        sing.close()
        
        
        A[1][svdcut:] = 0.
        b = matrixmultiply(A[0],matrixmultiply(identity(len(A[1]))*A[1], A[2]))
        b = (b *sqrt(n_turns))+b_mean

        A = singular_value_decomposition(b)
        print A[1]
        sing=open(options.file+"sing_val_cut","w")
        for xx in A[1]:
            print >> sing,xx

        sing.close()
        #sys.exit()
        #sys.exit()


        

  

        
        #b = b*sqrt(n_turns)
        if plane == 'x':
            tx[turn:,:] = b
        elif plane == 'y':
            ty[turn:,:] = b
        else:
            print "no tbt data to analyze"
        nturns = shape(tx)[0]