o
    i e                     @   sL   d Z ddlZddlZddlm  mZ G dd dZG dd dej	j
ZdS )z%Tests utils for preprocessing layers.    Nc                   @   s   e Zd Zdd Zdd ZdS )	ArrayLikec                 C   s
   || _ d S N)values)selfr    r   f/var/www/myenv/lib/python3.10/site-packages/keras/src/layers/preprocessing/preprocessing_test_utils.py__init__   s   
zArrayLike.__init__c                 C   s   t | jS r   )nparrayr   )r   r   r   r   	__array__   s   zArrayLike.__array__N)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   sP   e Zd ZdZdddZdddZeZdd Zd	d
 Zdd Z	dd Z
dd ZdS )PreprocessingLayerTestz7Base test class for preprocessing layer API validation.Nc                 C   s   |du s|du r| j |||d dS t|ttfr7| t|t| t||D ]\}}| j|||d q(dS t|tj	j
rj| t|t| | D ]\}}|| }|r^| d| dnd}| ||| qLdS t|ts|t|drt|jtjr| j|||d dS | j |||d dS )zBAsserts that elements are close (if numeric) or equal (if string).Nmsgz ()dtype)assertAllEqual
isinstancelisttupleassertEquallenzipassertAllCloseOrEqualcollectionsabcMappingitemsfloathasattrr	   
issubdtyper   numberassertAllClose)r   abr   a_valueb_valuekeyerror_messager   r   r   r   %   s,   z,PreprocessingLayerTest.assertAllCloseOrEqualc                 C   s(   | |}| |}| j|||d d S )Nr   )extractr   )r   combineracc1acc2r   data_1data_2r   r   r   assert_extracted_output_equal<   s   

z4PreprocessingLayerTest.assert_extracted_output_equalc              	   C   s  t |dk rtdt | dt|d g}t|d g}t|dd }||}|||||||g}| j||dd	 |||||||g}	| j||	d
d	 ||||||||gg}
| j||
dd	 ||||||}| j||dd	 |||||||g}| j||dd	 ||||g||||g}| j||dd	 | j||dd	 dS )zNValidate that various combinations of compute and merge are
        identical.   z7Data must have at least 4 elements. Received len(data)=.r         Nz0Sharding data should not change the data output.r   z?The order of merge arguments should not change the data output.z:Nesting merge arguments should not change the data output.z<Nesting compute arguments should not change the data output.zAMixing merge and compute calls should not change the data output.zHCalling merge with a data length of 1 should not change the data output.z;Calculated accumulators did not match expected accumulator.)r   AssertionErrorr	   r
   computemergecompare_accumulators)r   r,   dataexpecteddata_0r/   r0   single_compute	all_mergeunordered_all_mergehierarchical_mergenested_computemixed_computesingle_merger   r   r    validate_accumulator_computationE   s   
			
z7PreprocessingLayerTest.validate_accumulator_computationc                 C   s$   | |}||}| || dS )z?Validate that the expected results of computing and extracting.N)r7   r+   r   )r   r,   r:   r;   accextracted_datar   r   r   validate_accumulator_extract   s   

z3PreprocessingLayerTest.validate_accumulator_extractc                 C   sB   | |}||}||}| ||| | ||| dS )z7Validate that the extract<->restore loop loses no data.N)r7   r+   restorer1   r   )r   r,   r:   r;   rE   rF   restored_accr   r   r   (validate_accumulator_extract_and_restore   s
   


z?PreprocessingLayerTest.validate_accumulator_extract_and_restorec                 C   s:   | |}||}||}| || | || dS )z=Validate that the serialize<->deserialize loop loses no data.N)r7   	serializedeserializer9   )r   r,   r:   r;   rE   serialized_datadeserialized_datar   r   r   .validate_accumulator_serialize_and_deserialize   s
   


zEPreprocessingLayerTest.validate_accumulator_serialize_and_deserializec                 C   s0   | |}| |}| || | || dS )zAValidate that every call to compute creates a unique accumulator.N)r7   assertIsNotr9   )r   r,   r:   rE   r.   r   r   r   validate_accumulator_uniqueness   s   

z6PreprocessingLayerTest.validate_accumulator_uniquenessr   )r   r   r   __doc__r   r1   r9   rD   rG   rJ   rO   rQ   r   r   r   r   r      s    

g

r   )rR   r   numpyr	   tensorflow.compat.v2compatv2tfr   testTestCaser   r   r   r   r   <module>   s   