





"""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

_UpperCamelCase							=      random.Random()



def 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE=1.0    ,						SCREAMING_SNAKE_CASE=None    ,						SCREAMING_SNAKE_CASE=None   ):


							'''simple docstring'''







							if rng is None:
														__lowerCamelCase      :      List[str]         =global_rng

							__lowerCamelCase      :      List[str]         =[]
							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 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				def __init__(     self  :Dict						,    __lowercase  :List[Any]						,    __lowercase  :int=7						,    __lowercase  :Optional[Any]=400						,    __lowercase  :List[Any]=2000						,    __lowercase  :str=24						,    __lowercase  :Optional[int]=24						,    __lowercase  :str=0.0						,    __lowercase  :int=1_6000						,    __lowercase  :Optional[int]=True						,    __lowercase  :Dict=True						,    ):
											__lowerCamelCase      :      Optional[Any]         =parent
											__lowerCamelCase      :      int         =batch_size
											__lowerCamelCase      :      Any         =min_seq_length
											__lowerCamelCase      :      List[str]         =max_seq_length
											__lowerCamelCase      :      str         =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											__lowerCamelCase      :      Any         =feature_size
											__lowerCamelCase      :      Optional[int]         =num_mel_bins
											__lowerCamelCase      :      List[str]         =padding_value
											__lowerCamelCase      :      Union[str, Any]         =sampling_rate
											__lowerCamelCase      :      Union[str, Any]         =return_attention_mask
											__lowerCamelCase      :      str         =do_normalize




				def 					__lowercase  (     self  :Optional[Any]			):
											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 					__lowercase  (     self  :List[str]						,    __lowercase  :Optional[Any]=False						,    __lowercase  :List[str]=False			):




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

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

@require_torch
@require_torchaudio
class 		SCREAMING_SNAKE_CASE_							(    _UpperCAmelCase       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       List[Any]	 =		SpeechaTextFeatureExtractor if is_speech_available() else None




				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      List[Any]         =SpeechaTextFeatureExtractionTester(self			)




				def 					__lowercase  (     self  :int						,    __lowercase  :Tuple			):
											self.assertTrue(np.all(np.mean(lowerCamelCase_						,    axis=0			) < 1e-3			)			)
											self.assertTrue(np.all(np.abs(np.var(lowerCamelCase_						,    axis=0			) - 1			) < 1e-3			)			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Union[str, Any]         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											# create three inputs of length 800, 1000, and 1200
											__lowerCamelCase      :      List[Any]         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]
											__lowerCamelCase      :      Tuple         =[np.asarray(lowerCamelCase_			) for speech_input in speech_inputs]

											# Test feature size
											__lowerCamelCase      :      str         =feature_extractor(lowerCamelCase_						,    padding=lowerCamelCase_						,    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      :      Optional[Any]         =feature_extractor(speech_inputs[0]						,    return_tensors='''np'''			).input_features
											__lowerCamelCase      :      int         =feature_extractor(np_speech_inputs[0]						,    return_tensors='''np'''			).input_features
											self.assertTrue(np.allclose(lowerCamelCase_						,    lowerCamelCase_						,    atol=1e-3			)			)

											# Test batched
											__lowerCamelCase      :      Optional[int]         =feature_extractor(lowerCamelCase_						,    return_tensors='''np'''			).input_features
											__lowerCamelCase      :      Any         =feature_extractor(lowerCamelCase_						,    return_tensors='''np'''			).input_features
											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      :      Tuple         =[floats_list((1, x)			)[0] for x in (800, 800, 800)]
											__lowerCamelCase      :      str         =np.asarray(lowerCamelCase_			)
											__lowerCamelCase      :      Tuple         =feature_extractor(lowerCamelCase_						,    return_tensors='''np'''			).input_features
											__lowerCamelCase      :      Optional[Any]         =feature_extractor(lowerCamelCase_						,    return_tensors='''np'''			).input_features
											for enc_seq_a, enc_seq_a in zip(lowerCamelCase_						,    lowerCamelCase_			):
																		self.assertTrue(np.allclose(lowerCamelCase_						,    lowerCamelCase_						,    atol=1e-3			)			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Optional[int]         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      Optional[Any]         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]

											__lowerCamelCase      :      Dict         =['''longest''', '''max_length''', '''do_not_pad''']
											__lowerCamelCase      :      Optional[Any]         =[None, 16, None]
											for max_length, padding in zip(lowerCamelCase_						,    lowerCamelCase_			):
																		__lowerCamelCase      :      Optional[Any]         =feature_extractor(
																		    lowerCamelCase_						,    padding=lowerCamelCase_						,    max_length=lowerCamelCase_						,    return_attention_mask=lowerCamelCase_			)
																		__lowerCamelCase      :      List[Any]         =inputs.input_features
																		__lowerCamelCase      :      Optional[Any]         =inputs.attention_mask
																		__lowerCamelCase      :      Optional[Any]         =[np.sum(lowerCamelCase_			) 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 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      Any         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      Dict         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]

											__lowerCamelCase      :      str         =['''longest''', '''max_length''', '''do_not_pad''']
											__lowerCamelCase      :      Optional[int]         =[None, 16, None]
											for max_length, padding in zip(lowerCamelCase_						,    lowerCamelCase_			):
																		__lowerCamelCase      :      List[Any]         =feature_extractor(
																		    lowerCamelCase_						,    max_length=lowerCamelCase_						,    padding=lowerCamelCase_						,    return_tensors='''np'''						,    return_attention_mask=lowerCamelCase_			)
																		__lowerCamelCase      :      List[Any]         =inputs.input_features
																		__lowerCamelCase      :      str         =inputs.attention_mask
																		__lowerCamelCase      :      Dict         =[np.sum(lowerCamelCase_			) 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 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      Optional[Any]         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      List[Any]         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]
											__lowerCamelCase      :      int         =feature_extractor(
											    lowerCamelCase_						,    padding='''max_length'''						,    max_length=4						,    truncation=lowerCamelCase_						,    return_tensors='''np'''						,    return_attention_mask=lowerCamelCase_						,    )
											__lowerCamelCase      :      Dict         =inputs.input_features
											__lowerCamelCase      :      Tuple         =inputs.attention_mask
											__lowerCamelCase      :      Optional[Any]         =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 					__lowercase  (     self  :Optional[Any]			):
											__lowerCamelCase      :      int         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      List[Any]         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]
											__lowerCamelCase      :      Dict         =feature_extractor(
											    lowerCamelCase_						,    padding='''longest'''						,    max_length=4						,    truncation=lowerCamelCase_						,    return_tensors='''np'''						,    return_attention_mask=lowerCamelCase_						,    )
											__lowerCamelCase      :      int         =inputs.input_features
											__lowerCamelCase      :      int         =inputs.attention_mask
											__lowerCamelCase      :      Optional[int]         =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      :      Optional[int]         =[floats_list((1, x)			)[0] for x in range(800						,    1400						,    200			)]
											__lowerCamelCase      :      int         =feature_extractor(
											    lowerCamelCase_						,    padding='''longest'''						,    max_length=16						,    truncation=lowerCamelCase_						,    return_tensors='''np'''						,    return_attention_mask=lowerCamelCase_						,    )
											__lowerCamelCase      :      int         =inputs.input_features
											__lowerCamelCase      :      Union[str, Any]         =inputs.attention_mask
											__lowerCamelCase      :      Union[str, Any]         =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 					__lowercase  (     self  :Optional[Any]			):
											import torch

											__lowerCamelCase      :      str         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      Tuple         =np.random.rand(100						,    32			).astype(np.floataa			)
											__lowerCamelCase      :      str         =np_speech_inputs.tolist()

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




				def 					__lowercase  (     self  :Union[str, Any]						,    __lowercase  :List[Any]			):
											from datasets import load_dataset

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

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




				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      Union[str, Any]         =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      :      List[Any]         =self._load_datasamples(1			)
											__lowerCamelCase      :      Union[str, Any]         =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
											__lowerCamelCase      :      List[str]         =feature_extractor(lowerCamelCase_						,    return_tensors='''pt'''			).input_features
											self.assertEquals(input_features.shape						,    (1, 584, 24)			)
											self.assertTrue(np.allclose(input_features[0, 0, :30]						,    lowerCamelCase_						,    atol=1e-4			)			)











"""simple docstring"""





import warnings

from ...utils import logging
from .image_processing_flava import FlavaImageProcessor


_UpperCamelCase							=      logging.get_logger(__name__)
class 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
				"""simple docstring"""




				def __init__(     self  :List[str]						,    *__lowercase  :int						,    **__lowercase  :Any			):
											warnings.warn(
											    '''The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'''
											    ''' use FlavaImageProcessor instead.'''						,    __lowercase						,    )
											super().__init__(*__lowercase						,    **__lowercase			)




