

"""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					:List[Any]    = random.Random()




def  _lowercase					( __lowerCAmelCase  ,					__lowerCAmelCase=1.0  ,					__lowerCAmelCase=None  ,					__lowerCAmelCase=None							)       ->				List[str]:
      if rng is None:
            SCREAMING_SNAKE_CASE__   :      List[str]      		=	global_rng

      SCREAMING_SNAKE_CASE__   :      Tuple      		=	[]
      for batch_idx in range(shape[0]							):
            values.append([]							)
            for _ in range(shape[1]							):
                  values[-1].append(rng.random() * scale							)

      return values

class      __a							(unittest.TestCase):






     '''simple docstring'''

     def __init__(						self	,    _a	,    _a=7	,    _a=400	,    _a=2_000	,    _a=1	,    _a=0.0	,    _a=16_000	,    _a=True	,    _a=True	,    )  ->      Dict:


           """simple docstring"""


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

     def 					_a			(						self  )  ->      List[Any]:


           """simple docstring"""


           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	,    _a=False	,    _a=False  )  ->      Optional[int]:


           """simple docstring"""


           def _flatten(_a  ):
                 return list(itertools.chain(*lowerCamelCase__  )  )

           if equal_length:
                 SCREAMING_SNAKE_CASE__   :      Any      		=	floats_list((self.batch_size, self.max_seq_length)  )
           else:
                 # make sure that inputs increase in size
                 SCREAMING_SNAKE_CASE__   :      List[str]      		=	[
                     _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:
                 SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	[np.asarray(lowerCamelCase__  ) for x in speech_inputs]

           return speech_inputs





class      __a							(UpperCAmelCase__   ,	unittest.TestCase):






     '''simple docstring'''
     _SCREAMING_SNAKE_CASE						:Optional[int]       = WavaVecaFeatureExtractor

     def 					_a			(						self  )  ->      Dict:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	WavaVecaFeatureExtractionTester(self  )

     def 					_a			(						self	,    _a  )  ->      Tuple:


           """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  )  ->      int:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           # create three inputs of length 800, 1000, and 1200
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	[floats_list((1, x)  )[0] for x in range(800	,    1_400	,    200  )]
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	[np.asarray(lowerCamelCase__  ) for speech_input in speech_inputs]

           # Test not batched input
           SCREAMING_SNAKE_CASE__   :      str      		=	feat_extract(speech_inputs[0]	,    return_tensors="""np"""  ).input_values
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	feat_extract(np_speech_inputs[0]	,    return_tensors="""np"""  ).input_values
           self.assertTrue(np.allclose(lowerCamelCase__	,    lowerCamelCase__	,    atol=1E-3  )  )

           # Test batched
           SCREAMING_SNAKE_CASE__   :      Dict      		=	feat_extract(lowerCamelCase__	,    return_tensors="""np"""  ).input_values
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	feat_extract(lowerCamelCase__	,    return_tensors="""np"""  ).input_values
           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.
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	[floats_list((1, x)  )[0] for x in (800, 800, 800)]
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	np.asarray(lowerCamelCase__  )
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	feat_extract(lowerCamelCase__	,    return_tensors="""np"""  ).input_values
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	feat_extract(lowerCamelCase__	,    return_tensors="""np"""  ).input_values
           for enc_seq_a, enc_seq_a in zip(lowerCamelCase__	,    lowerCamelCase__  ):
                 self.assertTrue(np.allclose(lowerCamelCase__	,    lowerCamelCase__	,    atol=1E-3  )  )

     def 					_a			(						self  )  ->      Union[str, Any]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	[floats_list((1, x)  )[0] for x in range(800	,    1_400	,    200  )]

           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	["longest", "max_length", "do_not_pad"]
           SCREAMING_SNAKE_CASE__   :      int      		=	[None, 1_600, None]
           for max_length, padding in zip(lowerCamelCase__	,    lowerCamelCase__  ):
                 SCREAMING_SNAKE_CASE__   :      Any      		=	feat_extract(lowerCamelCase__	,    padding=lowerCamelCase__	,    max_length=lowerCamelCase__	,    return_tensors="""np"""  )
                 SCREAMING_SNAKE_CASE__   :      Any      		=	processed.input_values

                 self._check_zero_mean_unit_variance(input_values[0][:800]  )
                 self.assertTrue(input_values[0][800:].sum() < 1E-6  )
                 self._check_zero_mean_unit_variance(input_values[1][:1_000]  )
                 self.assertTrue(input_values[0][1_000:].sum() < 1E-6  )
                 self._check_zero_mean_unit_variance(input_values[2][:1_200]  )

     def 					_a			(						self  )  ->      Optional[int]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      int      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           SCREAMING_SNAKE_CASE__   :      int      		=	range(800	,    1_400	,    200  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	[floats_list((1, x)  )[0] for x in lengths]

           SCREAMING_SNAKE_CASE__   :      List[str]      		=	["longest", "max_length", "do_not_pad"]
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	[None, 1_600, None]

           for max_length, padding in zip(lowerCamelCase__	,    lowerCamelCase__  ):
                 SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	feat_extract(lowerCamelCase__	,    max_length=lowerCamelCase__	,    padding=lowerCamelCase__  )
                 SCREAMING_SNAKE_CASE__   :      str      		=	processed.input_values

                 self._check_zero_mean_unit_variance(input_values[0][:800]  )
                 self._check_zero_mean_unit_variance(input_values[1][:1_000]  )
                 self._check_zero_mean_unit_variance(input_values[2][:1_200]  )

     def 					_a			(						self  )  ->      Any:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	[floats_list((1, x)  )[0] for x in range(800	,    1_400	,    200  )]
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	feat_extract(
               lowerCamelCase__	,    truncation=lowerCamelCase__	,    max_length=1_000	,    padding="""max_length"""	,    return_tensors="""np"""  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800]  )
           self._check_zero_mean_unit_variance(input_values[1]  )
           self._check_zero_mean_unit_variance(input_values[2]  )

     def 					_a			(						self  )  ->      int:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Tuple      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	[floats_list((1, x)  )[0] for x in range(800	,    1_400	,    200  )]
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	feat_extract(
               lowerCamelCase__	,    truncation=lowerCamelCase__	,    max_length=1_000	,    padding="""longest"""	,    return_tensors="""np"""  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	processed.input_values

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

           SCREAMING_SNAKE_CASE__   :      int      		=	[floats_list((1, x)  )[0] for x in range(800	,    1_400	,    200  )]
           SCREAMING_SNAKE_CASE__   :      Dict      		=	feat_extract(
               lowerCamelCase__	,    truncation=lowerCamelCase__	,    max_length=2_000	,    padding="""longest"""	,    return_tensors="""np"""  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800]  )
           self._check_zero_mean_unit_variance(input_values[1, :1_000]  )
           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, 1_200)  )

     @require_torch
     def 					_a			(						self  )  ->      Optional[Any]:


           """simple docstring"""


           import torch

           SCREAMING_SNAKE_CASE__   :      List[str]      		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	np.random.rand(100  ).astype(np.floataa  )
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	np_speech_inputs.tolist()

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





     @slow
     @require_torch
     def 					_a			(						self  )  ->      Dict:


           """simple docstring"""


           for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
                 SCREAMING_SNAKE_CASE__   :      List[Any]      		=	WavaVecaConfig.from_pretrained(lowerCamelCase__  )
                 SCREAMING_SNAKE_CASE__   :      List[str]      		=	WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase__  )

                 # 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"""


import re
from pathlib import Path
from unittest import TestCase

import pytest

@pytest.mark.integration
class      __a							(UpperCamelCase_):






     '''simple docstring'''

     def 					_a			(						self	,    _a  )  ->      Union[str, Any]:


           """simple docstring"""


           with open(_a	,    encoding="""utf-8"""  ) as input_file:
                 SCREAMING_SNAKE_CASE__   :      str      		=	re.compile(r"""(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)"""  )
                 SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	input_file.read()
                 SCREAMING_SNAKE_CASE__   :      str      		=	regexp.search(_a  )

           return match

     def 					_a			(						self	,    _a  )  ->      Optional[Any]:


           """simple docstring"""


           with open(_a	,    encoding="""utf-8"""  ) as input_file:
                 SCREAMING_SNAKE_CASE__   :      Tuple      		=	re.compile(r"""#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()"""	,    re.DOTALL  )
                 SCREAMING_SNAKE_CASE__   :      List[Any]      		=	input_file.read()
                 # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
                 SCREAMING_SNAKE_CASE__   :      Dict      		=	regexp.finditer(_a  )

           SCREAMING_SNAKE_CASE__   :      int      		=	[match for match in matches if match is not None and match.group(1  ) is not None]
           return matches[0] if matches else None

     def 					_a			(						self  )  ->      str:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	Path("""./datasets"""  )
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	list(dataset_paths.absolute().glob("""**/*.py"""  )  )

           for dataset in dataset_files:
                 if self._no_encoding_on_file_open(str(_a  )  ):
                       raise AssertionError(f'''open(...) must use utf-8 encoding in {dataset}'''  )





     def 					_a			(						self  )  ->      Dict:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	Path("""./datasets"""  )
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	list(dataset_paths.absolute().glob("""**/*.py"""  )  )

           for dataset in dataset_files:
                 if self._no_print_statements(str(_a  )  ):
                       raise AssertionError(f'''print statement found in {dataset}. Use datasets.logger/logging instead.'''  )




