o
    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   _match_cumulative_cdfr   )arraytemplateexpected_arraymatched r   i/var/www/html/scripts/venv/lib/python3.10/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 ]+}|| \}}	|| \}
}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 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 J t ||d}t ||d}| |}| |}tt	|D ]+}|| \}}	|| \}
}t
|
D ]\}}t ||  }t|| |	| dd qPq>dS )r   r   r   r   r   N)r#   moveaxis	image_rgbtemplate_rgbr   r   dtyper   r    r!   r"   r$   r%   r   )r&   r   r'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r   r   r   "test_match_histograms_channel_axis6   s.   

z5TestMatchHistogram.test_match_histograms_channel_axisr7   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)r5   astyper6   r   r   r7   r   )r&   r7   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 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_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 ]!}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_countsr   )r7   )ndim	transposer#   r   uniquecumsumr;   float64appendasarrayobject)clsr'   channelschannels_pdfr+   channel_valuescounts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 pathsFr9   N)r5   r6   r;   r#   rH   r   r   r   )r&   image_u8reference_u8	image_f64reference_f64
matched_u8matched_f64r   r   r   !test_match_histograms_consistencyt   s   z4TestMatchHistogram.test_match_histograms_consistency)__name__
__module____qualname__r   chelsear5   	astronautr6   r=   markparametrizer3   r8   r#   float16float32rH   r<   r@   classmethodr   rX   r   r   r   r   r      s(    ,




r   )numpyr#   r=   numpy.testingr   r   skimager   r   skimage._shared.utilsr   skimage.exposurer   r^   r_   arangerandomrandonesr   r   r   r   r   r   <module>   s    
