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

__lowerCAmelCase	:       int						       =	random.Random()





def       lowerCAmelCase					(		UpperCamelCase__				:   List[str]    ,					UpperCamelCase__				:   List[str]=1.0    ,					UpperCamelCase__				:   Optional[Any]=None    ,					UpperCamelCase__				:   Optional[Any]=None       ):


	"""simple docstring"""

	if rng is None:
		__UpperCAmelCase							=			global_rng

	__UpperCAmelCase							=			[]
	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   ):





							def __init__(      self				:							Union[str, Any]     ,		__a				:							Optional[int]     ,		__a				:							str=7     ,		__a				:							Any=4_0_0     ,		__a				:							Tuple=2_0_0_0     ,		__a				:							str=2_4     ,		__a				:							Tuple=2_4     ,		__a				:							Any=0.0     ,		__a				:							Optional[Any]=1_6_0_0_0     ,		__a				:							Tuple=True     ,		__a				:							Union[str, Any]=True     ,		)							->	int:
								__UpperCAmelCase							=			parent
								__UpperCAmelCase							=			batch_size
								__UpperCAmelCase							=			min_seq_length
								__UpperCAmelCase							=			max_seq_length
								__UpperCAmelCase							=			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								__UpperCAmelCase							=			feature_size
								__UpperCAmelCase							=			num_mel_bins
								__UpperCAmelCase							=			padding_value
								__UpperCAmelCase							=			sampling_rate
								__UpperCAmelCase							=			return_attention_mask
								__UpperCAmelCase							=			do_normalize





							def       snake_case__						(      self				:							Tuple	)							->	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       snake_case__						(      self				:							str     ,		__a				:							str=False     ,		__a				:							Optional[Any]=False	)							->	Optional[int]:
								def _flatten(__a				:							List[Any]	):
									return list(itertools.chain(*A_	)	)

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






@require_torch
@require_torchaudio
class 						A   (   _SCREAMING_SNAKE_CASE       ,	unittest.TestCase   ):
							a_        =      SpeechaTextFeatureExtractor if is_speech_available() else None





							def       snake_case__						(      self				:							Union[str, Any]	)							->	int:
								__UpperCAmelCase							=			SpeechaTextFeatureExtractionTester(self	)





							def       snake_case__						(      self				:							int     ,		__a				:							List[Any]	)							->	Optional[Any]:
								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       snake_case__						(      self				:							Optional[int]	)							->	Tuple:
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								# create three inputs of length 800, 1000, and 1200
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]
								__UpperCAmelCase							=			[np.asarray(A_	) for speech_input in speech_inputs]

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

								# Test batched
								__UpperCAmelCase							=			feature_extractor(A_     ,		return_tensors='''np'''	).input_features
								__UpperCAmelCase							=			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.
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
								__UpperCAmelCase							=			np.asarray(A_	)
								__UpperCAmelCase							=			feature_extractor(A_     ,		return_tensors='''np'''	).input_features
								__UpperCAmelCase							=			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       snake_case__						(      self				:							Tuple	)							->	List[Any]:
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]

								__UpperCAmelCase							=			['''longest''', '''max_length''', '''do_not_pad''']
								__UpperCAmelCase							=			[None, 1_6, None]
								for max_length, padding in zip(A_     ,		A_	):
									__UpperCAmelCase							=			feature_extractor(
									    A_     ,		padding=A_     ,		max_length=A_     ,		return_attention_mask=A_	)
									__UpperCAmelCase							=			inputs.input_features
									__UpperCAmelCase							=			inputs.attention_mask
									__UpperCAmelCase							=			[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       snake_case__						(      self				:							Tuple	)							->	Dict:
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]

								__UpperCAmelCase							=			['''longest''', '''max_length''', '''do_not_pad''']
								__UpperCAmelCase							=			[None, 1_6, None]
								for max_length, padding in zip(A_     ,		A_	):
									__UpperCAmelCase							=			feature_extractor(
									    A_     ,		max_length=A_     ,		padding=A_     ,		return_tensors='''np'''     ,		return_attention_mask=A_	)
									__UpperCAmelCase							=			inputs.input_features
									__UpperCAmelCase							=			inputs.attention_mask
									__UpperCAmelCase							=			[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       snake_case__						(      self				:							Union[str, Any]	)							->	Dict:
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]
								__UpperCAmelCase							=			feature_extractor(
								    A_     ,		padding='''max_length'''     ,		max_length=4     ,		truncation=A_     ,		return_tensors='''np'''     ,		return_attention_mask=A_     ,		)
								__UpperCAmelCase							=			inputs.input_features
								__UpperCAmelCase							=			inputs.attention_mask
								__UpperCAmelCase							=			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       snake_case__						(      self				:							Optional[int]	)							->	Optional[int]:
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]
								__UpperCAmelCase							=			feature_extractor(
								    A_     ,		padding='''longest'''     ,		max_length=4     ,		truncation=A_     ,		return_tensors='''np'''     ,		return_attention_mask=A_     ,		)
								__UpperCAmelCase							=			inputs.input_features
								__UpperCAmelCase							=			inputs.attention_mask
								__UpperCAmelCase							=			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, 2_4)	)

								__UpperCAmelCase							=			[floats_list((1, x)	)[0] for x in range(8_0_0     ,		1_4_0_0     ,		2_0_0	)]
								__UpperCAmelCase							=			feature_extractor(
								    A_     ,		padding='''longest'''     ,		max_length=1_6     ,		truncation=A_     ,		return_tensors='''np'''     ,		return_attention_mask=A_     ,		)
								__UpperCAmelCase							=			inputs.input_features
								__UpperCAmelCase							=			inputs.attention_mask
								__UpperCAmelCase							=			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, 2_4)	)





							def       snake_case__						(      self				:							Dict	)							->	Dict:
								import torch

								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			np.random.rand(1_0_0     ,		3_2	).astype(np.floataa	)
								__UpperCAmelCase							=			np_speech_inputs.tolist()

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





							def       snake_case__						(      self				:							int     ,		__a				:							Tuple	)							->	Optional[Any]:
								from datasets import load_dataset

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

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






							def       snake_case__						(      self				:							Optional[int]	)							->	int:
								__UpperCAmelCase							=			np.array([
								    -1.5_7_4_5, -1.7_7_1_3, -1.7_0_2_0, -1.6_0_6_9, -1.2_2_5_0, -1.1_1_0_5, -0.9_0_7_2, -0.8_2_4_1,
								    -1.2_3_1_0, -0.8_0_9_8, -0.3_3_2_0, -0.4_1_0_1, -0.7_9_8_5, -0.4_9_9_6, -0.8_2_1_3, -0.9_1_2_8,
								    -1.0_4_2_0, -1.1_2_8_6, -1.0_4_4_0, -0.7_9_9_9, -0.8_4_0_5, -1.2_2_7_5, -1.5_4_4_3, -1.4_6_2_5,
								]	)
								# fmt: on

								__UpperCAmelCase							=			self._load_datasamples(1	)
								__UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
								__UpperCAmelCase							=			feature_extractor(A_     ,		return_tensors='''pt'''	).input_features
								self.assertEquals(input_features.shape     ,		(1, 5_8_4, 2_4)	)
								self.assertTrue(np.allclose(input_features[0, 0, :3_0]     ,		A_     ,		atol=1e-4	)	)

'''simple docstring'''


def       lowerCAmelCase					(		UpperCamelCase__				:   int       ):


	"""simple docstring"""

	if not isinstance(UpperCamelCase__    ,					UpperCamelCase__       ):
		__UpperCAmelCase							=			f"""Input value of [number={number}] must be an integer"""
		raise TypeError(UpperCamelCase__       )

	if number < 1:
		__UpperCAmelCase							=			f"""Input value of [number={number}] must be > 0"""
		raise ValueError(UpperCamelCase__       )

	__UpperCAmelCase							=			1

	for i in range(1    ,					UpperCamelCase__       ):
		current_number *= 4 * i - 2
		current_number //= i + 1

	return current_number


if __name__ == "__main__":
					import doctest

					doctest.testmod()
