a
    ƒ³b>  ã                   @   s‚  d Z ddlZddlZddlZddlZddlmZ ddlZddlm	Z
 ddlmZ ddlmZ ddlmZ eeddgd	�d
�Zejdri e¤Žejdd„ ƒƒZe d¡ejdddd�ejdddd�ejddde g d¢¡dd�e d¡e d¡ejdd„ ƒƒƒƒƒƒƒZe d¡e d¡ejd d„ ƒƒƒZe d!¡ejdd"d#d�ejddd$d�e d¡e d%¡ejd&d'„ ƒƒƒƒƒƒZe d(¡ejdd"d#d�ejddd$d�e d¡e d%¡ejd)d*„ ƒƒƒƒƒƒZe d+¡ejd,d-dd.d�ejd/d0dd1d�ejddd2d�ejddd$d�e d3¡e d%¡ejd4d5„ ƒƒƒƒƒƒƒƒZe d6¡ejd7d8d9d:�ejd,d;d<d=d�ejd>d?eg d@¢dAdB�ejdCdDe dEdFg¡dFdGdH�ejdd"d#d�ejddd$d�ejdIdJe dKdLdH�ejdMdNdO�dPdQ„ ƒƒƒƒƒƒƒƒƒZ!dRdS„ Z"e dT¡ejd7d8dUd:�ejdCdVdWedXdY�ejdZd[edNd\dH�ejdd"d#d�ejddd$d�e d¡d]dT„ ƒƒƒƒƒƒƒZ#e d^¡ejd7d8dUd:�ejdCdVdWedUdY�ejdZd[edNd\dH�ejdd"d#d�ejddd$d�ejd_d`dad�e d¡dbdc„ ƒƒƒƒƒƒƒƒZ$e dd¡ejd7d8dUd:�ejdCdeedfdg�ejdd"d#d�ejddd$d�e d¡dhdi„ ƒƒƒƒƒƒZ%e dj¡ejd7d8dkdld�e dm¡dndj„ ƒƒƒZ&dodp„ Z'dqe(k�r~e'ƒ  dS )sz
The wirecell-img main
é    N)ÚCounter)Úunits)Úunitify)Úarioz-hz--help)Zhelp_option_names)Zcontext_settingsÚimgc                 C   s   dS )z·
    Wire Cell Toolkit Imaging Commands

    A cluster file is produced by ClusterFileSink and is an archive
    holding JSON or Numpy or as a special case may be a single JSON.

    N© )Úctxr   r   ú=/home/yuhw/wc/wire-cell-python-pr-11/wirecell/img/__main__.pyÚcli   s    r
   z
plot-blobsz--speedzCAssign x position based on drift speed, use units like '1.6*mm/us'.)ÚdefaultÚhelpz--t0z0*nszAArbitrary additive time used in drift speed assignment, use unitsz-pz--plotÚx)Útr   ÚyÚzZtxÚtyZtzzThe plot to make.)r   Útyper   zcluster-filez	plot-filec                    sˆ   ddl m}m‰ ddlm  m} t|d| ƒ‰ˆdurHtˆƒ‰tˆƒ‰d‡ ‡‡‡‡‡‡fdd„	}t| 	ˆ ¡ƒD ]\}	}
||
|	ƒ qpdS )	z4
    Produce plots related to blobs in cluster.
    é   ©ÚtapÚ	converterr   NZplot_c                    sh   ˆd urˆ  | ˆˆ¡} d|  ¡ kr4t dˆ  ¡ d S ˆ| ƒ}ˆ}d|v rP|| }| |¡ t |¡ d S ©Nr   úno verticies in %sú%)ÚundriftÚnumber_of_nodesÚclickÚechoÚsavefig)ÚgrÚnÚfigÚpname©Úcluster_filer   Ú	plot_fileZplotterÚspeedZspeed_unitsÚt0r   r	   Údo_one4   s    
zplot_blobs.<locals>.do_one)r   )
Ú r   r   Zwirecell.img.plot_blobsr   Ú
plot_blobsÚgetattrr   Ú	enumerateÚload)r   r&   r'   Úplotr$   r%   r   Zplottersr(   r    r   r   r#   r	   r*      s    r*   Úinspectc                    s   ddl m}m}m} t |¡}| ¡ s8td|› �ƒ dS |j 	d¡rNtdƒ nd|jv r`tdƒ t
| t|ƒ¡ƒ}td	t|ƒ› �ƒ t|ƒD �]\}}| |¡}	t|› d
| ¡ › d| ¡ › d�ƒ tt|jdd�ƒ ¡ ƒ}
t|
 ¡ ƒD ]²\‰ }tdˆ › d
|› d�ƒ ˆ dk�r<t‡ fdd„|jdd�D ƒƒ}td|› �ƒ qæˆ dkræd}|	 d¡D ]4}|	jj| }|d }|tdd„ | ¡ D ƒƒ7 }�qRtd|› �ƒ qæqæqŒdS )z 
    Inspect a cluster file
    r   )r   r   Úclusterszno such file: Nz.jsonz&JSON file assuming from JsonClusterTapz.tarz&TAR file assuming from ClusterFileSinkznumber of graphs: z: z vertices, z edgesÚcode)Údataú	z nodesÚbc                    s$   g | ]\}}|d  ˆ kr|d ‘qS )r1   Úvaluer   )Ú.0Úcr    ©r1   r   r	   Ú
<listcomp>e   ó    zinspect.<locals>.<listcomp>Tz		total charge: Úsr   Úsignalc                 S   s   g | ]}|d  ‘qS )Úvalr   )r6   Úvr   r   r	   r9   n   r:   )r)   r   r   r0   ÚpathlibÚPathÚexistsÚprintÚnameÚendswithÚlistr-   ÚstrÚlenr,   Ú
ClusterMapr   Znumber_of_edgesr   ÚdictZnodesÚvaluesÚsortedÚitemsÚsumZnodes_oftyper   )r   r$   r   r   r0   ÚpathZgraphsZigr   ÚcmZcounterÚcountÚqZsnodeZsdatÚsigr   r8   r	   r/   G   s:    



"
zparaview-blobsz	1.6*mm/uszDrift speed (with units)z(Absolute time of first tick (with units)zparaview-filec           	         sš   ddl m‰m} ddlm‰ ˆ d¡s.tdƒ tˆƒ‰tˆƒ‰tdˆtj	tj
  d›d	�ƒ d‡ ‡‡‡‡‡fd
d„	}t| ˆ ¡ƒD ]\}}|||ƒ q‚dS )z€
    Convert a cluster file to a ParaView .vtu files of blobs

    Speed and t0 converts time to relative drift coordinate.
    r   ©r   r   r   ©Ú
write_dataz.vtuzTwarning: blobs are written as UnstructuredGrid and paraview expects a .vtu extensionzdrift speed: ú.3fú mm/usc                    sb   ˆ  | ˆˆ¡} d|  ¡ kr,t dˆ  ¡ d S ˆ | ¡}ˆ}dˆv rJˆ| }ˆ||ƒ t |¡ d S r   )r   r   r   r   Zclusters2blobs)r   r    ÚdatÚfname©r$   r   Úparaview_filer&   r'   rU   r   r	   r(   ‹   s    

zparaview_blobs.<locals>.do_oneN)r   )r)   r   r   Útvtk.apirU   rD   rB   r   r   ÚmmÚusr,   r-   ©	r   r&   r'   r$   r[   r   r(   r    r   r   rZ   r	   Úparaview_blobss   s    
r`   zparaview-activityc           	         s|   ddl m‰m} ddlm‰ ˆ d¡s.tdƒ tˆƒ‰tˆƒ‰d
‡ ‡‡‡‡‡fdd„	}t| 	ˆ ¡ƒD ]\}}|||ƒ qdd	S )zB
    Convert cluster files to ParaView .vti files of activity
    r   rS   r   rT   z.vtizLwarning: activity is saved as an image and paraview expects a .vti extensionc                    s’   ˆ  | ˆˆ¡} tj ˆ¡\}}d|v r.|| }d|  ¡ krLt dˆ  ¡ d S ˆ | ¡}| ¡ D ].\}}|› d|› |› �}ˆ||ƒ t |¡ q^d S )Nr   r   r   z-plane)	r   ÚosrN   Úsplitextr   r   r   Zclusters2viewsrL   )r   r    rY   ÚextZalldatÚwpidrX   r"   rZ   r   r	   r(   ²   s    

z!paraview_activity.<locals>.do_oneN)r   )
r)   r   r   r\   rU   rD   rB   r   r,   r-   r_   r   rZ   r	   Úparaview_activity�   s    
re   zparaview-deposz-gz--generationzThe depo generation indexz-iz--indexzThe depos set index in the filezApply a drift speedz	depo-filec                 C   sÆ   ddl m} ddlm} ddlm  m}	 | d¡s<tdƒ |	 	|||¡}
t
|ƒ}|dur”t
|ƒ}td|tjtj  › d	�ƒ ||
d
 |  |
d< n|
d
  |7  < | |
¡}|||ƒ t |¡ dS )a�  
    Convert an NPZ file to a ParaView .vtp file of depos.

    If speed is given, a depo.X is calculated as (time+t0)*speed and
    depo.T is untouched.

    Else, depo.T will have t0 added and depo.X untouched.

    Note, a t0 of the ductors "start_time" will generally bring depos
    into alignement with products for simulated frames.

    See also "wirecell-gen plot-depos".
    r   )r   r   rT   Nz.vtpzGWarning: depos are saved as PolyData, paraview expects a .vtp extensionzapplying speed: rW   r   r   )r)   r   r\   rU   Zwirecell.gen.deposÚgenÚdeposrD   rB   r-   r   r   r]   r^   Z	depos2ptsr   r   )r   Z
generationÚindexr&   r'   Z	depo_filer[   r   rU   Zdeposmodrg   Zugridr   r   r	   Úparaview_deposÇ   s     



ri   z	bee-blobsz-oz--outputzThe output Bee JSON file name)r   z--geomZ	protodunez!The name of the detector geometryz--rseé   )r   r   r   z<The '<run> <subrun> <event>' numbers as a triple of integers)Únargsr   r   r   z-sz
--samplingÚcenterÚuniformz7The sampling technique to turn blob volumes into points)r   r   r   z-dz	--densityg      "@z6For samplings which care, specify target points per cczcluster-fileséÿÿÿÿ)rk   c                    sÆ  ddl m}m‰  t|ƒ}t|ƒ}t|d |d |d |dtƒ tƒ tƒ tƒ d�	}	dd„ }
ˆd	tjd
  9 ‰tˆ j‡ ‡fdd„d�| }|D �]}t| 	|¡ƒd }ˆ  
|||¡}td| ¡  ƒ d| ¡ krØtd| ƒ qˆˆ  ||¡}td||jd f ƒ |	d  |
|dd…df tj ƒ7  < |	d  |
|dd…df tj ƒ7  < |	d  |
|dd…df tj ƒ7  < |	d  |
|dd…d
f ƒ7  < qˆddl}ddlm} dd„ |_| |	t| ddd�¡ dS )z6
    Produce a Bee JSON file from a cluster file.
    r   r   r   é   z	wire-cell)	ZrunNoZsubRunNoZeventNoÚgeomr   r   r   r   rQ   c                 S   s   dd„ | D ƒS )Nc                 S   s   g | ]}t |d ƒ‘qS )rj   )Úround)r6   Úar   r   r	   r9     r:   z-bee_blobs.<locals>.fclean.<locals>.<listcomp>r   )Úarrr   r   r	   Úfclean  s    zbee_blobs.<locals>.fcleang      ð?rj   c                    s   ˆ   | ˆ¡S )N)Zblob_uniform_sample)r4   ©r   Údensityr   r	   Ú<lambda>  r:   zbee_blobs.<locals>.<lambda>)rl   rm   zgot %dzskipping empty graph %sz%s: %d pointsr   Nr   r   rQ   )Úencoderc                 S   s
   t | dƒS )NrV   )Úformat)Úor   r   r	   rw   0  r:   ÚwÚutf8)Úencoding)r)   r   r   r   rI   rE   r   rO   Zblob_centerr-   r   rB   r   Z
blobpointsÚshapeÚjsonrx   Z
FLOAT_REPRÚdumpÚopen)Úoutputrp   ZrseZsamplingr&   r'   rv   Zcluster_filesr   rX   rt   Zsampling_funcZctfr   rs   r   rx   r   ru   r	   Ú	bee_blobsú   s>    ÿþý
&&&"
rƒ   c                 C   s4   | dkrg d¢S | dkr g d¢S t d| › �ƒ | gS )z?
    Return list of channels in each plane based on total.
    i 
  )é�  r„   r„   r„   éà  r…   i@   )é`	  r†   i€  z8not a canonical number of channels in a known detector: )rB   )Znchr   r   r	   Údivine_planes4  s    r‡   ÚactivityzThe output plot file namez--slicesro   zRange of slice IDs)rk   r   r   z-Sz--slice-linezDraw a line down a slicec                 C   sJ  ddl m} ddlm}m}m}	 t|ƒ}t|ƒ}t| |¡ƒd }
t	 
|
||¡}
| |
¡}|	 |¡}|j}td|j› �ƒ tƒ }|r²|dd…|d |d …f }|d |d g}nd|jd g}||jd |jd g7 }tjddd�\}}| dd	¡ t d
¡}|j||d|ƒ |d�}|dk�rF|j||g|jd |jd gddd� d}t|jd ƒD ]:}||7 }||jd  }|j|dd… ||gddd� �qXddlm} |ƒ }|j |¡ |jddd� |jddd� |jddd� ztj||d� W n. t�y   tdƒ tdt  |¡ƒ Y dS 0 | !|¡ | "d¡ | #d¡ | $| ¡ dS ) z
    Plot activity
    r   )ÚLogNormr   ©r   r0   Úplotszchannel x slice array shape: N)ZnrowsZncolsg      !@g      &@Zgist_rainbowZnone)ÚcmapÚinterpolationZnormÚextentçš™™™™™¹?Úblack©Ú	linewidthÚcolorro   Zgray)r“   r’   )ÚAutoMinorLocatorZboth)ÚwhichÚwidthÚmajoré   )r•   ÚlengthÚminorrj   )Úaxz+colorbar complains, probably have zero dataztotal:úslice IDúchannel IDs)%Zmatplotlib.colorsr‰   r)   r   r0   r‹   r   rE   r-   r   r   rH   rˆ   rs   rB   r~   ÚrangeyÚpltZsubplotsZset_size_inchesZget_cmapZimshowr.   r‡   Zmatplotlib.tickerr”   ZyaxisZset_minor_locatorZtick_paramsZcolorbarÚ
ValueErrorÚnumpyrM   Ú	set_titleÚ
set_xlabelÚ
set_ylabelr   )r‚   ÚslicesÚ
slice_liner&   r'   r$   r‰   r   r0   r‹   r   rO   Úahistrs   rŽ   r!   r›   rŒ   ZimÚboundaryÚchunkr   r”   ZminorLocatorr   r   r	   rˆ   ?  s\    



ÿ"


zblob-activity-maskz--found/--missedTzMask what blobs found or missedc                 C   sŒ  ddl m}m}m}	 t|ƒ}t|ƒ}t| |¡ƒd }
t |
||¡}
| 	|
¡}|	 
|¡}| ¡ }|	 ||¡ |r~dd„ }d}ndd„ }d}tƒ }|râ|jd	d	…|d |d …f }|jd	d	…|d |d …f }|d |d g}n|j}|j}d|jd g}||jd |jd g7 }|	 ||||¡\}}|dk�rX|j||g|jd |jd gd
dd� | d||f ¡ | d¡ | d¡ | | ¡ d	S )z3
    Plot blobs as maskes on channel activity.
    r   rŠ   r   c                 S   s   | dkS ©Nr   r   ©rr   r   r   r	   rw   ¢  r:   z$blob_activity_mask.<locals>.<lambda>z
found maskc                 S   s   | dk S rª   r   r«   r   r   r	   rw   ¥  r:   zmissed maskNr�   r�   r‘   z%s %srœ   r�   )r)   r   r0   r‹   r   rE   r-   r   r   rH   rˆ   ZlikeZblobsrs   r~   rž   Z
mask_blobsr.   r¢   r£   r¤   r   )r‚   r¥   r¦   r&   r'   Úfoundr$   r   r0   r‹   r   rO   r§   ZbhistZselÚtitlerŽ   rr   r4   r!   r›   r   r   r	   Úblob_activity_mask…  s@    


ÿ

r®   zwire-slice-activityz	--sliceidzThe slice ID to plot)r   r   c                 C   sh   ddl m}m}m} t|ƒ}t|ƒ}t| |¡ƒ}t |||¡}| 	|¡}	| 
|	|¡\}
}|
 | ¡ dS )z;
    Plot the activity in one slice as wires and blobs
    r   rŠ   N)r)   r   r0   r‹   r   Únextr-   r   r   rH   Zwire_blob_slicer   )r‚   Zsliceidr&   r'   r$   r   r0   r‹   r   rO   r!   Zaxesr   r   r	   Úwire_slice_activity¼  s    
r°   Úanidfgz
anidfg.gifzOutput fileÚlogfilec                 C   s4   ddl m} | t|ƒ¡}| |¡}| || ¡ dS )zg
    Produce an animated graph visualization from a log produced by
    TbbFlow with "dfg" output.
    r   )r±   N)r)   r±   Z	parse_logr�   Zgenerate_graphZrender_graph)r‚   r²   r±   ÚlogZgar   r   r	   r±   Ò  s    
c                   C   s   t tƒ d� d S )N)Úobj)r
   rI   r   r   r   r	   Úmainà  s    rµ   Ú__main__)r   ))Ú__doc__ra   r   r   r?   Úcollectionsr   r¡   Zmatplotlib.pyplotZpyplotrŸ   Úwirecellr   Zwirecell.util.functionsr   Zwirecell.utilr   rI   ZcmddefÚgroupZpass_contextr
   ZcommandÚoptionZChoiceZargumentr*   r/   r`   re   ri   ÚintÚfloatrƒ   r‡   rˆ   r®   r°   r±   rµ   Ú__name__r   r   r   r	   Ú<module>   s(  	ÿÿ
þ)ÿÿ"ÿÿ"
ÿ
ÿÿÿ'
ÿÿÿÿÿÿ+ÿÿÿÿ;ÿÿÿÿÿ*ÿÿ
