a
    ×ýdd‘  ã                   @   s    d Z dd„ ZG dd„ deƒZdS )z•
Handle cluster graphs.

A cluster graph is a networkx graph with nodes holding a letter "type
code" (c,w,b,s,m) and a code-dependent data structure
c                 C   s2   |D ](}|| vr dS | | || kr dS qdS )zU
    return True if all keys of want are in have and all their values are equal.
    FT© )ÚhaveZwantÚwr   r   ú7/home/yuhw/wc/wire-cell-python/wirecell/img/clusters.pyÚ
match_dict	   s    r   c                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
dd„ Zdd„ ZdS )Ú
ClusterMapzN
    Add some indexing and lookups to meta data on cluster graph vertices
    c                 C   sÈ   || _ tƒ | _tƒ | _tƒ | _tƒ | _tƒ | _|j ¡ D ]Š\}}|d dkrr|| j|d < || j|d |d f< q8|d dkr8|| j|d |d f< || j|d |d f< || j|d |d f< q8q8d S )	NÚcodeÚcÚidentÚwpidÚindexr   ÚchidÚseg)	ÚgrÚdictÚ_id2chÚ_pi2chÚ_cs2wireÚ	_wip2wireÚ	_wid2wireÚnodesÚdata)Úselfr   Únoder   r   r   r   Ú__init__   s     zClusterMap.__init__c                 C   s    t |ƒtkr| j| S | j| S )z�
        Return a channel node by a key.  If key is scalar it is a
        channel ident number, else assumed to be a pair of (wpid,
        index).
        )ÚtypeÚtupler   r   )r   Úkeyr   r   r   Úchannel,   s    
zClusterMap.channelc                 C   s   | j ||f S )z?
        Return a wire node by its channel and segment
        )r   )r   Úchanr   r   r   r   Úwire_chanseg6   s    zClusterMap.wire_chansegc                 C   s   | j ||f S )z[
        Return a wire node by its wire-in-plane number in the given wire-plane ID
        )r   )r   r
   Úwipr   r   r   Úwire_wip<   s    zClusterMap.wire_wipc                 C   s   | j |tf S )zI
        Return a wire node by its wire-ident and wire-plane ID.
        )r   r    )r   r
   Zwidr   r   r   Úwire_widB   s    zClusterMap.wire_widNc                 K   sP   t ƒ }| jj ¡ D ]8\}}|r4| jj| d |kr4qt||ƒs@q| |¡ q|S )zx
        Return nodes with data matching kwds.  If typecode is given,
        only consider nodes of that type.
        r   )Úlistr   r   r   r   Úappend)r   ÚtypecodeÚkwdsÚretr   r   r   r   r   ÚfindI   s    
zClusterMap.findc                    s   ‡ fdd„| j j ¡ D ƒS )ú;
        Return a list of nodes of given type code
        c                    s    g | ]\}}|d  ˆ kr|‘qS ©r   r   ©Ú.0ÚnÚd©r%   r   r   Ú
<listcomp>\   ó    z+ClusterMap.nodes_oftype.<locals>.<listcomp>©r   r   r   ©r   r%   r   r/   r   Únodes_oftypeX   s    zClusterMap.nodes_oftypec                    s   ‡ fdd„| j j ¡ D ƒS )r)   c                    s    g | ]\}}|d  ˆ kr|‘qS r*   r   r+   r/   r   r   r0   a   r1   z*ClusterMap.data_oftype.<locals>.<listcomp>r2   r3   r   r/   r   Údata_oftype]   s    zClusterMap.data_oftypec                 C   s8   t ƒ }| j| D ]"}| jj| d |kr| |¡ q|S )zN
        Return all connected nodes of the given node and given type.
        r   )r#   r   r   r$   )r   r   r%   r'   Únnr   r   r   Úneighbors_oftypec   s
    zClusterMap.neighbors_oftype)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r"   r(   r4   r5   r7   r   r   r   r   r      s   

r   N)r;   r   Úobjectr   r   r   r   r   Ú<module>   s   