Exemple #1
0
def random2DArray( matrix, ranNr=1, mask=None):
    """
    Create randomized 2D array containing ones and zeros.

    :param matrix: matrix to randomize
    :type  matrix: 2D array
    :param mask: mask OR None (default: None)
    :type  mask: list(1|0)
    :param ranNr: number of matricies to add up (default: 1)
    :type  ranNr: integer

    :return: 2D array or |ranNr| added contact matricies
    :rtype:2D array

    :raise MathUtilError: if mask does not fit matrix
    """
    ## get shape of matrix
    a,b = N0.shape( matrix )

    ## get array from matrix that is to be randomized
    if mask is not None:
        if len(mask) == len( N0.ravel(matrix) ):
            array = N0.compress( mask, N0.ravel(matrix) )

        if len(mask) != len( N0.ravel(matrix) ):
            raise MathUtilError(
                'MatUtils.random2DArray - mask of incorrect length' +
                '\tMatrix length: %i Mask length: %i'\
                %(len( N0.ravel(matrix) ), len(mask)))

    if not mask:
        array = N0.ravel(matrix)

    ## number of ones and length of array
    nOnes = int( N0.sum( array ) )
    lenArray = len( array )
    ranArray = N0.zeros( lenArray )

    ## create random array
    for n in range(ranNr):
        ranArray += randomMask( nOnes, lenArray )

    ## blow up to size of original matix
    if mask is not None:
        r = N0.zeros(a*b)
        N0.put( r, N0.nonzero(mask), ranArray)
        return N0.reshape( r, (a,b) )

    if not mask:
        return  N0.reshape( ranArray, (a,b) )
Exemple #2
0
def random2DArray(matrix, ranNr=1, mask=None):
    """
    Create randomized 2D array containing ones and zeros.

    :param matrix: matrix to randomize
    :type  matrix: 2D array
    :param mask: mask OR None (default: None)
    :type  mask: list(1|0)
    :param ranNr: number of matricies to add up (default: 1)
    :type  ranNr: integer

    :return: 2D array or |ranNr| added contact matricies
    :rtype:2D array

    :raise MathUtilError: if mask does not fit matrix
    """
    ## get shape of matrix
    a, b = N0.shape(matrix)

    ## get array from matrix that is to be randomized
    if mask is not None:
        if len(mask) == len(N0.ravel(matrix)):
            array = N0.compress(mask, N0.ravel(matrix))

        if len(mask) != len(N0.ravel(matrix)):
            raise MathUtilError(
                'MatUtils.random2DArray - mask of incorrect length' +
                '\tMatrix length: %i Mask length: %i'\
                %(len( N0.ravel(matrix) ), len(mask)))

    if not mask:
        array = N0.ravel(matrix)

    ## number of ones and length of array
    nOnes = int(N0.sum(array))
    lenArray = len(array)
    ranArray = N0.zeros(lenArray)

    ## create random array
    for n in range(ranNr):
        ranArray += randomMask(nOnes, lenArray)

    ## blow up to size of original matix
    if mask is not None:
        r = N0.zeros(a * b)
        N0.put(r, N0.nonzero(mask), ranArray)
        return N0.reshape(r, (a, b))

    if not mask:
        return N0.reshape(ranArray, (a, b))
Exemple #3
0
def unpackBinaryMatrix( pcm, raveled=0 ):
    """
    Uncompress array of 0 and 1 that was compressed with :class:`packBinaryMatrix`.

    :param pcm: {'shape':(X,Y,..), 'nonzero':[int]}
    :type  pcm: dict
    :param raveled: return raveled (default: 0)
    :type  raveled: 1|0

    :return: N0.array(X by Y by ..) of int
    :rtype: 2D array
    """
    if type( pcm ) != dict:
        return pcm

    s = pcm['shape']

    m = N0.zeros( N0.cumproduct( s )[-1], N0.Int)
    pass  ## m.savespace( 1 )
    N0.put( m, pcm['nonzero'], 1 )

    if raveled:
        return m

    return N0.reshape( m, s )
Exemple #4
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 is not 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 is not None and \
           len( N0.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 = N0.zeros(lenRec * lenLig)
            N0.put(m, self.contacts['result'], 1)

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

        return self.contacts
Exemple #5
0
def unpackBinaryMatrix(pcm, raveled=0):
    """
    Uncompress array of 0 and 1 that was compressed with :class:`packBinaryMatrix`.

    :param pcm: {'shape':(X,Y,..), 'nonzero':[int]}
    :type  pcm: dict
    :param raveled: return raveled (default: 0)
    :type  raveled: 1|0

    :return: N0.array(X by Y by ..) of int
    :rtype: 2D array
    """
    if type(pcm) != dict:
        return pcm

    s = pcm['shape']

    m = N0.zeros(N0.cumproduct(s)[-1], N0.Int)
    pass  ## m.savespace( 1 )
    N0.put(m, pcm['nonzero'], 1)

    if raveled:
        return m

    return N0.reshape(m, s)
Exemple #6
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 is not 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 is not None and \
           len( N0.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 = N0.zeros( lenRec * lenLig )
            N0.put( m, self.contacts['result'], 1 )

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

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

        @return: N0.array( n_clusters x n_atoms_masked x 3 )
        @rtype: array
        """
        lenAtoms = N0.shape( self.fcCenters )[1] / 3
        return N0.reshape( self.fcCenters, ( self.n_clusters, lenAtoms, 3))
Exemple #8
0
    def __frame(self):
        """Collect next complete coordinate frame

        :return: coordinate frame
        :rtype: array
        """
        self.xyz = [self.__nextLine() for i in range(self.lines_per_frame)]

        return N0.reshape(self.xyz, (self.n, 3)).astype(N0.Float32)
Exemple #9
0
    def __frame( self ):
        """Collect next complete coordinate frame

        :return: coordinate frame
        :rtype: array
        """
        self.xyz = [ self.__nextLine() for i in range(self.lines_per_frame) ]

        return N0.reshape(self.xyz, ( self.n, 3 ) ).astype(N0.Float32)
Exemple #10
0
def listToMatrix( lcm ):
    """
    Convert result of :class:`matrixToList` back into Numeric array

    Note: Not used right now.

    :param lcm: {'shape':(int,..), 'lst':[..] }
    :type  lcm: dict    

    :return: array
    :rtype: 
    """
    if type( lcm ) != dict:
        return lcm

    s = lcm['shape']
    return N0.reshape( lcm['lst'], s )
Exemple #11
0
def listToMatrix(lcm):
    """
    Convert result of :class:`matrixToList` back into Numeric array

    Note: Not used right now.

    :param lcm: {'shape':(int,..), 'lst':[..] }
    :type  lcm: dict    

    :return: array
    :rtype: 
    """
    if type(lcm) != dict:
        return lcm

    s = lcm['shape']
    return N0.reshape(lcm['lst'], s)
Exemple #12
0
    def color_array( self, a, resetLimits=1 ):
        """
        :param a: array of float
        :type  a: array of float
        :param resetLimits: re-define color range on max and min of values
                            (default: 1)
        :type  resetLimits: 1|0
        
        :return: matrix of color codes with same dimensions as a
        :rtype: array of float
        """
        s = N0.shape( a )
        v = N0.ravel( a )

        r = self.colors( v, resetLimits=resetLimits )

        r = N0.reshape( r, s )

        return r
Exemple #13
0
    def pcMovie( self, ev, steps, factor=1., ref=0, morph=1 ):
        """
        Morph between the two extreme values of a single principal
        component.

        :param ev: EigenVector to visualize
        :type  ev: int
        :param steps: number of intermediate frames
        :type  steps: int
        :param factor: exageration factor (default: 1 = No exageration)
        :type  factor: float
        :param ref: take other eigenvecors from this frame (default: 1)
        :type  ref: int
        :param morph: morph between min and max (1) or take real values (0)
                      (default: 1)
        :type  morph: 1|0

        :return: Trajectory with frames visualizing the morphing.
        :rtype: Trajectory
        """
        fit = 1
        if self.pc is not None:
            fit = self.pc['fit']
        pc = self.getPca( fit=fit )

        ## eigenvectors (rows)
        U = pc['u']

        ## raveled and centered frames
        x_avg = N0.average(self.frames, 0)
        X = N0.array( [N0.ravel(x) for x in self.frames - x_avg] )

        ## ev'th eigenvector of reference frame
        alpha_0 = N0.dot( X[ref], U[ev] )

        ## list of deviations of ev'th eigenvector of each frame from ref
        alpha_range = N0.dot(X, U[ev]) - alpha_0

        ## get some representative alphas...
        if morph:
            a_min = factor * min(alpha_range)
            a_max = factor * max(alpha_range)
            delta = (a_max - a_min) / steps
            alpha_range = [ a_min + i*(delta) for i in range(0, steps) ]
        else:
            alpha_range = N0.sort( alpha_range )
            delta = len(alpha_range) / (steps * 1.0)
            alpha_range = [ alpha_range[ int(round( i*delta )) ]
                            for i in range(0,steps) ]

        ## scale ev'th eigenvector of ref with different alphas 
        Y = N0.array( [ X[ref] + alpha * U[ev] for alpha in alpha_range] )

        ## back convert to N x 3 coordinates
        Y = N0.reshape(Y, (Y.shape[0], -1, 3))
        Y = x_avg + Y

        result = self.__class__()
        result.ref = self.ref

        result.frames = Y
        return result