




"""simple docstring"""
import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


a_					:     List[str]					    =  random.Random()

if is_torch_available():
					import torch





def     UpperCAmelCase					(      A__:     Dict				,					A__:     str=1.0				,					A__:     Any=None				,					A__:     Tuple=None  )	->  Union[str, Any]:


			if rng is None:
						__lowerCamelCase       :						int													=					global_rng

			__lowerCamelCase       :						int													=					[]
			for batch_idx in range(shape[0]  ):
						values.append([]  )
						for _ in range(shape[1]  ):
									values[-1].append(rng.random() * scale  )

			return values

class __lowercase(   unittest.TestCase					):




					'''simple docstring'''


					def __init__(     self    ,						__a    ,						__a=7    ,						__a=400    ,						__a=2000    ,						__a=1    ,						__a=0.0    ,						__a=16000    ,						__a=True    ,						__a=True    ,						):
								__lowerCamelCase       :						Union[str, Any]													=					parent
								__lowerCamelCase       :						int													=					batch_size
								__lowerCamelCase       :						List[Any]													=					min_seq_length
								__lowerCamelCase       :						Tuple													=					max_seq_length
								__lowerCamelCase       :						List[Any]													=					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								__lowerCamelCase       :						List[str]													=					feature_size
								__lowerCamelCase       :						Tuple													=					padding_value
								__lowerCamelCase       :						str													=					sampling_rate
								__lowerCamelCase       :						Optional[Any]													=					return_attention_mask
								__lowerCamelCase       :						Optional[int]													=					do_normalize
					def      snake_case_					(     self  ):
								return {
								    "feature_size": self.feature_size,
								    "padding_value": self.padding_value,
								    "sampling_rate": self.sampling_rate,
								    "return_attention_mask": self.return_attention_mask,
								    "do_normalize": self.do_normalize,
								}





					def      snake_case_					(     self    ,						__a=False    ,						__a=False  ):


								def _flatten(__a  ):
											return list(itertools.chain(*__lowerCAmelCase  )  )

								if equal_length:
											__lowerCamelCase       :						Any													=					floats_list((self.batch_size, self.max_seq_length)  )
								else:
											# make sure that inputs increase in size
											__lowerCamelCase       :						str													=					[
											    _flatten(floats_list((x, self.feature_size)  )  )
											    for x in range(self.min_seq_length    ,						self.max_seq_length    ,						self.seq_length_diff  )
											]

								if numpify:
											__lowerCamelCase       :						Union[str, Any]													=					[np.asarray(__lowerCAmelCase  ) for x in speech_inputs]

								return speech_inputs



@require_torch
@require_torchaudio
class __lowercase(   lowerCAmelCase__						,						unittest.TestCase					):




					'''simple docstring'''


					__a    :    Optional[int]  		=			ASTFeatureExtractor
					def      snake_case_					(     self  ):
								__lowerCamelCase       :						Tuple													=					ASTFeatureExtractionTester(self  )
					def      snake_case_					(     self  ):
								__lowerCamelCase       :						Tuple													=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
								# create three inputs of length 800, 1000, and 1200
								__lowerCamelCase       :						List[str]													=					[floats_list((1, x)  )[0] for x in range(800    ,						1400    ,						200  )]
								__lowerCamelCase       :						int													=					[np.asarray(__lowerCAmelCase  ) for speech_input in speech_inputs]

								# Test not batched input
								__lowerCamelCase       :						Optional[int]													=					feat_extract(speech_inputs[0]    ,						return_tensors='np'  ).input_values
								__lowerCamelCase       :						List[str]													=					feat_extract(np_speech_inputs[0]    ,						return_tensors='np'  ).input_values
								self.assertTrue(np.allclose(__lowerCAmelCase    ,						__lowerCAmelCase    ,						atol=1E-3  )  )

								# Test batched
								__lowerCamelCase       :						str													=					feat_extract(__lowerCAmelCase    ,						padding=__lowerCAmelCase    ,						return_tensors='np'  ).input_values
								__lowerCamelCase       :						str													=					feat_extract(__lowerCAmelCase    ,						padding=__lowerCAmelCase    ,						return_tensors='np'  ).input_values
								for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase    ,						__lowerCAmelCase  ):
											self.assertTrue(np.allclose(__lowerCAmelCase    ,						__lowerCAmelCase    ,						atol=1E-3  )  )

								# Test 2-D numpy arrays are batched.
								__lowerCamelCase       :						Optional[int]													=					[floats_list((1, x)  )[0] for x in (800, 800, 800)]
								__lowerCamelCase       :						Optional[Any]													=					np.asarray(__lowerCAmelCase  )
								__lowerCamelCase       :						Tuple													=					feat_extract(__lowerCAmelCase    ,						return_tensors='np'  ).input_values
								__lowerCamelCase       :						List[str]													=					feat_extract(__lowerCAmelCase    ,						return_tensors='np'  ).input_values
								for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase    ,						__lowerCAmelCase  ):
											self.assertTrue(np.allclose(__lowerCAmelCase    ,						__lowerCAmelCase    ,						atol=1E-3  )  )
					@require_torch
					def      snake_case_					(     self  ):
								import torch

								__lowerCamelCase       :						str													=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
								__lowerCamelCase       :						Optional[int]													=					np.random.rand(100  ).astype(np.floataa  )
								__lowerCamelCase       :						str													=					np_speech_inputs.tolist()

								for inputs in [py_speech_inputs, np_speech_inputs]:
											__lowerCamelCase       :						str													=					feature_extractor.pad([{'input_values': inputs}]    ,						return_tensors='np'  )
											self.assertTrue(np_processed.input_values.dtype == np.floataa  )
											__lowerCamelCase       :						Optional[Any]													=					feature_extractor.pad([{'input_values': inputs}]    ,						return_tensors='pt'  )
											self.assertTrue(pt_processed.input_values.dtype == torch.floataa  )
					def      snake_case_					(     self    ,						__a  ):
								from datasets import load_dataset

								__lowerCamelCase       :						Tuple													=					load_dataset('hf-internal-testing/librispeech_asr_dummy'    ,						'clean'    ,						split='validation'  )
								# automatic decoding with librispeech
								__lowerCamelCase       :						str													=					ds.sort('id'  ).select(range(__lowerCAmelCase  )  )[:num_samples]['audio']

								return [x["array"] for x in speech_samples]





					@require_torch
					def      snake_case_					(     self  ):
								__lowerCamelCase       :						str													=					torch.tensor(
								    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
								     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
								     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
								     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]  )
								# fmt: on

								__lowerCamelCase       :						Optional[Any]													=					self._load_datasamples(1  )
								__lowerCamelCase       :						Union[str, Any]													=					ASTFeatureExtractor()
								__lowerCamelCase       :						Dict													=					feature_extractor(__lowerCAmelCase    ,						return_tensors='pt'  ).input_values
								self.assertEquals(input_values.shape    ,						(1, 1024, 128)  )
								self.assertTrue(torch.allclose(input_values[0, 0, :30]    ,						__lowerCAmelCase    ,						atol=1E-4  )  )




'''simple docstring'''




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
						import PIL


__lowercase        =    logging.get_logger(__name__)






def 							snake_case__				(     _A:   Dict ,  _A:   Any					)       ->      Union[str, Any]:

		'''simple docstring'''


		lowerCAmelCase     =    b.T
		lowerCAmelCase     =    np.sum(np.square(_A					) ,  axis=1					)
		lowerCAmelCase     =    np.sum(np.square(_A					) ,  axis=0					)
		lowerCAmelCase     =    np.matmul(_A ,  _A					)
		lowerCAmelCase     =    aa[:, None] - 2 * ab + ba[None, :]
		return d






def 							snake_case__				(     _A:   Dict ,  _A:   Dict					)       ->      Optional[Any]:

		'''simple docstring'''


		lowerCAmelCase     =    x.reshape(-1 ,  3					)
		lowerCAmelCase     =    squared_euclidean_distance(_A ,  _A					)
		return np.argmin(_A ,  axis=1					)






class 							a__(   lowerCAmelCase__      ):


			'''simple docstring'''






			UpperCAmelCase_     :	List[Any]						=			['''pixel_values''']



			def __init__(  self			,       __lowerCAmelCase = None			,       __lowerCAmelCase = True			,       __lowerCAmelCase = None			,       __lowerCAmelCase = PILImageResampling.BILINEAR			,       __lowerCAmelCase = True			,       __lowerCAmelCase = True			,       **__lowerCAmelCase			,       ):




					"""simple docstring"""


					super().__init__(**__lowerCAmelCase)
					lowerCAmelCase     =    size if size is not None else {"""height""": 256, """width""": 256}
					lowerCAmelCase     =    get_size_dict(__lowerCAmelCase)
					lowerCAmelCase     =    np.array(__lowerCAmelCase) if clusters is not None else None
					lowerCAmelCase     =    do_resize
					lowerCAmelCase     =    size
					lowerCAmelCase     =    resample
					lowerCAmelCase     =    do_normalize
					lowerCAmelCase     =    do_color_quantize



			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase = PILImageResampling.BILINEAR			,       __lowerCAmelCase = None			,       **__lowerCAmelCase			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    get_size_dict(__lowerCAmelCase)
					if "height" not in size or "width" not in size:
							raise ValueError(f"Size dictionary must contain both height and width keys. Got {size.keys()}")
					return resize(
					    __lowerCAmelCase			,       size=(size["""height"""], size["""width"""])			,       resample=__lowerCAmelCase			,       data_format=__lowerCAmelCase			,       **__lowerCAmelCase)



			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase = None			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    rescale(image=__lowerCAmelCase			,       scale=1 / 127.5			,       data_format=__lowerCAmelCase)
					lowerCAmelCase     =    image - 1
					return image







			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = None			,       __lowerCAmelCase = ChannelDimension.FIRST			,       **__lowerCAmelCase			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    do_resize if do_resize is not None else self.do_resize
					lowerCAmelCase     =    size if size is not None else self.size
					lowerCAmelCase     =    get_size_dict(__lowerCAmelCase)
					lowerCAmelCase     =    resample if resample is not None else self.resample
					lowerCAmelCase     =    do_normalize if do_normalize is not None else self.do_normalize
					lowerCAmelCase     =    do_color_quantize if do_color_quantize is not None else self.do_color_quantize
					lowerCAmelCase     =    clusters if clusters is not None else self.clusters
					lowerCAmelCase     =    np.array(__lowerCAmelCase)

					lowerCAmelCase     =    make_list_of_images(__lowerCAmelCase)

					if not valid_images(__lowerCAmelCase):
							raise ValueError(
							    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
							    """torch.Tensor, tf.Tensor or jax.ndarray.""")

					if do_resize and size is None or resample is None:
							raise ValueError("""Size and resample must be specified if do_resize is True.""")

					if do_color_quantize and clusters is None:
							raise ValueError("""Clusters must be specified if do_color_quantize is True.""")

					# All transformations expect numpy arrays.
					lowerCAmelCase     =    [to_numpy_array(__lowerCAmelCase) for image in images]

					if do_resize:
							lowerCAmelCase     =    [self.resize(image=__lowerCAmelCase			,       size=__lowerCAmelCase			,       resample=__lowerCAmelCase) for image in images]

					if do_normalize:
							lowerCAmelCase     =    [self.normalize(image=__lowerCAmelCase) for image in images]

					if do_color_quantize:
							lowerCAmelCase     =    [to_channel_dimension_format(__lowerCAmelCase			,       ChannelDimension.LAST) for image in images]
							# color quantize from (batch_size, height, width, 3) to (batch_size, height, width)
							lowerCAmelCase     =    np.array(__lowerCAmelCase)
							lowerCAmelCase     =    color_quantize(__lowerCAmelCase			,       __lowerCAmelCase).reshape(images.shape[:-1])

							# flatten to (batch_size, height*width)
							lowerCAmelCase     =    images.shape[0]
							lowerCAmelCase     =    images.reshape(__lowerCAmelCase			,       -1)

							# We need to convert back to a list of images to keep consistent behaviour across processors.
							lowerCAmelCase     =    list(__lowerCAmelCase)
					else:
							lowerCAmelCase     =    [to_channel_dimension_format(__lowerCAmelCase			,       __lowerCAmelCase) for image in images]

					lowerCAmelCase     =    {"""input_ids""": images}
					return BatchFeature(data=__lowerCAmelCase			,       tensor_type=__lowerCAmelCase)




