U
    td@                     @   sf   d dl mZmZmZmZ d dlmZ dZedd dfddZddd	Z	dddZ
dddZdddZdS )    )GET_ADJACENCY_BOTHGET_ADJACENCY_LOWERGET_ADJACENCY_UPPER	GraphBase)Matrix)_get_adjacency_get_adjacency_sparse_get_adjlist_get_incidence_get_inclistNFc           
         st  |t kr.|tkr.|tkr.|| }}|dkr.t}|rPtt||}|d8 }|S |dkrhtt|S |j kr~td fddt	
 D } rʈjD ]}|| ||j |j< qt|S |tkrjD ].}|j\}}	|| || |	< || ||	 |< qn`|tkrBjD ]"}|| |t|j t|j< qn*jD ]"}|| |t|j t|j< qHt|S )a   Returns the adjacency matrix of a graph.

    @param type: either C{GET_ADJACENCY_LOWER} (uses the lower
      triangle of the matrix) or C{GET_ADJACENCY_UPPER}
      (uses the upper triangle) or C{GET_ADJACENCY_BOTH}
      (uses both parts). Ignored for directed graphs.
    @param attribute: if C{None}, returns the ordinary adjacency
      matrix. When the name of a valid edge attribute is given
      here, the matrix returned will contain the default value
      at the places where there is no edge or the value of the
      given attribute where there is an edge. Multiple edges are
      not supported, the value written in the matrix in this case
      will be unpredictable. This parameter is ignored if
      I{eids} is C{True}
    @param default: the default value written to the cells in the
      case of adjacency matrices with attributes.
    @param eids: specifies whether the edge IDs should be returned
      in the adjacency matrix. Since zero is a valid edge ID, the
      cells in the matrix that correspond to unconnected vertex
      pairs will contain -1 instead of 0 if I{eids} is C{True}.
      If I{eids} is C{False}, the number of edges will be returned
      in the matrix for each vertex pair.
    @return: the adjacency matrix as a L{Matrix}.
    N   Attribute does not existc                    s   g | ]} g   qS  )vcount).0_defaultselfr   I/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/adjacency.py
<listcomp>C   s     z"_get_adjacency.<locals>.<listcomp>)r   r   r   r   r   Zget_adjacencyesattribute_names
ValueErrorranger   is_directedsourcetargettupleminmax)
r   type	attributer   Zeidsresultdataedger   r   r   r   r   r      sB    






"
 r   c                 C   s   zddl m} W n tk
r,   tdY nX |  }|dkrNdgt| }n || j krdtd| j| }|  }|j	|t
t| f||fd}|  s|||dj ||dj }|S )	a  Returns the adjacency matrix of a graph as a SciPy CSR matrix.

    @param attribute: if C{None}, returns the ordinary adjacency
      matrix. When the name of a valid edge attribute is given
      here, the matrix returned will contain the default value
      at the places where there is no edge or the value of the
      given attribute where there is an edge.
    @return: the adjacency matrix as a C{scipy.sparse.csr_matrix}.
    r   )sparsez6You should install scipy in order to use this functionNr   r   )shape)Zscipyr&   ImportErrorZget_edgelistlenr   r   r   r   Z
csr_matrixlistzipr   ZtriuTZtril)r   r"   r&   edgesweightsNZmtxr   r   r   r   Y   s    

 r   outc                    s    fddt  D S )a  Returns the adjacency list representation of the graph.

    The adjacency list representation is a list of lists. Each item of the
    outer list belongs to a single vertex of the graph. The inner list
    contains the neighbors of the given vertex.

    @param mode: if C{"out"}, returns the successors of the vertex. If
      C{"in"}, returns the predecessors of the vertex. If C{"all""}, both
      the predecessors and the successors will be returned. Ignored
      for undirected graphs.
    c                    s   g | ]} | qS r   )Z	neighborsr   idxmoder   r   r   r      s     z _get_adjlist.<locals>.<listcomp>r   r   )r   r5   r   r4   r   r	   y   s    r	   r!   c                 O   s$   ddl m} t|| j|f||S )ay  Returns the incidence matrix of a bipartite graph. The incidence matrix
    is an M{n} times M{m} matrix, where M{n} and M{m} are the number of
    vertices in the two vertex classes.

    @param types: an igraph vector containing the vertex types, or an
      attribute name. Anything that evalulates to C{False} corresponds to
      vertices of the first kind, everything else to the second kind.
    @return: the incidence matrix and two lists in a triplet. The first
      list defines the mapping between row indices of the matrix and the
      original vertex IDs. The second list is the same for the column
      indices.
    r   )Graph)Zigraphr7   superZget_incidence)graphtypesargskwdsr7   r   r   r   r
      s    r
   c                    s    fddt   D S )a  Returns the incidence list representation of the graph.

    The incidence list representation is a list of lists. Each
    item of the outer list belongs to a single vertex of the graph.
    The inner list contains the IDs of the incident edges of the
    given vertex.

    @param mode: if C{"out"}, returns the successors of the vertex. If
      C{"in"}, returns the predecessors of the vertex. If C{"all"}, both
      the predecessors and the successors will be returned. Ignored
      for undirected graphs.
    c                    s   g | ]}  |qS r   )Zincidentr2   r9   r5   r   r   r      s     z _get_inclist.<locals>.<listcomp>r6   r=   r   r=   r   r      s    r   )N)r1   )r!   )r1   )Zigraph._igraphr   r   r   r   Zigraph.datatypesr   __all__r   r   r	   r
   r   r   r   r   r   <module>   s   
   
F
 

