






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

A_		:		Tuple		     =			random.Random()

def   snake_case (UpperCAmelCase__     ,   UpperCAmelCase__=1.0     ,   UpperCAmelCase__=None     ,   UpperCAmelCase__=None  )	->    Optional[int]:







  if rng is None:
    UpperCamelCase_: List[Any]   				=    global_rng

  UpperCamelCase_: Any   				=    []
  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     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







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





       def       _a   ( self							):
         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       _a   ( self  ,      _lowerCamelCase=False  ,      _lowerCamelCase=False							):
         def _flatten(_lowerCamelCase							):
           return list(itertools.chain(*__A							)							)

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







@require_torch
@require_torchaudio
class     _lowerCAmelCase(	UpperCAmelCase_							,     unittest.TestCase ):





       """simple docstring"""







       a    :		Union[str, Any]									=SpeechaTextFeatureExtractor if is_speech_available() else None





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    SpeechaTextFeatureExtractionTester(self							)





       def       _a   ( self  ,      _lowerCamelCase							):
         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       _a   ( self							):
         # Tests that all call wrap to encode_plus and batch_encode_plus
         UpperCamelCase_: Optional[Any]   				=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
         # create three inputs of length 800, 1000, and 1200
         UpperCamelCase_: Any   				=    [floats_list((1, x)							)[0] for x in range(8_0_0  ,      1_4_0_0  ,      2_0_0							)]
         UpperCamelCase_: Dict   				=    [np.asarray(__A							) for speech_input in speech_inputs]

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

         # Test batched
         UpperCamelCase_: str   				=    feature_extractor(__A  ,      return_tensors='np'							).input_features
         UpperCamelCase_: Any   				=    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_: List[str]   				=    [floats_list((1, x)							)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
         UpperCamelCase_: Optional[Any]   				=    np.asarray(__A							)
         UpperCamelCase_: str   				=    feature_extractor(__A  ,      return_tensors='np'							).input_features
         UpperCamelCase_: Optional[int]   				=    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       _a   ( self							):
         UpperCamelCase_: Dict   				=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
         UpperCamelCase_: Any   				=    [floats_list((1, x)							)[0] for x in range(8_0_0  ,      1_4_0_0  ,      2_0_0							)]

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

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





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

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





       def       _a   ( self							):
         import torch

         UpperCamelCase_: str   				=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
         UpperCamelCase_: int   				=    np.random.rand(1_0_0  ,      3_2							).astype(np.floataa							)
         UpperCamelCase_: List[Any]   				=    np_speech_inputs.tolist()

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





       def       _a   ( self  ,      _lowerCamelCase							):
         from datasets import load_dataset

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

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





       def       _a   ( self							):
         # fmt: off
         UpperCamelCase_: List[str]   				=    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_: List[str]   				=    self._load_datasamples(1							)
         UpperCamelCase_: List[str]   				=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
         UpperCamelCase_: List[str]   				=    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							)							)











def   snake_case (UpperCAmelCase__     ,   UpperCAmelCase__  )	->    int:
  while b:
    UpperCamelCase_				,UpperCamelCase_: int   				=    b, a % b
  return a

def   snake_case (UpperCAmelCase__     ,   UpperCAmelCase__  )	->    int:
  return a if b == 0 else euclidean_gcd_recursive(UpperCAmelCase__     ,   a % b  )

def   snake_case ()	->    int:
  print(F'''euclidean_gcd(3, 5) = {euclidean_gcd(3     ,   5  )}'''  )
  print(F'''euclidean_gcd(5, 3) = {euclidean_gcd(5     ,   3  )}'''  )
  print(F'''euclidean_gcd(1, 3) = {euclidean_gcd(1     ,   3  )}'''  )
  print(F'''euclidean_gcd(3, 6) = {euclidean_gcd(3     ,   6  )}'''  )
  print(F'''euclidean_gcd(6, 3) = {euclidean_gcd(6     ,   3  )}'''  )

  print(F'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3     ,   5  )}'''  )
  print(F'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5     ,   3  )}'''  )
  print(F'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1     ,   3  )}'''  )
  print(F'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3     ,   6  )}'''  )
  print(F'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6     ,   3  )}'''  )


if __name__ == "__main__":
   main()