a
    ƒ³bt	  ã                   @   sZ   d Z ddlZddlZddlmZ ddlmZ ddlZG dd„ de	ƒZ
dd„ Zd
dd	„ZdS )z'
Some general purpose plotting helpers
é    N)ÚPdfPagesc                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚNameSequencer   c                 C   s   t j |¡\| _| _|| _dS )a(  
        Every time called, emit a new name with an index.

        Name may be a filename with .-separated extension.

        The name may include zero or one %-format mark which will be
        fed an integer counting the number of output calls.

        If no format marker then the index marker is added between
        file base and extension (if any).

        The first may give the starting index sequence or if None no
        sequence will be used and the name will be kept as-is.

        This is a callable and it mimics PdfPages.
        N)ÚosÚpathÚsplitextÚbaseÚextÚindex)ÚselfÚnameÚfirst© r   ú?/home/yuhw/wc/wire-cell-python-pr-11/wirecell/util/plottools.pyÚ__init__   s    zNameSequence.__init__c                 C   sf   | j d u r| j| j S z| j| j  }W n" tyH   d| j| j f }Y n0 |  j d7  _ || j }|S )Nz%s%04dé   )r	   r   r   Ú	TypeError)r
   ÚfnÚretr   r   r   Ú__call__!   s    

zNameSequence.__call__c                 O   sB   | ƒ }t j |¡}|r,t j |¡s,t  |¡ tj|fi |¤Ž dS )z#
        Act like PdfPages
        N)r   r   ÚdirnameÚexistsÚmakedirsÚpltÚsavefig)r
   ÚargsÚkwdsr   Zdirnr   r   r   r   .   s
    
zNameSequence.savefigc                 C   s   | S ©Nr   )r
   r   r   r   Ú	__enter__8   s    zNameSequence.__enter__c                 C   s   d S r   r   )r
   ÚtypÚvalueÚ	tracebackr   r   r   Ú__exit__:   s    zNameSequence.__exit__N)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r!   r   r   r   r   r      s
   

r   c                 C   s   |   d¡rt| ƒS t| ƒS )Nz.pdf)Úendswithr   r   )r   r   r   r   Úpages>   s    
r&   c                 C   s    |du rt  t  | ¡¡}|s$d| }| j}t  | ¡ d¡} d| t  | |k | | k¡< | |k}| | k }t  | | | ¡| |< t  | |  | ¡ | |< |  |¡S )a*  
    Apply the "signed log" transform to an array.

    Result is +/-log10(|arr|*scale) with the sign of arr preserved in
    the result and any values that are in eps of zero set to zero.

    If eps is not given it is the smalles absolute value

    If scale is not given then 1/eps is used.
    Nr   éÿÿÿÿg        )ÚnumpyÚminÚabsÚshapeÚarrayÚreshapeÚlogical_andÚlog10)ÚarrÚepsÚscaler+   ÚposÚnegr   r   r   Úlg10D   s    
r5   )NN)Ú__doc__r   r(   ÚnpZmatplotlib.backends.backend_pdfr   Úmatplotlib.pyplotÚpyplotr   Úobjectr   r&   r5   r   r   r   r   Ú<module>   s   2