コード例 #1
0
ファイル: layout.py プロジェクト: katerinabc/networkx
def _sparse_spectral(A, dim=2):
    # Input adjacency matrix A
    # Uses sparse eigenvalue solver from scipy
    # Could use multilevel methods here, see Koren "On spectral graph drawing"

    try:
        from scipy.sparse import spdiags
        from scipy.sparse.linalg import eigen_symmetric
    except ImportError:
        raise ImportError("_sparse_spectral() scipy numpy: http://scipy.org/ ")
    try:
        nnodes, _ = A.shape
    except AttributeError:
        raise nx.NetworkXError(\
            "sparse_spectral() takes an adjacency matrix as input")

    # form Laplacian matrix
    data = np.asarray(A.sum(axis=1).T)
    D = spdiags(data, 0, nnodes, nnodes)
    L = D - A
    # number of Lanczos vectors for ARPACK solver, what is the right scaling?
    ncv = int(np.sqrt(nnodes))
    # return smalest dim+1 eigenvalues and eigenvectors
    eigenvalues, eigenvectors = eigen_symmetric(L,
                                                dim + 1,
                                                which='SM',
                                                ncv=ncv)
    index = np.argsort(eigenvalues)[1:dim + 1]  # 0 index is zero eigenvalue
    return np.real(eigenvectors[:, index])
コード例 #2
0
ファイル: layout.py プロジェクト: rafaelpiresm/projetos_gae
def _sparse_spectral(A,dim=2,weighted=True):
    # Input adjacency matrix A
    # Uses sparse eigenvalue solver from scipy
    # Could use multilevel methods here, see Koren "On spectral graph drawing" 
    
    try:
        from scipy.sparse import spdiags
        from scipy.sparse.linalg import eigen_symmetric
    except ImportError:
        raise ImportError, \
          "_sparse_spectral() scipy numpy: http://scipy.org/ "
    try:
        nnodes,_=A.shape
    except AttributeError:
        raise nx.NetworkXError(\
            "sparse_spectral() takes an adjacency matrix as input")
    
    # form Laplacian matrix
    data=np.asarray(A.sum(axis=1).T)
    D=spdiags(data,0,nnodes,nnodes)
    L=D-A
    # number of Lanczos vectors for ARPACK solver, what is the right scaling?
    ncv=int(np.sqrt(nnodes)) 
    # return smalest dim+1 eigenvalues and eigenvectors
    eigenvalues,eigenvectors=eigen_symmetric(L,dim+1,which='SM',ncv=ncv)
    index=np.argsort(eigenvalues)[1:dim+1] # 0 index is zero eigenvalue
    return np.real(eigenvectors[:,index])
コード例 #3
0
def dm_eigenvector(data,
                   k=0,
                   mean_center=True,
                   metricpar={},
                   verbose=True,
                   callback=None):
    r'''Return the :math:`k`-th eigenvector of the distance matrix.

The matrix of pairwise distances is symmetric, so it has an orthonormal basis of eigenvectors. The parameter :math:`k` can be either an integer or an array of integers (for multi-dimensional filter functions). The index is zero-based, and eigenvalues are sorted by absolute value, so :math:`k=0` returns the eigenvector corresponding to the largest eigenvalue in magnitude.

If `mean_center` is ``True``, the distance matrix is double-mean-centered before the eigenvalue decomposition.

Reference: [R6]_, subsection “Principal metric SVD filters”.
    '''
    # comp can be an integer or a list of integers
    # todo: check validity of comp
    if data.ndim == 1:
        # dissimilarity matrix
        assert metricpar == {}, ('No optional parameter is allowed for a '
                                 'dissimilarity matrix.')
        D = data
        N = n_obs(D)
    else:
        # vector data
        D = pdist(data, **metricpar)
        N = len(data)

    DD = squareform(D)
    del D

    if mean_center:
        md = DD.mean(axis=1)
        DD -= md
        DD -= (md - md.mean())[:, np.newaxis]

    karray = np.atleast_1d(k)
    assert karray.ndim == 1
    maxk = 1 + karray.max()

    if callback:
        callback('Computing: distance matrix eigenvectors.')

    if hasattr(spla, 'eigsh'):
        w, v = spla.eigsh(DD, k=maxk, which='LM')
    else:  # for SciPy < 0.9.0
        w, v = spla.eigen_symmetric(DD, k=maxk, which='LM')

    sortedorder = np.argsort(np.abs(w))[::-1]
    if verbose:
        print('Eigenvalues:\n{}'.format(w[sortedorder]))

    ret = v[:, sortedorder[k]]

    # normalize
    return ret / np.sqrt((ret * ret).sum(axis=0))
コード例 #4
0
    def _cov_eigen(self, X):
        """
        Perform direct computation of covariance matrix eigen{values,vectors},
        given a scipy.sparse matrix.
        """

        v, W = eigen_symmetric(X.T.dot(X) / X.shape[0], k=self.num_components)

        # The resulting components are in *ascending* order of eigenvalue, and
        # W contains eigenvectors in its *columns*, so we simply reverse both.
        return v[::-1], W[:, ::-1]
コード例 #5
0
ファイル: pca.py プロジェクト: Alienfeel/pylearn2
    def _cov_eigen(self, X):
        """
        Perform direct computation of covariance matrix eigen{values,vectors},
        given a scipy.sparse matrix.
        """

        v, W = eigen_symmetric(X.T.dot(X) / X.shape[0], k=self.num_components)

        # The resulting components are in *ascending* order of eigenvalue, and
        # W contains eigenvectors in its *columns*, so we simply reverse both.
        return v[::-1], W[:, ::-1]
コード例 #6
0
ファイル: filters.py プロジェクト: Sandy4321/mapper
def dm_eigenvector(data, k=0, mean_center=True,
        metricpar={}, verbose=True, callback=None):
    r'''Return the :math:`k`-th eigenvector of the distance matrix.

The matrix of pairwise distances is symmetric, so it has an orthonormal basis of eigenvectors. The parameter :math:`k` can be either an integer or an array of integers (for multi-dimensional filter functions). The index is zero-based, and eigenvalues are sorted by absolute value, so :math:`k=0` returns the eigenvector corresponding to the largest eigenvalue in magnitude.

If `mean_center` is ``True``, the distance matrix is double-mean-centered before the eigenvalue decomposition.

Reference: [R6]_, subsection “Principal metric SVD filters”.
    '''
    # comp can be an integer or a list of integers
    # todo: check validity of comp
    if data.ndim==1:
        # dissimilarity matrix
        assert metricpar=={}, ('No optional parameter is allowed for a '
                               'dissimilarity matrix.')
        D = data
        N = n_obs(D)
    else:
        # vector data
        D = pdist(data, **metricpar)
        N = len(data)

    DD = squareform(D)
    del D

    if mean_center:
        md = DD.mean(axis=1)
        DD -= md
        DD -= (md-md.mean())[:,np.newaxis]

    karray = np.atleast_1d(k)
    assert karray.ndim == 1
    maxk = 1 + karray.max()

    if callback:
        callback('Computing: distance matrix eigenvectors.')

    if hasattr(spla, 'eigsh'):
        w, v = spla.eigsh(DD, k=maxk, which='LM')
    else: # for SciPy < 0.9.0
        w, v = spla.eigen_symmetric(DD, k=maxk, which='LM')

    sortedorder = np.argsort(np.abs(w))[::-1]
    if verbose:
        print('Eigenvalues:\n{}'.format(w[sortedorder]))

    ret = v[:,sortedorder[k]]

    # normalize
    return ret / np.sqrt((ret*ret).sum(axis=0))
コード例 #7
0
def _svd_left(X):
    """
    Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does
    not allow computation of rank(X) SVD.

    :param X: The input sparse matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
    """
    XtX = dot(X.T, X)
    if X.shape[1] > 1:
        numeric = int(scipy.version.version.replace(".", ""))
        if '0.9' in scipy.version.version or '0.10' in scipy.version.version or '0.11' in scipy.version.version or numeric > 90:
            # In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric
            # routine was renamed to eigsh
            # http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
            try:
                val, v_vec = sla.eigsh(XtX, k=X.shape[1] - 1)
            except sla.ArpackNoConvergence as err:
                # If eigenvalue iteration fails to converge, partially
                # converged results can be accessed
                val = err.eigenvalues
                v_vec = err.eigenvectors
        else:
            val, v_vec = sla.eigen_symmetric(XtX, k=X.shape[1] - 1)
    else:
        val, v_vec = nla.eigh(XtX.todense())
    # remove insignificant eigenvalues
    keep = np.where(val > 1e-7)[0]
    v_vec = v_vec[:, keep]
    val = val[keep]
    # sort eigen vectors (descending)
    idx = np.argsort(val)[::-1]
    val = val[idx]
    # construct V
    V = sp.csr_matrix(v_vec[:, idx])
    # compute S
    tmp_val = np.sqrt(val)
    tmp_l = len(idx)
    S = sp.spdiags(tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
    # compute U from inverse of S
    inv_S = sp.spdiags(1. / tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
    U = X * V * inv_S
    V = V.T
    return U, S, V
コード例 #8
0
ファイル: linalg.py プロジェクト: marinkaz/nimfa
def _svd_left(X):
    """
    Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does
    not allow computation of rank(X) SVD.

    :param X: The input sparse matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
    """
    XtX = dot(X.T, X)
    if X.shape[1] > 1:
        if '0.9' in scipy.version.version or '0.10' in scipy.version.version or '0.11' in scipy.version.version:
            # In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric
            # routine was renamed to eigsh
            # http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
            try:
                val, v_vec = sla.eigsh(XtX, k=X.shape[1] - 1)
            except sla.ArpackNoConvergence as err:
                # If eigenvalue iteration fails to converge, partially
                # converged results can be accessed
                val = err.eigenvalues
                v_vec = err.eigenvectors
        else:
            val, v_vec = sla.eigen_symmetric(XtX, k=X.shape[1] - 1)
    else:
        val, v_vec = nla.eigh(XtX.todense())
    # remove insignificant eigenvalues
    keep = np.where(val > 1e-7)[0]
    v_vec = v_vec[:, keep]
    val = val[keep]
    # sort eigen vectors (descending)
    idx = np.argsort(val)[::-1]
    val = val[idx]
    # construct V
    V = sp.csr_matrix(v_vec[:, idx])
    # compute S
    tmp_val = np.sqrt(val)
    tmp_l = len(idx)
    S = sp.spdiags(tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
    # compute U from inverse of S
    inv_S = sp.spdiags(1. / tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
    U = X * V * inv_S
    V = V.T
    return U, S, V
コード例 #9
0
ファイル: linalg.py プロジェクト: bjzu/MF
def _svd_left(X):
    """
    Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does not allow computation of rank(X) SVD.
    
    :param X: The input sparse matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
    """
    XtX = dot(X.T, X)
    if X.shape[1] > 1:
        if scipy.version.version == '0.9.0':
            # In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric routine was renamed to eigsh
            # see http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
            try:
                val, v_vec = sla.eigsh(XtX, k = X.shape[1] - 1)
            except sla.ArpackNoConvergence, err:
                # If eigenvalue iteration fails to converge, partially converged results can be accessed
                val = err.eigenvalues
                v_vec = err.eigenvectors
        else:
            val, v_vec = sla.eigen_symmetric(XtX, k = X.shape[1] - 1)
コード例 #10
0
def _svd_left(X):
    """
    Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does not allow computation of rank(X) SVD.

    :param X: The input sparse matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
    """
    XtX = dot(X.T, X)
    if X.shape[1] > 1:
        if '0.9' in scipy.version.version or '0.10' in scipy.version.version or '0.11' in scipy.version.version:
            # In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric routine was renamed to eigsh
            # see
            # http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
            try:
                val, v_vec = sla.eigsh(XtX, k=X.shape[1] - 1)
            except sla.ArpackNoConvergence, err:
                # If eigenvalue iteration fails to converge, partially
                # converged results can be accessed
                val = err.eigenvalues
                v_vec = err.eigenvectors
        else:
            val, v_vec = sla.eigen_symmetric(XtX, k=X.shape[1] - 1)
コード例 #11
0
ファイル: gnm.py プロジェクト: sixpi/ProDy
    def calcModes(self, n_modes=20, zeros=False, turbo=True, hinges=True):
        """Calculate normal modes.  This method uses :func:`scipy.linalg.eigh`
        function to diagonalize the Kirchhoff matrix. When Scipy is not found,
        :func:`numpy.linalg.eigh` is used.

        :arg n_modes: number of non-zero eigenvalues/vectors to calculate.
              If ``None`` is given, all modes will be calculated.
        :type n_modes: int or None, default is 20

        :arg zeros: If ``True``, modes with zero eigenvalues will be kept.
        :type zeros: bool, default is ``False``

        :arg turbo: Use a memory intensive, but faster way to calculate modes.
        :type turbo: bool, default is ``True``

        :arg hinges: Identify hinge sites after modes are computed.
        :type hinges: bool, default is ``True``
        """

        if self._kirchhoff is None:
            raise ValueError('Kirchhoff matrix is not built or set')
        assert n_modes is None or isinstance(n_modes, int) and n_modes > 0, \
            'n_modes must be a positive integer'
        assert isinstance(zeros, bool), 'zeros must be a boolean'
        assert isinstance(turbo, bool), 'turbo must be a boolean'
        linalg = importLA()
        start = time.time()
        shift = 0
        if linalg.__package__.startswith('scipy'):
            if n_modes is None:
                eigvals = None
                n_modes = self._dof
            else:
                if n_modes >= self._dof:
                    eigvals = None
                    n_modes = self._dof
                else:
                    eigvals = (0, n_modes + shift)
            if eigvals:
                turbo = False
            if isinstance(self._kirchhoff, np.ndarray):
                values, vectors = linalg.eigh(self._kirchhoff, turbo=turbo,
                                              eigvals=eigvals)
            else:
                try:
                    from scipy.sparse import linalg as scipy_sparse_la
                except ImportError:
                    raise ImportError('failed to import scipy.sparse.linalg, '
                                      'which is required for sparse matrix '
                                      'decomposition')
                try:
                    values, vectors = (
                        scipy_sparse_la.eigsh(self._kirchhoff,
                                              k=n_modes + 1, which='SA'))
                except:
                    values, vectors = (
                        scipy_sparse_la.eigen_symmetric(self._kirchhoff,
                                                        k=n_modes + 1,
                                                        which='SA'))
        else:
            if n_modes is not None:
                LOGGER.info('Scipy is not found, all modes are calculated.')
            values, vectors = linalg.eigh(self._kirchhoff)
        n_zeros = sum(values < ZERO)
        if n_zeros < 1:
            LOGGER.warning('Less than 1 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        elif n_zeros > 1:
            LOGGER.warning('More than 1 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        if zeros:
            shift = -1
        self._eigvals = values[1+shift:]
        self._vars = 1 / self._eigvals
        self._trace = self._vars.sum()
        self._array = vectors[:, 1+shift:]
        self._n_modes = len(self._eigvals)
        if hinges:
            self.calcHinges()
        LOGGER.debug('{0} modes were calculated in {1:.2f}s.'
                     .format(self._n_modes, time.time()-start))
コード例 #12
0
ファイル: anm.py プロジェクト: crosvera/ProDy
    def calcModes(self, n_modes=20, zeros=False, turbo=True):
        """Calculate normal modes.  This method uses :func:`scipy.linalg.eigh` 
        function to diagonalize the Hessian matrix. When Scipy is not found, 
        :func:`numpy.linalg.eigh` is used.

        :arg n_modes: number of non-zero eigenvalues/vectors to calculate. 
            If ``None`` is given, all modes will be calculated. 
        :type n_modes: int or None, default is 20
        
        :arg zeros: If ``True``, modes with zero eigenvalues will be kept.
        :type zeros: bool, default is ``False``
        
        :arg turbo: Use a memory intensive, but faster way to calculate modes.
        :type turbo: bool, default is ``True``
        """

        if self._hessian is None:
            raise ValueError("Hessian matrix is not built or set")
        assert n_modes is None or isinstance(n_modes, int) and n_modes > 0, "n_modes must be a positive integer"
        assert isinstance(zeros, bool), "zeros must be a boolean"
        assert isinstance(turbo, bool), "turbo must be a boolean"
        linalg = importLA()
        start = time.time()
        shift = 5
        if linalg.__package__.startswith("scipy"):
            if n_modes is None:
                eigvals = None
                n_modes = self._dof
            else:
                if n_modes >= self._dof:
                    eigvals = None
                    n_modes = self._dof
                else:
                    eigvals = (0, n_modes + shift)
            if eigvals:
                turbo = False
            if isinstance(self._hessian, np.ndarray):
                values, vectors = linalg.eigh(self._hessian, turbo=turbo, eigvals=eigvals)
            else:
                try:
                    from scipy.sparse import linalg as scipy_sparse_la
                except ImportError:
                    raise ImportError(
                        "failed to import scipy.sparse.linalg, " "which is required for sparse matrix " "decomposition"
                    )
                try:
                    values, vectors = scipy_sparse_la.eigsh(self._hessian, k=n_modes + 6, which="SA")
                except:
                    values, vectors = scipy_sparse_la.eigen_symmetric(self._hessian, k=n_modes + 6, which="SA")

        else:
            values, vectors = linalg.eigh(self._hessian)
        n_zeros = sum(values < ZERO)
        if n_zeros < 6:
            LOGGER.warning("Less than 6 zero eigenvalues are calculated.")
            shift = n_zeros - 1
        elif n_zeros > 6:
            LOGGER.warning("More than 6 zero eigenvalues are calculated.")
            shift = n_zeros - 1
        if zeros:
            shift = -1
        self._eigvals = values[1 + shift :]
        self._vars = 1 / self._eigvals
        self._trace = self._vars.sum()
        self._array = vectors[:, 1 + shift :]
        self._n_modes = len(self._eigvals)
        LOGGER.debug("{0:d} modes were calculated in {1:.2f}s." "".format(self._n_modes, time.time() - start))
コード例 #13
0
ファイル: anm.py プロジェクト: fongchun/ProDy
    def calcModes(self, n_modes=20, zeros=False, turbo=True):
        """Calculate normal modes.  This method uses :func:`scipy.linalg.eigh`
        function to diagonalize the Hessian matrix. When Scipy is not found,
        :func:`numpy.linalg.eigh` is used.

        :arg n_modes: number of non-zero eigenvalues/vectors to calculate.
            If **None** or ``'all'`` is given, all modes will be calculated.
        :type n_modes: int or None, default is 20

        :arg zeros: If **True**, modes with zero eigenvalues will be kept.
        :type zeros: bool, default is **True**

        :arg turbo: Use a memory intensive, but faster way to calculate modes.
        :type turbo: bool, default is **True**
        """

        if self._hessian is None:
            raise ValueError('Hessian matrix is not built or set')
        if str(n_modes).lower() == 'all':
            n_modes = None
        assert n_modes is None or isinstance(n_modes, int) and n_modes > 0, \
            'n_modes must be a positive integer'
        assert isinstance(zeros, bool), 'zeros must be a boolean'
        assert isinstance(turbo, bool), 'turbo must be a boolean'
        self._clear()
        linalg = importLA()
        LOGGER.timeit('_anm_calc_modes')
        shift = 5
        if linalg.__package__.startswith('scipy'):
            if n_modes is None:
                eigvals = None
                n_modes = self._dof
            else:
                if n_modes >= self._dof:
                    eigvals = None
                    n_modes = self._dof
                else:
                    eigvals = (0, n_modes + shift)
            if eigvals:
                turbo = False
            if isinstance(self._hessian, np.ndarray):
                values, vectors = linalg.eigh(self._hessian, turbo=turbo,
                                              eigvals=eigvals)
            else:
                try:
                    from scipy.sparse import linalg as scipy_sparse_la
                except ImportError:
                    raise ImportError('failed to import scipy.sparse.linalg, '
                                      'which is required for sparse matrix '
                                      'decomposition')
                try:
                    values, vectors = (
                        scipy_sparse_la.eigsh(self._hessian, k=n_modes+6,
                                              which='SA'))
                except:
                    values, vectors = (
                        scipy_sparse_la.eigen_symmetric(self._hessian,
                                                        k=n_modes+6,
                                                        which='SA'))

        else:
            if n_modes is not None:
                LOGGER.info('Scipy is not found, all modes are calculated.')
            values, vectors = np.linalg.eigh(self._hessian)
        n_zeros = sum(values < ZERO)

        if n_zeros < 6:
            LOGGER.warning('Less than 6 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        elif n_zeros > 6:
            LOGGER.warning('More than 6 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        if zeros:
            shift = -1
        if n_zeros > n_modes:
            self._eigvals = values[1+shift:]
        else:
            self._eigvals = values[1+shift:]
        self._vars = 1 / self._eigvals
        self._trace = self._vars.sum()
        
        if shift:
            self._array = vectors[:, 1+shift:].copy()
        else:
            self._array = vectors
        self._n_modes = len(self._eigvals)
        LOGGER.report('{0} modes were calculated in %.2fs.'
                     .format(self._n_modes), label='_anm_calc_modes')
コード例 #14
0
def graph_Laplacian(data, eps, n=1, k=1, weighted_edges=False, sigma_eps=1.,
                    normalized=True,
                    metricpar={}, verbose=True,
                    callback=None):
    r'''Graph Laplacian of the neighborhood graph.

* First, if *k* is 1, form the *eps*-neighborhood graph of the data set: vertices are the data points; two points are connected if their distance is at most *eps*.

* Alternatively, if *k* is greater than 1, form the neighborhood graph from the
  :math:`k`-th nearest neighbors of each point. Each point counts as its first
  nearest neighbor, so feasible values start with :math:`k=2`.

* If *weighted_edges* is ``False``, each edge gets weight 1. Otherwise, each
  edge is weighted with

  .. math::

    \exp\left(-\frac{d^2}{2\sigma^2}\right),

  where :math:`\sigma=\mathtt{eps}\cdot\mathtt{sigma\_eps}` and :math:`d` is
  the distance between the two points.

* Form the graph Laplacian. The graph Laplacian is a self-adjoint operator on
  the real vector space spanned by the vertices and can thus be described by a
  symmetric matrix :math:`L`:

  If *normalized* is false, :math:`L` is closely related to the adjacency matrix of the graph: it has entries :math:`-w(i,j)` whenever nodes :math:`i` and :math:`j` are connected by an edge of weight :math:`w(i,j)` and zero if there is no edge. The :math:`i`-th diagonal entry holds the degree :math:`\deg(i)` of the corresponding vertex, so that row and column sums are zero.

  If *normalized* is true, each row :math:`i` of :math:`L` is additionally scaled by :math:`1/\sqrt{\deg(i)}`, and so is each column. This destroys the zero row and column sums but preserves symmetry.

* Return the :math:`n`-th eigenvector of the graph Laplacian. The index is 0-based: the 0-th eigenvector is constant on all vertices, corresponding to the eigenvalue 0. :math:`n=1` returns the Fiedler vector, which is the second smallest eigenvector after 0.

The normalized variant seems to give consistently better results, so this is always chosen in the GUI. However, this experience is based on few examples only, so please do not hesitate to also try the non-normalized version if there is a reason for it.

Reference: [R9]_; see especially Section 6.3 for normalization.'''
    assert n>=1, 'The rank of the eigenvector must be positive.'
    assert isinstance(k, int)
    assert k>=1
    if data.ndim==1:
        # dissimilarity matrix
        assert metricpar=={}, ('No optional parameter is allowed for a '
                               'dissimilarity matrix.')
        D = data
        N = n_obs(D)
    else:
        # vector data
        D = pdist(data, **metricpar)
        N = len(data)
    if callback:
        callback('Computing: neighborhood graph.')
    rowstart, targets, weights = \
        neighborhood_graph(D, k, eps, diagonal=True,
                           verbose=verbose, callback=callback)

    c = ncomp(rowstart, targets)
    if (c>1):
        print('The neighborhood graph has {0} components. Return zero values.'.
              format(c))
        return zero_filter(data)

    weights = Laplacian(rowstart, targets, weights, weighted_edges,
                        eps, sigma_eps, normalized)

    L = scipy.sparse.csr_matrix((weights, targets, rowstart))
    del weights, targets, rowstart

    if callback:
        callback('Computing: eigenvectors.')

    assert n<N, ('The rank of the eigenvector must be smaller than the number '
                 'of data points.')

    if hasattr(spla, 'eigsh'):
        w, v = spla.eigsh(L, k=n+1, which='SA')
    else: # for SciPy < 0.9.0
        w, v = spla.eigen_symmetric(L, k=n+1, which='SA')
    # Strange: computing more eigenvectors seems faster.
    #w, v = spla.eigsh(L, k=n+1, sigma=0., which='LM')
    if verbose:
        print('Eigenvalues: {0}.'.format(w))
    order = np.argsort(w)
    if w[order[0]]<0 and w[order[1]]<abs(w[order[0]]):
        raise RuntimeError('Negative eigenvalue of the graph Laplacian found: {0}'.format(w))

    return v[:,order[n]]
コード例 #15
0
ファイル: filters.py プロジェクト: Sandy4321/mapper
def graph_Laplacian(data, eps, n=1, k=1, weighted_edges=False, sigma_eps=1.,
                    normalized=True,
                    metricpar={}, verbose=True,
                    callback=None):
    r'''Graph Laplacian of the neighborhood graph.

* First, if *k* is 1, form the *eps*-neighborhood graph of the data set: vertices are the data points; two points are connected if their distance is at most *eps*.

* Alternatively, if *k* is greater than 1, form the neighborhood graph from the
  :math:`k`-th nearest neighbors of each point. Each point counts as its first
  nearest neighbor, so feasible values start with :math:`k=2`.

* If *weighted_edges* is ``False``, each edge gets weight 1. Otherwise, each
  edge is weighted with

  .. math::

    \exp\left(-\frac{d^2}{2\sigma^2}\right),

  where :math:`\sigma=\mathtt{eps}\cdot\mathtt{sigma\_eps}` and :math:`d` is
  the distance between the two points.

* Form the graph Laplacian. The graph Laplacian is a self-adjoint operator on
  the real vector space spanned by the vertices and can thus be described by a
  symmetric matrix :math:`L`:

  If *normalized* is false, :math:`L` is closely related to the adjacency matrix of the graph: it has entries :math:`-w(i,j)` whenever nodes :math:`i` and :math:`j` are connected by an edge of weight :math:`w(i,j)` and zero if there is no edge. The :math:`i`-th diagonal entry holds the degree :math:`\deg(i)` of the corresponding vertex, so that row and column sums are zero.

  If *normalized* is true, each row :math:`i` of :math:`L` is additionally scaled by :math:`1/\sqrt{\deg(i)}`, and so is each column. This destroys the zero row and column sums but preserves symmetry.

* Return the :math:`n`-th eigenvector of the graph Laplacian. The index is 0-based: the 0-th eigenvector is constant on all vertices, corresponding to the eigenvalue 0. :math:`n=1` returns the Fiedler vector, which is the second smallest eigenvector after 0.

The normalized variant seems to give consistently better results, so this is always chosen in the GUI. However, this experience is based on few examples only, so please do not hesitate to also try the non-normalized version if there is a reason for it.

Reference: [R9]_; see especially Section 6.3 for normalization.'''
    assert n>=1, 'The rank of the eigenvector must be positive.'
    assert isinstance(k, int)
    assert k>=1
    if data.ndim==1:
        # dissimilarity matrix
        assert metricpar=={}, ('No optional parameter is allowed for a '
                               'dissimilarity matrix.')
        D = data
        N = n_obs(D)
    else:
        # vector data
        D = pdist(data, **metricpar)
        N = len(data)
    if callback:
        callback('Computing: neighborhood graph.')
    rowstart, targets, weights = \
        neighborhood_graph(D, k, eps, diagonal=True,
                           verbose=verbose, callback=callback)

    c = ncomp(rowstart, targets)
    if (c>1):
        print('The neighborhood graph has {0} components. Return zero values.'.
              format(c))
        return zero_filter(data)

    weights = Laplacian(rowstart, targets, weights, weighted_edges,
                        eps, sigma_eps, normalized)

    L = scipy.sparse.csr_matrix((weights, targets, rowstart))
    del weights, targets, rowstart

    if callback:
        callback('Computing: eigenvectors.')

    assert n<N, ('The rank of the eigenvector must be smaller than the number '
                 'of data points.')

    if hasattr(spla, 'eigsh'):
        w, v = spla.eigsh(L, k=n+1, which='SA')
    else: # for SciPy < 0.9.0
        w, v = spla.eigen_symmetric(L, k=n+1, which='SA')
    # Strange: computing more eigenvectors seems faster.
    #w, v = spla.eigsh(L, k=n+1, sigma=0., which='LM')
    if verbose:
        print('Eigenvalues: {0}.'.format(w))
    order = np.argsort(w)
    if w[order[0]]<0 and w[order[1]]<abs(w[order[0]]):
        raise RuntimeError('Negative eigenvalue of the graph Laplacian found: {0}'.format(w))

    return v[:,order[n]]
コード例 #16
0
ファイル: gnm.py プロジェクト: brezal/ProDy
    def calcModes(self, n_modes=20, zeros=False, turbo=True, hinges=True):
        """Calculate normal modes.  This method uses :func:`scipy.linalg.eigh`
        function to diagonalize the Kirchhoff matrix. When Scipy is not found,
        :func:`numpy.linalg.eigh` is used.

        :arg n_modes: number of non-zero eigenvalues/vectors to calculate.
              If **None** or ``'all'`` is given, all modes will be calculated.
        :type n_modes: int or None, default is 20

        :arg zeros: If **True**, modes with zero eigenvalues will be kept.
        :type zeros: bool, default is **True**

        :arg turbo: Use a memory intensive, but faster way to calculate modes.
        :type turbo: bool, default is **True**

        :arg hinges: Identify hinge sites after modes are computed.
        :type hinges: bool, default is **True**
        """

        if self._kirchhoff is None:
            raise ValueError('Kirchhoff matrix is not built or set')
        if str(n_modes).lower() == 'all':
            n_modes = None
        assert n_modes is None or isinstance(n_modes, int) and n_modes > 0, \
            'n_modes must be a positive integer'
        assert isinstance(zeros, bool), 'zeros must be a boolean'
        assert isinstance(turbo, bool), 'turbo must be a boolean'
        self._clear()
        linalg = importLA()
        start = time.time()
        shift = 0
        if linalg.__package__.startswith('scipy'):
            if n_modes is None:
                eigvals = None
                n_modes = self._dof
            else:
                if n_modes >= self._dof:
                    eigvals = None
                    n_modes = self._dof
                else:
                    eigvals = (0, n_modes + shift)
            if eigvals:
                turbo = False
            if isinstance(self._kirchhoff, np.ndarray):
                values, vectors = linalg.eigh(self._kirchhoff, turbo=turbo,
                                              eigvals=eigvals)
            else:
                try:
                    from scipy.sparse import linalg as scipy_sparse_la
                except ImportError:
                    raise ImportError('failed to import scipy.sparse.linalg, '
                                      'which is required for sparse matrix '
                                      'decomposition')
                try:
                    values, vectors = (
                        scipy_sparse_la.eigsh(self._kirchhoff,
                                              k=n_modes + 1, which='SA'))
                except:
                    values, vectors = (
                        scipy_sparse_la.eigen_symmetric(self._kirchhoff,
                                                        k=n_modes + 1,
                                                        which='SA'))
        else:
            if n_modes is not None:
                LOGGER.info('Scipy is not found, all modes are calculated.')
            values, vectors = linalg.eigh(self._kirchhoff)
        n_zeros = sum(values < ZERO)
        if n_zeros < 1:
            LOGGER.warning('Less than 1 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        elif n_zeros > 1:
            LOGGER.warning('More than 1 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        if zeros:
            shift = -1
        self._eigvals = values[1+shift:]
        self._vars = 1 / self._eigvals
        self._trace = self._vars.sum()
        self._array = vectors[:, 1+shift:]
        self._n_modes = len(self._eigvals)
        if hinges:
            self.calcHinges()
        LOGGER.debug('{0} modes were calculated in {1:.2f}s.'
                     .format(self._n_modes, time.time()-start))
コード例 #17
0
ファイル: anm.py プロジェクト: hrch3n/cNMA
    def calcModes(self, n_modes=20, zeros=False, turbo=True):
        """Calculate normal modes.  This method uses :func:`scipy.linalg.eigh`
        function to diagonalize the Hessian matrix. When Scipy is not found,
        :func:`numpy.linalg.eigh` is used.

        :arg n_modes: number of non-zero eigenvalues/vectors to calculate.
            If ``None`` is given, all modes will be calculated.
        :type n_modes: int or None, default is 20

        :arg zeros: If ``True``, modes with zero eigenvalues will be kept.
        :type zeros: bool, default is ``False``

        :arg turbo: Use a memory intensive, but faster way to calculate modes.
        :type turbo: bool, default is ``True``
        """

        if self._hessian is None:
            raise ValueError('Hessian matrix is not built or set')
        assert n_modes is None or isinstance(n_modes, int) and n_modes > 0, \
            'n_modes must be a positive integer'
        assert isinstance(zeros, bool), 'zeros must be a boolean'
        assert isinstance(turbo, bool), 'turbo must be a boolean'
        linalg = importLA()
        LOGGER.timeit('_anm_calc_modes')
        shift = 5
        if linalg.__package__.startswith('scipy'):
            if n_modes is None:
                eigvals = None
                n_modes = self._dof
            else:
                if n_modes >= self._dof:
                    eigvals = None
                    n_modes = self._dof
                else:
                    eigvals = (0, n_modes + shift)
            if eigvals:
                turbo = False
            if isinstance(self._hessian, np.ndarray):
                values, vectors = linalg.eigh(self._hessian, turbo=turbo,
                                              eigvals=eigvals)
            else:
                try:
                    from scipy.sparse import linalg as scipy_sparse_la
                except ImportError:
                    raise ImportError('failed to import scipy.sparse.linalg, '
                                      'which is required for sparse matrix '
                                      'decomposition')
                try:
                    values, vectors = (
                        scipy_sparse_la.eigsh(self._hessian, k=n_modes+6,
                                              which='SA'))
                except:
                    values, vectors = (
                        scipy_sparse_la.eigen_symmetric(self._hessian,
                                                        k=n_modes+6,
                                                        which='SA'))

        else:
            if n_modes is not None:
                LOGGER.info('Scipy is not found, all modes are calculated.')
            values, vectors = linalg.eigh(self._hessian)
        n_zeros = sum(values < ZERO)
        if n_zeros < 6:
            LOGGER.warning('Less than 6 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        elif n_zeros > 6:
            LOGGER.warning('More than 6 zero eigenvalues are calculated.')
            shift = n_zeros - 1
        if zeros:
            shift = -1
        self._eigvals = values[1+shift:]
        self._vars = 1 / self._eigvals
        self._trace = self._vars.sum()
        if shift:
            self._array = vectors[:, 1+shift:].copy()
        else:
            self._array = vectors
        self._n_modes = len(self._eigvals)
        LOGGER.report('{0} modes were calculated in %.2fs.'
                     .format(self._n_modes), label='_anm_calc_modes')