U
    Ç-eW  ã                   @   s(   d dl ZddgZddd„Zd	dd„ZdS )
é    NÚcytoscape_dataÚcytoscape_graphÚnameÚidc           
      C   s¦  ||krt  d¡‚dt| j ¡ ƒi}|  ¡ |d< |  ¡ |d< g g dœ|d< |d d }|d d }| j ¡ D ]^\}}d| ¡ i}| 	|¡pt
|ƒ|d d	< ||d d
< | 	|¡p¶t
|ƒ|d d< | |¡ ql|  ¡ rJ| jddD ]d}	d| j|	d  |	d  |	d   ¡ i}|	d |d d< |	d |d d< |	d |d d< | |¡ qânX|  ¡ D ]N}	d| j|	d  |	d   ¡ i}|	d |d d< |	d |d d< | |¡ qR|S )a±  Returns data in Cytoscape JSON format (cyjs).

    Parameters
    ----------
    G : NetworkX Graph
        The graph to convert to cytoscape format
    name : string
        A string which is mapped to the 'name' node element in cyjs format.
        Must not have the same value as `ident`.
    ident : string
        A string which is mapped to the 'id' node element in cyjs format.
        Must not have the same value as `name`.

    Returns
    -------
    data: dict
        A dictionary with cyjs formatted data.

    Raises
    ------
    NetworkXError
        If the values for `name` and `ident` are identical.

    See Also
    --------
    cytoscape_graph: convert a dictionary in cyjs format to a graph

    References
    ----------
    .. [1] Cytoscape user's manual:
       http://manual.cytoscape.org/en/stable/index.html

    Examples
    --------
    >>> G = nx.path_graph(2)
    >>> nx.cytoscape_data(G)  # doctest: +SKIP
    {'data': [],
     'directed': False,
     'multigraph': False,
     'elements': {'nodes': [{'data': {'id': '0', 'value': 0, 'name': '0'}},
       {'data': {'id': '1', 'value': 1, 'name': '1'}}],
      'edges': [{'data': {'source': 0, 'target': 1}}]}}
    ú!name and ident must be different.ÚdataÚdirectedÚ
multigraph)ÚnodesÚedgesÚelementsr
   r   r   Úvaluer   T)Úkeysr   é   é   ÚsourceÚtargetÚkey)ÚnxÚNetworkXErrorÚlistÚgraphÚitemsZis_directedZis_multigraphr
   ÚcopyÚgetÚstrÚappendr   Zadj)
ÚGr   ÚidentZjsondatar
   r   ÚiÚjÚnÚe© r#   úh/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/networkx/readwrite/json_graph/cytoscape.pyr      s6    ,

&c                 C   st  ||krt  d¡‚|  d¡}|  d¡}|r4t  ¡ }nt  ¡ }|rH| ¡ }t|  d¡ƒ|_| d d D ]v}|d  ¡ }|d d }|d  |¡r |d  |¡||< |d  |¡rÀ|d  |¡||< | 	|¡ |j
|  |¡ qd| d d D ]†}|d  ¡ }	|d d	 }
|d d
 }|rN|d  dd¡}|j|
||d |j|
||f  |	¡ qè| |
|¡ |j|
|f  |	¡ qè|S )a@  
    Create a NetworkX graph from a dictionary in cytoscape JSON format.

    Parameters
    ----------
    data : dict
        A dictionary of data conforming to cytoscape JSON format.
    name : string
        A string which is mapped to the 'name' node element in cyjs format.
        Must not have the same value as `ident`.
    ident : string
        A string which is mapped to the 'id' node element in cyjs format.
        Must not have the same value as `name`.

    Returns
    -------
    graph : a NetworkX graph instance
        The `graph` can be an instance of `Graph`, `DiGraph`, `MultiGraph`, or
        `MultiDiGraph` depending on the input data.

    Raises
    ------
    NetworkXError
        If the `name` and `ident` attributes are identical.

    See Also
    --------
    cytoscape_data: convert a NetworkX graph to a dict in cyjs format

    References
    ----------
    .. [1] Cytoscape user's manual:
       http://manual.cytoscape.org/en/stable/index.html

    Examples
    --------
    >>> data_dict = {
    ...     'data': [],
    ...     'directed': False,
    ...     'multigraph': False,
    ...     'elements': {'nodes': [{'data': {'id': '0', 'value': 0, 'name': '0'}},
    ...       {'data': {'id': '1', 'value': 1, 'name': '1'}}],
    ...      'edges': [{'data': {'source': 0, 'target': 1}}]}
    ... }
    >>> G = nx.cytoscape_graph(data_dict)
    >>> G.name
    ''
    >>> G.nodes()
    NodeView((0, 1))
    >>> G.nodes(data=True)[0]
    {'id': '0', 'value': 0, 'name': '0'}
    >>> G.edges(data=True)
    EdgeDataView([(0, 1, {'source': 0, 'target': 1})])
    r   r	   r   r   r   r
   r   r   r   r   r   r   )r   )r   r   r   Z
MultiGraphZGraphZto_directedÚdictr   r   Úadd_noder
   ÚupdateZadd_edger   )r   r   r   r	   r   r   ÚdZ	node_dataÚnodeZ	edge_dataZsourZtargr   r#   r#   r$   r   S   s<    7




)r   r   )r   r   )Znetworkxr   Ú__all__r   r   r#   r#   r#   r$   Ú<module>   s   
M