a
    ¦ˆc  ã                   @   sÐ   d Z ddlZddlZddlmZ ddlZddlm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZmZ G dd„ deƒZej e¡Zej ed¡Zej ed	¡Zeed
dej ¡ dd�ZeZe dkrÌe !¡  dS )a;  
Visualization
=============

Traits allows you to instantly create a graphical user interface
for your ``HasTraits`` classes.  If you have TraitsUI and a suitable
GUI toolkit (such as PyQt5 or PySide2) installed in your Python
environment then you can create a dialog view of an instance of
your class by simply doing::

    >>> image.configure_traits()

This gives you a default UI out of the box with no further effort,
but it usually is not what you would want to provide for your users.

With a little more effort using the features of TraitsUI, you can
design a dialog which is more pleasing::

    from traitsui.api import Item, View

    class Image(HasStrictTraits):
        ...

        view = View(
            Item('filename'),
            Item('sample_id', label='Sample ID'),
            Item('operator'),
            Item('date_acquired'),
            Item('scan_width', label='Width (m):'),
            Item('scan_height', label='Height (m):')
        )

TraitsUI can be used as the building block for complete scientific
applications, including 2D and 3D plotting.

é    N)ÚImage)
ÚArrayÚDateÚFileÚFloatÚHasStrictTraitsÚPropertyÚStrÚTupleÚcached_propertyÚobserve)ÚItemÚViewc                   @   s  e Zd ZdZedd�Zeƒ ZedƒZe	ƒ Z
eeeƒZeedd�Zeedd�Zeddd	�Zeed
d�Zeedd�Zd2dd„Zedƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zed!d"„ ƒZ e!e"dƒe"d#d$d%�e"d&ƒe"d'ƒe"d(d)d%�e"d*d+d%�e"d,d-d.„ d/d0�ƒZ#d1S )3r   z  An SEM image stored in a file. T)ÚexistszN/AÚ	scan_size)Z
depends_on)NNZuint8)ÚshapeZdtypezscan_height,scan_width,imageÚimager   éÿ   c                 C   s   | j |k| j |k@ S )z) Compute a threshold mask for the array. )r   )ÚselfÚlowÚhigh© r   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/examples/introduction/6_visualization.pyÚ	threshold^   s    zImage.thresholdÚfilenamec                 C   s"   t  | j¡ d¡}t |¡| _d S )NÚL)ÚPILImageÚopenr   ÚconvertÚnpÚarrayr   )r   ÚeventZ	pil_imager   r   r   Ú
read_imaged   s    zImage.read_imagec                 C   s
   t j ¡ S )N)ÚdatetimeÚdateÚtoday©r   r   r   r   Ú_date_acquired_defaultk   s    zImage._date_acquired_defaultc                 C   s   dS )N©çñhãˆµøä>r)   r   r&   r   r   r   Ú_scan_size_defaultn   s    zImage._scan_size_defaultc                 C   s
   | j d S ©Nr   ©r   r&   r   r   r   Ú_get_scan_widths   s    zImage._get_scan_widthc                 C   s   || j d f| _ d S ©Né   r,   ©r   Úvaluer   r   r   Ú_set_scan_widthv   s    zImage._set_scan_widthc                 C   s
   | j d S r.   r,   r&   r   r   r   Ú_get_scan_heighty   s    zImage._get_scan_heightc                 C   s   | j d |f| _ d S r+   r,   r0   r   r   r   Ú_set_scan_height|   s    zImage._set_scan_heightc                 C   s(   | j jdkr | j| j | j j S dS d S r+   )r   ÚsizeÚscan_heightÚ
scan_widthr&   r   r   r   Ú_get_pixel_area   s    zImage._get_pixel_areac                 C   s   t j| jdddd�\}}|S )Né   )r   r9   T)ÚbinsÚrangeZdensity)r   Ú	histogramr   )r   Úhistr:   r   r   r   Ú_get_histogram…   s    ü
zImage._get_histogramÚ	sample_idz	Sample ID)ÚlabelÚoperatorÚdate_acquiredr7   z
Width (m):r6   zHeight (m):Ú
pixel_areac                 C   s   d  | d ¡S )Nu   {:0.3f} ÂµmÂ²g ÈNgmÁ«C)Úformat)Úarear   r   r   Ú<lambda>š   ó    zImage.<lambda>Úreadonly)Zformat_funcÚstyleN)r   r   )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r	   r?   rA   r   rB   r
   r   r   r   r7   r6   r   r   rC   r<   r   r   r"   r'   r*   r-   r2   r3   r4   r8   r   r>   r   r   Úviewr   r   r   r   r   =   sH   







ýùr   Zimageszsample_0001.pngZHannesZ0001r(   )r   rA   r?   rB   r   Ú__main__)"rM   Úosr#   ZPILr   r   Únumpyr   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   Ztraitsui.apir   r   ÚpathÚdirnameÚ__file__Zthis_dirÚjoinZ	image_dirr   r%   r   ÚdemorJ   Zconfigure_traitsr   r   r   r   Ú<module>   s(   %0gû