


"""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					(a_						:Dict				,				a_						:Any=1.0				,				a_						:Any=None				,				a_						:Optional[int]=None)     ->							Dict:
       if rng is None:
              lowercase		:List[Any]       							=				global_rng

       lowercase		:Dict       							=				[]
       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 					__magic_name__		(			unittest.TestCase     ):






       def __init__(   self  :    List[str]  ,     snake_case__  :    Dict  ,     snake_case__  :    Union[str, Any]=7  ,     snake_case__  :    int=4_0_0  ,     snake_case__  :    Optional[int]=2_0_0_0  ,     snake_case__  :    Any=2_4  ,     snake_case__  :    str=2_4  ,     snake_case__  :    Union[str, Any]=0.0  ,     snake_case__  :    Dict=1_6_0_0_0  ,     snake_case__  :    Any=True  ,     snake_case__  :    Any=True  ,     ):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				parent
              lowercase		:Tuple       							=				batch_size
              lowercase		:int       							=				min_seq_length
              lowercase		:Union[str, Any]       							=				max_seq_length
              lowercase		:int       							=				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
              lowercase		:int       							=				feature_size
              lowercase		:Tuple       							=				num_mel_bins
              lowercase		:int       							=				padding_value
              lowercase		:Any       							=				sampling_rate
              lowercase		:List[str]       							=				return_attention_mask
              lowercase		:Tuple       							=				do_normalize






       def  __snake_case						(   self  :    str				):

              '''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  __snake_case						(   self  :    Union[str, Any]  ,     snake_case__  :    Dict=False  ,     snake_case__  :    List[str]=False				):

              '''simple docstring'''






              def _flatten(snake_case__  :    Union[str, Any]				):
                     return list(itertools.chain(*snake_case__				)				)

              if equal_length:
                     lowercase		:Any       							=				[floats_list((self.max_seq_length, self.feature_size)				) for _ in range(self.batch_size				)]
              else:
                     # make sure that inputs increase in size
                     lowercase		:Optional[int]       							=				[
                         floats_list((x, self.feature_size)				)
                         for x in range(self.min_seq_length  ,     self.max_seq_length  ,     self.seq_length_diff				)
                     ]
              if numpify:
                     lowercase		:Union[str, Any]       							=				[np.asarray(snake_case__				) for x in speech_inputs]
              return speech_inputs







@require_torch
@require_torchaudio
class 					__magic_name__		(			__UpperCAmelCase			,						unittest.TestCase     ):
       __A			:      Union[str, Any]        =   SpeechaTextFeatureExtractor if is_speech_available() else None






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:str       							=				SpeechaTextFeatureExtractionTester(self				)






       def  __snake_case						(   self  :    Union[str, Any]  ,     snake_case__  :    Optional[Any]				):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:List[str]       							=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
              # create three inputs of length 800, 1000, and 1200
              lowercase		:Tuple       							=				[floats_list((1, x)				)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0				)]
              lowercase		:Union[str, Any]       							=				[np.asarray(snake_case__				) for speech_input in speech_inputs]

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

              # Test batched
              lowercase		:int       							=				feature_extractor(snake_case__  ,     return_tensors='''np'''				).input_features
              lowercase		:List[Any]       							=				feature_extractor(snake_case__  ,     return_tensors='''np'''				).input_features
              for enc_seq_a, enc_seq_a in zip(snake_case__  ,     snake_case__				):
                     self.assertTrue(np.allclose(snake_case__  ,     snake_case__  ,     atol=1e-3				)				)

              # Test 2-D numpy arrays are batched.
              lowercase		:Optional[Any]       							=				[floats_list((1, x)				)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
              lowercase		:List[Any]       							=				np.asarray(snake_case__				)
              lowercase		:Union[str, Any]       							=				feature_extractor(snake_case__  ,     return_tensors='''np'''				).input_features
              lowercase		:Tuple       							=				feature_extractor(snake_case__  ,     return_tensors='''np'''				).input_features
              for enc_seq_a, enc_seq_a in zip(snake_case__  ,     snake_case__				):
                     self.assertTrue(np.allclose(snake_case__  ,     snake_case__  ,     atol=1e-3				)				)






       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:str       							=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
              lowercase		:Optional[Any]       							=				[floats_list((1, x)				)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0				)]

              lowercase		:List[Any]       							=				['''longest''', '''max_length''', '''do_not_pad''']
              lowercase		:Optional[int]       							=				[None, 1_6, None]
              for max_length, padding in zip(snake_case__  ,     snake_case__				):
                     lowercase		:int       							=				feature_extractor(
                         snake_case__  ,     padding=snake_case__  ,     max_length=snake_case__  ,     return_attention_mask=snake_case__				)
                     lowercase		:Dict       							=				inputs.input_features
                     lowercase		:Optional[Any]       							=				inputs.attention_mask
                     lowercase		:Dict       							=				[np.sum(snake_case__				) 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  :    Optional[Any]				):

              '''simple docstring'''






              lowercase		:Any       							=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
              lowercase		:Union[str, Any]       							=				[floats_list((1, x)				)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0				)]

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

              '''simple docstring'''






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

              '''simple docstring'''






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

              lowercase		:Union[str, Any]       							=				[floats_list((1, x)				)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0				)]
              lowercase		:Tuple       							=				feature_extractor(
                  snake_case__  ,     padding='''longest'''  ,     max_length=1_6  ,     truncation=snake_case__  ,     return_tensors='''np'''  ,     return_attention_mask=snake_case__  ,     )
              lowercase		:Dict       							=				inputs.input_features
              lowercase		:Tuple       							=				inputs.attention_mask
              lowercase		: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, : 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  :    List[Any]				):

              '''simple docstring'''






              import torch

              lowercase		:Tuple       							=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
              lowercase		:Any       							=				np.random.rand(1_0_0  ,     3_2				).astype(np.floataa				)
              lowercase		:Dict       							=				np_speech_inputs.tolist()

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






       def  __snake_case						(   self  :    int  ,     snake_case__  :    List[str]				):

              '''simple docstring'''






              from datasets import load_dataset

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

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







       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				np.array([
                  -1.57_45, -1.77_13, -1.70_20, -1.60_69, -1.22_50, -1.11_05, -0.90_72, -0.82_41,
                  -1.23_10, -0.80_98, -0.33_20, -0.41_01, -0.79_85, -0.49_96, -0.82_13, -0.91_28,
                  -1.04_20, -1.12_86, -1.04_40, -0.79_99, -0.84_05, -1.22_75, -1.54_43, -1.46_25,
              ]				)
              # fmt: on

              lowercase		:Tuple       							=				self._load_datasamples(1				)
              lowercase		:Optional[int]       							=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
              lowercase		:Optional[int]       							=				feature_extractor(snake_case__  ,     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]  ,     snake_case__  ,     atol=1e-4				)				)





"""simple docstring"""



import math






def       lowerCamelCase					(a_						:int)     ->							bool:
       assert isinstance(a_				,				a_) and (
           number >= 0
       ), "'number' must been an int and positive"

       if 1 < number < 4:
              # 2 and 3 are primes
              return True
       elif number < 2 or not number % 2:
              # Negatives, 0, 1 and all even numbers are not primes
              return False

       lowercase		:Any       							=				range(3				,				int(math.sqrt(a_) + 1)				,				2)
       return not any(not number % i for i in odd_numbers)






def       lowerCamelCase					(a_						:Optional[int]				,				a_						:Optional[int]=1				,				**a_						:List[str])     ->							Any:
       lowercase		:str       							=				factor * value
       lowercase		:int       							=				value

       while not is_prime(a_):
              value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1

       if value == first_value_val:
              return next_prime(value + 1				,				**a_)
       return value

