U
    ¹mœd  ã                   @   s4   d dl mZ d dlZddgZd	dd„Zd
dd„ZdS )é    )ÚchainNÚ	tree_dataÚ
tree_graphÚidÚchildrenc                    s�   |   ¡ |  ¡ d krtdƒ‚|  ¡ s,tdƒ‚t | ¡s>tdƒ‚ˆˆkrPt d¡‚‡ ‡‡fdd„‰ tt| j	|  
¡ ˆ|fgƒƒ}ˆ || ƒ|ˆ< |S )a  Returns data in tree format that is suitable for JSON serialization
    and use in Javascript documents.

    Parameters
    ----------
    G : NetworkX graph
       G must be an oriented tree

    root : node
       The root of the tree

    ident : string
        Attribute name for storing NetworkX-internal graph data. `ident` must
        have a different value than `children`. The default is 'id'.

    children : string
        Attribute name for storing NetworkX-internal graph data. `children`
        must have a different value than `ident`. The default is 'children'.

    Returns
    -------
    data : dict
       A dictionary with node-link formatted data.

    Raises
    ------
    NetworkXError
        If `children` and `ident` attributes are identical.

    Examples
    --------
    >>> from networkx.readwrite import json_graph
    >>> G = nx.DiGraph([(1, 2)])
    >>> data = json_graph.tree_data(G, root=1)

    To serialize with json

    >>> import json
    >>> s = json.dumps(data)

    Notes
    -----
    Node attributes are stored in this format but keys
    for attributes must be strings if you want to serialize with JSON.

    Graph and edge attributes are not stored.

    See Also
    --------
    tree_graph, node_link_data, adjacency_data
    é   zG is not a tree.zG is not directed.zG is not weakly connected.z5The values for `id` and `children` must be different.c                    sh   ||  }t |ƒdkrg S g }|D ]B}tt|j|  ¡ ˆ|fgƒƒ}ˆ ||ƒ}|rX||ˆ< | |¡ q |S )Nr   )ÚlenÚdictr   ÚnodesÚitemsÚappend)ÚnÚGZnbrsÚ	children_ÚchildÚdÚc©Úadd_childrenr   Úident© ú[/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/networkx/readwrite/json_graph/tree.pyr   F   s    
ztree_data.<locals>.add_children)Znumber_of_nodesZnumber_of_edgesÚ	TypeErrorZis_directedÚnxZis_weakly_connectedZNetworkXErrorr	   r   r
   r   )r   Úrootr   r   Údatar   r   r   r      s    4

c                    sb   t  ¡ ‰‡ ‡‡‡fdd„‰ | ˆ }|  ˆg ¡}‡‡fdd„|  ¡ D ƒ}ˆj|f|Ž ˆ ||ƒ ˆS )a  Returns graph from tree data format.

    Parameters
    ----------
    data : dict
        Tree formatted graph data

    ident : string
        Attribute name for storing NetworkX-internal graph data. `ident` must
        have a different value than `children`. The default is 'id'.

    children : string
        Attribute name for storing NetworkX-internal graph data. `children`
        must have a different value than `ident`. The default is 'children'.

    Returns
    -------
    G : NetworkX DiGraph

    Examples
    --------
    >>> from networkx.readwrite import json_graph
    >>> G = nx.DiGraph([(1, 2)])
    >>> data = json_graph.tree_data(G, root=1)
    >>> H = json_graph.tree_graph(data)

    See Also
    --------
    tree_data, node_link_data, adjacency_data
    c                    sb   |D ]X}|ˆ }ˆ  | |¡ | ˆg ¡}|r6ˆ ||ƒ ‡‡fdd„| ¡ D ƒ}ˆj|f|Ž qd S )Nc                    s*   i | ]"\}}|ˆkr|ˆ krt |ƒ|“qS r   ©Ústr©Ú.0ÚkÚv©r   r   r   r   Ú
<dictcomp>€   s
       z4tree_graph.<locals>.add_children.<locals>.<dictcomp>)Zadd_edgeÚgetr   Úadd_node)Úparentr   r   r   ZgrandchildrenÚnodedata©r   r   Úgraphr   r   r   r   y   s    
ÿz tree_graph.<locals>.add_childrenc                    s*   i | ]"\}}|ˆkr|ˆ krt |ƒ|“qS r   r   r   r"   r   r   r#   ‡   s
        ztree_graph.<locals>.<dictcomp>)r   ZDiGraphr$   r   r%   )r   r   r   r   r   r'   r   r(   r   r   X   s    
)r   r   )r   r   )Ú	itertoolsr   Znetworkxr   Ú__all__r   r   r   r   r   r   Ú<module>   s   
P