U
    ?h                  
   @   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 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)_supported_float_type)histogram_matchingzarray, template, expected_array
   d   	         c                 C   s   t | |}t|| d S N)r   Z_match_cumulative_cdfr   )arraytemplateZexpected_arraymatched r   `/var/www/html/venv/lib/python3.8/site-packages/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d ZdS )TestMatchHistogramzimage, reference, channel_axisNr   c                 C   s   t j|||d}| |}| |}tt|D ]V}|| \}}	|| \}
}t|
D ]0\}}t||  }t	|| |	| dd qTq0dS )sAssert that pdf of matched image is close to the reference's pdf for
        all channels and all values of matchedchannel_axis   decimalN)
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_histogramsr   )r   r   r   c                 C   s   t | jd|}t | jd|}tj|||d}|j|jks@tt ||d}t ||d}| |}| |}t	t
|D ]V}|| \}}	|| \}
}t|
D ]0\}}t ||  }t|| |	| dd qq|dS )r   r   r   r   r   N)r!   Zmoveaxis	image_rgbtemplate_rgbr   r   dtypeAssertionErrorr   r   r   r    r"   r#   r   )r$   r   r%   r&   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r   r   r   "test_match_histograms_channel_axis6   s(    

z5TestMatchHistogram.test_match_histograms_channel_axisr4   c                 C   sB   | j j|dd}| jj|dd}t||}|jt|ks>tdS )z-float16 or float32 inputs give float32 outputFcopyN)r2   astyper3   r   r   r4   r   r5   )r$   r4   r%   r&   r   r   r   r   !test_match_histograms_float_dtypeQ   s    z4TestMatchHistogram.test_match_histograms_float_dtypezimage, referencec              	   C   s&   t t t|| W 5 Q R X d S r   )pytestZraises
ValueErrorr   r   )r$   r%   r&   r   r   r   ,test_raises_value_error_on_channels_mismatchY   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 ]B}tj|dd\}}t|tj}||d	  }|||f q0tj	|t
d
S )zpHelper function for calculating empirical probability density
        function of a given image for all channels   r   r   Fr   )r8   ZndminT)Zreturn_countsr   )r4   )ndimZ	transposer!   r   uniqueZcumsumr9   float64appendZasarrayobject)clsr%   ZchannelsZchannels_pdfr)   Zchannel_valuescountsZchannel_quantilesr   r   r   r   a   s    
z1TestMatchHistogram._calculate_image_empirical_pdfc                 C   sZ   | j }| j}| j tj}| jjtjdd}t||}t||}t|tj| dS )z@ensure equivalent results for float and integer-based code pathsFr7   N)r2   r3   r9   r!   rA   r   r   r   )r$   Zimage_u8Zreference_u8Z	image_f64Zreference_f64Z
matched_u8Zmatched_f64r   r   r   !test_match_histograms_consistencyt   s    z4TestMatchHistogram.test_match_histograms_consistency)__name__
__module____qualname__r   Zchelsear2   Z	astronautr3   r;   markparametrizer1   r6   r!   Zfloat16Zfloat32rA   r:   r=   classmethodr   rF   r   r   r   r   r      s&   ,




r   )numpyr!   r;   Znumpy.testingr   r   Zskimager   r   Zskimage._shared.utilsr   Zskimage.exposurer   rJ   rK   ZarangerandomZrandZonesr   r   r   r   r   r   <module>   s   
