




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

lowerCamelCase				:		Optional[int]     =   random.Random()

def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       List[Any]	,							lowercase	:       str=1.0	,							lowercase	:       Tuple=None	,							lowercase	:       str=None     ):


      '''simple docstring'''



      if rng is None:
            lowerCamelCase_		     =    global_rng

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



 '''simple docstring'''


 def __init__(				self					:  List[str]						,							A_					:  str						,							A_					:  Union[str, Any]=7						,							A_					:  List[str]=400						,							A_					:  List[Any]=2000						,							A_					:  Dict=24						,							A_					:  Dict=24						,							A_					:  List[Any]=0.0						,							A_					:  Tuple=16000						,							A_					:  Union[str, Any]=True						,							A_					:  str=True						,							)   ->	Optional[int]:

       """simple docstring"""






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

 def 				a__ (				self					:  Tuple  )   ->	Tuple:

       """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]						,							A_					:  Optional[int]=False						,							A_					:  Dict=False  )   ->	List[str]:

       """simple docstring"""






       def _flatten(A_					:  Optional[int]  ):
             return list(itertools.chain(*A_  )  )

       if equal_length:
             lowerCamelCase_		     =    [floats_list((self.max_seq_length, self.feature_size)  ) for _ in range(self.batch_size  )]
       else:
             # make sure that inputs increase in size
             lowerCamelCase_		     =    [
                 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_		     =    [np.asarray(A_  ) for x in speech_inputs]
       return speech_inputs







@require_torch
@require_torchaudio
class   A(						UpperCamelCase							,      unittest.TestCase   ):



 '''simple docstring'''


 UpperCamelCase						   =						SpeechaTextFeatureExtractor if is_speech_available() else None

 def 				a__ (				self					:  Optional[Any]  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    SpeechaTextFeatureExtractionTester(self  )

 def 				a__ (				self					:  str						,							A_					:  Tuple  )   ->	str:

       """simple docstring"""






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

 def 				a__ (				self					:  Tuple  )   ->	Any:

       """simple docstring"""






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

       # Test feature size
       lowerCamelCase_		     =    feature_extractor(A_						,							padding=A_						,							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
       lowerCamelCase_		     =    feature_extractor(speech_inputs[0]						,							return_tensors='np'  ).input_features
       lowerCamelCase_		     =    feature_extractor(np_speech_inputs[0]						,							return_tensors='np'  ).input_features
       self.assertTrue(np.allclose(A_						,							A_						,							atol=1E-3  )  )

       # Test batched
       lowerCamelCase_		     =    feature_extractor(A_						,							return_tensors='np'  ).input_features
       lowerCamelCase_		     =    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.
       lowerCamelCase_		     =    [floats_list((1, x)  )[0] for x in (800, 800, 800)]
       lowerCamelCase_		     =    np.asarray(A_  )
       lowerCamelCase_		     =    feature_extractor(A_						,							return_tensors='np'  ).input_features
       lowerCamelCase_		     =    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 				a__ (				self					:  int  )   ->	Union[str, Any]:

       """simple docstring"""






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

       lowerCamelCase_		     =    ['longest', 'max_length', 'do_not_pad']
       lowerCamelCase_		     =    [None, 16, None]
       for max_length, padding in zip(A_						,							A_  ):
             lowerCamelCase_		     =    feature_extractor(
                 A_						,							padding=A_						,							max_length=A_						,							return_attention_mask=A_  )
             lowerCamelCase_		     =    inputs.input_features
             lowerCamelCase_		     =    inputs.attention_mask
             lowerCamelCase_		     =    [np.sum(A_  ) 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					:  Any  )   ->	Optional[Any]:

       """simple docstring"""






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

       lowerCamelCase_		     =    ['longest', 'max_length', 'do_not_pad']
       lowerCamelCase_		     =    [None, 16, None]
       for max_length, padding in zip(A_						,							A_  ):
             lowerCamelCase_		     =    feature_extractor(
                 A_						,							max_length=A_						,							padding=A_						,							return_tensors='np'						,							return_attention_mask=A_  )
             lowerCamelCase_		     =    inputs.input_features
             lowerCamelCase_		     =    inputs.attention_mask
             lowerCamelCase_		     =    [np.sum(A_  ) 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					:  Union[str, Any]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
       lowerCamelCase_		     =    [floats_list((1, x)  )[0] for x in range(800						,							1400						,							200  )]
       lowerCamelCase_		     =    feature_extractor(
           A_						,							padding='max_length'						,							max_length=4						,							truncation=A_						,							return_tensors='np'						,							return_attention_mask=A_						,							)
       lowerCamelCase_		     =    inputs.input_features
       lowerCamelCase_		     =    inputs.attention_mask
       lowerCamelCase_		     =    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]  )   ->	Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_		     =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
       lowerCamelCase_		     =    [floats_list((1, x)  )[0] for x in range(800						,							1400						,							200  )]
       lowerCamelCase_		     =    feature_extractor(
           A_						,							padding='longest'						,							max_length=4						,							truncation=A_						,							return_tensors='np'						,							return_attention_mask=A_						,							)
       lowerCamelCase_		     =    inputs.input_features
       lowerCamelCase_		     =    inputs.attention_mask
       lowerCamelCase_		     =    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)  )

       lowerCamelCase_		     =    [floats_list((1, x)  )[0] for x in range(800						,							1400						,							200  )]
       lowerCamelCase_		     =    feature_extractor(
           A_						,							padding='longest'						,							max_length=16						,							truncation=A_						,							return_tensors='np'						,							return_attention_mask=A_						,							)
       lowerCamelCase_		     =    inputs.input_features
       lowerCamelCase_		     =    inputs.attention_mask
       lowerCamelCase_		     =    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]  )   ->	List[str]:

       """simple docstring"""






       import torch

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

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

 def 				a__ (				self					:  Tuple						,							A_					:  str  )   ->	Tuple:

       """simple docstring"""






       from datasets import load_dataset

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

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





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

       """simple docstring"""






       lowerCamelCase_		     =    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

       lowerCamelCase_		     =    self._load_datasamples(1  )
       lowerCamelCase_		     =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
       lowerCamelCase_		     =    feature_extractor(A_						,							return_tensors='pt'  ).input_features
       self.assertEquals(input_features.shape						,							(1, 584, 24)  )
       self.assertTrue(np.allclose(input_features[0, 0, :30]						,							A_						,							atol=1E-4  )  )









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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


lowerCamelCase				:		Optional[Any]     =   logging.get_logger(__name__)


if is_vision_available():
 import PIL







class   A(						UpperCamelCase   ):



 '''simple docstring'''


 UpperCamelCase						   =						['''pixel_values''']

 def __init__(				self					:  List[Any]						,							A_					:  bool = True						,							A_					:  Dict[str, int] = None						,							A_					:  PILImageResampling = PILImageResampling.BICUBIC						,							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_					:  bool = True						,							**A_					:  Dict						,							)   ->	None:

       """simple docstring"""






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

       lowerCamelCase_		     =    do_resize
       lowerCamelCase_		     =    size
       lowerCamelCase_		     =    resample
       lowerCamelCase_		     =    do_center_crop
       lowerCamelCase_		     =    crop_size
       lowerCamelCase_		     =    do_rescale
       lowerCamelCase_		     =    rescale_factor
       lowerCamelCase_		     =    do_normalize
       lowerCamelCase_		     =    image_mean if image_mean is not None else OPENAI_CLIP_MEAN
       lowerCamelCase_		     =    image_std if image_std is not None else OPENAI_CLIP_STD
       lowerCamelCase_		     =    do_convert_rgb

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

       """simple docstring"""






       lowerCamelCase_		     =    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()}"""  )
       lowerCamelCase_		     =    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 				a__ (				self					:  Tuple						,							A_					:  np.ndarray						,							A_					:  Dict[str, int]						,							A_					:  Optional[Union[str, ChannelDimension]] = None						,							**A_					:  Dict						,							)   ->	np.ndarray:

       """simple docstring"""






       lowerCamelCase_		     =    get_size_dict(A_  )
       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(A_						,							size=(size['height'], size['width'])						,							data_format=A_						,							**A_  )

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

       """simple docstring"""






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

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

       """simple docstring"""






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





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

       """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(A_						,							param_name='size'						,							default_to_square=A_  )
       lowerCamelCase_		     =    resample if resample is not None else self.resample
       lowerCamelCase_		     =    do_center_crop if do_center_crop is not None else self.do_center_crop
       lowerCamelCase_		     =    crop_size if crop_size is not None else self.crop_size
       lowerCamelCase_		     =    get_size_dict(A_						,							param_name='crop_size'						,							default_to_square=A_  )
       lowerCamelCase_		     =    do_rescale if do_rescale is not None else self.do_rescale
       lowerCamelCase_		     =    rescale_factor if rescale_factor is not None else self.rescale_factor
       lowerCamelCase_		     =    do_normalize if do_normalize is not None else self.do_normalize
       lowerCamelCase_		     =    image_mean if image_mean is not None else self.image_mean
       lowerCamelCase_		     =    image_std if image_std is not None else self.image_std
       lowerCamelCase_		     =    do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

       lowerCamelCase_		     =    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.'  )

       # PIL RGBA images are converted to RGB
       if do_convert_rgb:
             lowerCamelCase_		     =    [convert_to_rgb(A_  ) for image in images]

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

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

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

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

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

       lowerCamelCase_		     =    [to_channel_dimension_format(A_						,							A_  ) for image in images]

       lowerCamelCase_		     =    {'pixel_values': images}
       return BatchFeature(data=A_						,							tensor_type=A_  )



