U
    ?h                     @   s   d dl Z d dlZe d d dlmZmZmZmZ dd Z	dd Z
dd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    NZsklearn)	learn_gmmfisher_vectorFisherVectorExceptionDescriptorExceptionc                	   C   s&   t t tddd W 5 Q R X dS )z\Test that DescriptorException is raised when wrong type for descriptions
    is passed.
    zcompletely wrong test   n_modesNpytestraisesr   r    r   r   Z/var/www/html/venv/lib/python3.8/site-packages/skimage/feature/tests/test_fisher_vector.py"test_gmm_wrong_descriptor_format_1   s    r   c                	   C   s6   t t" ttdtdgdd W 5 Q R X dS )zbTest that DescriptorException is raised when descriptors are of
    different dimensionality.
    )         
   r   r   Nr
   r   r   r   npzerosr   r   r   r   "test_gmm_wrong_descriptor_format_2   s    r   c                	   C   s6   t t" ttdtdgdd W 5 Q R X dS )zXTest that DescriptorException is raised when not all descriptors are of
    rank 2.
    r   r   )r   r   r   r   r   Nr   r   r   r   r   "test_gmm_wrong_descriptor_format_3   s    r   c                	   C   s6   t t" tdddgdddggdd W 5 Q R X dS )zTest that DescriptorException is raised when elements of descriptor list
    are of the incorrect type (i.e. not a NumPy ndarray).
    r         r   Nr	   r   r   r   r   "test_gmm_wrong_descriptor_format_4(   s    r   c                	   C   s6   t t" ttdtdgdd W 5 Q R X dS )zyTest that FisherVectorException is raised when incorrect type for
    n_modes is passed into the learn_gmm function.
    r   r   Z	not_validr   Nr
   r   r   r   r   r   r   r   r   r   !test_gmm_wrong_num_modes_format_11   s    r   c                	   C   s6   t t" ttdtdgdd W 5 Q R X dS )zTest that FisherVectorException is raised when a number that is not a
    positive integer is passed into the n_modes argument of learn_gmm.
    r   r   r   Nr   r   r   r   r   !test_gmm_wrong_num_modes_format_2:   s    r    c                	   C   s4   t t  ttjddddid W 5 Q R X dS )zqTest that FisherVectorException is raised when wrong covariance type is
    passed in as a keyword argument.
    r   r   r   covariance_typefullr   Zgm_argsN)r
   r   r   r   r   randomr   r   r   r   test_gmm_wrong_covariance_typeC   s    
 r&   c                  C   sH   t tjddddid} | jdk	s(t| jdk	s6t| jdk	sDtdS )ztTest that GMM estimation is successful when the correct covariance type
    is passed in as a keyword argument.
    r!   r   r"   Zdiagr$   Nr   r   r%   Zmeans_AssertionErrorZcovariances_Zweights_gmmr   r   r    test_gmm_correct_covariance_typeO   s    
 r+   c                  C   sB   t tjddd} | jdk	s"t| jdk	s0t| jdk	s>tdS )a  
    Test the GMM estimation. Since this is essentially a wrapper for the
    scikit-learn GaussianMixture class, the testing of the actual inner
    workings of the GMM estimation is left to scikit-learn and its
    dependencies.

    We instead simply assert that the estimation was successful based on the
    fact that the GMM object will have associated mixture weights, means, and
    variances after estimation is successful/complete.
    )d   @   r   r   Nr'   r)   r   r   r   test_gmm_e2e^   s    r.   c               	   C   s^   zddl m}  W n tk
r,   td Y nX tt tddddgg|   W 5 Q R X dS )	z
    Test that DescriptorException is raised when the incorrect type for the
    descriptors is passed into the fisher_vector function.
    r   GaussianMixturezmscikit-learn is not installed. Please ensure it is installed in order to use the Fisher vector functionality.r   r   r   r   N)Zsklearn.mixturer0   ImportErrorprintr
   r   r   r   r/   r   r   r   test_fv_wrong_descriptor_typesq   s    
r3   c               	   C   s:   G dd d} t t ttd|   W 5 Q R X dS )z
    Test that FisherVectorException is raised when a GMM not of type
    sklearn.mixture.GaussianMixture is passed into the fisher_vector
    function.
    c                   @   s   e Zd ZdS )z:test_fv_wrong_gmm_type.<locals>.MyDifferentGaussianMixtureN)__name__
__module____qualname__r   r   r   r   MyDifferentGaussianMixture   s   r7   r!   N)r
   r   r   r   r   r   )r7   r   r   r   test_fv_wrong_gmm_type   s    r8   c                     s\   d d} d|    |  } fddt dD }t|| d}t|d |}t||ksXtd	S )
a  
    Test the Fisher vector computation given a GMM returned from the learn_gmm
    function. We simply assert that the dimensionality of the resulting Fisher
    vector is correct.

    The dimensionality of a Fisher vector is given by 2KD + K, where K is the
    number of Gaussians specified in the associated GMM, and D is the
    dimensionality of the descriptors using to estimate the GMM.
          r   c                    s&   g | ]}t jt jd d fqS r      r   r%   randint.0_dimr   r   
<listcomp>   s   ztest_fv_e2e.<locals>.<listcomp>r   r   r   Nranger   r   lenr(   Z	num_modesZexpected_dimZdescriptorsr*   Z
fisher_vecr   rB   r   test_fv_e2e   s    
rI   c                     s`   d d} d|    |  } fddt dD }t|| d}t|d |d	d
}t||ks\tdS )a  
    Test the improved Fisher vector computation given a GMM returned from the
    learn_gmm function. We simply assert that the dimensionality of the
    resulting Fisher vector is correct.

    The dimensionality of a Fisher vector is given by 2KD + K, where K is the
    number of Gaussians specified in the associated GMM, and D is the
    dimensionality of the descriptors using to estimate the GMM.
    r9   r:   r   c                    s&   g | ]}t jt jd d fqS r;   r=   r?   rB   r   r   rD      s   z(test_fv_e2e_improved.<locals>.<listcomp>r   r   r   T)ZimprovedNrE   rH   r   rB   r   test_fv_e2e_improved   s    
rJ   )r
   numpyr   ZimportorskipZskimage.feature.fisher_vectorr   r   r   r   r   r   r   r   r   r    r&   r+   r.   r3   r8   rI   rJ   r   r   r   r   <module>   s    
						