3
MdD                 @   sL   d Z ddlZddlmZ ddlmZmZmZm	Z	 dgZ
eddd
dZdS )u  Function for computing Local and global consistency algorithm by Zhou et al.

References
----------
Zhou, D., Bousquet, O., Lal, T. N., Weston, J., & Schölkopf, B. (2004).
Learning with local and global consistency.
Advances in neural information processing systems, 16(16), 321-328.
    N)not_implemented_for)_get_label_info_init_label_matrix
_propagate_predictlocal_and_global_consistencyZdirectedGz?   labelc          !      s>  yddl  W n. tk
r: } ztdd|W Y dd}~X nX yddlm W n. tk
rz } ztdd|W Y dd}~X nX  fdd	} fd
d}tj| }t| |\}}	|jd dkrtjd| d |jd }
|	jd }t	|
|}||||}|||||}|}x$|dkr.t
|||}|d8 }qW t||	}|S )u  Node classification by Local and Global Consistency

    Parameters
    ----------
    G : NetworkX Graph
    alpha : float
        Clamping factor
    max_iter : int
        Maximum number of iterations allowed
    label_name : string
        Name of target labels to predict

    Returns
    ----------
    predicted : array, shape = [n_samples]
        Array of predicted labels

    Raises
    ------
    NetworkXError
        If no nodes on `G` has `label_name`.

    Examples
    --------
    >>> from networkx.algorithms import node_classification
    >>> G = nx.path_graph(4)
    >>> G.nodes[0]["label"] = "A"
    >>> G.nodes[3]["label"] = "B"
    >>> G.nodes(data=True)
    NodeDataView({0: {'label': 'A'}, 1: {}, 2: {}, 3: {'label': 'B'}})
    >>> G.edges()
    EdgeView([(0, 1), (1, 2), (2, 3)])
    >>> predicted = node_classification.local_and_global_consistency(G)
    >>> predicted
    ['A', 'A', 'B', 'B']


    References
    ----------
    Zhou, D., Bousquet, O., Lal, T. N., Weston, J., & Schölkopf, B. (2004).
    Learning with local and global consistency.
    Advances in neural information processing systems, 16(16), 321-328.
    r   Nz/local_and_global_consistency() requires numpy: zhttp://numpy.org/ )sparsez/local_and_global_consistensy() requires scipy: zhttp://scipy.org/ c                sN   | j ddjd }d||dk<  jjd| dd}||j| j| }|S )a  Build propagation matrix of Local and global consistency

        Parameters
        ----------
        X : scipy sparse matrix, shape = [n_samples, n_samples]
            Adjacency matrix
        labels : array, shape = [n_samples, 2]
            Array of pairs of node id and label id
        alpha : float
            Clamping factor

        Returns
        ----------
        S : scipy sparse matrix, shape = [n_samples, n_samples]
            Propagation matrix

        r   )Zaxis   g      ?)offsets)sumAsqrtZdiagsdot)XlabelsalphadegreesZD2S)npr    ]/var/www/html/virt/lib/python3.6/site-packages/networkx/algorithms/node_classification/lgc.py_build_propagation_matrixP   s
    z?local_and_global_consistency.<locals>._build_propagation_matrixc                sD   | j d } j||f}d| ||dddf |dddf f< |S )a#  Build base matrix of Local and global consistency

        Parameters
        ----------
        X : scipy sparse matrix, shape = [n_samples, n_samples]
            Adjacency matrix
        labels : array, shape = [n_samples, 2]
            Array of pairs of node id and label id
        alpha : float
            Clamping factor
        n_classes : integer
            The number of classes (distinct labels) on the input graph

        Returns
        ----------
        B : array, shape = [n_samples, n_classes]
            Base matrix
        r   r   N)shapeZzeros)r   r   r   	n_classes	n_samplesB)r   r   r   _build_base_matrixh   s    
(z8local_and_global_consistency.<locals>._build_base_matrixz*No node on the input graph is labeled by 'z'.r   )ZnumpyImportErrorZscipyr   nxZto_scipy_sparse_matrixr   r   ZNetworkXErrorr   r   r   )Gr   Zmax_iterZ
label_nameer   r   r   r   Z
label_dictr   r   FPr   Zremaining_iterZ	predictedr   )r   r   r   r      s<    -




)r   r	   r
   )__doc__Znetworkxr!   Znetworkx.utils.decoratorsr   Z-networkx.algorithms.node_classification.utilsr   r   r   r   __all__r   r   r   r   r   <module>   s   