



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

A    =			random.Random()

def   lowerCAmelCase__			(     lowerCamelCase__				,      lowerCamelCase__=1.0				,      lowerCamelCase__=None				,      lowerCamelCase__=None			) ->							List[Any]:
							if rng is None:
														A         =					global_rng

							A         =					[]
							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      UpperCAmelCase__		(      unittest.TestCase     ):



		def __init__( self  :    List[Any]      ,							snake_case  :    Tuple      ,							snake_case  :    int=7      ,							snake_case  :    List[Any]=400      ,							snake_case  :    str=2_000      ,							snake_case  :    List[Any]=24      ,							snake_case  :    Tuple=24      ,							snake_case  :    Union[str, Any]=0.0      ,							snake_case  :    int=16_000      ,							snake_case  :    List[str]=True      ,							snake_case  :    Union[str, Any]=True      ,							)			->						List[Any]:






									'''simple docstring'''

									A         =					parent
									A         =					batch_size
									A         =					min_seq_length
									A         =					max_seq_length
									A         =					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
									A         =					feature_size
									A         =					num_mel_bins
									A         =					padding_value
									A         =					sampling_rate
									A         =					return_attention_mask
									A         =					do_normalize



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[int]:






									'''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  :    List[Any]      ,							snake_case  :    Dict=False      ,							snake_case  :    List[str]=False				)			->						Optional[Any]:






									'''simple docstring'''

									def _flatten(snake_case  :    Dict				):
																return list(itertools.chain(*snake_case				)				)

									if equal_length:
																A         =					[floats_list((self.max_seq_length, self.feature_size)				) for _ in range(self.batch_size				)]
									else:
																# make sure that inputs increase in size
																A         =					[
																    floats_list((x, self.feature_size)				)
																    for x in range(self.min_seq_length      ,							self.max_seq_length      ,							self.seq_length_diff				)
																]
									if numpify:
																A         =					[np.asarray(snake_case				) for x in speech_inputs]
									return speech_inputs






@require_torch
@require_torchaudio
class      UpperCAmelCase__		(      UpperCamelCase   ,unittest.TestCase     ):
		lowerCAmelCase_       :	List[Any] 							=     SpeechaTextFeatureExtractor if is_speech_available() else None



		def 					A_		( self  :    Dict				)			->						Optional[int]:






									'''simple docstring'''

									A         =					SpeechaTextFeatureExtractionTester(self				)



		def 					A_		( self  :    List[str]      ,							snake_case  :    Tuple				)			->						int:






									'''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 					A_		( self  :    Any				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									# create three inputs of length 800, 1000, and 1200
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					[np.asarray(snake_case				) for speech_input in speech_inputs]

									# Test feature size
									A         =					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
									A         =					feature_extractor(speech_inputs[0]      ,							return_tensors='np'				).input_features
									A         =					feature_extractor(np_speech_inputs[0]      ,							return_tensors='np'				).input_features
									self.assertTrue(np.allclose(snake_case      ,							snake_case      ,							atol=1E-3				)				)

									# Test batched
									A         =					feature_extractor(snake_case      ,							return_tensors='np'				).input_features
									A         =					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.
									A         =					[floats_list((1, x)				)[0] for x in (800, 800, 800)]
									A         =					np.asarray(snake_case				)
									A         =					feature_extractor(snake_case      ,							return_tensors='np'				).input_features
									A         =					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 					A_		( self  :    Union[str, Any]				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]

									A         =					['longest', 'max_length', 'do_not_pad']
									A         =					[None, 16, None]
									for max_length, padding in zip(snake_case      ,							snake_case				):
																A         =					feature_extractor(
																    snake_case      ,							padding=snake_case      ,							max_length=snake_case      ,							return_attention_mask=snake_case				)
																A         =					inputs.input_features
																A         =					inputs.attention_mask
																A         =					[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 					A_		( self  :    str				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]

									A         =					['longest', 'max_length', 'do_not_pad']
									A         =					[None, 16, None]
									for max_length, padding in zip(snake_case      ,							snake_case				):
																A         =					feature_extractor(
																    snake_case      ,							max_length=snake_case      ,							padding=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case				)
																A         =					inputs.input_features
																A         =					inputs.attention_mask
																A         =					[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 					A_		( self  :    str				)			->						str:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='max_length'      ,							max_length=4      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					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  :    Optional[int]				)			->						int:






									'''simple docstring'''

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='longest'      ,							max_length=4      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					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)				)

									A         =					[floats_list((1, x)				)[0] for x in range(800      ,							1_400      ,							200				)]
									A         =					feature_extractor(
									    snake_case      ,							padding='longest'      ,							max_length=16      ,							truncation=snake_case      ,							return_tensors='np'      ,							return_attention_mask=snake_case      ,							)
									A         =					inputs.input_features
									A         =					inputs.attention_mask
									A         =					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  :    List[str]				)			->						Optional[int]:






									'''simple docstring'''

									import torch

									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					np.random.rand(100      ,							32				).astype(np.floataa				)
									A         =					np_speech_inputs.tolist()

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



		def 					A_		( self  :    Tuple      ,							snake_case  :    List[Any]				)			->						Optional[Any]:






									'''simple docstring'''

									from datasets import load_dataset

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

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



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[int]:






									'''simple docstring'''

									A         =					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

									A         =					self._load_datasamples(1				)
									A         =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
									A         =					feature_extractor(snake_case      ,							return_tensors='pt'				).input_features
									self.assertEquals(input_features.shape      ,							(1, 584, 24)				)
									self.assertTrue(np.allclose(input_features[0, 0, :30]      ,							snake_case      ,							atol=1E-4				)				)











"""simple docstring"""







from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching

def   lowerCAmelCase__			(     ) ->							Optional[Any]:
							import os as original_os
							from os import path as original_path
							from os import rename as original_rename
							from os.path import dirname as original_dirname
							from os.path import join as original_join

							assert _test_patching.os is original_os
							assert _test_patching.path is original_path
							assert _test_patching.join is original_join

							assert _test_patching.renamed_os is original_os
							assert _test_patching.renamed_path is original_path
							assert _test_patching.renamed_join is original_join

							A         =					'__test_patch_submodule_mock__'
							with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
														# Every way to access os.path.join must be patched, and the rest must stay untouched

														# check os.path.join
														assert isinstance(_test_patching.os				,      _PatchedModuleObj			)
														assert isinstance(_test_patching.os.path				,      _PatchedModuleObj			)
														assert _test_patching.os.path.join is mock

														# check path.join
														assert isinstance(_test_patching.path				,      _PatchedModuleObj			)
														assert _test_patching.path.join is mock

														# check join
														assert _test_patching.join is mock

														# check that the other attributes are untouched
														assert _test_patching.os.rename is original_rename
														assert _test_patching.path.dirname is original_dirname
														assert _test_patching.os.path.dirname is original_dirname

														# Even renamed modules or objects must be patched

														# check renamed_os.path.join
														assert isinstance(_test_patching.renamed_os				,      _PatchedModuleObj			)
														assert isinstance(_test_patching.renamed_os.path				,      _PatchedModuleObj			)
														assert _test_patching.renamed_os.path.join is mock

														# check renamed_path.join
														assert isinstance(_test_patching.renamed_path				,      _PatchedModuleObj			)
														assert _test_patching.renamed_path.join is mock

														# check renamed_join
														assert _test_patching.renamed_join is mock

														# check that the other attributes are untouched
														assert _test_patching.renamed_os.rename is original_rename
														assert _test_patching.renamed_path.dirname is original_dirname
														assert _test_patching.renamed_os.path.dirname is original_dirname

							# check that everthing is back to normal when the patch is over

							assert _test_patching.os is original_os
							assert _test_patching.path is original_path
							assert _test_patching.join is original_join

							assert _test_patching.renamed_os is original_os
							assert _test_patching.renamed_path is original_path
							assert _test_patching.renamed_join is original_join

def   lowerCAmelCase__			(     ) ->							str:
							assert _test_patching.open is open

							A         =					'__test_patch_submodule_builtin_mock__'
							# _test_patching has "open" in its globals
							assert _test_patching.open is open
							with patch_submodule(_test_patching				,      'open'				,      lowerCamelCase__			):
														assert _test_patching.open is mock

							# check that everthing is back to normal when the patch is over

							assert _test_patching.open is open

def   lowerCAmelCase__			(     ) ->							List[Any]:
							# pandas.read_csv is not present in _test_patching
							A         =					'__test_patch_submodule_missing_mock__'
							with patch_submodule(_test_patching				,      'pandas.read_csv'				,      lowerCamelCase__			):
														pass

def   lowerCAmelCase__			(     ) ->							Union[str, Any]:
							# builtin should always be mocked even if they're not in the globals
							# in case they're loaded at one point
							A         =					'__test_patch_submodule_missing_builtin_mock__'
							# _test_patching doesn't have "len" in its globals
							assert getattr(_test_patching				,      'len'				,      lowerCamelCase__			) is None
							with patch_submodule(_test_patching				,      'len'				,      lowerCamelCase__			):
														assert _test_patching.len is mock
							assert _test_patching.len is len

def   lowerCAmelCase__			(     ) ->							Union[str, Any]:
							A         =					'__test_patch_submodule_start_and_stop_mock__'
							A         =					patch_submodule(_test_patching				,      'open'				,      lowerCamelCase__			)
							assert _test_patching.open is open
							patch.start()
							assert _test_patching.open is mock
							patch.stop()
							assert _test_patching.open is open

def   lowerCAmelCase__			(     ) ->							int:
							from os import rename as original_rename
							from os.path import dirname as original_dirname
							from os.path import join as original_join

							A         =					'__test_patch_submodule_successive_join__'
							A         =					'__test_patch_submodule_successive_dirname__'
							A         =					'__test_patch_submodule_successive_rename__'
							assert _test_patching.os.path.join is original_join
							assert _test_patching.os.path.dirname is original_dirname
							assert _test_patching.os.rename is original_rename

							with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
														with patch_submodule(_test_patching				,      'os.rename'				,      lowerCamelCase__			):
																					with patch_submodule(_test_patching				,      'os.path.dirname'				,      lowerCamelCase__			):
																												assert _test_patching.os.path.join is mock_join
																												assert _test_patching.os.path.dirname is mock_dirname
																												assert _test_patching.os.rename is mock_rename

    # try another order
							with patch_submodule(_test_patching				,      'os.rename'				,      lowerCamelCase__			):
														with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
																					with patch_submodule(_test_patching				,      'os.path.dirname'				,      lowerCamelCase__			):
																												assert _test_patching.os.path.join is mock_join
																												assert _test_patching.os.path.dirname is mock_dirname
																												assert _test_patching.os.rename is mock_rename

							assert _test_patching.os.path.join is original_join
							assert _test_patching.os.path.dirname is original_dirname
							assert _test_patching.os.rename is original_rename

def   lowerCAmelCase__			(     ) ->							Optional[Any]:
							A         =					'__test_patch_submodule_doesnt_exist_mock__'
							with patch_submodule(_test_patching				,      '__module_that_doesn_exist__.__attribute_that_doesn_exist__'				,      lowerCamelCase__			):
														pass
							with patch_submodule(_test_patching				,      'os.__attribute_that_doesn_exist__'				,      lowerCamelCase__			):
														pass



