³ò
C±¶Jc        	   @   sh   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d e f d „  ƒ  YZ d S(   s+   
twoD.py

Library for handling 2D datasets
iÿÿÿÿNt   Datasetc           B   sú  e  Z d  Z d0 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z e e d
 „ ƒ Z e d „ Z e d „  ƒ Z d d „ Z d „  Z d „  Z d d d „ Z e d „ Z d0 d0 d0 d0 d „ Z d d d0 d0 d „ Z d d d0 d0 d0 e d „ Z d0 d0 e d „ Z d0 d0 d e d „ Z d „  Z d „  Z d d „ Z d e d  „ Z d d! e d" „ Z  d# d$ d% „ Z! d# d$ d& d0 d' „ Z" d d( „ Z# d d) „ Z$ d! d* „ Z% d d! d+ „ Z& e d d, d- „ ƒ Z' e( d. „ Z) d d d/ „ Z* RS(1   s   
    2D data set
    c         C   s  t  i | ƒ } | d j	 o t  i | ƒ } n t  i | i ƒ } t  i | ƒ on t  i | ƒ o^ t | i ƒ d j o t d ƒ ‚ n t  i | i d ƒ | } t  i | i d ƒ | } nt  i | ƒ } t  i | ƒ } t | i ƒ d j oˆ| i | i j ou| i | i j obt  i t	 t
 | ƒ ƒ ƒ } h  } x( t t | ƒ ƒ D] } | | | | <qMWt  i t	 t
 | ƒ ƒ ƒ } h  }	 x( t t | ƒ ƒ D] } | |	 | | <q™Wt  i t | ƒ t | ƒ g ƒ t  i }
 t  i t | ƒ t | ƒ g ƒ } xb t t | ƒ ƒ D]N } | | |
 | | | |	 | | f <| | | | | | |	 | | f <qW| } | } |
 } | } nU t | i ƒ d j o+ | i d  | i j o | i d | i j p t d ƒ ‚ n | |  _ | |  _ | |  _ | |  _ t i ƒ  d S(   s„  
        Dataset(x, y, z)
        Create a dataset.
        
        formats:
        
        1) x, y - scalars, z - 2D array NxM.
            z - the data, x and y are used as steps in the NxM grid
        
        2) x, y - N and M long 1D arrays, z - 2D array NxM.
            z - the data, x and y are used as the points of the grid
        
        3) x, y, z - 1D arrays
            create a grid based on all combinations of values in x and y
            and fill it with z, set NaN where z is not specified.
            
        1a), 2a), 3) - similar to 1-3, but an extra argument 'mask' is supplied, similar in shape to 'z'.
        i   s'   'z' should be 2D, if x and y are scalari    i   s   Unsupported argumentsN(   t   numpyt   asarrayt   Nonet   onest   shapet   isscalart   lent
   ValueErrort   aranget   sortedt   sett   ranget   zerost   NaNt   xt   yt   zt   maskt   gct   collect(   t   selfR   R   R   R   t   Xt   XXt   it   Yt   YYt   Zt   M(    (    s   c:\py\lib\twoD.pyt   __init__   sN     <    (! $(
D				c         C   s   t  |  i ƒ S(   s   number of columns(   R   R   (   R   (    (    s   c:\py\lib\twoD.pyt   n_xO   s    c         C   s   t  |  i ƒ S(   s   number of rows(   R   R   (   R   (    (    s   c:\py\lib\twoD.pyt   n_yP   s    c         C   sK   |  i  ƒ  d j o1 t |  i i ƒ  |  i i ƒ  ƒ |  i  ƒ  d n t i S(   s   average column spacingi    i   (   R   t   floatR   t   maxt   minR   R   (   R   (    (    s   c:\py\lib\twoD.pyt   dxR   s    c         C   sK   |  i  ƒ  d j o1 t |  i i ƒ  |  i i ƒ  ƒ |  i  ƒ  d n t i S(   s   average row spacingi    i   (   R   R    R   R!   R"   R   R   (   R   (    (    s   c:\py\lib\twoD.pyt   dyS   s    c         C   s   |  i  i ƒ  S(   N(   R   R"   (   R   (    (    s   c:\py\lib\twoD.pyt   min_xT   s    c         C   s   |  i  i ƒ  S(   N(   R   R!   (   R   (    (    s   c:\py\lib\twoD.pyt   max_xU   s    c         C   s   |  i  i ƒ  S(   N(   R   R"   (   R   (    (    s   c:\py\lib\twoD.pyt   min_yV   s    c         C   s   |  i  i ƒ  S(   N(   R   R!   (   R   (    (    s   c:\py\lib\twoD.pyt   max_yW   s    c         C   sq   t  i |  | o d n d ƒ } | o | i ƒ  \ } } } } n | i ƒ  \ } } } d  } t | | | | ƒ S(   Ni   i   (   t   ascii2numpyt	   loadasciit	   transposeR   R    (   t   filenamet	   load_maskt   dataR   R   R   R   (    (    s   c:\py\lib\twoD.pyR*   Y   s     c         C   s=   |  i  | ƒ } t i | | d t d | o d n d ƒd  S(   Nt   appendt   headers"   # [0] x
# [1] y
# [2] z
# [3] masks   # [0] x
# [1] y
# [2] z(   t   flattent   flibt	   saveasciit   False(   R   R,   t	   save_maskR.   (    (    s   c:\py\lib\twoD.pyR3   c   s    c         C   s´   t  i |  d t ƒd } yu | d } | d } | d i t | ƒ t | ƒ g ƒ } | d i t | ƒ t | ƒ g ƒ } t | | | | ƒ SWn t j
 o t d ƒ ‚ n Xd  S(   Nt   fulli   R   R   R   R   s   Invalid file format(   R2   t
   loadbinaryt   Truet   reshapeR   R    t   KeyErrorR   (   R,   t   dR   R   R   R   (    (    s   c:\py\lib\twoD.pyR7   h   s    

%%t    c         C   s_   t  i | h  t | ƒ d <|  i d <|  i d <|  i i ƒ  d <|  i i ƒ  d <ƒ d  S(   NR0   R   R   R   R   (   R2   t
   savebinaryt   reprR   R   R   R1   R   (   R   R,   R0   (    (    s   c:\py\lib\twoD.pyR=   t   s    c         C   s#   t  i |  i g |  i ƒ  ƒ i ƒ  S(   s7   return a 2D array of x coordinates of corresponding z's(   R   t   vstackR   R   R+   (   R   (    (    s   c:\py\lib\twoD.pyt   x2Dw   s    c         C   s   t  i |  i g |  i ƒ  ƒ S(   s7   return a 2D array of y coordinates of corresponding z's(   R   R?   R   R   (   R   (    (    s   c:\py\lib\twoD.pyt   y2D{   s    g        c         C   s(   |  i  ƒ  | d |  i ƒ  | d d S(   sT   return a 2D array of distances from given point (x0,y0) to the points of the Dataseti   g      à?(   R@   RA   (   R   t   x0t   y0(    (    s   c:\py\lib\twoD.pyt   r2D   s    c         C   sh   |  i  ƒ  i ƒ  } |  i ƒ  i ƒ  } |  i i ƒ  } | o | | | |  i i ƒ  f Sn | | | f Sd S(   s3   convert into three/four 1D arrays: x, y, z (, mask)N(   R@   R1   RA   R   R   (   R   t   flatten_maskR   R   R   (    (    s   c:\py\lib\twoD.pyR1   ƒ   s    c         C   s  | d  j o |  i ƒ  } n | d  j o |  i ƒ  } n | d  j o |  i ƒ  } n | d  j o |  i ƒ  } n |  i | j |  i | j @} |  i | j |  i | j @} t |  i | |  i | |  i | d  d  … f d  d  … | f |  i	 | d  d  … f d  d  … | f ƒ S(   N(
   R   R%   R&   R'   R(   R   R   R    R   R   (   R   R%   R'   R&   R(   t   ii_xt   ii_y(    (    s   c:\py\lib\twoD.pyt   window   s        iÿÿÿÿc         C   s   | d j	 o t | |  i ƒ  ƒ } n | d j	 o t | |  i ƒ  ƒ } n |  i ƒ  } |  i ƒ  } | | j p | d j  o | d } n | | j p | d j  o | d } n t i |  i t	 i
 |  i d t ƒ| | ƒ \ } } }	 }
 t i ƒ  t | |  i ƒ  | |  i ƒ  |	 |
 ƒ S(   sÏ  
        Calculate 2D correlation over the dataset.
        The range of Delta x, Delta y is symmetric around origin and
        defined by max_di/max_dx, max_dj/max_dy.
        max_di and max_dj define the range in units of dx() and dy().
        max_dx and max_dy define them in the units of the original dataset.
        
        'mask' of the dataset is used as a boolean map:
        only points with 'mask != 0' are used.
        
        The result is return as a Dataset object with a range spanning
        from -max_dx, -max_dy to max_dy, max_dy.
        In the result 'mask' contains numbers of pairs of points of the
        original dataset used to calculate a particular point in the correlation.
        i    i   t   dtypeN(   R   t   intR#   R$   R   R   t   _twoDt   correlationR   R   R   R   t   boolR   R   R    (   R   t   max_dit   max_djt   max_dxt   max_dyR   R   t   iit   jjt   Kt   N(    (    s   c:\py\lib\twoD.pyRL   ç   s    9
i   c      	   C   s6  | d j og | d j	 o t | |  i ƒ  ƒ } n | d j	 o t | |  i ƒ  ƒ } n t i | | g ƒ } nE t i | ƒ } t | i ƒ d j o t	 d ƒ ‚ n | i \ } } t i | d t i
 ƒ} | t i | ƒ :} | d } | d } |  i ƒ  | | d }	 |  i ƒ  | | d }
 t i |  i | ƒ } | o' |  i | |	 … | |
 … f | } n t i ƒ  |  i ƒ  | d d j o. d |  i | |	 !|  i | d |	 d !} n |  i | |	 !} |  i ƒ  | d d j o. d |  i | |
 !|  i | d |
 d !} n |  i | |
 !} t | | | ƒ S(   s3  
        Apply a low pass filter to the dataset.
        The filter has a step-function kernel, e.g.
        For every point except near the edge a rectangle di by dj points around it are averaged
        to get value value. The rectangle can be defined in number of pixels
        using 'di' and 'dj' or in lengths using 'dx' and 'dy'.
        
        'filter_mask' can be used to make a non-square filtering window.
        It is then a filtering 
        of different points within a window.
        
        The result is return as a Dataset object with a di by dj smaller size
        than the original one.
        
        If 'residue' is True, the difference between a subset of the original
        dataset, in a window where the filtered version exists,
        and the filtered version is returned.
        i   s"   'filter_mask' should be a 2D arrayRI   i   i    g      à?N(   R   RJ   R#   R$   R   R   R   R   R   R   R    t   sumR   R   RK   t   filterR   R   R   R   R   R    (   R   t   dit   djR#   R$   t   filter_maskt   residuet   min_it   min_jt   max_it   max_jt   fR   R   (    (    s   c:\py\lib\twoD.pyRW     s6    

'
..c         C   s-  | d  j o | d  j o t d ƒ ‚ n | d  j	 o | |  i ƒ  } n t t i | ƒ ƒ } | d j  o |  i ƒ  Sn t t i | | d ƒ t i | | d ƒ t i	 d | d d | d g ƒ ƒ } | i
 ƒ  } | i ƒ  } | d | d d } t i | | j d t ƒ} |  i d | d | ƒ S(   Ns%   Either 'n' or 'r' should be specifiedi   i   g      à?RI   RZ   R[   (   R   R   R#   RJ   R   t   ceilt   copyR    R	   R   R@   RA   R   RW   (   R   t   nt   rR[   R   R   R   (    (    s   c:\py\lib\twoD.pyt   round_filter?  s    Si   c         C   s!  | d  j o | d  j o t d ƒ ‚ n | d  j	 o | |  i ƒ  } n t t i | ƒ ƒ } | d j  o |  i ƒ  Sn t t i | | | | d ƒ t i | | | | d ƒ t i	 d | | d d | | d g ƒ ƒ } t i
 d | i ƒ  d | d ƒ } |  i d | d | ƒ S(   Ns%   Either 'n' or 'r' should be specifiedi   i   g      à¿g      ð?RZ   R[   (   R   R   R#   RJ   R   Ra   Rb   R    R	   R   t   expRD   RW   (   R   Rc   Rd   RU   R[   R   (    (    s   c:\py\lib\twoD.pyt   round_gaussian_filterP  s    k%c         C   s@   d |  i  ƒ  |  i ƒ  |  i ƒ  |  i ƒ  |  i ƒ  |  i ƒ  f S(   Ns'   2D Dataset (%dx%d pixels, %.3gx%.3g um)(   R   R   R&   R%   R(   R'   (   R   (    (    s   c:\py\lib\twoD.pyt   __repr__^  s    c         C   s7   t  |  i i ƒ  |  i i ƒ  |  i i ƒ  |  i i ƒ  ƒ S(   s   return a copy of the dataset(   R    R   Rb   R   R   R   (   R   (    (    s   c:\py\lib\twoD.pyRb   `  s    id   c         C   s   t  i |  i | d t d t ƒS(   s@   
        return a histogram of the height distribution.
        t   normedt   new(   R   t	   histogramR   R8   (   R   t   bins(    (    s   c:\py\lib\twoD.pyt   hist_zd  s    c   	      C   s¦   |  i  t ƒ \ } } } } | | j } | d | d d } | | | | | | } } } t i | ƒ } | o | | | | | | f Sn | | | | f Sd S(   s•   
        return a tuple of (r, z), where r=(x**2 + y**2)**0.5 is the distance
        from the origin to the specified point on the dataset.
        i   g      à?N(   R1   R8   R   t   argsort(	   R   t   minNt   return_maskR   R   R   RU   RR   Rd   (    (    s   c:\py\lib\twoD.pyt   rzj  s     iè  c   	      C   sä   |  i  | t ƒ \ } } } t i | ƒ oC t | ƒ t | ƒ | } t i t | ƒ t | ƒ | | ƒ } n t i | ƒ } t i	 | | | | ƒ \ } } } | d j } | o | | | | | | f Sn | | | | f Sd S(   sŽ   
        return a tuple of (r, z), where results of rz() are grouped together based on 'bins'
        and averaged within each group.
        i    N(
   Rq   R8   R   R   R!   R"   R	   R   RK   t   group1d(	   R   Ro   Rl   Rp   Rd   R   RU   t   drRR   (    (    s   c:\py\lib\twoD.pyt   group_rzy  s    )!t   nearestt   lowerc      
   K   s–   |  i  ƒ  |  i ƒ  | d j o |  i ƒ  n
 |  i ƒ  | d j o |  i ƒ  n
 |  i ƒ  g } d d k l } | |  i i ƒ  d | d | d | | S(   s1   
        Plot the map using pylab.imshow
        Rv   iÿÿÿÿ(   t   imshowt   interpolationt   origint   extent(   R%   R&   R'   R(   t   pylabRw   R   R+   (   R   Rx   Ry   t   vargsRz   Rw   (    (    s   c:\py\lib\twoD.pyt   plotŒ  s    #)t   verticalc   
      K   si   d d k  l } l } |  i | | |  } | d | ƒ }	 | d j	 o |	 i | ƒ | ƒ  n | |	 f S(   s1   
        Plot the map using pylab.imshow
        iÿÿÿÿ(   t   colorbart   drawt   orientationN(   R{   R   R€   R}   R   t	   set_label(
   R   Rx   Ry   R   t   labelR|   R   R€   t   mt   p(    (    s   c:\py\lib\twoD.pyt   plot_and_colorbar˜  s    c         K   s[   d d k  l } |  i | ƒ \ } } | d | d  } | d | d  d | | d | |  S(   sD   
        plot a histogram (PDF) of the height distribution.
        iÿÿÿÿ(   t   bari   t   leftt   heightt   width(   R{   R‡   Rm   (   R   Rl   R|   R‡   Rc   t   w(    (    s   c:\py\lib\twoD.pyt   plot_hist_z¥  s    c         K   s5   d d k  l } |  i | ƒ \ } } | | | |  S(   sP   
        plot a dependence of the height 'z' on the size of the matrix.
        iÿÿÿÿ(   R}   (   R{   R}   Rq   (   R   Ro   R|   R}   Rd   R   (    (    s   c:\py\lib\twoD.pyt   plot_rz®  s    c         K   s~   d d k  l } d |  i (|  i d d d | ƒ \ } } t i t i | ƒ ƒ t i t i | ƒ ƒ @} | | | | | |  S(   Niÿÿÿÿ(   t   loglogg      ð?Ro   i    Rl   (   R{   RŽ   R   Rt   R   t   isfinitet   log(   R   Rl   R|   RŽ   Rd   R   RR   (    (    s   c:\py\lib\twoD.pyt   plot_log_rz¶  s
    
.c         K   s8   d d k  l } |  i | | ƒ \ } } | | | |  S(   sP   
        plot a dependence of the height 'z' on the size of the matrix.
        iÿÿÿÿ(   R}   (   R{   R}   Rt   (   R   Ro   Rl   R|   R}   Rd   R   (    (    s   c:\py\lib\twoD.pyt   plot_grouped_rz½  s    g      ð?c      	   C   s4   d d k  } t | | | i i | | |  | f ƒ ƒ S(   s¶   
        return a randomly generated dataset Nx by Ny points at 'dx'/'dy' spacing,
        heights are uncorrelated normally distributed with given 'mean' and 'std'.        
        iÿÿÿÿN(   t   numpy.randomR    t   randomt   normal(   t   Nxt   NyR#   R$   t   meant   stdR   (    (    s   c:\py\lib\twoD.pyR”   Å  s    c   	      C   s  | oÊ |  i  } | i \ } } t d t i t i | ƒ ƒ ƒ } t d t i t i | ƒ ƒ ƒ } | | d | d | d | | d … d  d  … f d  d  … | d | d | d | | d … f } n
 |  i  } t i i | ƒ |  i ƒ  |  i	 ƒ  } | i \ } } | t i
 t | d | ƒ t d | d ƒ g ƒ d  d  … f d  d  … t i
 t | d | ƒ t d | d ƒ g ƒ f } t d |  i ƒ  | t i | d | | d ƒ d |  i	 ƒ  | t i | d | | d ƒ t | ƒ t i | ƒ ƒ } t i ƒ  | S(   Ni   i    g      ð?(   R   R   RJ   R   t   floort   log2t   fftt   fft2R#   R$   t   concatenateR   R    R	   t   absR2   t   phaseR   R   (	   R   t   best_cutR   R   R   t   N_xt   N_yR‹   R   (    (    s   c:\py\lib\twoD.pyRœ   Î  s     	""n	&E30-
c         C   s×   |  i  ƒ  } | i t i | i ƒ 8_ | d j oI | i t i t i | i ƒ  i ƒ  | i i ƒ  | ƒ | i ƒ  ƒ 8_ n | d j oI | i t i t i | i ƒ  i ƒ  | i i ƒ  | ƒ | i ƒ  ƒ 8_ n | S(   s!  
        return a copy of the dataset with a background subtracted.
        the background is a sum of best fit polynomials of orders 'Nx' and 'Ny' in x and y,
        respectively. If both equal to 0, only the mean value is subtracted.
        The mask is not taken into account.
        i    (	   Rb   R   R   R˜   t   polyvalt   polyfitR@   R1   RA   (   R   R–   R—   t   s(    (    s   c:\py\lib\twoD.pyt   sub_polyâ  s    IIN(+   t   __name__t
   __module__t   __doc__R   R   R   R   R#   R$   R%   R&   R'   R(   t   staticmethodR4   R*   R3   R7   R=   R@   RA   RD   R1   RH   RL   RW   Re   Rg   Rh   Rb   Rm   Rq   Rt   R}   R†   RŒ   R   R‘   R’   R”   R8   Rœ   R§   (    (    (    s   c:\py\lib\twoD.pyR       sN   ?											X 8			(	   Rª   R   R2   R)   R   t	   numpy.fftRK   t   objectR    (    (    (    s   c:\py\lib\twoD.pys   <module>   s   0