ó
‡ˆ\c           @   s°  d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l	 m
 Z
 m Z m Z m Z m Z m Z d  d l m Z m Z m Z m Z d „  Z d „  Z e d	 e ƒ d
 „  ƒ Z e d	 e ƒ d „  ƒ Z d „  Z d d „ Z d d „ Z e ƒ  Z e e ƒ Z e e ƒ Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d „  Z0 d S(   iÿÿÿÿ(   t   divisionN(   t   ndimage(   t   connected_components(   t   img_to_grapht   grid_to_grapht   extract_patches_2dt   reconstruct_from_patches_2dt   PatchExtractort   extract_patches(   t   assert_equalt   assert_truet   assert_raisest   ignore_warningsc          C   s‹   t  j d  d … d  d … f d \ }  } t |  ƒ } t | ƒ } t | j | j ƒ t  j j | j | j d k | j | j d k ƒ d  S(   Ni   i
   i    (   t   npt   mgridR   R	   t   nnzt   testingt   assert_array_equalt   data(   t   xt   yt   grad_xt   grad_y(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_img_to_graph   s    )c          C   sñ  d }  d } t  j |  |  f d t  j ƒ} t | d | … d | … f <t | | d  … | d  … f <| j |  d ƒ } t d |  d |  d | d t  j ƒ } t | ƒ d d k s· t ‚ t  j	 |  |  f d t  j
 ƒ} t d |  d |  d	 |  d | ƒ } t | ƒ d d k st ‚ t  j	 |  |  f ƒ } t d |  d |  d	 |  d | d t  j ƒ } | j t  j k sit ‚ t d |  d |  d	 |  d | d t  j ƒ } | j t  j k s«t ‚ t d |  d |  d	 |  d | d t  j ƒ } | j t  j k sít ‚ d  S(
   Ni   i   t   dtypei    t   n_xt   n_yt   maskt	   return_ast   n_z(   R   t   zerost   boolt   Truet   reshapeR   t   ndarrayR   t   AssertionErrort   onest   int16R   t   intt   float64(   t   sizet   roi_sizeR   t   A(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_grid_to_graph   s&    $!**t   categoryc          C   s    y t  j d t ƒ }  Wn3 t k
 rK d d l m } | j d t ƒ }  n XxM d D]E } |  | k } t |  | ƒ } t t j	 | ƒ d t
 | ƒ d ƒ qS Wd  S(	   Nt   grayiÿÿÿÿ(   t   misci2   i–   i   i    (   i2   i–   (   t   spt   faceR    t   AttributeErrort   scipyR.   R   R	   R   t   labelR   (   R0   R.   t   thrR   t   graph(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_connect_regions;   s    c          C   sà   y t  j d t ƒ }  Wn3 t k
 rK d d l m } | j d t ƒ }  n X|  d k } t d | |  j Œ } t t	 j
 | ƒ d t | ƒ d ƒ |  d k } t d | d	 d  |  j Œ } t t	 j
 | ƒ d t | ƒ d ƒ d  S(
   NR-   iÿÿÿÿ(   R.   i2   R   i   i    i–   R   (   R/   R0   R    R1   R2   R.   R   t   shapeR	   R   R3   R   t   None(   R0   R.   R   R5   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_connect_regions_with_gridI   s    $c          C   s‚  y t  j d t ƒ }  Wn3 t k
 rK d d l m } | j d t ƒ }  n X|  j t j ƒ }  |  d  d  d … d  d  d … f |  d d  d … d  d  d … f |  d  d  d … d d  d … f |  d d  d … d d  d … f }  |  d  d  d … d  d  d … f |  d d  d … d  d  d … f |  d  d  d … d d  d … f |  d d  d … d d  d … f }  |  j t j ƒ }  |  d }  |  S(   NR-   iÿÿÿÿ(   R.   i   i   g      0@(	   R/   R0   R    R1   R2   R.   t   astypeR   t   float32(   R0   R.   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   _downsampled_faceZ   s    ‚‚
c         C   s¦   |  d  k r t ƒ  n |  }  t j |  j d ƒ } d |  | d  d  … d  d  … d f <d |  d | d  d  … d  d  … d f <d |  d | d  d  … d  d  … d f <| S(   Ni   i   i    i   i   i   (   i   (   R8   R<   R   R   R7   (   R0   t
   face_color(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   _orange_facek   s    #''c         C   s[   |  d  k r t ƒ  n |  }  t j d |  j ƒ } |  | d <|  d | d <|  d | d <| S(   Ni   i    i   i   (   i   (   R8   R<   R   R   R7   (   R0   t   images(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   _make_imagest   s    
c          C   sm   t  }  |  j \ } } d \ } } | | d | | d } t |  | | f ƒ } t | j | | | f ƒ d  S(   Ni   i   (   i   i   (   t   downsampled_faceR7   R   R	   (   R0   t   i_ht   i_wt   p_ht   p_wt   expected_n_patchest   patches(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_extract_patches_all‚   s    c          C   st   t  }  |  j d  \ } } d \ } } | | d | | d } t |  | | f ƒ } t | j | | | d f ƒ d  S(   Ni   i   i   i   (   i   i   (   t   orange_faceR7   R   R	   (   R0   RB   RC   RD   RE   RF   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_extract_patches_all_color‹   s    c          C   s‰   t  }  |  d  d  … d d … f }  |  j \ } } d \ } } | | d | | d } t |  | | f ƒ } t | j | | | f ƒ d  S(   Ni    ia   i   i   i   (   i   i   (   RA   R7   R   R	   (   R0   RB   RC   RD   RE   RF   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_extract_patches_all_rect”   s    c          C   sï   t  }  |  j \ } } d \ } } t |  | | f d d ƒ} t | j d | | f ƒ t d | | d | | d ƒ } t |  | | f d d ƒ} t | j | | | f ƒ t t t |  | | f d d ƒt t t |  | | f d d ƒd  S(	   Ni   t   max_patchesid   g      à?i   g       @g      ð¿(   i   i   (   RA   R7   R   R	   R&   R   t
   ValueError(   R0   RB   RC   RD   RE   RG   RF   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt    test_extract_patches_max_patchesŸ   s    $c          C   s6   t  }  t |  |  j d d ƒ} t | j d d ƒ d  S(   NRL   i   i    i   (   RA   R   R7   R	   (   R0   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt"   test_extract_patch_same_size_image±   s    c          C   s„   t  }  |  j \ } } d | d d | d } } | | d | | d } t |  | | f d d ƒ} t | j | | | f ƒ d  S(   Ni   i   i   RL   i   (   RA   R7   R   R	   (   R0   RB   RC   RD   RE   RF   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt*   test_extract_patches_less_than_max_patches¹   s    c          C   sP   t  }  d \ } } t |  | | f ƒ } t | |  j ƒ } t j j |  | ƒ d  S(   Ni   (   i   i   (   RA   R   R   R7   R   R   t   assert_array_almost_equal(   R0   RD   RE   RG   t   face_reconstructed(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt    test_reconstruct_patches_perfectÄ   s
    c          C   sP   t  }  d \ } } t |  | | f ƒ } t | |  j ƒ } t j j |  | ƒ d  S(   Ni   (   i   i   (   RI   R   R   R7   R   R   RQ   (   R0   RD   RE   RG   RR   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt&   test_reconstruct_patches_perfect_colorÍ   s
    c          C   s@   t  }  t d d d d d d ƒ } | | j |  ƒ k s< t ‚ d  S(   Nt
   patch_sizei   RL   id   t   random_statei    (   i   i   (   t   face_collectionR   t   fitR#   (   t   facest   extr(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_patch_extractor_fitÖ   s    c    	      C   s  t  }  |  j d d !\ } } d
 \ } } d } t |  ƒ | } t d | | f d | d d ƒ } | j |  ƒ } | j | | | f k sŒ t ‚ d	 } t |  ƒ t | | d | | d | ƒ } t d | | f d | d d ƒ } | j |  ƒ } | j | | | f k st ‚ d  S(   Ni   i   i   id   RU   RL   RV   i    g      à?(   i   i   (   RW   R7   t   lenR   t	   transformR#   R&   (	   RY   RB   RC   RD   RE   RL   RF   RZ   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt    test_patch_extractor_max_patchesÜ   s     	"	c          C   sQ   t  }  t d d d d ƒ } | j |  ƒ } t | j t |  ƒ d d d f ƒ d  S(   NRL   id   RV   i    i   i   (   RW   R   R]   R	   R7   R\   (   RY   RZ   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt(   test_patch_extractor_max_patches_defaultñ   s    c          C   s˜   t  }  |  j d d !\ } } d \ } } t |  ƒ | | d | | d } t d | | f d d ƒ } | j |  ƒ } | j | | | f k s” t ‚ d  S(   Ni   i   i   RU   RV   i    (   i   i   (   RW   R7   R\   R   R]   R#   (   RY   RB   RC   RD   RE   RF   RZ   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt    test_patch_extractor_all_patchesø   s    $c          C   s¡   t  t ƒ }  |  j d d !\ } } d \ } } t |  ƒ | | d | | d } t d | | f d d ƒ } | j |  ƒ } | j | | | d f k s t ‚ d  S(   Ni   i   i   RU   RV   i    (   i   i   (   R@   RI   R7   R\   R   R]   R#   (   RY   RB   RC   RD   RE   RF   RZ   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_patch_extractor_color  s    $c          C   s/  d d d d g }  d d d d g } d d d d g } d  d! d" d# g } d$ d% d& d' g } d( d) d* d+ g } d, d- d. d/ g } d0 d1 d2 d3 g } d4 d5 d6 d7 g } d8 d9 d: d; g }	 d< d= d> d? g }
 d@ dA dB dC g } dD dE dF dG g } dH dI dJ dK g } dL dM dN dO g } |  | |
 } | | | } | | | } | | | } | |	 | } xÔ t  | | | | | ƒ D]º \ } } } } } t j t j | ƒ ƒ j | ƒ } t | d | d | ƒ} t | ƒ } | j |  | k sàt ‚ t	 d „  t  | | ƒ Dƒ ƒ } t
 | dP | | | j ƒ  k j ƒ  ƒ qmWd  S(Q   Ni
   i   i   i   i   i   i   i	   i   i   i   i   i    i   i   t   patch_shapet   extraction_stepc         s   s+   |  ]! \ } } t  | | | d  ƒ Vq d  S(   N(   t   sliceR8   (   t   .0t   it   j(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pys	   <genexpr>3  s    iÿÿÿÿ(   i
   (   i
   (   i   (   i
   (   i   (   i   (   i   (   i   (   i   (   i   (   i   (   i   (   i
   (   i	   (   i   (   i   (   i
   (   i   (   i   (   i   (   i
   i   (   i
   i   (   i
   i   (   i   i   (   i   i   (   i
   i
   (   i
   i   (   i   i   (   i   i   (   i   i
   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i    i
   (   i    i   (   i   i   (   i   i   i   (   i   i   i   (   i   i   i	   (   i   i   i	   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i
   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i    (   i   i   i   (   i   i   i   (   i   i   i   (   iÿÿÿÿNN(   t   zipR   t   aranget   prodR!   R   R\   R7   R#   t   tupleR
   R8   t   squeezet   all(   t   image_shapes_1Dt   patch_sizes_1Dt   patch_steps_1Dt   expected_views_1Dt   last_patch_1Dt   image_shapes_2Dt   patch_sizes_2Dt   patch_steps_2Dt   expected_views_2Dt   last_patch_2Dt   image_shapes_3Dt   patch_sizes_3Dt   patch_steps_3Dt   expected_views_3Dt   last_patch_3Dt   image_shapest   patch_sizest   patch_stepst   expected_viewst   last_patchest   image_shapeRU   t
   patch_stept   expected_viewt
   last_patcht   imageRG   t   ndimt   last_patch_slices(    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_extract_patches_strided  s@    !		c          C   st   t  }  |  j \ } } d } | | d | | d f } t |  d | ƒ} t | j | d | d | | f k ƒ d  S(   Ni   i   Rb   i    (   RA   R7   R   R
   (   R0   RB   RC   t   pRF   RG   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_extract_patches_square9  s    c          C   s]   t  j d d d g d d d g d d d	 g g ƒ }  t t t |  d
 ƒ t t t |  d ƒ d  S(   Ni   i   i   i   i   i   i   i   i	   (   i   i   (   i   i   (   R   t   arrayR   RM   R   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   test_width_patchD  s    3(1   t
   __future__R    t   numpyR   R2   R/   R   t   scipy.sparse.csgraphR   t    sklearn.feature_extraction.imageR   R   R   R   R   R   t   sklearn.utils.testingR	   R
   R   R   R   R+   t   DeprecationWarningR6   R9   R<   R8   R>   R@   RA   RI   RW   RH   RJ   RK   RN   RO   RP   RS   RT   R[   R^   R_   R`   Ra   R‰   R‹   R   (    (    (    sJ   lib/python2.7/site-packages/sklearn/feature_extraction/tests/test_image.pyt   <module>   s@   ."																						
	
	-	