


'''simple docstring'''

import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
							from transformers import WhisperFeatureExtractor

if is_torch_available():
							import torch

lowerCAmelCase				:Dict        =      random.Random()





def lowerCamelCase      (      lowerCAmelCase    :     Union[str, Any]		,		lowerCAmelCase    :     str=1.0		,		lowerCAmelCase    :     Optional[int]=None		,		lowerCAmelCase    :     Union[str, Any]=None		) ->    Any:





	"""simple docstring"""







	if rng is None:
		__magic_name__	:   Optional[Any]					     =					global_rng

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

	return values
@require_torch
@require_torchaudio
class _lowerCamelCase	(						unittest.TestCase							):



		'''simple docstring'''







		def __init__( self		:						Union[str, Any]  ,					_A		:						int  ,					_A		:						Optional[int]=7  ,					_A		:						Any=400  ,					_A		:						Tuple=2000  ,					_A		:						Tuple=10  ,					_A		:						List[Any]=160  ,					_A		:						str=8  ,					_A		:						List[str]=0.0  ,					_A		:						str=4000  ,					_A		:						Any=False  ,					_A		:						List[str]=True  ,					)  ->			Tuple:
			__magic_name__	:   int					     =					parent
			__magic_name__	:   Any					     =					batch_size
			__magic_name__	:   str					     =					min_seq_length
			__magic_name__	:   Any					     =					max_seq_length
			__magic_name__	:   int					     =					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
			__magic_name__	:   Optional[Any]					     =					padding_value
			__magic_name__	:   List[str]					     =					sampling_rate
			__magic_name__	:   Optional[int]					     =					return_attention_mask
			__magic_name__	:   Any					     =					do_normalize
			__magic_name__	:   Union[str, Any]					     =					feature_size
			__magic_name__	:   Any					     =					chunk_length
			__magic_name__	:   Optional[Any]					     =					hop_length







		def        __lowerCAmelCase		( self		:						List[Any]      )  ->			str:
			return {
			    "feature_size": self.feature_size,
			    "hop_length": self.hop_length,
			    "chunk_length": self.chunk_length,
			    "padding_value": self.padding_value,
			    "sampling_rate": self.sampling_rate,
			    "return_attention_mask": self.return_attention_mask,
			    "do_normalize": self.do_normalize,
			}







		def        __lowerCAmelCase		( self		:						List[str]  ,					_A		:						Any=False  ,					_A		:						List[str]=False      )  ->			Any:
			def _flatten(_A		:						str      ):
				return list(itertools.chain(*_A      )      )

			if equal_length:
				__magic_name__	:   List[Any]					     =					[floats_list((self.max_seq_length, self.feature_size)      ) for _ in range(self.batch_size      )]
			else:
				# make sure that inputs increase in size
				__magic_name__	:   str					     =					[
				    floats_list((x, self.feature_size)      )
				    for x in range(self.min_seq_length  ,					self.max_seq_length  ,					self.seq_length_diff      )
				]
			if numpify:
				__magic_name__	:   str					     =					[np.asarray(_A      ) for x in speech_inputs]
			return speech_inputs







@require_torch
@require_torchaudio
class _lowerCamelCase	(						lowercase__		,					unittest.TestCase							):



		'''simple docstring'''







		A_     :      Union[str, Any]				   =    WhisperFeatureExtractor if is_speech_available() else None







		def        __lowerCAmelCase		( self		:						Optional[Any]      )  ->			Dict:
			__magic_name__	:   Dict					     =					WhisperFeatureExtractionTester(self      )







		def        __lowerCAmelCase		( self		:						Optional[Any]      )  ->			Union[str, Any]:
			__magic_name__	:   Optional[Any]					     =					self.feature_extraction_class(**self.feat_extract_dict      )

			with tempfile.TemporaryDirectory() as tmpdirname:
				__magic_name__	:   List[Any]					     =					feat_extract_first.save_pretrained(_A      )[0]
				check_json_file_has_correct_format(_A      )
				__magic_name__	:   Optional[int]					     =					self.feature_extraction_class.from_pretrained(_A      )

			__magic_name__	:   Any					     =					feat_extract_first.to_dict()
			__magic_name__	:   Any					     =					feat_extract_second.to_dict()
			__magic_name__	:   Optional[int]					     =					feat_extract_first.mel_filters
			__magic_name__	:   Optional[int]					     =					feat_extract_second.mel_filters
			self.assertTrue(np.allclose(_A  ,					_A      )      )
			self.assertEqual(_A  ,					_A      )







		def        __lowerCAmelCase		( self		:						List[str]      )  ->			List[str]:
			__magic_name__	:   Optional[int]					     =					self.feature_extraction_class(**self.feat_extract_dict      )

			with tempfile.TemporaryDirectory() as tmpdirname:
				__magic_name__	:   List[str]					     =					os.path.join(_A  ,					'feat_extract.json'      )
				feat_extract_first.to_json_file(_A      )
				__magic_name__	:   List[Any]					     =					self.feature_extraction_class.from_json_file(_A      )

			__magic_name__	:   Optional[Any]					     =					feat_extract_first.to_dict()
			__magic_name__	:   Optional[Any]					     =					feat_extract_second.to_dict()
			__magic_name__	:   List[Any]					     =					feat_extract_first.mel_filters
			__magic_name__	:   Tuple					     =					feat_extract_second.mel_filters
			self.assertTrue(np.allclose(_A  ,					_A      )      )
			self.assertEqual(_A  ,					_A      )







		def        __lowerCAmelCase		( self		:						str      )  ->			List[str]:
			# Tests that all call wrap to encode_plus and batch_encode_plus
			__magic_name__	:   List[str]					     =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
			# create three inputs of length 800, 1000, and 1200
			__magic_name__	:   Optional[Any]					     =					[floats_list((1, x)      )[0] for x in range(800  ,					1400  ,					200      )]
			__magic_name__	:   Any					     =					[np.asarray(_A      ) for speech_input in speech_inputs]

			# Test feature size
			__magic_name__	:   Any					     =					feature_extractor(_A  ,					padding='max_length'  ,					return_tensors='np'      ).input_features
			self.assertTrue(input_features.ndim == 3      )
			self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames      )
			self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size      )

			# Test not batched input
			__magic_name__	:   Tuple					     =					feature_extractor(speech_inputs[0]  ,					return_tensors='np'      ).input_features
			__magic_name__	:   Optional[int]					     =					feature_extractor(np_speech_inputs[0]  ,					return_tensors='np'      ).input_features
			self.assertTrue(np.allclose(_A  ,					_A  ,					atol=1E-3      )      )

			# Test batched
			__magic_name__	:   Tuple					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			__magic_name__	:   Optional[Any]					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			for enc_seq_a, enc_seq_a in zip(_A  ,					_A      ):
				self.assertTrue(np.allclose(_A  ,					_A  ,					atol=1E-3      )      )

			# Test 2-D numpy arrays are batched.
			__magic_name__	:   Any					     =					[floats_list((1, x)      )[0] for x in (800, 800, 800)]
			__magic_name__	:   Union[str, Any]					     =					np.asarray(_A      )
			__magic_name__	:   Dict					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			__magic_name__	:   Any					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			for enc_seq_a, enc_seq_a in zip(_A  ,					_A      ):
				self.assertTrue(np.allclose(_A  ,					_A  ,					atol=1E-3      )      )

			# Test truncation required
			__magic_name__	:   List[str]					     =					[floats_list((1, x)      )[0] for x in range(200  ,					(feature_extractor.n_samples + 500)  ,					200      )]
			__magic_name__	:   Union[str, Any]					     =					[np.asarray(_A      ) for speech_input in speech_inputs]

			__magic_name__	:   List[str]					     =					[x[: feature_extractor.n_samples] for x in speech_inputs]
			__magic_name__	:   int					     =					[np.asarray(_A      ) for speech_input in speech_inputs_truncated]

			__magic_name__	:   int					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			__magic_name__	:   str					     =					feature_extractor(_A  ,					return_tensors='np'      ).input_features
			for enc_seq_a, enc_seq_a in zip(_A  ,					_A      ):
				self.assertTrue(np.allclose(_A  ,					_A  ,					atol=1E-3      )      )







		def        __lowerCAmelCase		( self		:						List[Any]      )  ->			List[str]:
			import torch

			__magic_name__	:   Optional[int]					     =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
			__magic_name__	:   Any					     =					np.random.rand(100  ,					32      ).astype(np.floataa      )
			__magic_name__	:   List[str]					     =					np_speech_inputs.tolist()

			for inputs in [py_speech_inputs, np_speech_inputs]:
				__magic_name__	:   List[Any]					     =					feature_extractor.pad([{'input_features': inputs}]  ,					return_tensors='np'      )
				self.assertTrue(np_processed.input_features.dtype == np.floataa      )
				__magic_name__	:   Any					     =					feature_extractor.pad([{'input_features': inputs}]  ,					return_tensors='pt'      )
				self.assertTrue(pt_processed.input_features.dtype == torch.floataa      )







		def        __lowerCAmelCase		( self		:						List[str]  ,					_A		:						Union[str, Any]      )  ->			Tuple:
			__magic_name__	:   Optional[Any]					     =					load_dataset('hf-internal-testing/librispeech_asr_dummy'  ,					'clean'  ,					split='validation'      )
			# automatic decoding with librispeech
			__magic_name__	:   int					     =					ds.sort('id'      ).select(range(_A      )      )[:num_samples]['audio']

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







		def        __lowerCAmelCase		( self		:						Dict      )  ->			Union[str, Any]:
			# fmt: off
			__magic_name__	:   int					     =					torch.tensor(
			    [
			        0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951,
			        0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678,
			        0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554,
			        -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854
			    ]      )
			# fmt: on

			__magic_name__	:   Tuple					     =					self._load_datasamples(1      )
			__magic_name__	:   Any					     =					WhisperFeatureExtractor()
			__magic_name__	:   int					     =					feature_extractor(_A  ,					return_tensors='pt'      ).input_features
			self.assertEqual(input_features.shape  ,					(1, 80, 3000)      )
			self.assertTrue(torch.allclose(input_features[0, 0, :30]  ,					_A  ,					atol=1E-4      )      )







		def        __lowerCAmelCase		( self		:						List[str]      )  ->			Optional[int]:
			__magic_name__	:   Any					     =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
			__magic_name__	:   int					     =					self._load_datasamples(1      )[0]
			__magic_name__	:   List[Any]					     =					((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
			__magic_name__	:   Union[str, Any]					     =					feat_extract.zero_mean_unit_var_norm([audio]  ,					attention_mask=_A      )[0]

			self.assertTrue(np.all(np.mean(_A      ) < 1E-3      )      )
			self.assertTrue(np.all(np.abs(np.var(_A      ) - 1      ) < 1E-3      )      )



'''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 (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowerCAmelCase				:Tuple        =      logging.get_logger(__name__)
class _lowerCamelCase	(						lowercase__							):



		'''simple docstring'''







		A_     :      int				   =    ["""pixel_values"""]







		def __init__( self		:						Any  ,					_A		:						bool = True  ,					_A		:						Optional[Dict[str, int]] = None  ,					_A		:						PILImageResampling = PILImageResampling.BILINEAR  ,					_A		:						bool = True  ,					_A		:						Dict[str, int] = None  ,					_A		:						bool = True  ,					_A		:						Union[int, float] = 1 / 255  ,					_A		:						bool = True  ,					_A		:						Optional[Union[float, List[float]]] = None  ,					_A		:						Optional[Union[float, List[float]]] = None  ,					**_A		:						Optional[Any]  ,					)  ->			None:
			super().__init__(**_A      )
			__magic_name__	:   List[str]					     =					size if size is not None else {'shortest_edge': 256}
			__magic_name__	:   str					     =					get_size_dict(_A  ,					default_to_square=_A      )
			__magic_name__	:   List[str]					     =					crop_size if crop_size is not None else {'height': 224, 'width': 224}
			__magic_name__	:   Optional[int]					     =					get_size_dict(_A      )
			__magic_name__	:   Union[str, Any]					     =					do_resize
			__magic_name__	:   List[Any]					     =					size
			__magic_name__	:   List[str]					     =					resample
			__magic_name__	:   Dict					     =					do_center_crop
			__magic_name__	:   List[str]					     =					crop_size
			__magic_name__	:   int					     =					do_rescale
			__magic_name__	:   Tuple					     =					rescale_factor
			__magic_name__	:   List[str]					     =					do_normalize
			__magic_name__	:   Union[str, Any]					     =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
			__magic_name__	:   Dict					     =					image_std if image_std is not None else IMAGENET_STANDARD_STD







		def        __lowerCAmelCase		( self		:						Optional[Any]  ,					_A		:						np.ndarray  ,					_A		:						Dict[str, int]  ,					_A		:						PILImageResampling = PILImageResampling.BICUBIC  ,					_A		:						Optional[Union[str, ChannelDimension]] = None  ,					**_A		:						List[str]  ,					)  ->			np.ndarray:
			__magic_name__	:   Optional[Any]					     =					get_size_dict(_A  ,					default_to_square=_A      )
			if "shortest_edge" not in size:
				raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'      )
			__magic_name__	:   Dict					     =					get_resize_output_image_size(_A  ,					size=size['shortest_edge']  ,					default_to_square=_A      )
			return resize(_A  ,					size=_A  ,					resample=_A  ,					data_format=_A  ,					**_A      )







		def        __lowerCAmelCase		( self		:						Dict  ,					_A		:						np.ndarray  ,					_A		:						Dict[str, int]  ,					_A		:						Optional[Union[str, ChannelDimension]] = None  ,					**_A		:						Optional[int]  ,					)  ->			np.ndarray:
			__magic_name__	:   int					     =					get_size_dict(_A      )
			return center_crop(_A  ,					size=(size['height'], size['width'])  ,					data_format=_A  ,					**_A      )







		def        __lowerCAmelCase		( self		:						List[str]  ,					_A		:						np.ndarray  ,					_A		:						float  ,					_A		:						Optional[Union[str, ChannelDimension]] = None  ,					**_A		:						Tuple      )  ->			np.ndarray:
			return rescale(_A  ,					scale=_A  ,					data_format=_A  ,					**_A      )







		def        __lowerCAmelCase		( self		:						str  ,					_A		:						np.ndarray  ,					_A		:						Union[float, List[float]]  ,					_A		:						Union[float, List[float]]  ,					_A		:						Optional[Union[str, ChannelDimension]] = None  ,					**_A		:						int  ,					)  ->			np.ndarray:
			return normalize(_A  ,					mean=_A  ,					std=_A  ,					data_format=_A  ,					**_A      )







		def        __lowerCAmelCase		( self		:						List[str]  ,					_A		:						ImageInput  ,					_A		:						Optional[bool] = None  ,					_A		:						Dict[str, int] = None  ,					_A		:						PILImageResampling = None  ,					_A		:						bool = None  ,					_A		:						Dict[str, int] = None  ,					_A		:						Optional[bool] = None  ,					_A		:						Optional[float] = None  ,					_A		:						Optional[bool] = None  ,					_A		:						Optional[Union[float, List[float]]] = None  ,					_A		:						Optional[Union[float, List[float]]] = None  ,					_A		:						Optional[Union[str, TensorType]] = None  ,					_A		:						Union[str, ChannelDimension] = ChannelDimension.FIRST  ,					**_A		:						List[Any]  ,					)  ->			List[str]:
			__magic_name__	:   int					     =					do_resize if do_resize is not None else self.do_resize
			__magic_name__	:   Tuple					     =					size if size is not None else self.size
			__magic_name__	:   Optional[Any]					     =					get_size_dict(_A  ,					default_to_square=_A      )
			__magic_name__	:   Dict					     =					resample if resample is not None else self.resample
			__magic_name__	:   Optional[Any]					     =					do_center_crop if do_center_crop is not None else self.do_center_crop
			__magic_name__	:   Dict					     =					crop_size if crop_size is not None else self.crop_size
			__magic_name__	:   List[str]					     =					get_size_dict(_A      )
			__magic_name__	:   Union[str, Any]					     =					do_rescale if do_rescale is not None else self.do_rescale
			__magic_name__	:   str					     =					rescale_factor if rescale_factor is not None else self.rescale_factor
			__magic_name__	:   Any					     =					do_normalize if do_normalize is not None else self.do_normalize
			__magic_name__	:   Tuple					     =					image_mean if image_mean is not None else self.image_mean
			__magic_name__	:   Union[str, Any]					     =					image_std if image_std is not None else self.image_std

			__magic_name__	:   int					     =					make_list_of_images(_A      )

			if not valid_images(_A      ):
				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:
				raise ValueError('Size must be specified if do_resize is True.'      )

			if do_center_crop and crop_size is None:
				raise ValueError('Crop size must be specified if do_center_crop is True.'      )

			if do_rescale and rescale_factor is None:
				raise ValueError('Rescale factor must be specified if do_rescale is True.'      )

			if do_normalize and (image_mean is None or image_std is None):
				raise ValueError('Image mean and std must be specified if do_normalize is True.'      )

			# All transformations expect numpy arrays.
			__magic_name__	:   List[Any]					     =					[to_numpy_array(_A      ) for image in images]

			if do_resize:
				__magic_name__	:   Union[str, Any]					     =					[self.resize(image=_A  ,					size=_A  ,					resample=_A      ) for image in images]

			if do_center_crop:
				__magic_name__	:   Union[str, Any]					     =					[self.center_crop(image=_A  ,					size=_A      ) for image in images]

			if do_rescale:
				__magic_name__	:   List[Any]					     =					[self.rescale(image=_A  ,					scale=_A      ) for image in images]

			if do_normalize:
				__magic_name__	:   Optional[Any]					     =					[self.normalize(image=_A  ,					mean=_A  ,					std=_A      ) for image in images]

			__magic_name__	:   Union[str, Any]					     =					[to_channel_dimension_format(_A  ,					_A      ) for image in images]

			__magic_name__	:   List[str]					     =					{'pixel_values': images}
			return BatchFeature(data=_A  ,					tensor_type=_A      )