U
    td7                     @   s~   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dZG d	d
 d
Zdd ZG dd dZdddZdS )z"Summary representation of a graph.    N)median)islice)ceil)	Texttable)TextWrapper)GraphSummarysummaryc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	FakeWrapperz[Object whose interface is compatible with C{textwrap.TextWrapper}
    but does no wrapping.c                 O   s   d S N )selfargskwdsr   r   G/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/summary.py__init__   s    zFakeWrapper.__init__c                 C   s   |gS r
   r   r   textr   r   r   fill   s    zFakeWrapper.fillc                 C   s   |gS r
   r   r   r   r   r   wrap   s    zFakeWrapper.wrapN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r	      s   r	   c                 O   s$   | dkrt ||S t|d| i|S )zReturns a text wrapper that wraps text for the given width.

    @param width: the maximal width of each line that the text wrapper
      produces. C{None} means that no wrapping will be performed.
    Nwidth)r	   r   )r   r   r   r   r   r   _get_wrapper_for_width   s    
r   c                   @   sv   e Zd ZdZd ddZd	d
 Zdd Zdd Zdd Zdd Z	dd Z
dd Zd!ddZd"ddZdd Zdd ZdS )#r   a  Summary representation of a graph.

    The summary representation includes a header line and the list of
    edges. The header line consists of C{IGRAPH}, followed by a
    four-character long code, the number of vertices, the number of
    edges, two dashes (C{--}) and the name of the graph (i.e.
    the contents of the C{name} attribute, if any). For instance,
    a header line may look like this::

        IGRAPH U--- 4 5 --

    The four-character code describes some basic properties of the
    graph. The first character is C{U} if the graph is undirected,
    C{D} if it is directed. The second letter is C{N} if the graph
    has a vertex attribute called C{name}, or a dash otherwise. The
    third letter is C{W} if the graph is weighted (i.e. it has an
    edge attribute called C{weight}), or a dash otherwise. The
    fourth letter is C{B} if the graph has a vertex attribute called
    C{type}; this is usually used for bipartite graphs.

    Edges may be presented as an ordinary edge list or an adjacency
    list. By default, this depends on the number of edges; however,
    you can control it with the appropriate constructor arguments.
    r   N   auto韆 Fc
           
      C   s   |	rd}d}d}t |d}|| _| | _t|| _|| _|| _|| _|| _	|| _
t| j
dd| _| j rtd| _nd| _ddg| j  | _d	| j | _d
S )a  Constructs a summary representation of a graph.

        @param verbosity: the verbosity of the summary. If zero, only
          the header line will be returned. If one, the header line
          and the list of edges will both be returned.
        @param width: the maximal width of each line in the summary.
          C{None} means that no limit will be enforced.
        @param max_rows: the maximal number of rows to print in a single
          table (e.g., vertex attribute table or edge attribute table)
        @param edge_list_format: format of the edge list in the summary.
          Supported formats are: C{compressed}, C{adjlist}, C{edgelist},
          C{auto}, which selects automatically from the other three based
          on some simple criteria.
        @param print_graph_attributes: whether to print graph attributes
          if there are any.
        @param print_vertex_attributes: whether to print vertex attributes
          if there are any.
        @param print_edge_attributes: whether to print edge attributes
          if there are any.
        @param full: False has no effect; True turns on the attribute
          printing for graph, vertex and edge attributes with verbosity 1.
        T   F)break_on_hyphensz+ edges (vertex names):z+ edges:z--z->z%%s%s%%sN)max_graphloweredge_list_formatintmax_rowsprint_graph_attributesprint_vertex_attributesprint_edge_attributes	verbosityr   r   wrapperis_named_edges_headeris_directed_arrow_arrow_format)
r   graphr)   r   r#   r%   r&   r'   r(   fullr   r   r   r   C   s&    "



zGraphSummary.__init__c                    s  | j g}| j dkrdS | j r| jjd  tdd  D }d|| jf }t D ]>\}}| j|}d	 fdd|D }|
|||f  qXnrtt| j }d	| d
| jf }t| j D ]:}| j|}d	fdd|D }|
|||f  q| jdk	rtdd |dd D }t| jd t|d  }|dkrt|d }tt|t| }	dd t|	D }
t|dd D ]"\}}|
||	  
|| qdd |
D |dd< |S )zaConstructs the part in the summary that prints the edge list in an
        adjacency list format.r   Nnamec                 s   s   | ]}t t|V  qd S r
   )lenstr.0r2   r   r   r   	<genexpr>   s     z;GraphSummary._construct_edgelist_adjlist.<locals>.<genexpr>z%%%ds %s %%s, c                 3   s   | ]}t  | V  qd S r
   )r4   r6   Zv2)namesr   r   r7      s     z%%%ddz	%s %s %%s c                 3   s   | ]} | V  qd S r
   r   r9   )
num_formatr   r   r7      s     c                 s   s   | ]}t |V  qd S r
   )r3   r6   liner   r   r   r7      s     r      c                 S   s   g | ]}g qS r   r   )r6   _r   r   r   
<listcomp>   s     z<GraphSummary._construct_edgelist_adjlist.<locals>.<listcomp>c                 S   s   g | ]}d  |qS )z   )join)r6   rowr   r   r   rA      s     )r,   r!   vcountr+   vsr    r.   	enumerate
successorsrB   appendr3   r4   ranger   r$   r   floatljust)r   resultmaxlen
format_strZv1r2   ZneisZcolcountZnrowsZ	colheightZnewrowsirC   r   )r:   r<   r   _construct_edgelist_adjlist|   s:    

z(GraphSummary._construct_edgelist_adjlistc                    sn   | j g}| j | j rD| jjd d fdd| jjD }nd fdd| jjD }|| |S )zConstructs the part in the summary that prints the edge list in a
        compressed format suitable for graphs with mostly small degrees.r2   r8   c                 3   s&   | ]} |j  |j f V  qd S r
   )sourcetargetr6   edgearrowr:   r   r   r7      s   z>GraphSummary._construct_edgelist_compressed.<locals>.<genexpr>r;   c                 3   s   | ]} |j  V  qd S r
   )tuplerS   )rV   r   r   r7      s     )r,   r/   r!   r+   rE   rB   esrH   )r   rL   edgesr   rU   r   _construct_edgelist_compressed   s    


z+GraphSummary._construct_edgelist_compressedc                 C   sz   t | j }| jddg| d}|jt| |d| jdd |ddg| j	|d  | j
g}|| d	 |S )
z`Constructs the part in the summary that prints the edge list in a
        full edge list format. rT   headersr   Fheaderl)
edge_attrs
)sortedr!   edge_attributes
_new_tableadd_rowsr   _edge_attribute_iteratorr%   set_cols_align_infer_column_alignmentr,   extenddrawsplitr   attrstablerL   r   r   r   _construct_edgelist_edgelist   s     z)GraphSummary._construct_edgelist_edgelistc                 C   sR   | j  }|sg S dg}|  |D ](}|d|f  |t| j |  q$|S )zCConstructs the part in the summary that lists the graph attributes.z+ graph attributes:z[[%s]])r!   
attributessortrH   r4   )r   rn   rL   attrr   r   r   _construct_graph_attributes   s    
z(GraphSummary._construct_graph_attributesc                 C   s   t | j }|r&t|dkr*d|kr*g S | jdg| d}|jt| |d| jdd |	dg| j
|d	  d
g}|| d |S )zDConstructs the part in the summary that lists the vertex attributes.r   r2   r[   r\   r   Fr^   r`   )vertex_attrsz+ vertex attributes:rb   )rc   r!   vertex_attributesr3   re   rf   r   _vertex_attribute_iteratorr%   rh   ri   rj   rk   rl   rm   r   r   r   _construct_vertex_attributes   s    z)GraphSummary._construct_vertex_attributesc                 C   s  | j }td|  d|  d|  dd| k | | d}d| kr`|d |d< nd|d< d	| g}d
d t	| D }|
dd t	| D  |
dd t	| D  |r|dd|  | jdk	rd| j_| j|d |dd< d| j_|S )z*Constructs the header part of the summary.ZUDz-Nz-Wz-Ttype)ZdirectednamedZweightedtypedrD   ecountr2   r[   zSIGRAPH %(directed)s%(named)s%(weighted)s%(typed)s %(vcount)d %(ecount)d -- %(name)sc                 S   s   g | ]}d |f qS )z%s (g)r   r5   r   r   r   rA      s     z2GraphSummary._construct_header.<locals>.<listcomp>c                 s   s   | ]}d |f V  qdS )z%s (v)Nr   r5   r   r   r   r7      s     z1GraphSummary._construct_header.<locals>.<genexpr>c                 s   s   | ]}d |f V  qdS )z%s (e)Nr   r5   r   r   r   r7      s     z
+ attr: %sr8   Nz  )r!   dictr-   r+   Zis_weightedrv   rD   r|   rq   rc   rj   rd   rH   rB   r*   subsequent_indentr   )r   r0   paramsrL   rn   r   r   r   _construct_header   s4    


zGraphSummary._construct_headerc                 #   s   | j }| j rf| jjd }| jjD ]> || j | j f }d j |g fdd|D  V  q$n:| jjD ]0 | j }d j |g fdd|D  V  qndS )zReturns an iterator that yields the rows of the edge attribute table
        in the summary. C{attribute_order} must be a list containing the names of
        the attributes to be presented in this table.r2   [%d]c                    s   g | ]} | qS r   r   r6   rs   rT   r   r   rA     s    z9GraphSummary._edge_attribute_iterator.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r   r   r   r   rA     s    N)	r/   r!   r+   rE   rX   rQ   rR   indexrW   )r   attribute_orderrV   r:   Zformatted_edger   r   r   rg     s    

z%GraphSummary._edge_attribute_iteratorNc              	      s   g }|dk	r4| j jdd |fdd|D  |dk	rd| j jdd  | fdd|D  g }|D ]Nd}zdd D  W n tk
r   d	}Y nX |r|d
 ql|d ql|S )a  Infers the preferred alignment for the given vertex and edge attributes
        in the tables by peeking into the attribute values of the first 100 vertices
        or edges. Numeric attributes will be aligned right, everything else will be
        aligned left.Nd   c                 3   s   | ]} | V  qd S r
   r   r   )rE   r   r   r7   #  s     z7GraphSummary._infer_column_alignment.<locals>.<genexpr>c                 3   s   | ]} | V  qd S r
   r   r   )rX   r   r   r7   &  s     Tc                 S   s   g | ]}t |qS r   )rJ   )r6   xr   r   r   rA   ,  s     z8GraphSummary._infer_column_alignment.<locals>.<listcomp>Frr`   )r!   rE   rj   rX   
ValueErrorrH   )r   ru   ra   valuesrL   Z
is_numericr   )rX   rE   r   ri     s$    
z$GraphSummary._infer_column_alignmentc                 C   s*   t dd}|d |dk	r&|| |S )zAConstructs a new table to pretty-print vertex and edge attributesr   )	max_widthN)r   Zset_decor_   )r   r]   ro   r   r   r   re   6  s
    


zGraphSummary._new_tablec                 #   s2   | j jD ]$ d j g fdd|D  V  qdS )zReturns an iterator that yields the rows of the vertex attribute table
        in the summary. C{attribute_order} must be a list containing the names of
        the attributes to be presented in this table.r   c                    s   g | ]} | qS r   r   r   Zvertexr   r   rA   C  s     z;GraphSummary._vertex_attribute_iterator.<locals>.<listcomp>N)r!   rE   r   )r   r   r   r   r   rw   >  s    z'GraphSummary._vertex_attribute_iteratorc                    s      } jr|    jr0|    jdkrDd|S  j	 dkr j
dkr jrr j rrd}qt jjdddk rd}qd	}n j
}d
| }t |r|t |   jdk	rd fdd|D S d|S )z/Returns the summary representation as a string.r   rb   r   Zedgelistout)moder?   
compressedZadjlistz_construct_edgelist_%sNc                 3   s    | ]}d   j|V  qdS )rb   N)rB   r*   r   r=   r   r   r   r7   b  s     z'GraphSummary.__str__.<locals>.<genexpr>)r   r&   rj   rt   r'   rx   r)   rB   r!   r|   r#   r(   rd   r   Zdegreehasattrgetattrr*   )r   outputformatmethod_namer   r   r   __str__E  s*    




zGraphSummary.__str__)r   r   r   r   FFFF)NN)N)r   r   r   r   r   rP   rZ   rp   rt   rx   r   rg   ri   re   rw   r   r   r   r   r   r   )   s*           
9( 

r   c                 O   sH   |dkrt j}t| dr,|| j|| n|t|  |d dS )ay  Prints a summary of object o to a given stream

    Positional and keyword arguments not explicitly mentioned here are passed
    on to the underlying C{summary()} method of the object if it has any.

    @param obj: the object about which a human-readable summary is requested.
    @param stream: the stream to be used. If C{None}, the standard output
      will be used.
    Nr   rb   )sysstdoutr   writer   r4   )objstreamr   r   r   r   r   r   g  s    

r   )N)r   r   Zigraph.statisticsr   	itertoolsr   mathr   Z	texttabler   textwrapr   __all__r	   r   r   r   r   r   r   r   <module>   s     @