



"""simple docstring"""







import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
							from transformers import SpeechaTextFeatureExtractor

A    =			random.Random()

def   lowerCAmelCase__			(     lowerCamelCase__				,      lowerCamelCase__=1.0				,      lowerCamelCase__=None				,      lowerCamelCase__=None			) ->							List[Any]:
							if rng is None:
														A         =					global_rng

							A         =					[]
							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      UpperCAmelCase__		(      unittest.TestCase     ):



		def __init__( self  :    List[Any]      ,							snake_case  :    Tuple      ,							snake_case  :    int=7      ,							snake_case  :    List[Any]=400      ,							snake_case  :    str=2_000      ,							snake_case  :    List[Any]=24      ,							snake_case  :    Tuple=24      ,							snake_case  :    Union[str, Any]=0.0      ,							snake_case  :    int=16_000      ,							snake_case  :    List[str]=True      ,							snake_case  :    Union[str, Any]=True      ,							)			->						List[Any]:






									'''simple docstring'''

									A         =					parent
									A         =					batch_size
									A         =					min_seq_length
									A         =					max_seq_length
									A         =					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
									A         =					feature_size
									A         =					num_mel_bins
									A         =					padding_value
									A         =					sampling_rate
									A         =					return_attention_mask
									A         =					do_normalize



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[int]:






									'''simple docstring'''

									return {
									    "feature_size": self.feature_size,
									    "num_mel_bins": self.num_mel_bins,
									    "padding_value": self.padding_value,
									    "sampling_rate": self.sampling_rate,
									    "return_attention_mask": self.return_attention_mask,
									    "do_normalize": self.do_normalize,
									}



		def 					A_		( self  :    List[Any]      ,							snake_case  :    Dict=False      ,							snake_case  :    List[str]=False				)			->						Optional[Any]:






									'''simple docstring'''

									def _flatten(snake_case  :    Dict				):
																return list(itertools.chain(*snake_case				)				)

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






@require_torch
@require_torchaudio
class      UpperCAmelCase__		(      UpperCamelCase   ,unittest.TestCase     ):
		lowerCAmelCase_       :	List[Any] 							=     SpeechaTextFeatureExtractor if is_speech_available() else None



		def 					A_		( self  :    Dict				)			->						Optional[int]:






									'''simple docstring'''

									A         =					SpeechaTextFeatureExtractionTester(self				)



		def 					A_		( self  :    List[str]      ,							snake_case  :    Tuple				)			->						int:






									'''simple docstring'''

									self.assertTrue(np.all(np.mean(snake_case      ,							axis=0				) < 1E-3				)				)
									self.assertTrue(np.all(np.abs(np.var(snake_case      ,							axis=0				) - 1				) < 1E-3				)				)



		def 					A_		( self  :    Any				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									# create three inputs of length 800, 1000, and 1200
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					[np.asarray(snake_case				) for speech_input in speech_inputs]

									# Test feature size
									A         =					feature_extractor(snake_case      ,							padding=snake_case      ,							return_tensors='np'				).input_features
									self.assertTrue(input_features.ndim == 3				)
									self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size				)

									# Test not batched input
									A         =					feature_extractor(speech_inputs[0]      ,							return_tensors='np'				).input_features
									A         =					feature_extractor(np_speech_inputs[0]      ,							return_tensors='np'				).input_features
									self.assertTrue(np.allclose(snake_case      ,							snake_case      ,							atol=1E-3				)				)

									# Test batched
									A         =					feature_extractor(snake_case      ,							return_tensors='np'				).input_features
									A         =					feature_extractor(snake_case      ,							return_tensors='np'				).input_features
									for enc_seq_a, enc_seq_a in zip(snake_case      ,							snake_case				):
																self.assertTrue(np.allclose(snake_case      ,							snake_case      ,							atol=1E-3				)				)

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



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]

									A         =					['longest', 'max_length', 'do_not_pad']
									A         =					[None, 16, None]
									for max_length, padding in zip(snake_case      ,							snake_case				):
																A         =					feature_extractor(
																    snake_case      ,							padding=snake_case      ,							max_length=snake_case      ,							return_attention_mask=snake_case				)
																A         =					inputs.input_features
																A         =					inputs.attention_mask
																A         =					[np.sum(snake_case				) for x in attention_mask]

																self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]				)
																self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]				)
																self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]				)



		def 					A_		( self  :    str				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]

									A         =					['longest', 'max_length', 'do_not_pad']
									A         =					[None, 16, None]
									for max_length, padding in zip(snake_case      ,							snake_case				):
																A         =					feature_extractor(
																    snake_case      ,							max_length=snake_case      ,							padding=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case				)
																A         =					inputs.input_features
																A         =					inputs.attention_mask
																A         =					[np.sum(snake_case				) for x in attention_mask]

																self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]				)
																self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6				)
																self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]				)
																self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6				)
																self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]				)



		def 					A_		( self  :    str				)			->						str:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='max_length'      ,							max_length=4      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					np.sum(attention_mask == 1      ,							axis=1				)

									self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]				)
									self._check_zero_mean_unit_variance(input_features[1]				)
									self._check_zero_mean_unit_variance(input_features[2]				)



		def 					A_		( self  :    Optional[int]				)			->						int:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='longest'      ,							max_length=4      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					np.sum(attention_mask == 1      ,							axis=1				)

									self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]				)
									self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]				)
									self._check_zero_mean_unit_variance(input_features[2]				)

									# make sure that if max_length < longest -> then pad to max_length
									self.assertEqual(input_features.shape      ,							(3, 4, 24)				)

									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='longest'      ,							max_length=16      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					np.sum(attention_mask == 1      ,							axis=1				)

									self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]				)
									self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]				)
									self._check_zero_mean_unit_variance(input_features[2]				)

									# make sure that if max_length < longest -> then pad to max_length
									self.assertEqual(input_features.shape      ,							(3, 6, 24)				)



		def 					A_		( self  :    List[str]				)			->						Optional[int]:






									'''simple docstring'''

									import torch

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					np.random.rand(100      ,							32				).astype(np.floataa				)
									A         =					np_speech_inputs.tolist()

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



		def 					A_		( self  :    Tuple      ,							snake_case  :    List[Any]				)			->						Optional[Any]:






									'''simple docstring'''

									from datasets import load_dataset

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

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



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[int]:






									'''simple docstring'''

									A         =					np.array([
									    -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
									    -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
									    -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
									]				)
									# fmt: on

									A         =					self._load_datasamples(1				)
									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					feature_extractor(snake_case      ,							return_tensors='pt'				).input_features
									self.assertEquals(input_features.shape      ,							(1, 584, 24)				)
									self.assertTrue(np.allclose(input_features[0, 0, :30]      ,							snake_case      ,							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 (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


A    =			logging.get_logger(__name__)






class      UpperCAmelCase__		(      UpperCamelCase     ):
		lowerCAmelCase_       :	Tuple 							=     ["""pixel_values"""]



		def __init__( self  :    int      ,							snake_case  :    bool = True      ,							snake_case  :    Optional[Dict[str, int]] = None      ,							snake_case  :    PILImageResampling = PILImageResampling.BICUBIC      ,							snake_case  :    bool = True      ,							snake_case  :    bool = True      ,							snake_case  :    Union[int, float] = 1 / 255      ,							snake_case  :    Dict[str, int] = None      ,							snake_case  :    bool = True      ,							snake_case  :    Optional[Union[float, List[float]]] = None      ,							snake_case  :    Optional[Union[float, List[float]]] = None      ,							**snake_case  :    Union[str, Any]      ,							)			->						None:






									'''simple docstring'''

									super().__init__(**snake_case				)
									A         =					size if size is not None else {'height': 224, 'width': 224}
									A         =					get_size_dict(snake_case				)
									A         =					crop_size if crop_size is not None else {'height': 224, 'width': 224}
									A         =					get_size_dict(snake_case      ,							default_to_square=snake_case      ,							param_name='crop_size'				)

									A         =					do_resize
									A         =					do_rescale
									A         =					do_normalize
									A         =					do_center_crop
									A         =					crop_size
									A         =					size
									A         =					resample
									A         =					rescale_factor
									A         =					image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
									A         =					image_std if image_std is not None else IMAGENET_DEFAULT_STD



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    np.ndarray      ,							snake_case  :    Dict[str, int]      ,							snake_case  :    PILImageResampling = PILImageResampling.BILINEAR      ,							snake_case  :    Optional[Union[str, ChannelDimension]] = None      ,							**snake_case  :    Dict      ,							)			->						np.ndarray:






									'''simple docstring'''

									A         =					get_size_dict(snake_case				)

									if "shortest_edge" in size:
																A         =					get_resize_output_image_size(snake_case      ,							size=size['shortest_edge']      ,							default_to_square=snake_case				)
																# size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
									elif "height" in size and "width" in size:
																A         =					(size['height'], size['width'])
									else:
																raise ValueError(f"""Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}"""				)
									return resize(snake_case      ,							size=snake_case      ,							resample=snake_case      ,							data_format=snake_case      ,							**snake_case				)



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    np.ndarray      ,							snake_case  :    Dict[str, int]      ,							snake_case  :    Optional[Union[str, ChannelDimension]] = None      ,							**snake_case  :    List[Any]      ,							)			->						np.ndarray:






									'''simple docstring'''

									A         =					get_size_dict(snake_case				)
									if "height" not in size or "width" not in size:
																raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""				)
									return center_crop(snake_case      ,							size=(size['height'], size['width'])      ,							data_format=snake_case      ,							**snake_case				)



		def 					A_		( self  :    Dict      ,							snake_case  :    np.ndarray      ,							snake_case  :    float      ,							snake_case  :    Optional[Union[str, ChannelDimension]] = None      ,							**snake_case  :    Dict				)			->						np.ndarray:






									'''simple docstring'''

									return rescale(snake_case      ,							scale=snake_case      ,							data_format=snake_case      ,							**snake_case				)



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    np.ndarray      ,							snake_case  :    Union[float, List[float]]      ,							snake_case  :    Union[float, List[float]]      ,							snake_case  :    Optional[Union[str, ChannelDimension]] = None      ,							**snake_case  :    Union[str, Any]      ,							)			->						np.ndarray:






									'''simple docstring'''

									return normalize(snake_case      ,							mean=snake_case      ,							std=snake_case      ,							data_format=snake_case      ,							**snake_case				)



		def 					A_		( self  :    int      ,							snake_case  :    ImageInput      ,							snake_case  :    Optional[bool] = None      ,							snake_case  :    Dict[str, int] = None      ,							snake_case  :    PILImageResampling = None      ,							snake_case  :    bool = None      ,							snake_case  :    int = None      ,							snake_case  :    Optional[bool] = None      ,							snake_case  :    Optional[float] = None      ,							snake_case  :    Optional[bool] = None      ,							snake_case  :    Optional[Union[float, List[float]]] = None      ,							snake_case  :    Optional[Union[float, List[float]]] = None      ,							snake_case  :    Optional[Union[str, TensorType]] = None      ,							snake_case  :    Union[str, ChannelDimension] = ChannelDimension.FIRST      ,							**snake_case  :    List[str]      ,							)			->						BatchFeature:






									'''simple docstring'''

									A         =					do_resize if do_resize is not None else self.do_resize
									A         =					do_rescale if do_rescale is not None else self.do_rescale
									A         =					do_normalize if do_normalize is not None else self.do_normalize
									A         =					do_center_crop if do_center_crop is not None else self.do_center_crop
									A         =					crop_size if crop_size is not None else self.crop_size
									A         =					get_size_dict(snake_case      ,							param_name='crop_size'      ,							default_to_square=snake_case				)
									A         =					resample if resample is not None else self.resample
									A         =					rescale_factor if rescale_factor is not None else self.rescale_factor
									A         =					image_mean if image_mean is not None else self.image_mean
									A         =					image_std if image_std is not None else self.image_std

									A         =					size if size is not None else self.size
									A         =					get_size_dict(snake_case				)

									if not is_batched(snake_case				):
																A         =					[images]

									if not valid_images(snake_case				):
																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.'				)

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

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

									if do_center_crop:
																A         =					[self.center_crop(image=snake_case      ,							size=snake_case				) for image in images]

									if do_rescale:
																A         =					[self.rescale(image=snake_case      ,							scale=snake_case				) for image in images]

									if do_normalize:
																A         =					[self.normalize(image=snake_case      ,							mean=snake_case      ,							std=snake_case				) for image in images]

									A         =					[to_channel_dimension_format(snake_case      ,							snake_case				) for image in images]

									A         =					{'pixel_values': images}
									return BatchFeature(data=snake_case      ,							tensor_type=snake_case				)



