a
    ƒ³bÅ  ã                   @   s@   d Z ddlZddlmZ ddlZddlmZ dd„ Zdd„ Z	dS )	zì
I/O for cluster files.

A cluster file is an archive (zip or tar, compressed or not) holding
an ICluster graph as a JSON object or as a set of Numpy arrays.  As a
special case, a bare .json file can be read for a single cluster
graph.
é    N)ÚPath)Úarioc                 C   sX   t j| d�}|d D ]&}|j|d fd|d i|d ¤Ž q|d D ]}|j|Ž  qD|S )z4
    Return networkx graph from dict base data 
    )ÚnameÚverticesÚidentÚcodeÚtypeÚdataÚedges)ÚnxZGraphZadd_nodeZadd_edge)r   ÚdatÚgrZvtxÚedge© r   ú8/home/yuhw/wc/wire-cell-python-pr-11/wirecell/img/tap.pyÚmake_nxgraph   s    $r   c                 c   sÜ   t | ƒ}|jdv rdt | t| ƒ ¡ ¡}t|tƒs6|g}t|ƒD ] \}}t	|j
› d|› �|ƒV  q>dS t | d¡}|D ]b}|j| }d|v rÎ|| }t|tƒs¢|g}t|ƒD ] \}}t	|j
› d|› �|ƒV  qªqttdƒ‚qtdS )zB
    Yield a sequence of graphs loaded from file like object.
    )ú.jsonÚ_NFr   z0Cluster graphs in Numpy format not yet supported)r   Úsuffixr   Ú	transformÚopenÚreadÚ
isinstanceÚlistÚ	enumerater   ÚstemÚloadÚmember_namesÚ
ValueError)ÚfilenameÚpathr   ÚcountÚoneZarfÚkeyÚmemberr   r   r   r      s$    



r   )
Ú__doc__ÚjsonÚpathlibr   Znetworkxr   Zwirecell.utilr   r   r   r   r   r   r   Ú<module>   s   	