




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

__magic_name__  :  Dict					=       random.Random()
def 		a_							( __lowerCAmelCase   , __lowerCAmelCase=1.0   , __lowerCAmelCase=None   , __lowerCAmelCase=None				):
  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     SCREAMING_SNAKE_CASE__		(unittest.TestCase			):
     def __init__(						self	:		List[Any]       ,						__lowerCamelCase	:		List[str]       ,						__lowerCamelCase	:		List[str]=7       ,						__lowerCamelCase	:		Optional[int]=4_00       ,						__lowerCamelCase	:		Any=20_00       ,						__lowerCamelCase	:		int=24       ,						__lowerCamelCase	:		int=24       ,						__lowerCamelCase	:		int=0.0       ,						__lowerCamelCase	:		Union[str, Any]=1_60_00       ,						__lowerCamelCase	:		Tuple=True       ,						__lowerCamelCase	:		List[str]=True       ,						):



       """simple docstring"""
       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        A__      (						self	:		List[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        A__      (						self	:		str       ,						__lowerCamelCase	:		List[str]=False       ,						__lowerCamelCase	:		str=False						):



       """simple docstring"""
       def _flatten(__lowerCamelCase	:		Tuple						):
         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     SCREAMING_SNAKE_CASE__		(_a    ,					unittest.TestCase			):
     lowercase_ :      Optional[int]				= SpeechaTextFeatureExtractor if is_speech_available() else None
     def        A__      (						self	:		int						):



       """simple docstring"""
       lowerCAmelCase__										=      SpeechaTextFeatureExtractionTester(self						)
     def        A__      (						self	:		List[Any]       ,						__lowerCamelCase	:		Dict						):



       """simple docstring"""
       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        A__      (						self	:		Dict						):



       """simple docstring"""
       # 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        A__      (						self	:		List[Any]						):



       """simple docstring"""
       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        A__      (						self	:		Dict						):



       """simple docstring"""
       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        A__      (						self	:		str						):



       """simple docstring"""
       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        A__      (						self	:		Dict						):



       """simple docstring"""
       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        A__      (						self	:		Optional[int]						):



       """simple docstring"""
       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        A__      (						self	:		Union[str, Any]       ,						__lowerCamelCase	:		Dict						):



       """simple docstring"""
       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        A__      (						self	:		Any						):



       """simple docstring"""
       # fmt: off
       lowerCAmelCase__										=      np.array([
           -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
           -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
           -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
       ]						)
       # 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						)						)







from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

__magic_name__  :  Any					=       TypeVar("""KEY""")
__magic_name__  :  Dict					=       TypeVar("""VAL""")





@dataclass(frozen=_a    ,					slots=_a			)
class     SCREAMING_SNAKE_CASE__		(Generic[KEY, VAL]			):
     lowercase_ :      KEY
     lowercase_ :      VAL





class     SCREAMING_SNAKE_CASE__		(_Item			):
     def __init__(						self	:		Tuple						):



       """simple docstring"""
       super().__init__(__lowerCamelCase       ,						__lowerCamelCase						)



     def __bool__(						self	:		Tuple						):



       """simple docstring"""
       return False



__magic_name__  :  str					=       _DeletedItem()





class     SCREAMING_SNAKE_CASE__		(MutableMapping[KEY, VAL]			):
     def __init__(						self	:		Any       ,						__lowerCamelCase	:		int = 8       ,						__lowerCamelCase	:		float = 0.75						):



       """simple docstring"""
       lowerCAmelCase__										=      initial_block_size
       lowerCAmelCase__										=      [None] * initial_block_size
       assert 0.0 < capacity_factor < 1.0
       lowerCAmelCase__										=      capacity_factor
       lowerCAmelCase__										=      0
     def        A__      (						self	:		Union[str, Any]       ,						__lowerCamelCase	:		KEY						):



       """simple docstring"""
       return hash(__lowerCamelCase						) % len(self._buckets						)
     def        A__      (						self	:		Tuple       ,						__lowerCamelCase	:		int						):



       """simple docstring"""
       return (ind + 1) % len(self._buckets						)
     def        A__      (						self	:		Dict       ,						__lowerCamelCase	:		int       ,						__lowerCamelCase	:		KEY       ,						__lowerCamelCase	:		VAL						):



       """simple docstring"""
       lowerCAmelCase__										=      self._buckets[ind]
       if not stored:
         lowerCAmelCase__										=      _Item(__lowerCamelCase       ,						__lowerCamelCase						)
         self._len += 1
         return True
       elif stored.key == key:
         lowerCAmelCase__										=      _Item(__lowerCamelCase       ,						__lowerCamelCase						)
         return True
       else:
         return False
     def        A__      (						self	:		Any						):



       """simple docstring"""
       lowerCAmelCase__										=      len(self._buckets						) * self._capacity_factor
       return len(self						) >= int(__lowerCamelCase						)
     def        A__      (						self	:		Tuple						):



       """simple docstring"""
       if len(self._buckets						) <= self._initial_block_size:
         return False
       lowerCAmelCase__										=      len(self._buckets						) * self._capacity_factor / 2
       return len(self						) < limit
     def        A__      (						self	:		List[Any]       ,						__lowerCamelCase	:		int						):



       """simple docstring"""
       lowerCAmelCase__										=      self._buckets
       lowerCAmelCase__										=      [None] * new_size
       lowerCAmelCase__										=      0
       for item in old_buckets:
         if item:
           self._add_item(item.key       ,						item.val						)
     def        A__      (						self	:		Any						):



       """simple docstring"""
       self._resize(len(self._buckets						) * 2						)
     def        A__      (						self	:		Dict						):



       """simple docstring"""
       self._resize(len(self._buckets						) // 2						)
     def        A__      (						self	:		List[str]       ,						__lowerCamelCase	:		KEY						):



       """simple docstring"""
       lowerCAmelCase__										=      self._get_bucket_index(__lowerCamelCase						)
       for _ in range(len(self._buckets						)						):
         yield ind
         lowerCAmelCase__										=      self._get_next_ind(__lowerCamelCase						)
     def        A__      (						self	:		Dict       ,						__lowerCamelCase	:		KEY       ,						__lowerCamelCase	:		VAL						):



       """simple docstring"""
       for ind in self._iterate_buckets(__lowerCamelCase						):
         if self._try_set(__lowerCamelCase       ,						__lowerCamelCase       ,						__lowerCamelCase						):
           break
     def __setitem__(						self	:		str       ,						__lowerCamelCase	:		KEY       ,						__lowerCamelCase	:		VAL						):



       """simple docstring"""
       if self._is_full():
         self._size_up()

       self._add_item(__lowerCamelCase       ,						__lowerCamelCase						)
     def __delitem__(						self	:		Tuple       ,						__lowerCamelCase	:		KEY						):



       """simple docstring"""
       for ind in self._iterate_buckets(__lowerCamelCase						):
         lowerCAmelCase__										=      self._buckets[ind]
         if item is None:
           raise KeyError(__lowerCamelCase						)
         if item is _deleted:
           continue
         if item.key == key:
           lowerCAmelCase__										=      _deleted
           self._len -= 1
           break
       if self._is_sparse():
         self._size_down()
     def __getitem__(						self	:		Optional[int]       ,						__lowerCamelCase	:		KEY						):



       """simple docstring"""
       for ind in self._iterate_buckets(__lowerCamelCase						):
         lowerCAmelCase__										=      self._buckets[ind]
         if item is None:
           break
         if item is _deleted:
           continue
         if item.key == key:
           return item.val
       raise KeyError(__lowerCamelCase						)
     def __len__(						self	:		int						):



       """simple docstring"""
       return self._len
     def __iter__(						self	:		Union[str, Any]						):



       """simple docstring"""
       yield from (item.key for item in self._buckets if item)



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



       """simple docstring"""
       lowerCAmelCase__										=      ''' ,'''.join(
           F"""{item.key}: {item.val}""" for item in self._buckets if item						)
       return F"""HashMap({val_string})"""

