def InnerProd(ser1, ser2, PSD):
  size = Numeric.shape(ser1)[0]
  pdlen = size/2
  nyquistf = 0.5/15.0   #   !!! hardcoded !!!!
  freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen)
  if(Numeric.shape(ser2)[0] != size):
     print "size of time series must be the same"
     sys.exit(1)
  if(Numeric.shape(PSD)[0] != pdlen):
     print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
     sys.exit(1)
  fourier1 = FFT.fft(ser1)
  fourier2 = FFT.fft(ser2)
  prod = Numeric.zeros(pdlen+1, dtype='d')
  prod[0] = 0.0
  prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1]))
  prod[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  Numeric.divide(prod[1:], PSD, prod[1:]) 
  olap0 = 0.0
  for i in xrange(pdlen):
      if (freqs[i] > 1.e-5 and freqs[i]<0.01):
           olap0 += prod[i]
  olap0 = 2.0*olap0/float(size)
 # olap0 =  2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt
  return  olap0
Exemple #2
0
    def rmsMatrixByMember( self, mirror=0, step=1 ):
        """
        Get result matrix ordered first by member then by time. (requires
        EnsembleTraj)

        @param mirror: mirror matrix at diagonal (only for intra-traj. rms)
                       (default: 0)
        @type  mirror: 0|1

        @param step: take only every step frame [1]
        @type  step: int
        """
        intra_traj = self.traj_2 is None

        m = self.getResult( mirror=intra_traj )

        i1 = i2 = self.traj_1.argsortMember( step=step )

        if self.traj_2 is not None:
            i2 = self.traj_2.argsortMember( step=step )

        a = N.take( m, i1, 0 )
        a = N.take( a, i2, 1 )

        if intra_traj and not mirror:
            for i in range( N.shape(a)[0] ):
                for j in range( i, N.shape(a)[1] ):
                    a[j,i] = 0.

        return a
def MaxInnerProd(ser1, ser2, PSD):
  size = Numeric.shape(ser1)[0]
  pdlen = size/2
  nyquistf = 0.5/15.0   #   !!! hardcoded !!!!
  freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen)
  if(Numeric.shape(ser2)[0] != size):
     print "size of time series must be the same"
     sys.exit(1)
  if(Numeric.shape(PSD)[0] != pdlen):
     print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
     sys.exit(1)
  fourier1 = FFT.fft(ser1)
  fourier2 = FFT.fft(ser2)
  prod1 = Numeric.zeros(pdlen+1, dtype='d')
  prod2 = Numeric.zeros(pdlen+1, dtype='d')
  prod1[0] = 0.0
  prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1]))
  prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  prod2[0] = 0.0
  prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j)))
  prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  Numeric.divide(prod1[1:], PSD, prod1[1:]) 
  Numeric.divide(prod2[1:], PSD, prod2[1:]) 
  olap0 = 0.0
  olappiby2 = 0.0
  for i in xrange(pdlen):
      if (freqs[i] > 1.e-5 and freqs[i]<0.01):
           olap0 += prod1[i]
	   olappiby2 += prod2[i]
  olap0 = 2.0*olap0/float(size)
  olappiby2 = 2.0*olappiby2/float(size)
#  olap0 =  2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt
#  olappiby2 =  2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt
  print "angle of maxim. = ", math.atan(olappiby2/olap0)
  return sqrt(olap0**2 + olappiby2**2) 
def MaxInnerProd(ser1, ser2, PSD):
  size = Numeric.shape(ser1)[0]
  pdlen = size/2
  nyquistf = 0.5/15.0   #   !!! hardcoded !!!!
  freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen)
  if(Numeric.shape(ser2)[0] != size):
     print "size of time series must be the same"
     sys.exit(1)
  if(Numeric.shape(PSD)[0] != pdlen):
     print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
     sys.exit(1)
  fourier1 = FFT.fft(ser1)
  fourier2 = FFT.fft(ser2)
  prod1 = Numeric.zeros(pdlen+1, dtype='d')
  prod2 = Numeric.zeros(pdlen+1, dtype='d')
  prod1[0] = 0.0
  prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1]))
  prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  prod2[0] = 0.0
  prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j)))
  prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  Numeric.divide(prod1[1:], PSD, prod1[1:]) 
  Numeric.divide(prod2[1:], PSD, prod2[1:]) 
  olap0 = 0.0
  olappiby2 = 0.0
  for i in xrange(pdlen):
      if (freqs[i] > fLow and freqs[i]<= fHigh):
           olap0 += prod1[i]
	   olappiby2 += prod2[i]
  olap0 = 2.0*olap0/float(size)
  olappiby2 = 2.0*olappiby2/float(size)
#  olap0 =  2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt
#  olappiby2 =  2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt
  print "angle of maxim. = ", math.atan(olappiby2/olap0)
  return sqrt(olap0**2 + olappiby2**2) 
def InnerProd(ser1, ser2, PSD):
  size = Numeric.shape(ser1)[0]
  pdlen = size/2
  nyquistf = 0.5/15.0   #   !!! hardcoded !!!!
  freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen)
  if(Numeric.shape(ser2)[0] != size):
     print "size of time series must be the same"
     sys.exit(1)
  if(Numeric.shape(PSD)[0] != pdlen):
     print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
     sys.exit(1)
  fourier1 = FFT.fft(ser1)
  fourier2 = FFT.fft(ser2)
  prod = Numeric.zeros(pdlen+1, dtype='d')
  prod[0] = 0.0
  prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1]))
  prod[pdlen] = fourier1[pdlen]*fourier2[pdlen]
  Numeric.divide(prod[1:], PSD, prod[1:]) 
  olap0 = 0.0
  for i in xrange(pdlen):
      if (freqs[i] > fLow and freqs[i]<= fHigh):
           olap0 += prod[i]
  olap0 = 2.0*olap0/float(size)
 # olap0 =  2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt
  return  olap0
Exemple #6
0
def sn(signal,noise,stime,npatches):
    """Compute the optimal S/N for signal, sampled at intervals of
    stime, and for the total duration represented in the array, against noise
    represented by the time series noise; npatches overlapping periods are used
    to estimate the PSD of the noise."""

    # compute signal spectrum without windowing or averaging
    sspec = spect(signal,stime,0)

    # compute the noise spectrum, using segment averaging
    nspec = spect(noise,stime,npatches)

    # interpolate the noise to be defined on the same frequencies
    # of the signal's spectrum

    ispec = Numeric.zeros(Numeric.shape(sspec),dtype='d')
    ispec[:,0] = sspec[:,0]

    # ispec[:,1] = arrayfns.interp(nspec[:,1],nspec[:,0],ispec[:,0])
    ispec[:,1] = linearinterpolate(nspec[:,1],nspec[:,0],ispec[:,0])

    # the (S/N)^2 is given by 2T times the integrated ratio
    # of the spectral densities (the factor of 2 because the spectral
    # density is one-sided); notice however that the df is 1/T,
    # so we need only to sum up the array containing the ratio,
    # and multiply by two

    sratio = Numeric.zeros(Numeric.shape(sspec)[0],dtype='d')
    sratio[1:] = sspec[1:,1] / ispec[1:,1]
    sn2 = 2.0 * sum(sratio[1:])

    return math.sqrt(sn2)
Exemple #7
0
 def test_constructor_shape(self):
     """__init__         -- make refCoords and resultCoords homogeneous"""
     n = len(self.random_points)
     ncoords = Ncoords(self.random_points)  ### tested call ###
     # confirm shape to be nx4
     self.assertEqual((n, 4), Numeric.shape(ncoords.resultCoords))
     self.assertEqual((n, 4), Numeric.shape(ncoords.refCoords))
     # cofirm that the last column is all ones
     self.assertEqual(
         Numeric.ones(n).tolist(), ncoords.resultCoords[:, 3].tolist())
     self.assertEqual(
         Numeric.ones(n).tolist(), ncoords.refCoords[:, 3].tolist())
 def test_constructor_shape(self):
     """__init__         -- make refCoords and resultCoords homogeneous"""
     n = len(self.random_points)
     ncoords = Ncoords( self.random_points) ### tested call ###
     # confirm shape to be nx4
     self.assertEqual( (n, 4), Numeric.shape(ncoords.resultCoords))
     self.assertEqual( (n, 4), Numeric.shape(ncoords.refCoords))
     # cofirm that the last column is all ones
     self.assertEqual(Numeric.ones(n).tolist(),
                      ncoords.resultCoords[:,3].tolist())
     self.assertEqual(Numeric.ones(n).tolist(),
                      ncoords.refCoords[:,3].tolist())
Exemple #9
0
    def getResult( self, mirror=0 ):
        """
        Get result matrix ordered such as input trajectory.

        @param mirror: mirror the matrix at diagonal (default: 1)
                       (only for intra-traj)
        @type  mirror: 1|0

        @return: array( (n_frames, n_frames), 'f'), matrix of pairwise rms
        @rtype: array
        """
        if self.verbose:   self.log.write('assembling result matrix...')

        intra_traj = self.traj_2 is None

        n1 = n2 = len( self.traj_1 )
        if self.traj_2 is not None:
            n2 = len( self.traj_2 )

        a  = N.zeros( (n1,n2), N.Float32 )

        if self.verbose: self.log.write('#')

        for key, value in self.result.items():
            i_start, i_stop = key[0]
            j_start, j_stop = key[1]

            window = N.reshape( value, (i_stop-i_start, j_stop-j_start) )
            window = window.astype(N.Float32)

            a[i_start:i_stop, j_start:j_stop] = window

        if self.verbose: self.log.write('#')

        if intra_traj:
            for i in range( N.shape(a)[0] ):
                for j in range( i, N.shape(a)[1] ):
                    if a[j,i] == 0:
                        a[j,i] = a[i,j]
                    else:
                        a[i,j] = a[j,i]

        if self.verbose: self.log.write('#')

        if intra_traj and not mirror:
            for i in range( N.shape(a)[0] ):
                for j in range( i, N.shape(a)[1] ):
                    a[j,i] = 0.

        if self.verbose:   self.log.add('done')

        return a
Exemple #10
0
   def setReg ( self, ireg ):

      try:
         self.ireg = array ( ireg, typecode=Int, copy=0 )
      except:
         raise TypeError( "Could not cast ireg to numpy array")

      if not ( rank(self.ireg) == 2 and
               shape(self.ireg)[0] >=2 and shape(self.ireg)[1] >= 2 ):
         raise TypeError( "ireg must be 2D")

      if self.shape != shape ( self.ireg ):
         raise InputError( "ireg has incompatible shape.")
Exemple #11
0
   def setTriangle ( self, triangle ):

      try:
         self.triangle = array ( triangle, typecode=Int16, copy=0 )
      except:
         raise TypeError( "Could not cast triangle to numpy array")

      if not ( rank(self.triangle) == 2 and
               shape(self.triangle)[0] >=2 and shape(self.triangle)[1] >= 2 ):
         raise TypeError( "triangle must be 2D")

      if self.shape != shape (self.triangle):
         raise InputError( "triangle has incompatible shape.")
Exemple #12
0
    def test_Analyzer(self):
        """Dock.Analyzer test """
        from Biskit import Trajectory
        from Biskit.Dock import ComplexList

        ## create a minimal 1-frame receptor trajectory from a pdb file
        self.t_rec = Trajectory([t.testRoot() + '/rec/1A2P.pdb'],
                                verbose=self.local)
        t.dump(self.t_rec, self.f_out)

        ## load a complex list
        cl = t.load(t.testRoot() + '/dock/hex/complexes.cl')

        self.a = Analyzer(rec=self.f_out,
                          lig=t.testRoot() + '/lig_pcr_00/traj.dat',
                          ref=t.testRoot() + '/com/ref.complex',
                          verbose=self.local)

        ## shuffle this list five times
        shuff_lst = self.a.shuffledLists(5, range(8))

        ## create two random contact matrices
        rand_mat = self.a.random_contacts(cl[0].atomContacts(), 2)

        self.assertEqual(N.shape(rand_mat[1]), (1075, 876))
Exemple #13
0
    def test_MatrixPlot(self):
        """MatrixPlot test"""
        n = 30

        z = N.zeros((n, n), N.Float)

        for i in range(N.shape(z)[0]):
            for j in range(N.shape(z)[1]):
                z[i, j] = N.exp(-0.01 * ((i - n / 2)**2 + (j - n / 2)**2))

        self.p = MatrixPlot(z, palette='sausage', legend=1)

        if self.local or self.VERBOSITY > 2:
            self.p.show()

        self.assert_(self.p is not None)
Exemple #14
0
 def trim(self):
     """get rid of any universal gaps in the alignment.
     """
     #make sure we have an alignment
     if len(self)==0:
         return
     #make sure we have an up-to-date matrix
     if not hasattr(self,'matrix'):
         self.makeMatrix()
     nsequences,nresidues = Numeric.shape(self.matrix)
     if (nsequences != len(self.sequences) or
         nresidues != len(self)):
         self.makeMatrix()
     transpose = Numeric.transpose(self.matrix)
     gaplist = []
     #any row with sum=0 in the transpose corresponds to a column in the alignment
     #which is all gaps. So add the positions of these columns to the gaplist
     for x in range(len(transpose)):
         line = transpose[x]
         if Numeric.sum(line)==0:
             gaplist.append(x)
     #now can simply pop the unwanted gaps out of each sequence.
     gaplist.reverse()            
     for sequence in self:
         for gap in gaplist:
             junk=sequence.sequence.pop(gap)
             junk=sequence.gappednumbers.pop(gap)
Exemple #15
0
    def test_Analyzer( self):
        """Dock.Analyzer test """
        from Biskit import Trajectory
        from Biskit.Dock import ComplexList

        ## create a minimal 1-frame receptor trajectory from a pdb file
        self.t_rec = Trajectory( [t.testRoot()+'/rec/1A2P.pdb'],
                                 verbose=self.local)
        t.dump( self.t_rec, self.f_out )

        ## load a complex list
        cl = t.load( t.testRoot() + '/dock/hex/complexes.cl')

        self.a= Analyzer( rec = self.f_out,
                          lig = t.testRoot()+'/lig_pcr_00/traj.dat',
                          ref = t.testRoot()+'/com/ref.complex',
                          verbose = self.local)

        ## shuffle this list five times
        shuff_lst = self.a.shuffledLists( 5, range(8) )

        ## create two random contact matrices
        rand_mat = self.a.random_contacts( cl[0].atomContacts(), 2 )
        
        self.assertEqual( N.shape(rand_mat[1]), (1075, 876) ) 
Exemple #16
0
def wpdg(series,detrend=0,win='triangle'):
    samples = Numeric.shape(series)[0]

    wrange = Numeric.arange(0,samples,dtype='d') / (samples - 1.0);

    if win == 'blackman':
        window = 0.42 - 0.5 * Numeric.cos(2*math.pi*wrange) + 0.08 * Numeric.cos(4*math.pi*wrange)
    elif win == 'sin4':
        window = Numeric.sin(math.pi*wrange)**4.0
    else:
        # if we don't recognize a window, default to triangle
        pdlen = (samples - 1) / 2.0
        window = 1.0 - abs(Numeric.arange(0,samples,dtype='d') - pdlen) / (pdlen)

    wseries = series.copy()

    if detrend == 1:
        leastsquares(wseries,detrend=1)
    
    wseries *= window

    weight = samples * Numeric.sum(window ** 2)
    wpdgram = pdg(wseries) * (1.0 * samples**2 / weight)

    return wpdgram
Exemple #17
0
 def _checkOrth(self, T, TT, eps=0.0001, output=False):
     """check if the basis is orthogonal on a set of points x: TT == T*transpose(T) == c*Identity
     INPUT:  T: matrix of values of polynomials calculated at common reference points (x)
             TT = T * transpose(T)
             eps: max numeric error
     """
     TTd0 = (-1. * Numeric.identity(Numeric.shape(TT)[0]) +
             1) * TT  # TTd0 = TT with 0s on the main diagonal
     s = Numeric.sum(Numeric.sum(Numeric.absolute(TTd0)))
     minT = MLab.min(MLab.min(T))
     maxT = MLab.max(MLab.max(T))
     minTTd0 = MLab.min(MLab.min(TTd0))
     maxTTd0 = MLab.max(MLab.max(TTd0))
     if not s < eps:
         out = "NOT ORTHOG, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (
             minTTd0, maxTTd0, s)
         if output:
             print out
             return False
         else:
             raise out
     elif output:
         out = "ORTHOGONAL, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (
             minTTd0, maxTTd0, s)
         print out
     return True
def main():
    """
    main for convdbnumeric2ascii.py - load Numeric pickle and output as ascii
    """
    if len(sys.argv) != 2:
        usage(sys.argv[0])

    dbfile = sys.argv[1]
    db = pickle.load(open(dbfile))
    for pdbid,dbtablist in db.iteritems():
        tabnum = 0
        while tabnum < len(dbtablist):
            omega = dbtablist[tabnum]
            n = Numeric.shape(omega)[0]
            name = pdbid
            if len(dbtablist) > 1:
                name += str(tabnum)
            sys.stdout.write('%6s %4d\n' % (name, n))
            for i in xrange(n):
                for j in xrange(i+1):
                    if isNaN(omega[i,j]):
                        angle = 0.0
                    else:
                        angle = omega[i,j]
                    sys.stdout.write('%6.3f ' % angle)
                sys.stdout.write('\n')
            sys.stdout.write('\n')
            tabnum += 1
Exemple #19
0
    def __atomContacts(self, cutoff, rec_mask, lig_mask, cache):
        """
        Intermolecular distances below cutoff after applying the two masks.
        
        @param cutoff: cutoff for B{atom-atom} contact in \AA
        @type  cutoff: float
        @param rec_mask: atom mask
        @type  rec_mask: [1|0]
        @param lig_mask: atom mask
        @type  lig_mask: [1|0]
        @param cache: cache pairwise atom distance matrix
        @type  cache: 1|0
        
        @return: atom contact matrix, array sum_rec_mask x sum_lig_mask
        @rtype: array
        """
        ## get atom coordinats as array 3 x all_atoms
        rec_xyz = self.rec().getXyz()
        lig_xyz = self.lig().getXyz()

        ## get pair-wise distances -> atoms_rec x atoms_lig
        dist = getattr( self, 'pw_dist', None )
        if dist is None or \
               N.shape( dist ) != ( N.sum(rec_mask), N.sum(lig_mask) ):
            dist = self.__pairwiseDistances(N.compress( rec_mask, rec_xyz, 0),
                                            N.compress( lig_mask, lig_xyz, 0) )
        if cache:
            self.pw_dist = dist

        ## reduce to 1 (distance < cutoff) or 0 -> n_atoms_rec x n_atoms_lig
        return N.less( dist, cutoff )
Exemple #20
0
def getobs2(snum,stime,observables):
    if len(Numeric.shape(observables)) == 0:
        return Numeric.asarray(map(observables,Numeric.arange(0,snum*stime,stime)),dtype='d')
    else:
        def mapfun(x):
            return map(lambda y: y(x),observables)
        return Numeric.asarray(map(mapfun,Numeric.arange(0,snum*stime,stime)),dtype='d')
Exemple #21
0
    def loadResContacts( self ):
        """
        Uncompress residue contact matrix if necessary.
        
        @return: dict with contact matrix and parameters OR None
        @rtype: dict OR None
        """
        ## Backwards compatibility
        if self.contacts != None and type( self.contacts ) == str:
            self.contacts = t.load( self.contacts )
            EHandler.warning("loading old-style pickled contacts.") 
            return self.contacts

        ## New, uncompression from list of indices into raveled array
        if self.contacts != None and \
           len( N.shape( self.contacts['result'])) == 1:

            try:
                lenRec, lenLig = self.contacts['shape']
            except:
                EHandler.warning("uncompressing contacts without shape")
                lenRec = self.rec().lenResidues()
                lenLig = self.lig().lenResidues()

            m = N.zeros( lenRec * lenLig )
            N.put( m, self.contacts['result'], 1 )

            self.contacts['result'] = N.reshape( m, (lenRec, lenLig) )

        return self.contacts
Exemple #22
0
 def _checkOrth(self, T, TT, eps=0.0001, output=False):
     """check if the basis is orthogonal on a set of points x: TT == T*transpose(T) == c*Identity
     INPUT:  T: matrix of values of polynomials calculated at common reference points (x)
             TT = T * transpose(T)
             eps: max numeric error
     """
     TTd0 = (-1.*Numeric.identity(Numeric.shape(TT)[0])+1) * TT  # TTd0 = TT with 0s on the main diagonal
     s = Numeric.sum(Numeric.sum(Numeric.absolute(TTd0)))
     minT = MLab.min(MLab.min(T))
     maxT = MLab.max(MLab.max(T))
     minTTd0 = MLab.min(MLab.min(TTd0))
     maxTTd0 = MLab.max(MLab.max(TTd0))
     if not s < eps:
         out = "NOT ORTHOG, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (minTTd0, maxTTd0, s)
         if output:
             print out
             return False
         else:
             raise out
     elif output:
         out = "ORTHOGONAL, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (minTTd0, maxTTd0, s)
         print out
     return True
Exemple #23
0
def spect(series,sampling,patches=1,detrend=0,overlap=1,win='triangle'):
    nyquistf = 0.5 / sampling

    if patches==0:
        period = pdg(series)
    elif patches==1:
        period = wpdg(series,detrend,win)
    else:
        if overlap==0:
            period = nopwpdg(series,patches,detrend,win)
        else:
            period = opwpdg(series,patches,detrend,win)

    pdlen = Numeric.shape(period)[0]-1

    freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen)

    deltaf = nyquistf / pdlen
   
    period[0] = 2 * period[0] / deltaf
    period[1:pdlen] = period[1:pdlen] / deltaf
    period[pdlen] = 2 * period[pdlen] / deltaf

    spectrum = Numeric.zeros((pdlen+1,2),dtype='d')

    spectrum[:,0] = freqs[:]
    spectrum[:,1] = period[:]

    return spectrum
Exemple #24
0
def area(curve, start=0.0, stop=1.0):
    """
    Numerically add up the area under the given curve.
    The curve is a 2-D array or list of tupples.
    The x-axis is the first column of this array (curve[:,0]).
    (originally taken from Biskit.Statistics.ROCalyzer)

    @param curve: a list of x,y coordinates
    @type  curve: [ (y,x), ] or N.array
    @param start: lower boundary (in x) (default: 0.0)
    @type  start: float
    @param stop: upper boundary (in x) (default: 1.0)
    @type  stop: float
    @return: the area underneath the curve between start and stop.
    @rtype: float
    """
    ## convert and swap axes
    curve = N.array(curve)
    c = N.zeros(N.shape(curve), curve.dtype)
    c[:, 0] = curve[:, 1]
    c[:, 1] = curve[:, 0]

    assert len(N.shape(c)) == 2

    ## apply boundaries  ## here we have a problem with flat curves
    mask = N.greater_equal(c[:, 1], start)
    mask *= N.less_equal(c[:, 1], stop)
    c = N.compress(mask, c, axis=0)

    ## fill to boundaries -- not absolutely accurate: we actually should
    ## interpolate to the neighboring points instead
    c = N.concatenate((N.array([
        [c[0, 0], start],
    ]), c, N.array([
        [c[-1, 0], stop],
    ])))
    x = c[:, 1]
    y = c[:, 0]

    dx = x[1:] - x[:-1]  # distance on x between points
    dy = y[1:] - y[:-1]  # distance on y between points

    areas1 = y[:-1] * dx  # the rectangles between all points
    areas2 = dx * dy / 2.0  # the triangles between all points

    return N.sum(areas1) + N.sum(areas2)
Exemple #25
0
def darkentime(series,patches=1):
    samples = Numeric.shape(series)[0]

    patlen = samples / patches
    
    for j in xrange(0,patches-1):
        for i in xrange(0,patlen-1):
            series[j*patlen+i] = series[j*patlen+i+1] - series[j*patlen+i]
Exemple #26
0
    def random_contacts(self, contMat, n, maskRec=None, maskLig=None):
        """
        Create randomized surface contact matrix with same number of
        contacts and same shape as given contact matrix.
        
        @param contMat: template contact matrix
        @type  contMat: matrix
        @param n: number of matrices to generate
        @type  n: int
        @param maskRec: surface masks (or something similar)
        @type  maskRec: [1|0]
        @param maskLig: surface masks (or something similar)
        @type  maskLig: [1|0]
        
        @return: list of [n] random contact matricies
        @rtype: [matrix]
        """
        a, b = N.shape(contMat)
        nContacts = N.sum(N.sum(contMat))

        if not maskLig:
            r_size, l_size = N.shape(contMat)
            maskLig = N.ones(l_size)
            maskRec = N.ones(r_size)

        c_mask = N.ravel(N.outerproduct(maskRec, maskLig))
        c_pos = N.nonzero(c_mask)

        # get array with surface positions from complex
        cont = N.take(N.ravel(contMat), c_pos)
        length = len(cont)

        result = []

        for i in range(n):
            # create random array
            ranCont = mathUtils.randomMask(nContacts, length)

            # blow up to size of original matrix
            r = N.zeros(a * b)
            N.put(r, c_pos, ranCont)

            result += [N.reshape(r, (a, b))]

        return result
Exemple #27
0
def simplify(obj):
    """A helper function to convert results obtained from Octave.
    This will convert all 1x1 arrays to scalars, vectors to 1D arrays,
    1xN and 0x0 character arrays to strings, 1xN, Nx1 and 0x0 cell
    arrays to lists, and strip scalar dicts. It will work recursively."""

    def get_typecode(array):
	"""gets the typecode from both Numeric and NumPy array"""
	try:
	    tc = array.typecode()
	except:
	    tc = array.dtype.char
	return tc

    def vectordims(dims,column_allowed = True):
	return (len(dims) == 2 and 
		((dims[0] == 1 or (column_allowed and dims[1] == 1)) or
		(dims[0] == 0 and dims[1] == 0)))

    if isinstance(obj,Numeric.ArrayType):
	tc = get_typecode(obj)
	if tc == 'O':
	    if vectordims(Numeric.shape(obj)):
		return map(simplify,narrowlist(obj))
	elif tc == 'c':
	    if vectordims(Numeric.shape(obj), False):
		return obj.tostring()
	else:
	    dims = Numeric.shape(obj)
	    if dims == (1,1):
		return obj[0,0]
	    elif vectordims(dims):
		return Numeric.ravel(obj)
    elif isinstance(obj,dict):
	sobj = {}
	for key in obj:
	    sval = simplify(obj[key])
	    if isinstance(sval,list) and len(sval) == 1:
		sval = sval[0]
	    sobj[key] = sval
	return sobj
    elif isinstance(obj,tuple):
	return tuple(map(simplify,obj))
    ## default.
    return obj
def simplify(obj):
    """A helper function to convert results obtained from Octave.
    This will convert all 1x1 arrays to scalars, vectors to 1D arrays,
    1xN and 0x0 character arrays to strings, 1xN, Nx1 and 0x0 cell
    arrays to lists, and strip scalar dicts. It will work recursively."""

    def get_typecode(array):
        """gets the typecode from both Numeric and NumPy array"""
        try:
            tc = array.typecode()
        except:
            tc = array.dtype.char
        return tc

    def vectordims(dims, column_allowed=True):
        return len(dims) == 2 and (
            (dims[0] == 1 or (column_allowed and dims[1] == 1)) or (dims[0] == 0 and dims[1] == 0)
        )

    if isinstance(obj, Numeric.ArrayType):
        tc = get_typecode(obj)
        if tc == "O":
            if vectordims(Numeric.shape(obj)):
                return map(simplify, narrowlist(obj))
        elif tc == "c":
            if vectordims(Numeric.shape(obj), False):
                return obj.tostring()
        else:
            dims = Numeric.shape(obj)
            if dims == (1, 1):
                return obj[0, 0]
            elif vectordims(dims):
                return Numeric.ravel(obj)
    elif isinstance(obj, dict):
        sobj = {}
        for key in obj:
            sval = simplify(obj[key])
            if isinstance(sval, list) and len(sval) == 1:
                sval = sval[0]
            sobj[key] = sval
        return sobj
    elif isinstance(obj, tuple):
        return tuple(map(simplify, obj))
        ## default.
    return obj
Exemple #29
0
def getobscount(snum,stime,observables,zerotime=0.0):
    fullinittime = time()
    inittime = int(fullinittime)
    lasttime = 0
    
    print "Processing...",
    sys.stdout.flush()

    try:
        if len(Numeric.shape(observables)) == 0:
            array = Numeric.zeros(snum,dtype='d')
            for i in Numeric.arange(0,snum):
                array[i] = observables(zerotime+i*stime)
                if i % 1024 == 0:
                    lasttime = dotime(i,snum,inittime,lasttime)
        else:
            obslen = Numeric.shape(observables)[0]
            array = Numeric.zeros((snum,obslen),dtype='d')
            for i in Numeric.arange(0,snum):
                for j in xrange(0,obslen):
                    array[i,j] = observables[j](zerotime+i*stime)
                if i % 1024 == 0:
                    lasttime = dotime(i,snum,inittime,lasttime)
    except IndexError:
        print "lisautils::getobsc: I have trouble accessing time ", zerotime+i*stime,
        print "; you may try to reset your objects and repeat..."

        raise

    # there was good stuff here, but it's better to let the user deal with this
    # in particular, if observables is a noise-like variable, then it should
    # have the 'reset' method ['reset' in dir(observables)]; if observables is
    # a method of a TDI class, which should have the 'reset' method, the test
    # is ['reset' in dir(observables.im_self)], where "im_self" returns the
    # class instance for a given instance method

    currenttime = time() - fullinittime

    if currenttime > 0:
        vel = snum/currenttime
        print "\r...completed in %d s [%d (multi)samples/s].                           " % (int(currenttime),int(vel))  
    else:
        print "\r...completed.                                                         "

    return array
Exemple #30
0
    def random_contacts( self, contMat, n, maskRec=None, maskLig=None ):
        """
        Create randomized surface contact matrix with same number of
        contacts and same shape as given contact matrix.
        
        @param contMat: template contact matrix
        @type  contMat: matrix
        @param n: number of matrices to generate
        @type  n: int
        @param maskRec: surface masks (or something similar)
        @type  maskRec: [1|0]
        @param maskLig: surface masks (or something similar)
        @type  maskLig: [1|0]
        
        @return: list of [n] random contact matricies
        @rtype: [matrix]
        """
        a,b = N.shape( contMat )
        nContacts = N.sum( N.sum( contMat ))

        if not maskLig:
            r_size, l_size = N.shape( contMat )
            maskLig = N.ones( l_size )
            maskRec = N.ones( r_size )

        c_mask = N.ravel( N.outerproduct( maskRec, maskLig ) )
        c_pos = N.nonzero( c_mask )

        # get array with surface positions from complex
        cont = N.take( N.ravel(contMat), c_pos )
        length = len( cont )

        result = []

        for i in range( n ):
            # create random array
            ranCont = mathUtils.randomMask( nContacts,length )

            # blow up to size of original matrix
            r = N.zeros(a*b)
            N.put( r, c_pos, ranCont)

            result += [ N.reshape( r, (a,b) ) ]

        return result
Exemple #31
0
    def centers(self):
        """
        Get 'center structure' for each cluster.

        @return: N.array( n_clusters x n_atoms_masked x 3 )
        @rtype: array
        """
        lenAtoms = N.shape(self.fcCenters)[1] / 3
        return N.reshape(self.fcCenters, (self.n_clusters, lenAtoms, 3))
Exemple #32
0
    def centers( self ):
        """
        Get 'center structure' for each cluster.

        @return: N.array( n_clusters x n_atoms_masked x 3 )
        @rtype: array
        """
        lenAtoms = N.shape( self.fcCenters )[1] / 3
        return N.reshape( self.fcCenters, ( self.n_clusters, lenAtoms, 3))
Exemple #33
0
def plm ( y=None, x=None, ireg=None, mesh=None, **keywords ):
   """
   plm ( y, x, boundary=0/1, inhibit=0/1/2 )  or
   plm ( y, x, ireg, boundary=0/1, inhibit=0/1/2 )  or
   plm ( boundary=0/1, inhibit=0/1/2, mesh=myMesh )
   plm ( boundary=0/1, inhibit=0/1/2 )

      Plot a mesh of Y versus X.  Y and X must be 2D arrays with equal
      dimensions.  If present, IREG must be a 2D region number array
      for the mesh, with the same dimensions as X and Y.  The values of
      IREG should be positive region numbers, and zero for zones which do
      not exist.  The first row and column of IREG never correspond to any
      zone, and should always be zero.  The default IREG is 1 everywhere
      else.  If present, the BOUNDARY keyword determines whether the
      entire mesh is to be plotted (boundary=0, the default), or just the
      boundary of the selected region (boundary=1).  If present, the
      INHIBIT keyword causes the (X(,j),Y(,j)) lines to not be plotted
      (inhibit=1), or the (X(i,),Y(i,)) lines to not be plotted (inhibit=2).
      By default (inhibit=0), mesh lines in both logical directions are
      plotted.
      The Y, X, and IREG arguments may all be omitted to default to the
      mesh set by the most recent plmesh call.
      The following keywords are legal (each has a separate help entry):

    KEYWORDS: legend, hide, color, type, width, region, boundary, inhibit

    SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, plmesh,
              limits, logxy, ylimits, fma, hcp
   """
   global savedMesh

   if mesh is None:

      if x is None and y is None and ireg is None:
         print "Warning: using obsolete format; specify mesh or (y,x)."
         mesh = savedMesh
      else:
         assert x is not None and y is not None, "x and y need to be set"
         _x = array ( x, Float )
         if ireg is None:
            ireg = zeros ( shape(_x), Int )
            ireg[1:,1:] = 1
         mesh = Mesh ( x, y, ireg=ireg )

   else:

      if x is None and y is None and ireg is None:
#        .. Use mesh argument
         pass
      else:
         print "Warning: provided both (y,x) and mesh; will use mesh"
 
   print mesh.x
   print mesh.y
   print mesh.ireg
   plm_c ( mesh.y, mesh.x, mesh.ireg, keywords ) 
Exemple #34
0
 def test_getResultCoords_type(self):
     """getResultCoords  -- if not tolist: return nx4 Numeric.array"""
     n = len(self.random_points)
     nc = Ncoords(self.random_points, tolist=1)
     nc.tolist=0
     result = nc.getResultCoords() ### tested call ###
     # confirm shape
     self.assertEqual((n, 4), Numeric.shape(result))
     # confirm type
     self.assertEqual(type(Numeric.array([])), type(result))
Exemple #35
0
def area(curve, start=0.0, stop=1.0 ):
    """
    Numerically add up the area under the given curve.
    The curve is a 2-D array or list of tupples.
    The x-axis is the first column of this array (curve[:,0]).
    (originally taken from Biskit.Statistics.ROCalyzer)

    @param curve: a list of x,y coordinates
    @type  curve: [ (y,x), ] or N.array
    @param start: lower boundary (in x) (default: 0.0)
    @type  start: float
    @param stop: upper boundary (in x) (default: 1.0)
    @type  stop: float
    @return: the area underneath the curve between start and stop.
    @rtype: float
    """
    ## convert and swap axes
    curve = N.array( curve )
    c = N.zeros( N.shape(curve), curve.dtype )
    c[:,0] = curve[:,1]
    c[:,1] = curve[:,0]

    assert len( N.shape( c ) ) == 2

    ## apply boundaries  ## here we have a problem with flat curves
    mask = N.greater_equal( c[:,1], start )
    mask *= N.less_equal( c[:,1], stop )
    c = N.compress( mask, c, axis=0 )

    ## fill to boundaries -- not absolutely accurate: we actually should
    ## interpolate to the neighboring points instead
    c = N.concatenate((N.array([[c[0,0], start],]), c,
                       N.array([[c[-1,0],stop ],])) )
    x = c[:,1]
    y = c[:,0]

    dx = x[1:] - x[:-1] # distance on x between points 
    dy = y[1:] - y[:-1] # distance on y between points

    areas1 = y[:-1] * dx  # the rectangles between all points
    areas2 = dx * dy / 2.0 # the triangles between all points

    return N.sum(areas1) + N.sum(areas2)
Exemple #36
0
 def test_getResultCoords_shape(self):
     """getResultCoords  -- if tolist: return nx3 ListType"""
     n = len(self.random_points)
     nc = Ncoords(self.random_points, tolist=0)
     nc.tolist=1
     result = nc.getResultCoords() ### tested call ###
     # confirm shape
     self.assertEqual((n, 3), Numeric.shape(result))
     # confirm type
     self.assertEqual(type([]), type(result))
Exemple #37
0
 def test_getResultCoords_shape(self):
     """getResultCoords  -- if tolist: return nx3 ListType"""
     n = len(self.random_points)
     nc = Ncoords(self.random_points, tolist=0)
     nc.tolist = 1
     result = nc.getResultCoords()  ### tested call ###
     # confirm shape
     self.assertEqual((n, 3), Numeric.shape(result))
     # confirm type
     self.assertEqual(type([]), type(result))
Exemple #38
0
 def test_getResultCoords_type(self):
     """getResultCoords  -- if not tolist: return nx4 Numeric.array"""
     n = len(self.random_points)
     nc = Ncoords(self.random_points, tolist=1)
     nc.tolist = 0
     result = nc.getResultCoords()  ### tested call ###
     # confirm shape
     self.assertEqual((n, 4), Numeric.shape(result))
     # confirm type
     self.assertEqual(type(Numeric.array([])), type(result))
Exemple #39
0
 def coef_maxCut(self, appxCoef):
     """returns the coefficients different from zero up to the abs. max. coefficient
     where the first coefficient is excluded from finding the max.
     accepts 2d matrix of coefficients where rows represent different curves
     """
     assert len(appxCoef.shape) == 2
     k = Numeric.shape(appxCoef)[1]
     maxInd = Numeric.argmax(Numeric.absolute(appxCoef[:,1:]),1) + 1
     lowDiagOnes = Numeric.fromfunction(lambda i,j: i>=j, (k,k))
     coefSelector = Numeric.take(lowDiagOnes, maxInd, 0)
     return appxCoef*coefSelector
def ComputeNorm(ser, sampling, PSD):
     size = Numeric.shape(ser)[0]
     if(Numeric.shape(PSD)[0] != size/2):
         print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
         sys.exit(1)
     ser2 = synthlisa.spect(ser, sampling,0)
     norm = 0.0
     indx = 0
     #fout = open("psdTest.dat", 'w')
     for i in xrange(size/2-1):
        if (ser2[i][0] > fLow and ser2[i][0]<= fHigh):
	    norm += ser2[i][1]/PSD[i]
#	    record = str(ser2[i][0]) + spr + str(PSD[i]) + spr + str(2.0*norm) + "\n"
#	    fout.write(record)
	    indx = indx + 1
     norm = sqrt(2.0*norm)
#    fout.close()     
     #print "indx = ", indx
     #norm = sqrt(2.0 *numpy.sum(ser2[1:,1]/PSD))
     return norm
Exemple #41
0
 def coef_maxCut(self, appxCoef):
     """returns the coefficients different from zero up to the abs. max. coefficient
     where the first coefficient is excluded from finding the max.
     accepts 2d matrix of coefficients where rows represent different curves
     """
     assert len(appxCoef.shape) == 2
     k = Numeric.shape(appxCoef)[1]
     maxInd = Numeric.argmax(Numeric.absolute(appxCoef[:, 1:]), 1) + 1
     lowDiagOnes = Numeric.fromfunction(lambda i, j: i >= j, (k, k))
     coefSelector = Numeric.take(lowDiagOnes, maxInd, 0)
     return appxCoef * coefSelector
def ComputeNorm(ser, sampling, PSD):
     size = Numeric.shape(ser)[0]
     if(Numeric.shape(PSD)[0] != size/2):
         print "wrong size of psd: ", pdlen, Numeric.shape(PSD)
         sys.exit(1)
     ser2 = synthlisa.spect(ser, sampling,0)
     norm = 0.0
     indx = 0
    # fout = open("dChiTest.dat", 'w')
     for i in xrange(size/2-1):
        if (ser2[i][0] > 1.e-5 and ser2[i][0]<0.009):
	    norm += ser2[i][1]/PSD[i]
#	    record = str(ser2[i][0]) + "    " + str(2.0*norm) + "\n"
#	    fout.write(record)
	    indx = indx + 1
     norm = sqrt(2.0*norm)
 #    fout.close()     
     #print "indx = ", indx
     #norm = sqrt(2.0 *numpy.sum(ser2[1:,1]/PSD))
     return norm
Exemple #43
0
def getobs(snum,stime,observables,zerotime=0.0,display=0,forcepython=0):
    if len(Numeric.shape(observables)) == 0:
        obsobj = checkobs([observables])
                
        if obsobj and (not forcepython):
            array = Numeric.zeros(snum,dtype='d')

            if display:
                lisaswig.fastgetobsc(array,snum,stime,obsobj,zerotime)
            else:
                lisaswig.fastgetobs(array,snum,stime,obsobj,zerotime)
        else:
            if display:
                return getobscount(snum,stime,observables,zerotime)
            else:
                array = Numeric.zeros(snum,dtype='d')
            
                for i in Numeric.arange(0,snum):
                    array[i] = observables(zerotime+i*stime)
    else:
        obsobj = checkobs(observables)

        if obsobj and (not forcepython):
            obslen = Numeric.shape(observables)[0]
            array = Numeric.zeros((snum,obslen),dtype='d')

            if display:
                lisaswig.fastgetobsc(array,snum,stime,obsobj,zerotime)
            else:
                lisaswig.fastgetobs(array,snum,stime,obsobj,zerotime)
        else:
            if display:
                return getobscount(snum,stime,observables,zerotime)
            else:
                obslen = Numeric.shape(observables)[0]
                array = Numeric.zeros((snum,obslen),dtype='d')
            
                for i in Numeric.arange(0,snum):
                    for j in xrange(0,obslen):
                        array[i,j] = observables[j](zerotime+i*stime)
    return array
Exemple #44
0
def whitentime(series,patches=1):
    samples = Numeric.shape(series)[0]

    patlen = samples / patches
    
    for j in xrange(0,patches-1):
        integ = 0.0

        for i in xrange(0,patlen):
            next = series[j*patlen+i]
            series[j*patlen+i] = integ
            integ = integ+next
Exemple #45
0
    def slim(self):
        """
        Remove coordinates and atoms of ligand and receptor from memory,
        if they can be restored from file, compress contact matrix.
        @note: CALLED BEFORE PICKLING
        """
        self.lig_transformed = None
        self.pw_dist = None

##         self.ligandMatrix = self.ligandMatrix.tolist()

        if 'matrix' in self.info:
            del self.info['matrix']

        ## compress contact matrix array
        if self.contacts != None and \
               len(N.shape( self.contacts['result'] ) )==2:
            m = self.contacts['result']
            self.contacts['shape'] = N.shape( m )

            self.contacts['result'] = N.nonzero( N.ravel( m ) ).astype(N.Int32)
Exemple #46
0
    def test_FlexMaster(self):
        """TrajFlexMaster test"""
        from Biskit.MatrixPlot import MatrixPlot
        from numpy.oldnumeric.random_array import random

        assert len(hosts.cpus_all) > 0,\
               'Master requires at least 1 PVM node for initialisation.'

        traj_1 = T.load( T.testRoot() + '/lig_pcr_00/traj.dat' )
        traj_1 = traj2ensemble( traj_1 )

        ## create fake second trajectory by adding
        ## increasing noise to first
        frames = []
        for i in range( len( traj_1 ) ):
            f = traj_1.frames[i]
            d = N.zeros( N.shape( f ), N.Float32)
            if i > 0:
                d = random( N.shape( f ) ) * ((i / 10) + 1) 
            frames += [f + d]

        traj_2 = traj_1.clone()
        traj_2.frames = frames

        master = TrajFlexMaster( traj_1, traj_2,
                                 hosts=hosts.cpus_all,
                                 show_output= self.local,
                                 add_hosts=1,
                                 log=None,
                                 slaveLog=None,
                                 verbose= self.local,
                                 only_cross_member=0 )

        r = master.calculateResult( mirror=0 )

        if self.local:
            p = MatrixPlot( r, palette='plasma2', legend=1 )
            p.show()            
Exemple #47
0
def cat(a, b):
    """
    concatenate two arrays (the Numeric Python version is a mess)
    """
    #bruce comment 050518: these boolean tests look like bugs!
    # I bet they should be testing the number of entries being 0, or so.
    # So I added some debug code to warn us if this happens.
    if not a.any():
        if (_DEBUG_QUATS or debug_flags.atom_debug):
            print "_DEBUG_QUATS: cat(a, b) with false a -- is it right?", a
        return b
    if not b.any():
        if (_DEBUG_QUATS or debug_flags.atom_debug):
            print "_DEBUG_QUATS: cat(a, b) with false b -- is it right?", b
        return a
    r1 = Numeric.shape(a)
    r2 = Numeric.shape(b)
    if len(r1) == len(r2):
        return Numeric.concatenate((a, b))
    if len(r1) < len(r2):
        return Numeric.concatenate((Numeric.reshape(a,(1,) + r1), b))
    else:
        return Numeric.concatenate((a, Numeric.reshape(b,(1,) + r2)))