'''simple docstring'''

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			:      Union[str, Any]					      =				random.Random()







def      UpperCamelCase_						(					snake_case_       :       Tuple						,		snake_case_       :       Optional[int]=1.0						,		snake_case_       :       Optional[int]=None						,		snake_case_       :       Optional[int]=None      )					->     Dict:






 '''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 				_lowercase     (				unittest.TestCase			):


 '''simple docstring'''






 def __init__(      self   :	str      ,       SCREAMING_SNAKE_CASE__   :	Any      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=7      ,       SCREAMING_SNAKE_CASE__   :	List[Any]=4_00      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=20_00      ,       SCREAMING_SNAKE_CASE__   :	List[Any]=1      ,       SCREAMING_SNAKE_CASE__   :	int=0.0      ,       SCREAMING_SNAKE_CASE__   :	Tuple=1_60_00      ,       SCREAMING_SNAKE_CASE__   :	List[str]=True      ,       SCREAMING_SNAKE_CASE__   :	List[Any]=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											=						padding_value
  __lowerCAmelCase											=						sampling_rate
  __lowerCAmelCase											=						return_attention_mask
  __lowerCAmelCase											=						do_normalize
 def a					(      self   :	Dict							)  ->    Union[str, 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   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Dict=False      ,       SCREAMING_SNAKE_CASE__   :	str=False							)  ->    int:
  def _flatten(SCREAMING_SNAKE_CASE__   :	List[Any]							):
   return list(itertools.chain(*SCREAMING_SNAKE_CASE__							)							)

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

  return speech_inputs







class 				_lowercase     (				UpperCAmelCase__  ,							unittest.TestCase			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       str      	=					WavaVecaFeatureExtractor
 def a					(      self   :	Union[str, Any]							)  ->    List[Any]:
  __lowerCAmelCase											=						WavaVecaFeatureExtractionTester(self							)
 def a					(      self   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]							)  ->    Optional[Any]:
  self.assertTrue(np.all(np.mean(SCREAMING_SNAKE_CASE__      ,       axis=0							) < 1e-3							)							)
  self.assertTrue(np.all(np.abs(np.var(SCREAMING_SNAKE_CASE__      ,       axis=0							) - 1							) < 1e-3							)							)
 def a					(      self   :	Union[str, Any]							)  ->    Any:
  # 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(SCREAMING_SNAKE_CASE__							) 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(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       atol=1e-3							)							)

  # Test batched
  __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       return_tensors="""np"""							).input_values
  __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       return_tensors="""np"""							).input_values
  for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
   self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       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(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       return_tensors="""np"""							).input_values
  __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       return_tensors="""np"""							).input_values
  for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
   self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       atol=1e-3							)							)
 def a					(      self   :	Union[str, Any]							)  ->    List[str]:
  __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_00, None]
  for max_length, padding in zip(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
   __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       padding=SCREAMING_SNAKE_CASE__      ,       max_length=SCREAMING_SNAKE_CASE__      ,       return_tensors="""np"""							)
   __lowerCAmelCase											=						processed.input_values

   self._check_zero_mean_unit_variance(input_values[0][:8_00]							)
   self.assertTrue(input_values[0][8_00:].sum() < 1e-6							)
   self._check_zero_mean_unit_variance(input_values[1][:10_00]							)
   self.assertTrue(input_values[0][10_00:].sum() < 1e-6							)
   self._check_zero_mean_unit_variance(input_values[2][:12_00]							)
 def a					(      self   :	Any							)  ->    int:
  __lowerCAmelCase											=						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
  __lowerCAmelCase											=						range(8_00      ,       14_00      ,       2_00							)
  __lowerCAmelCase											=						[floats_list((1, x)							)[0] for x in lengths]

  __lowerCAmelCase											=						["""longest""", """max_length""", """do_not_pad"""]
  __lowerCAmelCase											=						[None, 16_00, None]

  for max_length, padding in zip(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
   __lowerCAmelCase											=						feat_extract(SCREAMING_SNAKE_CASE__      ,       max_length=SCREAMING_SNAKE_CASE__      ,       padding=SCREAMING_SNAKE_CASE__							)
   __lowerCAmelCase											=						processed.input_values

   self._check_zero_mean_unit_variance(input_values[0][:8_00]							)
   self._check_zero_mean_unit_variance(input_values[1][:10_00]							)
   self._check_zero_mean_unit_variance(input_values[2][:12_00]							)
 def a					(      self   :	List[str]							)  ->    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											=						feat_extract(
      SCREAMING_SNAKE_CASE__      ,       truncation=SCREAMING_SNAKE_CASE__      ,       max_length=10_00      ,       padding="""max_length"""      ,       return_tensors="""np"""							)
  __lowerCAmelCase											=						processed.input_values

  self._check_zero_mean_unit_variance(input_values[0, :8_00]							)
  self._check_zero_mean_unit_variance(input_values[1]							)
  self._check_zero_mean_unit_variance(input_values[2]							)
 def a					(      self   :	Optional[int]							)  ->    List[Any]:
  __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											=						feat_extract(
      SCREAMING_SNAKE_CASE__      ,       truncation=SCREAMING_SNAKE_CASE__      ,       max_length=10_00      ,       padding="""longest"""      ,       return_tensors="""np"""							)
  __lowerCAmelCase											=						processed.input_values

  self._check_zero_mean_unit_variance(input_values[0, :8_00]							)
  self._check_zero_mean_unit_variance(input_values[1, :10_00]							)
  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, 10_00)							)

  __lowerCAmelCase											=						[floats_list((1, x)							)[0] for x in range(8_00      ,       14_00      ,       2_00							)]
  __lowerCAmelCase											=						feat_extract(
      SCREAMING_SNAKE_CASE__      ,       truncation=SCREAMING_SNAKE_CASE__      ,       max_length=20_00      ,       padding="""longest"""      ,       return_tensors="""np"""							)
  __lowerCAmelCase											=						processed.input_values

  self._check_zero_mean_unit_variance(input_values[0, :8_00]							)
  self._check_zero_mean_unit_variance(input_values[1, :10_00]							)
  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, 12_00)							)
 @require_torch
 def a					(      self   :	str							)  ->    List[Any]:
  import torch

  __lowerCAmelCase											=						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
  __lowerCAmelCase											=						np.random.rand(1_00							).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   :	List[Any]							)  ->    Optional[Any]:
  # this test makes sure that models that are using
  # group norm don't have their feature extractor return the
  # attention_mask
  for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
   __lowerCAmelCase											=						WavaVecaConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
   __lowerCAmelCase											=						WavaVecaFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE__							)

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







'''simple docstring'''

def      UpperCamelCase_						(					snake_case_       :       int						,		snake_case_       :       int      )					->     str:






 '''simple docstring'''

 if a < 0 or b < 0:
  raise ValueError("""the value of both inputs must be positive"""      )

 __lowerCAmelCase											=						str(bin(snake_case_      )      )[2:]  # remove the leading "0b"
 __lowerCAmelCase											=						str(bin(snake_case_      )      )[2:]  # remove the leading "0b"

 __lowerCAmelCase											=						max(len(snake_case_      )						,		len(snake_case_      )      )

 return "0b" + "".join(
     str(int(char_a == """1""" and char_b == """1"""      )      )
     for char_a, char_b in zip(a_binary.zfill(snake_case_      )						,		b_binary.zfill(snake_case_      )      )      )


if __name__ == "__main__":
 import doctest

 doctest.testmod()






