


import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


_A     =  random.Random()






def      lowerCamelCase__       (       __lowerCAmelCase						:						Any					,							__lowerCAmelCase						:						Tuple=1.0					,							__lowerCAmelCase						:						Optional[Any]=None					,							__lowerCAmelCase						:						Tuple=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





class 			_lowerCAmelCase      (			unittest.TestCase ):




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase=7       ,       _UpperCamelCase=400       ,       _UpperCamelCase=2_000       ,       _UpperCamelCase=1       ,       _UpperCamelCase=0.0       ,       _UpperCamelCase=16_000       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       )       ->   Dict:
							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_      			=			padding_value
							lowerCAmelCase_      			=			sampling_rate
							lowerCAmelCase_      			=			return_attention_mask
							lowerCAmelCase_      			=			do_normalize




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







							def _flatten(_UpperCamelCase  ):
										return list(itertools.chain(*lowerCamelCase__  )  )

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

							return speech_inputs





class 			_lowerCAmelCase      (			UpperCAmelCase__      ,   unittest.TestCase ):
				_lowercase    	=WavaVecaFeatureExtractor




				def   __a		(      self  )       ->   Dict:
							lowerCAmelCase_      			=			WavaVecaFeatureExtractionTester(self  )




				def   __a		(      self       ,       _UpperCamelCase  )       ->   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   __a		(      self  )       ->   int:
							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       ,       1_400       ,       200  )]
							lowerCAmelCase_      			=			[np.asarray(lowerCamelCase__  ) for speech_input in speech_inputs]

							# Test not batched input
							lowerCAmelCase_      			=			feat_extract(speech_inputs[0]       ,       return_tensors="np"  ).input_values
							lowerCAmelCase_      			=			feat_extract(np_speech_inputs[0]       ,       return_tensors="np"  ).input_values
							self.assertTrue(np.allclose(lowerCamelCase__       ,       lowerCamelCase__       ,       atol=1e-3  )  )

							# Test batched
							lowerCAmelCase_      			=			feat_extract(lowerCamelCase__       ,       return_tensors="np"  ).input_values
							lowerCAmelCase_      			=			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  )  )

							# Test 2-D numpy arrays are batched.
							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in (800, 800, 800)]
							lowerCAmelCase_      			=			np.asarray(lowerCamelCase__  )
							lowerCAmelCase_      			=			feat_extract(lowerCamelCase__       ,       return_tensors="np"  ).input_values
							lowerCAmelCase_      			=			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  )  )




				def   __a		(      self  )       ->   Union[str, Any]:
							lowerCAmelCase_      			=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in range(800       ,       1_400       ,       200  )]

							lowerCAmelCase_      			=			["longest", "max_length", "do_not_pad"]
							lowerCAmelCase_      			=			[None, 1_600, None]
							for max_length, padding in zip(lowerCamelCase__       ,       lowerCamelCase__  ):
										lowerCAmelCase_      			=			feat_extract(lowerCamelCase__       ,       padding=lowerCamelCase__       ,       max_length=lowerCamelCase__       ,       return_tensors="np"  )
										lowerCAmelCase_      			=			processed.input_values

										self._check_zero_mean_unit_variance(input_values[0][:800]  )
										self.assertTrue(input_values[0][800:].sum() < 1e-6  )
										self._check_zero_mean_unit_variance(input_values[1][:1_000]  )
										self.assertTrue(input_values[0][1_000:].sum() < 1e-6  )
										self._check_zero_mean_unit_variance(input_values[2][:1_200]  )




				def   __a		(      self  )       ->   Optional[int]:
							lowerCAmelCase_      			=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
							lowerCAmelCase_      			=			range(800       ,       1_400       ,       200  )
							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in lengths]

							lowerCAmelCase_      			=			["longest", "max_length", "do_not_pad"]
							lowerCAmelCase_      			=			[None, 1_600, None]

							for max_length, padding in zip(lowerCamelCase__       ,       lowerCamelCase__  ):
										lowerCAmelCase_      			=			feat_extract(lowerCamelCase__       ,       max_length=lowerCamelCase__       ,       padding=lowerCamelCase__  )
										lowerCAmelCase_      			=			processed.input_values

										self._check_zero_mean_unit_variance(input_values[0][:800]  )
										self._check_zero_mean_unit_variance(input_values[1][:1_000]  )
										self._check_zero_mean_unit_variance(input_values[2][:1_200]  )




				def   __a		(      self  )       ->   Any:
							lowerCAmelCase_      			=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in range(800       ,       1_400       ,       200  )]
							lowerCAmelCase_      			=			feat_extract(
							    lowerCamelCase__       ,       truncation=lowerCamelCase__       ,       max_length=1_000       ,       padding="max_length"       ,       return_tensors="np"  )
							lowerCAmelCase_      			=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :800]  )
							self._check_zero_mean_unit_variance(input_values[1]  )
							self._check_zero_mean_unit_variance(input_values[2]  )




				def   __a		(      self  )       ->   int:
							lowerCAmelCase_      			=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in range(800       ,       1_400       ,       200  )]
							lowerCAmelCase_      			=			feat_extract(
							    lowerCamelCase__       ,       truncation=lowerCamelCase__       ,       max_length=1_000       ,       padding="longest"       ,       return_tensors="np"  )
							lowerCAmelCase_      			=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :800]  )
							self._check_zero_mean_unit_variance(input_values[1, :1_000]  )
							self._check_zero_mean_unit_variance(input_values[2]  )

							# make sure that if max_length < longest -> then pad to max_length
							self.assertTrue(input_values.shape == (3, 1_000)  )

							lowerCAmelCase_      			=			[floats_list((1, x)  )[0] for x in range(800       ,       1_400       ,       200  )]
							lowerCAmelCase_      			=			feat_extract(
							    lowerCamelCase__       ,       truncation=lowerCamelCase__       ,       max_length=2_000       ,       padding="longest"       ,       return_tensors="np"  )
							lowerCAmelCase_      			=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :800]  )
							self._check_zero_mean_unit_variance(input_values[1, :1_000]  )
							self._check_zero_mean_unit_variance(input_values[2]  )

							# make sure that if max_length > longest -> then pad to longest
							self.assertTrue(input_values.shape == (3, 1_200)  )




				@require_torch
				def   __a		(      self  )       ->   Optional[Any]:
							import torch

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

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




				@slow
				@require_torch
				def   __a		(      self  )       ->   Dict:







							for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
										lowerCAmelCase_      			=			WavaVecaConfig.from_pretrained(lowerCamelCase__  )
										lowerCAmelCase_      			=			WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase__  )

										# only "layer" feature extraction norm should make use of
										# attention_mask
										self.assertEqual(feat_extract.return_attention_mask       ,       config.feat_extract_norm == "layer"  )





from string import ascii_lowercase, ascii_uppercase






def      lowerCamelCase__       (       __lowerCAmelCase						:						str       ):




			"""simple docstring"""


			if not sentence:
						return ""
			lowerCAmelCase_      			=			dict(zip(__lowerCAmelCase					,							__lowerCAmelCase       )       )
			return lower_to_upper.get(sentence[0]					,							sentence[0]       ) + sentence[1:]


if __name__ == "__main__":
			from doctest import testmod

			testmod()



