
6Oc           @   s  d  Z  d d l m Z d Z d d l m Z d d l Z d d l Z e j d  Z d d l	 Z
 e
 j d d  d d l j Z d d	 l m Z m Z g  Z y d d l Z e j d
  Wn e j d  n Xy d d l Z e j d  Wn e j d  n Xd e f d     YZ d e f d     YZ d e f d     YZ d dM d     YZ e d   e d
 d d g e  e d d g e  g D  Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# d  dN d!     YZ$ d" e f d#     YZ% d$ e f d%     YZ& d& e& f d'     YZ' d( e' f d)     YZ( d* e' f d+     YZ) d, e' f d-     YZ* d. e' f d/     YZ+ d0 e' f d1     YZ, d2 dO d3     YZ- d4   Z. d5   Z/ d6 d6 d7 d8  Z0 e1 d9  Z2 d: d;  Z3 e2 d< d d= e5 e
 j6 d> d> g  d?  Z7 d@   Z8 e5 dA  Z9 dB   Z: dC   Z; d6 d6 d6 dD  Z< dE   Z= dF   Z> d dG  Z? d6 d6 dH  Z@ dI   ZA dJ   ZB dK   ZC eD dL k rd d lE ZE eE jF   n  d S(P   sH    Base class providing common functionality for analyzing Leed patterns. i(   t   divisiong333333?(   t   UserListNt   leedbaset   allt   raise(   t   optimizet   ndimaget   FITSs-   The pyfits package is not properly installed.t   IMGs2   The pil package (Image) is not properly installed.t   ImageLoaderc           B   sM   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s    Abstract base class for a class loading LEED images.

    Subclasses need to provide
        - get_energy(image_path)
        - get_image(image_path)
    c         C   s\   i  |  _  x* | D]" } |  j |  } | |  j  | <q Wt |  j  j    |  _ |  j   d  S(   N(   t   filest
   get_energyt   sortedt   keyst   energiest   restart(   t   selft   image_pathst
   image_patht   energy(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   __init__)   s    	c         C   s   |  j  |  j S(   s    Get current energy. (   R   t   index(   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   current_energy2   s    c         C   s   |  S(   N(    (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   __iter__6   s    c         C   s   d |  _  d S(   s    Start at lowest energy again. iN(   R   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   9   s    c         C   s[   |  j  d k r t d   n9 |  j  d 8_  |  j |  j  } |  j |  j |  | f Sd S(   s&    Get image at next lower beam energy. i    s   there is no previous imagei   N(   R   t   StopIterationR   t	   get_imageR
   (   R   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   previous=   s
    c         C   sb   |  j  t |  j  d k  rU |  j  d 7_  |  j |  j  } |  j |  j |  | f St    d S(   s'    Get image at next higher beam energy. i   N(   R   t   lenR   R   R
   R   (   R   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   nextF   s
    c         c   sa   t  |  t  |  j  } | r8 t d t |    n  x" | D] } |  j |  | f Vq? Wd S(   s5    Returns an iterator to iter over the given energies.s3   ImageLoader doesn't have the following elements: %sN(   t   setR   t	   Exceptiont   listR   (   R   R   t   non_elementsR   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   custom_iterP   s
    (
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R!   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR	   "   s   								
t   ImgImageLoaderc           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         K   s   t  j |  |  d  S(   N(   R	   R   (   R   R
   t   kwargs(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   Y   s    c         C   s-   t  | d   } |  j |  d SWd  QXd  S(   Nt   rbs   Beam Voltage (eV)(   t   opent   load_header(   R   R   t   f(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   \   s    c         C   sf  | j    } x d | k r* | j    } q Wt | j d  d j    } | j d  | j |  } i d d 6d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6} | j d  } x | D] } | j d  } | d | j   k r t | | d  t d  k r t | d  | | d <q^t | | d  t d  k r^| d j   | | d <q^q q W| S(   Ns   Header length:s   : i   i    s   Beam Voltage (eV)t    t   Datet   Commentt   x1t   y1t   x2t   y2s   Number of framess   
(   t   readlinet   intt   splitt   stript   seekt   readR   t   type(   R   R*   t   linet   header_lengtht
   header_rawt   headert   headerlinest   parts(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR)   `   s     >  "c         C   s   t  | d  | } |  j |  } | j   } | d | d d | d | d d f } t j d | | d d	 d
 d  } t j |  SWd  QXd  S(   NR'   R0   R.   i   R1   R/   t   Ft   raws   F;16i    (   R(   R)   R7   t   Imaget
   fromstringt   npt   asarray(   R   R   R*   R<   t   contentt   sizet   pilimage(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   y   s    ,!(   R"   R#   R   R   R)   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR%   X   s   			t   FitsImageLoaderc           B   s&   e  Z d  d  Z d   Z d   Z RS(   s   \d{1,3}(?=.fit)c         C   s   | |  _  t j |  |  d  S(   N(   t   regexR	   R   (   R   R
   RI   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR      s    	c         C   s%   t  j |  j |  } t | j    S(   N(   t   ret   searchRI   R3   t   group(   R   R   t   m(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR      s    c         C   sX   y. t  j |  } | d j } | j   | SWn# t k
 rS d | GHt    n Xd  S(   Ni    s   IOError while processing %s(   t   pyfitsR(   t   datat   closet   IOError(   R   R   t   hdulistRO   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR      s    
	(   R"   R#   R   R   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyRH      s   	t   ImageFormatc           B   s    e  Z d  Z d   Z d   Z RS(   s!   Class describing an image format.c         C   s   | |  _  | |  _ | |  _ d S(   s   
        abbrev: abbreviation (e.g. FITS)
        extensions: list of corresponding file extensions (e.g. .fit, .fits)
        loader: ImageLoader subclass for this format
        N(   t   abbrevt
   extensionst   loader(   R   RT   RU   RV   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR      s    		c         C   s   d j  |  j d j |  j   S(   Ns   {0}-Files ({1})t    (   t   formatRT   t   joinRU   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   __str__   s    (   R"   R#   R$   R   RZ   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyRS      s   		c         c   s0   |  ]& } | j  t k r t |  | g Vq d  S(   N(   RT   t   formats_availablet   str(   t   .0t   format_(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pys	   <genexpr>   s    s   *.fits   *.fitss   *.imgc         C   s`   |  j    |  j   } } | r, |  | }  n  d | | } | d k rS |  | }  n  |  j d  S(   s%    Returns a normalized array of uint8.g     o@g      ?t   uint8(   t   mint   maxt   astype(   t   arrayt   nmint   nmaxt   scale(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   _normalize255   s    c         C   sv   |  j  \ } } t |   }  t j |  j | | t j j  } x3 t d  D]% } | j | t j | | |   qI W| S(   s*    Converts numpy grayscale image to qimage.i   (	   t   shapeRg   t   _qtt   QImageRO   t   Format_Indexed8t   ranget   setColort   qRgb(   t   npimaget   ht   wt   qimaget   i(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   npimage2qimage   s    !#c            s7   t  t |   t |   d d      f d     D S(   s    returns int points lying in a square centered on 0,0 with given size.

        >>> [point for point in points_in_square(1)]
        [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]
    i   c         3   s(   |  ] }   D] } | | f Vq q d  S(   N(    (   R]   t   xt   y(   t   coords(    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pys	   <genexpr>   s    (   t   xrangeR3   (   RF   (    (   Rw   s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   points_in_square   s    #c         c   sQ   |  d } x@ t  |   D]2 \ } } | d | d | k r | | f Vq q Wd S(   sI    returns int points lying in a circle centered on 0, 0 with given radius.i   N(   Ry   (   t   radiust   radius_squaredRu   Rv   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   points_in_circle   s    
c         c   s\  d | } d } d | } d } | } |  | | f V|  | | f V|  | | f V|  | | f Vx | | k  rW| d k r | d 8} | d 7} | | 7} n  | d 7} | d 7} | | 7} |  | | | f V|  | | | f V|  | | | f V|  | | | f V|  | | | f V|  | | | f V|  | | | f V|  | | | f Vqe Wd S(   sv    Yields int points lying at the edge of an circle centered on x0, y0.
        Uses the Midpoint circle algorithm.
    i   ii    i   N(    (   t   x0t   y0Rz   R*   t   ddf_xt   ddf_yRu   Rv   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   points_at_circle   s2    






c         c   sq   |  d } | d } xV t  |   D]H \ } } | d | d } | | k  oV | k n r! | | f Vq! q! Wd S(   s    Yields integer points lying between two circles.
        
        >>> [point for point in points_in_annulus(2, 1)]
        [(-2, 0), (-1, -1), (-1, 1), (0, -2), (0, 2), (1, -1), (1, 1), (2, 0)]
    i   N(   Ry   (   t
   big_radiust   small_radiust   big_radius_squaredt   small_radius_squaredRu   Rv   t   distance_squared(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   points_in_annulus   s    

t	   SpotModelc           B   s    e  Z d  Z d   Z d   Z RS(   sM    Data model for a Spot that stores all the information in various lists.
    c         C   s1   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ d  S(   N(   Ru   Rv   t	   intensityR   Rz   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s
    				c         C   sT   |  j  j |  |  j j |  |  j j |  |  j j |  |  j j |  d  S(   N(   Ru   t   appendRv   R   R   Rz   (   R   Ru   Rv   R   R   Rz   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   update  s
    (   R"   R#   R$   R   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR      s   	t   ImageRegionc           B   s    e  Z d  Z d   Z d   Z RS(   s/    Region of a image defined by contained spots. c         C   s1   t  |  j   \ } } t j |  t j |  f S(   N(   t   zipRO   RC   t   mean(   R   t   ylistt   xlist(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   middle  s    c         C   s   t  t j  } | t j d S(   Ng      ?(   R   R   RO   RC   t   pi(   t   spott   nitems(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyRz     s    (   R"   R#   R$   R   Rz   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   	t   AbstractKalmanFilterc           B   sV   e  Z d  Z d   Z e j d  d  Z d	 d  Z d   Z	 d   Z
 d	 d  Z RS(
   s    Abstract implementation of a Kalman filter.
    
    Matrices and Vectors can be given in any input format np.matrix() understands.
    Vectors are internally transposed and should therefore be given as column vectors.
    c         C   sw   t  t |   j   t j |  j |  _ t j |  |  _ t j |  |  _ t j t j	 t
 |  j j    |  _ d S(   s    Initialize Kalman filter.

        x: start state vector
        P: start state covariance matrix
        H: measurement matrix
        N(   t   superR   R   RC   t   asmatrixt   TRu   t   Pt   Ht   identityRa   Rh   t   _1(   R   Ru   R   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   !  s
    i   c         C   sM   t  j |  } t  j |  } | |  j |  _ | |  j | j | |  _ d S(   sf    Predict next state.

        F: state transition matrix
        Q: process covariance matrix
        N(   RC   R   Ru   R   R   (   R   R?   t   Q(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   predict0  s    c         C   sD   | d k	 r( |  j |  j |  j j | S|  j |  j |  j j Sd S(   s;    Returns the covariance matrix of a predicted measurement. N(   t   NoneR   R   R   (   R   t   R(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   _calcS<  s    c         C   s   |  j  |  j S(   s    Predicted measurement result. (   R   Ru   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   _calczC  s    c         C   s   t  j |  j } t  j |  } |  j |  } |  j   } |  j |  j j | j } |  j | | | |  _ |  j	 | |  j |  j |  _ d S(   sh    Update state estimate.

        z: measurement vector
        R: measurement covariance matrix
        N(
   RC   R   R   R   R   R   R   t   IRu   R   (   R   t   zR   t   St   z_predictedt   K(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   G  s    c         C   sI   t  j |  j } |  j |  } |  j   } | | } | j | j | S(   sr    Returns the normalized distance squared of the given measurement.
        
        z: measurement vector
        (   RC   R   R   R   R   R   (   R   R   R   R   R   t   diff(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   measurement_distanceV  s
    
(   i   i   N(   R"   R#   R$   R   RC   t   zerosR   R   R   R   R   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   			t   AbstractPVKalmanFilterc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sN    Kalman filter for 2d-tracking using position and velocity as state variables.c         C   s_   | |  _  | | d d g } d d d d g d d d d g g } t t |   j | | |  d  S(   Ni    i   (   t   old_timeR   R   R   (   R   t   x_int   y_inR   t   timeRu   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   c  s    	$c         C   s$   t  |  j d  t  |  j d  f S(   Ni    i   (   t   floatRu   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   get_positioni  s    c         C   s,   t  |  j d  d t  |  j d  d f S(   Ni    g      ?i   (   i    i    (   i   i   (   R   R   (   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   get_position_errl  s    (   R"   R#   R$   R   R   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   a  s   		t   PVKalmanFilter0c           B   s   e  Z d    Z RS(   c         O   sx   | |  j  } d d | d g d d d | g d d d d g d d d d g g } t t |   j | | |  | |  _  d  S(   Ni   i    (   R   R   R   R   (   R   R   t   argsR&   t   dtR?   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   p  s    B(   R"   R#   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   o  s   t   PVKalmanFilter1c           B   s   e  Z d    Z RS(   c   	      O   s   | |  j  } d |  j  } d | | } | d | | d } d d | d g d d d | g d d | d g d d d | g g } t t |   j | | |  | |  _  d  S(   Ng      i   g      ?i   i    (   R   R   R   R   (	   R   R   R   R&   R   t   at   v_upt   pos_upR?   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   w  s    B(   R"   R#   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   v  s   t   PVKalmanFilter2c           B   s   e  Z d    Z RS(   c   
      O   s   | |  j  } d |  j  } d |  j  d } d | | | | d } | d | | d d
 | | d } d d	 | d	 g d	 d d	 | g d	 d	 | d	 g d	 d	 d	 | g g }	 t t |   j |	 | |  | |  _  d  S(   Ng      g      ?i   i   g      ?g      ?g      @i   i    gUUUUUU?(   R   R   R   R   (
   R   R   R   R&   R   R   t   a_dotR   R   R?   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    &B(   R"   R#   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   t   PVKalmanFilter3c           B   s   e  Z d    Z RS(   c         O   s  | |  j  } d |  j  } d |  j  d } d |  j  d } d | | | | d | | d } | d | | d d | | d d | | d }	 d d |	 d g d d d |	 g d d | d g d d d | g g }
 t t |   j |
 | |  | |  _  d  S(   Ng      g      ?i   g     i   i   g      ?g      ?g      @g      @i   i    gUUUUUU?g      ?(   R   R   R   R   (   R   R   R   R&   R   R   R   t   a_ddotR   R   R?   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    &6B(   R"   R#   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   t   PVKalmanFilterExactVc           B   s   e  Z d    Z RS(   c   
      O   s   | |  j  } d |  j  } d |  j  d } |  j  | d } | d | | d d | | d } d	 d
 | d
 g d
 d	 d
 | g d
 d
 | d
 g d
 d
 d
 | g g }	 t t |   j |	 | |  | |  _  d  S(   Ng      g      ?i   g      ?g      ?g      ?g      @i   i   i    gUUUUUU?(   R   R   R   R   (
   R   R   R   R&   R   R   R   R   R   R?   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    &B(   R"   R#   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   t   Trackerc           B   s)   e  Z d  Z d e d d  Z d   Z RS(   sE    Tracks spots through intensity information and velocity prediction. i   i    c   	      C   sy   | |  _  t j | | d d g  } t | | | |  |  _ | |  _ |  j ru | |  _ | d |  j  |  j |  _ n  d S(   s$    x_in, y_in: start position of spot i  g      ?N(   Rz   RC   t   diagR   t   kalmant   window_scalingt   min_window_sizet   c_size(	   R   R   R   Rz   R   t   input_precisionR   R   t	   cov_input(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    				c         C   s   | \ } } |  j  r3 |  j | d |  j |  _ n  |  j j | t j d d d d	 g   |  j j   \ } } t	 | | | |  j d |  j \ } } } |  j j
 | | g |  |  j j   \ }	 }
 t | |	 |
 |  j  } |	 |
 | | |  j f S(
   Ng      ?i
   iiR   g{Gz?g{Gz?gư>gư>(   R   R   R   Rz   R   R   RC   R   R   t   guesserR   t   calc_intensity(   R   t   imageRo   R   t   x_pt   y_pt   x_tht   y_tht	   guess_covRu   Rv   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt
   feed_image  s    	(*(   R"   R#   R$   t   FalseR   R   (    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s   
c         O   s   |  t  |   k } t |  |  S(   N(   t   otsut   com(   R   R   R&   t	   bin_image(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   guess_from_com  s    c         O   s"   t  t j t j |   |  j   S(   N(   t   reversedRC   t   unravel_indext   argmaxRh   (   R   R   R&   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   guess_from_brightest  s    i    i   c            s	    j    j   } t j   j     } | | | | | | | g }   f d   }   f d   } d }	 d
 | d |	 | d |	 f | d |	 | d |	 f d d d g }
 yI t j | t j |  d |
 d	 t } | d } | d } | d } Wn d  SX| | f S(   Nc            s)   t  j t |    t  j   j       S(   N(   RC   t   ravelt
   gaussian2dt   indicesRh   (   t   p(   R   (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   <lambda>  s    c            s3   t  j t |    t  j   j       d j   S(   Ni   (   RC   R   R   R   Rh   t   sum(   R   (   R   (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    i
   i    i   i   g      ?t   boundst   approx_grad(   i    N(   g      ?i
   (   g      ?i
   (   i    N(	   t   flattenRa   RC   R   R   R   t   fmin_l_bfgs_bRD   t   True(   R   t   x_midt   y_midRF   t   max_t   backt   paramst   errfunct   errfuncSquaredt   offsetR   t   valuet
   params_optt   x_rest   y_res(    (   R   s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   guess_from_fit  s    D'

c         K   s   t  |   } |  j   | k } t |  } | rd g  | D]$ } t |  | j   | j    ^ q7 } n g  | D] } | j   ^ qk } i | d 6S(   Nt   spots(   R   t   copyt
   find_spotst
   com_circleR   Rz   (   R   t   use_comR&   t	   thresholdR   R   R   t	   spots_pos(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   guess_from_spots  s    4i   c         K   sK  t  |   } t |  j    } | | d } | d | } |  | k }	 t |	  }
 g  |
 D]! } | | j   t |  g ^ qY }
 x| | k r| } | | 8} g  t j |   D]. \ } } | | k o | k  n r | ^ q } x0| D](\ } } g  } xn t |
  D]` \ } } | | d d d | | d d d d | d t j	 d } | j
 | | f  q	W| j   | d d | k  r| d d } |
 | d j
 | | f  |
 | d j   |
 | d <|
 | d c d 7<q |
 j
 t | | f g  | | f d g  q Wq Wg  |
 D]  } | d d k r!| d ^ q!} | S(   Ng?i   i   i    i   g      ?(   R   Ra   R   R   R   R   RC   t   ndenumeratet	   enumerateR   R   t   sortR   (   R   R   R   t   expansion_factorR&   t   otsu_thresht	   max_imaget	   incrementt   thresht   bin_image_oldR   R   t
   old_threshR   R   t
   new_pixelsRv   Ru   t	   distancesRs   t   distR   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   guess_from_am  s4    .
D+
3-i   i   i   c
      	      s     f d   }
 y@ t  |   |  | d   |   | d  \ } } } } Wn t k
 rl |
 d  SX|  | |  | |  f } | r t j j | d d d } n  | | d  | d   | d | d	 } | d  k r |
 d
  S| d } | j d  r| d } n% g  t t |   D] } |	 ^ q&} g  | D]  \ } } | | | | f ^ q?} g  t	 |  D]6 \ } } | j
 | t j d d g   | | f ^ qr} t |  \ } } } | \ } } | | } | | k r|
 d  S| | | f S(   Nc            s6   t  j d |      t j t j d d g   f S(   Ns   no guess, because i  (   t   loggert   infoRC   R   R   (   t   reason(   R   R   (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   failure  s    i   s   position outside imaget   modet   nearestR   R   RF   g?s   no bright spot foundR   t   covsg?s   no spot in validation gate(   t   adjust_slicet
   IndexErrorR   t   filterst   gaussian_filterR   t   has_keyRl   R   R   R   RC   R   R`   (   Ro   R   R   Rz   t   funct
   max_radiusR   t   gammat   smootht   default_covR
  t   x_mint   x_maxt   y_mint   y_maxR   t   resultR   R  Rs   t   x_spott   y_spotR   t   spots_decoratedt   distancet   min_spott	   min_indexR   R   t   cov(    (   R   R   s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s0    (*

%-I

c         C   sC  |  j  \ } } t } t |  t |  t |  t |  g } x9 t |  D]+ \ }	 }
 |
 d k  rL d | |	 <t } qL qL Wxg t |  D]Y \ }	 }
 |	 d k  r |
 | k r | | |	 <t } q q |
 | k r | | |	 <t } q q W| r t j d  n  t | d | d  s-t | d | d  r9t    n  t |  S(   sP  
    Adjusts slice if it is trying to get pieces outside the image.

    >>> image = np.ones((2, 2))
    >>> adjust_slice(image, 0, 1.5, 0, 2)
    (0, 1, 0, 2)
    >>> adjust_slice(image, -5.5, 2, -0.5, 10)
    (0, 2, 0, 2)
    >>> adjust_slice(image, -5, -4, -0.5, 10)
    Traceback (most recent call last):
    ...
    IndexError
    i    i   s&   slice had to be adjusted to fit image.i   i   (	   Rh   R   R3   R   R   R  t   warningR  t   tuple(   R   t   x_sl_mint   x_sl_maxt   y_sl_mint   y_sl_maxt   ymaxt   xmaxt   adjustedR   Rs   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR  1  s(    *


2c         C   sK   t  |  t |  | |  \ } } | rG | t |  | | |  | 8} n  | S(   sR    Calculates the intensity of the point.
        Uses background substraction.
    (   t   _integral_intensityR|   t   background_intensity(   Ro   Ru   Rv   Rz   t   background_substractionR   t   cnt(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   V  s    ! c         C   st   t  | | |  } g  | D] \ } } |  | | ^ q } | j   t d t |   } | | | !} t j |  S(   s+    Returns trimmed mean of points at circle. g?(   R   R   R3   R   RC   R   (   Ro   Ru   Rv   Rz   t   pixelst   intensitiest   off(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR.  _  s    '
c         C   sO   d } x< t  |  D]. \ } } | |  | | d | | d 7} q W| | f S(   su    Sums the values of the image at the given pixel increments.
        returns: summed intensity, number of pixels
    i    i   (   R   (   Ro   R1  Ru   Rv   R   R0  t   incs(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR-  h  s    &c            s          f d   S(   sE   Returns a two dimensional gaussian function with the given parametersc            s?   t  j  t  j  |   d  |  d d     S(   Ni   (   RC   RD   t   exp(   Ru   Rv   (   R   t   heightt   center_xt   center_yt   width_xt   width_y(    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   w  s   (    (   R6  R7  R8  R9  R:  R   t   slope_xt   slope_y(    (   R:  R6  R7  R8  R9  R   s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR   t  s    c         C   s  |  j    } t j |  j  \ } } | |  j    | } | |  j    | } |  d d  t |  f } t j t t j | j  | d |  j    | j     } |  t |  d d  f } t j t t j | j  | d |  j    | j     }	 |  j	   }
 |
 | | | |	 g S(   sz   Returns [height, x, y, width_x, width_y]
    the gaussian parameters of a 2D distribution by calculating its
    moments. Ni   (
   R   RC   R   Rh   R3   t   sqrtt   abst   arangeRF   Ra   (   RO   t   totalt   Xt   YRu   Rv   t   colR9  t   rowR:  R6  (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   moments{  s    ==c   	      C   s   | \ } } t  |  } t  |  } t j |   } xN t | d  D]< \ } } y d | | | | | f <WqD t k
 r qD XqD Wt |  |  } | S(   Ni   (   t   roundRC   t
   zeros_likeR|   R  R   (	   Ro   t   midRz   Rv   Ru   t   weightst   x_inct   y_incR  (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    c         C   s   | d  k	 r |  | 9}  n  d \ } } d } |  j \ } } xe t d |  D]T } xK t d |  D]: } |  | | }	 | | |	 7} | | |	 7} | |	 7} q` WqJ Wy" | | } | | } | | f SWn! t k
 r }
 t j d  d  SXd  S(   Ni    s   com couldn't be calculated(   i    i    (   R   Rh   Rl   t   ZeroDivisionErrorR  R$  (   Ro   RI  R   R   t
   normalizerR*  R+  Ru   Rv   R   t   FloatingPointError(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s$    

c            s         f d   S(   sF   Returns a one dimensional gaussian function with the given parameters.c            s7    t  j  |   d d   t  j |     S(   Ni   (   RC   R5  RD   (   Ru   (   t   slopeR6  R7  R9  R   (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    (    (   R6  R7  R9  R   RO  (    (   RO  R7  R   R9  R6  s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt
   gaussian1d  s    c         C   sq   g  } t  d   t j |   D  } xE | rl | j   } t | |  } | j |  | j t |   q( W| S(   sT    Returns a list of spots that are 8-connected.
    
    Performs flood filling.
    c         s   s!   |  ] \ } } | r | Vq d  S(   N(    (   R]   R   R   (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pys	   <genexpr>  s    (   R   RC   R   t   popt   _expandt   addR   R   (   R   R   t   white_pixelst
   startpixelt   point(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s    	c            s   |  \    t     f d   d d d g D  } | j |  } | | 8} | sW | S| sj t  |  g  S| j   } x$ | D] } | j t | |   q} W| S(   Nc         3   s9   |  ]/ } d  d d g D] }   |  | f Vq q d S(   ii    i   N(    (   R]   Rs   t   j(   Rv   Ru   (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pys	   <genexpr>  s    ii    i   (   R   t   intersectionR   R   RR  (   t   pixelRT  t   next_pixelsRV  t
   next_pixel(    (   Rv   Ru   s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyRR    s    (
c         C   s%  d } t  j |  d | \ } } | d  d t  j |  } |  j } t | |  } | d | d } | d } d }	 d }
 d } x t d | d  D] } | | | | | 7} | | | 7} | | | } | r | | | | } | | | }	 n d }	 |	 |
 k r |	 }
 | } q q W| | S(   s    Finds threshold using Otsu's method.

    image: image for which threshold is to be found.
    
    C++ code by Jordan Bevik <Jordan.Bevic@qtiworld.com>
    ported to ImageJ plugin by G.Landini
    ported to python by A.Mayer
    i,  t   binsig      ?i    i   (   RC   t	   histogramR   RF   R   Rl   (   R   t   nbinst   histt	   bin_edgest   bin_midt   NR   t   Skt   N1t   BCVt   BCVmaxt   kStart   kt   denomt   num(    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyR     s,    		
t   __main__(    (    (    (G   R$   t
   __future__R    t   __version__R   RJ   t   loggingt	   getLoggerR  t   numpyRC   t   seterrt   PyQt4.QtGuit   QtGuiRi   t   scipyR   R   R[   RN   R   R$  RA   t   objectR	   R%   RH   RS   t   dictt   IMAGE_FORMATSRg   Rt   Ry   R|   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R   R   R   R   R  R   R.  R-  R   RE  R   R   RP  R   RR  R   R"   t   doctestt   testmod(    (    (    s5   /home/salopaasi/Documents/Moo/Easyleednew/leedbase.pyt   <module>   s   6,		
						G
		"
*#	%									'