o
    :ήc7                  
   @   s   d dl Zd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ ejded	ed
edd
d	fejdededfgdd ZG dd dZdS )    N)assert_almost_equalassert_array_almost_equal)data)exposure)expected_warnings)_supported_float_type)histogram_matchingzarray, template, expected_array
   d   	         c                 C   s   t | |}t|| d S N)r   _match_cumulative_cdfr   )arraytemplateexpected_arraymatched r   U/tmp/pip-target-vg8gfxp4/lib/python/skimage/exposure/tests/test_histogram_matching.pytest_match_array_values   s   r   c                	   @   s   e Zd Ze Ze Zej	
deedfedddddf edddddf dfgdd Zej	
dd	d
d Zej	
dejejejgdd Zej	
deedddddf fedddddf efgdd Zedd ZdS )TestMatchHistogramzimage, reference, multichannelTNr   Fc                 C   s   t dg tj|||d}W d   n1 sw   Y  | |}| |}tt|D ]+}|| \}}	|| \}
}t|
D ]\}}t|| 	 }t
|| |	| dd q?q-dS )sAssert that pdf of matched image is close to the reference's pdf for
        all channels and all values of matchedz'`multichannel` is a deprecated argument)multichannelN   decimal)r   r   match_histograms_calculate_image_empirical_pdfrangelen	enumeratenpabsargminr   )selfimage	referencer   r   matched_pdfreference_pdfchannelreference_valuesreference_quantilesmatched_valuesmatched_quantilesimatched_value
closest_idr   r   r   test_match_histograms   s(   

z(TestMatchHistogram.test_match_histogramschannel_axis)r   r   c                 C   s   t | jd|}t | jd|}tj|||d}|j|jks J t ||d}t ||d}| |}| |}tt	|D ]+}|| \}}	|| \}
}t
|
D ]\}}t ||  }t|| |	| dd qPq>dS )r   r4   )r3   r   r   N)r"   moveaxis	image_rgbtemplate_rgbr   r   dtyper   r   r    r!   r#   r$   r   )r%   r3   r&   r'   r   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r   r   "test_match_histograms_channel_axis8   s.   

z5TestMatchHistogram.test_match_histograms_channel_axisr8   c                 C   sB   | j j|dd}| jj|dd}t||}|jt|ksJ dS )z-float16 or float32 inputs give float32 outputF)copyN)r6   astyper7   r   r   r8   r   )r%   r8   r&   r'   r   r   r   r   !test_match_histograms_float_dtypeS   s   z4TestMatchHistogram.test_match_histograms_float_dtypezimage, referencec                 C   s<   t t t|| W d    d S 1 sw   Y  d S r   )pytestraises
ValueErrorr   r   )r%   r&   r'   r   r   r   ,test_raises_value_error_on_channels_mismatch[   s   "z?TestMatchHistogram.test_raises_value_error_on_channels_mismatchc                 C   s   |j dkr|ddd}tj|ddd}g }|D ]!}tj|dd\}}t|tj}||d	  }|||f qtj	|t
d
S )zpHelper function for calculating empirical probability density
        function of a given image for all channels   r   r   Fr   )r:   ndminT)return_countsr4   )r8   )ndim	transposer"   r   uniquecumsumr;   float64appendasarrayobject)clsr&   channelschannels_pdfr*   channel_valuescountschannel_quantilesr   r   r   r   c   s   
z1TestMatchHistogram._calculate_image_empirical_pdf)__name__
__module____qualname__r   chelsear6   	astronautr7   r=   markparametrizer2   r9   r"   float16float32rH   r<   r@   classmethodr   r   r   r   r   r      s&    ,



r   )numpyr"   r=   numpy.testingr   r   skimager   r   skimage._shared.testingr   skimage._shared.utilsr   skimage.exposurer   rW   rX   arangerandomrandonesr   r   r   r   r   r   <module>   s    
