


'''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    _snake_case  (	A					,       A=1.0					,       A=None					,       A=None			)		->       int:
							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   ,       lowerCamelCase_   ,       lowerCamelCase_=7   ,       lowerCamelCase_=4_00   ,       lowerCamelCase_=20_00   ,       lowerCamelCase_=24   ,       lowerCamelCase_=24   ,       lowerCamelCase_=0.0   ,       lowerCamelCase_=1_60_00   ,       lowerCamelCase_=True   ,       lowerCamelCase_=True   ,       )			->				Any:
														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     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														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     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_=False   ,       lowerCamelCase_=False  )			->				List[str]:
														def _flatten(lowerCamelCase_  ):
																					return list(itertools.chain(*lowerCamelCase_  )  )

														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(lowerCamelCase_  ) for x in speech_inputs]
														return speech_inputs




@require_torch
@require_torchaudio
class 						a__							(				a__							,							unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  str      		= SpeechaTextFeatureExtractor if is_speech_available() else None







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      SpeechaTextFeatureExtractionTester(self  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				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     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														# Tests that all call wrap to encode_plus and batch_encode_plus
														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(8_00   ,       14_00   ,       2_00  )]
														lowerCAmelCase__			  =      [np.asarray(lowerCamelCase_  ) for speech_input in speech_inputs]

														# Test feature size
														lowerCAmelCase__			  =      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__			  =      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(lowerCamelCase_   ,       lowerCamelCase_   ,       atol=1e-3  )  )

														# Test batched
														lowerCAmelCase__			  =      feature_extractor(lowerCamelCase_   ,       return_tensors='''np'''  ).input_features
														lowerCAmelCase__			  =      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__			  =      [floats_list((1, x)  )[0] for x in (8_00, 8_00, 8_00)]
														lowerCAmelCase__			  =      np.asarray(lowerCamelCase_  )
														lowerCAmelCase__			  =      feature_extractor(lowerCamelCase_   ,       return_tensors='''np'''  ).input_features
														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
														lowerCAmelCase__			  =      [floats_list((1, x)  )[0] for x in range(8_00   ,       14_00   ,       2_00  )]

														lowerCAmelCase__			  =      ['''longest''', '''max_length''', '''do_not_pad''']
														lowerCAmelCase__			  =      [None, 16, None]
														for max_length, padding in zip(lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      feature_extractor(
																					    lowerCamelCase_   ,       padding=lowerCamelCase_   ,       max_length=lowerCamelCase_   ,       return_attention_mask=lowerCamelCase_  )
																					lowerCAmelCase__			  =      inputs.input_features
																					lowerCAmelCase__			  =      inputs.attention_mask
																					lowerCAmelCase__			  =      [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     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
														lowerCAmelCase__			  =      [floats_list((1, x)  )[0] for x in range(8_00   ,       14_00   ,       2_00  )]

														lowerCAmelCase__			  =      ['''longest''', '''max_length''', '''do_not_pad''']
														lowerCAmelCase__			  =      [None, 16, None]
														for max_length, padding in zip(lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      feature_extractor(
																					    lowerCamelCase_   ,       max_length=lowerCamelCase_   ,       padding=lowerCamelCase_   ,       return_tensors='''np'''   ,       return_attention_mask=lowerCamelCase_  )
																					lowerCAmelCase__			  =      inputs.input_features
																					lowerCAmelCase__			  =      inputs.attention_mask
																					lowerCAmelCase__			  =      [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     __SCREAMING_SNAKE_CASE				(						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(8_00   ,       14_00   ,       2_00  )]
														lowerCAmelCase__			  =      feature_extractor(
														    lowerCamelCase_   ,       padding='''max_length'''   ,       max_length=4   ,       truncation=lowerCamelCase_   ,       return_tensors='''np'''   ,       return_attention_mask=lowerCamelCase_   ,       )
														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     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
														lowerCAmelCase__			  =      [floats_list((1, x)  )[0] for x in range(8_00   ,       14_00   ,       2_00  )]
														lowerCAmelCase__			  =      feature_extractor(
														    lowerCamelCase_   ,       padding='''longest'''   ,       max_length=4   ,       truncation=lowerCamelCase_   ,       return_tensors='''np'''   ,       return_attention_mask=lowerCamelCase_   ,       )
														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(8_00   ,       14_00   ,       2_00  )]
														lowerCAmelCase__			  =      feature_extractor(
														    lowerCamelCase_   ,       padding='''longest'''   ,       max_length=16   ,       truncation=lowerCamelCase_   ,       return_tensors='''np'''   ,       return_attention_mask=lowerCamelCase_   ,       )
														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     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														import torch

														lowerCAmelCase__			  =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
														lowerCAmelCase__			  =      np.random.rand(1_00   ,       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     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Dict:
														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(lowerCamelCase_  )  )[:num_samples]['''audio''']

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														# fmt: off
														lowerCAmelCase__			  =      np.array([
														    -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
														    -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
														    -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
														]  )
														# fmt: on

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



'''simple docstring'''






from __future__ import annotations


def    _snake_case  (	A			)		->       bool:
							lowerCAmelCase__			  =      str(A			)
							return len(A			) == 9 and set(A			) == set('''123456789'''			)


def    _snake_case  (	)		->       int | None:
							for base_num in range(9999					,       4999					,       -1			):
														lowerCAmelCase__			  =      100002 * base_num
														if is_9_pandigital(A			):
																					return candidate

							for base_num in range(333					,       99					,       -1			):
														lowerCAmelCase__			  =      1002003 * base_num
														if is_9_pandigital(A			):
																					return candidate

							return None


if __name__ == "__main__":
		print(f"""{solution() = }""")