o
    h>                     @   s   d Z ddlZddlmZmZmZ ddlZg dZdddZ	ej
ddd	Zej
dd
ddddZdddZej
dd
ddddZej
d
ddddZej
dd
ddddZdS )a  Functions to convert NetworkX graphs to and from other formats.

The preferred way of converting data to a NetworkX graph is through the
graph constructor.  The constructor calls the to_networkx_graph() function
which attempts to guess the input type and convert it automatically.

Examples
--------
Create a graph with a single edge from a dictionary of dictionaries

>>> d = {0: {1: 1}}  # dict-of-dicts single edge (0,1)
>>> G = nx.Graph(d)

See Also
--------
nx_agraph, nx_pydot
    N)
Collection	GeneratorIterator)to_networkx_graphfrom_dict_of_dictsto_dict_of_dictsfrom_dict_of_liststo_dict_of_listsfrom_edgelistto_edgelistFc                 C   sj  t | dr>z&t| j||  d}|j| j | j D ]\}}|j| | q|W S  t	y= } zt
d|d}~ww t| trzt| ||dW S  t	y } z0|du rdt
dt| d| zt| |dW W  Y d}~S  t	y } ztd	|d}~ww d}~ww t| ttB t
jjB tB rzt| |dW S    Y t | d
rz	t
jj| |dW S  t	y } zt
d|d}~ww zUddl}	t| |	jr| jd | jd krz	t
j| |dW W S  t	y } zd}
t
|
|d}~ww z
t
j| d|dW W S  t	y } zd}
t
|
|d}~ww W n
 ty!   Y nw z*ddl}t| |j rKz	t
j!| |dW W S  t	yJ } zt
d|d}~ww W n
 tyV   Y nw z)ddl"}t | drz	t
j#| |dW W S  t	y~ } zt
d|d}~ww W n
 ty   Y nw t| t$t%B tB rzt| |dW S  t	y } zt
d|d}~ww t
d)a  Make a NetworkX graph from a known data structure.

    The preferred way to call this is automatically
    from the class constructor

    >>> d = {0: {1: {"weight": 1}}}  # dict-of-dicts single edge (0,1)
    >>> G = nx.Graph(d)

    instead of the equivalent

    >>> G = nx.from_dict_of_dicts(d)

    Parameters
    ----------
    data : object to be converted

        Current known types are:
         any NetworkX graph
         dict-of-dicts
         dict-of-lists
         container (e.g. set, list, tuple) of edges
         iterator (e.g. itertools.chain) that produces edges
         generator of edges
         Pandas DataFrame (row per edge)
         2D numpy array
         scipy sparse array
         pygraphviz agraph

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    multigraph_input : bool (default False)
        If True and  data is a dict_of_dicts,
        try to create a multigraph assuming dict_of_dict_of_lists.
        If data and create_using are both multigraphs then create
        a multigraph from a multigraph.

    adj)create_usingmultigraph_inputz&Input is not a correct NetworkX graph.NTz$converting multigraph_input raised:
z: )r   zInput is not known type.	is_strictz(Input is not a correct pygraphviz graph.r      z9Input is not a correct Pandas DataFrame adjacency matrix.)	edge_attrr   z2Input is not a correct Pandas DataFrame edge-list.z1Failed to interpret array as an adjacency matrix.formatz/Input is not a correct scipy sparse array type.zInput is not a valid edge listz.Input is not a known data type for conversion.)&hasattrr   r   is_multigraphgraphupdatenodesitems_node	ExceptionnxNetworkXError
isinstancedicttyper   	TypeErrorlisttuplereportviewsEdgeViewABCr   r
   	nx_agraphfrom_agraphpandas	DataFrameshapefrom_pandas_adjacencyfrom_pandas_edgelistImportErrornumpyndarrayfrom_numpy_arrayscipyfrom_scipy_sparse_arrayr   r   )datar   r   resultndderrerr1err2pdmsgnpr0    r<   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/convert.pyr   #   s   
(




r   c                    s:    du r|  i } D ]} fdd|  |D ||< q
|S )a&  Returns adjacency representation of graph as a dictionary of lists.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    Notes
    -----
    Completely ignores edge data for MultiGraph and MultiDiGraph.

    Nc                    s   g | ]}| v r|qS r<   r<   ).0nbrnodelistr<   r=   
<listcomp>   s    z$to_dict_of_lists.<locals>.<listcomp>)	neighbors)GrA   dr4   r<   r@   r=   r	      s   r	   T)graphsreturns_graphc                 C   s   t d|}||  | r3| s3i }|  D ]\}}|D ]}||vr+||| qd||< q|S |dd |  D  |S )a  Returns a graph from a dictionary of lists.

    Parameters
    ----------
    d : dictionary of lists
      A dictionary of lists adjacency representation.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> dol = {0: [1]}  # single edge (0,1)
    >>> G = nx.from_dict_of_lists(dol)

    or

    >>> G = nx.Graph(dol)  # use Graph constructor

    r   r   c                 s   s&    | ]\}}|D ]}||fV  qqd S Nr<   )r>   nodenbrlistr?   r<   r<   r=   	<genexpr>   s   $ z%from_dict_of_lists.<locals>.<genexpr>)r   empty_graphadd_nodes_fromr   is_directedr   add_edgeadd_edges_from)rE   r   rD   seenrI   rJ   r?   r<   r<   r=   r      s   

r   c                    s   i } du r.|du r|   D ]
\}}| ||< q|S |   D ]\}}|||||< q|S |du rU D ]}i ||<  fdd| |  D D ]
\}}||| |< qGq4|S  D ]}i ||<  fdd| | D D ]}||| |< qhqW|S )a>  Returns adjacency representation of graph as a dictionary of dictionaries.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    edge_data : scalar, optional
       If provided, the value of the dictionary will be set to `edge_data` for
       all edges. Usual values could be `1` or `True`. If `edge_data` is
       `None` (the default), the edgedata in `G` is used, resulting in a
       dict-of-dict-of-dicts. If `G` is a MultiGraph, the result will be a
       dict-of-dict-of-dict-of-dicts. See Notes for an approach to customize
       handling edge data. `edge_data` should *not* be a container.

    Returns
    -------
    dod : dict
       A nested dictionary representation of `G`. Note that the level of
       nesting depends on the type of `G` and the value of `edge_data`
       (see Examples).

    See Also
    --------
    from_dict_of_dicts, to_dict_of_lists

    Notes
    -----
    For a more custom approach to handling edge data, try::

        dod = {
            n: {nbr: custom(n, nbr, dd) for nbr, dd in nbrdict.items()}
            for n, nbrdict in G.adj.items()
        }

    where `custom` returns the desired edge data for each edge between `n` and
    `nbr`, given existing edge data `dd`.

    Examples
    --------
    >>> G = nx.path_graph(3)
    >>> nx.to_dict_of_dicts(G)
    {0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}}}

    Edge data is preserved by default (``edge_data=None``), resulting
    in dict-of-dict-of-dicts where the innermost dictionary contains the
    edge data:

    >>> G = nx.Graph()
    >>> G.add_edges_from(
    ...     [
    ...         (0, 1, {"weight": 1.0}),
    ...         (1, 2, {"weight": 2.0}),
    ...         (2, 0, {"weight": 1.0}),
    ...     ]
    ... )
    >>> d = nx.to_dict_of_dicts(G)
    >>> d  # doctest: +SKIP
    {0: {1: {'weight': 1.0}, 2: {'weight': 1.0}},
     1: {0: {'weight': 1.0}, 2: {'weight': 2.0}},
     2: {1: {'weight': 2.0}, 0: {'weight': 1.0}}}
    >>> d[1][2]["weight"]
    2.0

    If `edge_data` is not `None`, edge data in the original graph (if any) is
    replaced:

    >>> d = nx.to_dict_of_dicts(G, edge_data=1)
    >>> d
    {0: {1: 1, 2: 1}, 1: {0: 1, 2: 1}, 2: {1: 1, 0: 1}}
    >>> d[1][2]
    1

    This also applies to MultiGraphs: edge data is preserved by default:

    >>> G = nx.MultiGraph()
    >>> G.add_edge(0, 1, key="a", weight=1.0)
    'a'
    >>> G.add_edge(0, 1, key="b", weight=5.0)
    'b'
    >>> d = nx.to_dict_of_dicts(G)
    >>> d  # doctest: +SKIP
    {0: {1: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}},
     1: {0: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}}}
    >>> d[0][1]["b"]["weight"]
    5.0

    But multi edge data is lost if `edge_data` is not `None`:

    >>> d = nx.to_dict_of_dicts(G, edge_data=10)
    >>> d
    {0: {1: 10}, 1: {0: 10}}
    Nc                 3   s$    | ]\}}| v r||fV  qd S rH   r<   )r>   vr2   r@   r<   r=   rK   k  s   " z#to_dict_of_dicts.<locals>.<genexpr>c                 3   s    | ]	}| v r|V  qd S rH   r<   )r>   rR   r@   r<   r=   rK   p  s    )	adjacencycopyfromkeysr   )rD   rA   	edge_datadodunbrdictrR   r2   r<   r@   r=   r      s,   a"r   c                    s  t d|}||  |r| r1| r#|dd |  D  |S |dd |  D  |S | rht }|  D ])\ }| D ] \} f|vrd| fdd| D  | f qDq<|S t }|  D ])\ }| D ] \} f|vr| fdd| D  | f qwqo|S | r| st }|  D ]-\ }| D ]$\} f|vr|j	 dd |   d 
| | f qq|S |dd |  D  |S )	aO  Returns a graph from a dictionary of dictionaries.

    Parameters
    ----------
    d : dictionary of dictionaries
      A dictionary of dictionaries adjacency representation.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    multigraph_input : bool (default False)
       When True, the dict `d` is assumed
       to be a dict-of-dict-of-dict-of-dict structure keyed by
       node to neighbor to edge keys to edge data for multi-edges.
       Otherwise this routine assumes dict-of-dict-of-dict keyed by
       node to neighbor to edge data.

    Examples
    --------
    >>> dod = {0: {1: {"weight": 1}}}  # single edge (0,1)
    >>> G = nx.from_dict_of_dicts(dod)

    or

    >>> G = nx.Graph(dod)  # use Graph constructor

    r   c                 s   sD    | ]\}}|  D ]\}}|  D ]\}}||||fV  qq
qd S rH   r   r>   rX   nbrsrR   datadictkeyr2   r<   r<   r=   rK     s    
z%from_dict_of_dicts.<locals>.<genexpr>c                 s   sB    | ]\}}|  D ]\}}|  D ]
\}}|||fV  qq
qd S rH   rZ   r[   r<   r<   r=   rK     s    c                 3   s     | ]\}} ||fV  qd S rH   r<   r>   r^   r2   rX   rR   r<   r=   rK     s    
c                 3   s    | ]
\}} |fV  qd S rH   r<   r_   r`   r<   r=   rK     s    
)r^   c                 s   s0    | ]\}}|  D ]
\}}|||fV  q
qd S rH   rZ   )r>   rX   r\   rR   r2   r<   r<   r=   rK     s   . )r   rL   rM   rN   r   rP   r   setaddrO   r   )rE   r   r   rD   rQ   r\   r]   r2   r<   r`   r=   r   u  sd   

1
* 	r   )preserve_edge_attrsc                 C   s"   |du r
| j ddS | j |ddS )zReturns a list of edges in the graph.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    NT)r2   )edges)rD   rA   r<   r<   r=   r     s   r   c                 C   s   t d|}||  |S )a  Returns a graph from a list of edges.

    Parameters
    ----------
    edgelist : list or iterator
      Edge tuples

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> edgelist = [(0, 1)]  # single edge (0,1)
    >>> G = nx.from_edgelist(edgelist)

    or

    >>> G = nx.Graph(edgelist)  # use Graph constructor

    r   )r   rL   rP   )edgelistr   rD   r<   r<   r=   r
     s   
r
   )NFrH   )NN)__doc__warningscollections.abcr   r   r   networkxr   __all__r   _dispatchabler	   r   r   r   r   r
   r<   r<   r<   r=   <module>   s$    
 
(w
V