



"""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 				SCREAMING_SNAKE_CASE__   (		SCREAMING_SNAKE_CASE__			:	str					,     SCREAMING_SNAKE_CASE__			:	Optional[Any]=1.0					,     SCREAMING_SNAKE_CASE__			:	Tuple=None					,     SCREAMING_SNAKE_CASE__			:	Any=None      ):



					"""simple docstring"""





					if rng is None:
										snake_case_       :		int			       =       global_rng

					snake_case_       :		str			       =       []
					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  __lowercase  (       unittest.TestCase):







						"""simple docstring"""







						def __init__(self ,		lowercase__ ,		lowercase__=7 ,		lowercase__=4_00 ,		lowercase__=20_00 ,		lowercase__=24 ,		lowercase__=24 ,		lowercase__=0.0 ,		lowercase__=1_60_00 ,		lowercase__=True ,		lowercase__=True ,		):
											snake_case_       :		Union[str, Any]			       =       parent
											snake_case_       :		List[Any]			       =       batch_size
											snake_case_       :		Union[str, Any]			       =       min_seq_length
											snake_case_       :		List[Any]			       =       max_seq_length
											snake_case_       :		Optional[int]			       =       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											snake_case_       :		Any			       =       feature_size
											snake_case_       :		str			       =       num_mel_bins
											snake_case_       :		List[Any]			       =       padding_value
											snake_case_       :		List[str]			       =       sampling_rate
											snake_case_       :		str			       =       return_attention_mask
											snake_case_       :		Tuple			       =       do_normalize







						def     __UpperCamelCase				(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     __UpperCamelCase				(self ,		lowercase__=False ,		lowercase__=False							):
											def _flatten(lowercase__							):
																return list(itertools.chain(*a_							)							)

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







@require_torch
@require_torchaudio
class  __lowercase  (       _UpperCAmelCase       ,     unittest.TestCase):







						"""simple docstring"""







						_A		:				Optional[Any]											=		SpeechaTextFeatureExtractor if is_speech_available() else None







						def     __UpperCamelCase				(self							):
											snake_case_       :		Optional[Any]			       =       SpeechaTextFeatureExtractionTester(self							)







						def     __UpperCamelCase				(self ,		lowercase__							):
											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     __UpperCamelCase				(self							):
											# Tests that all call wrap to encode_plus and batch_encode_plus
											snake_case_       :		List[Any]			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											# create three inputs of length 800, 1000, and 1200
											snake_case_       :		Any			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]
											snake_case_       :		List[Any]			       =       [np.asarray(a_							) for speech_input in speech_inputs]

											# Test feature size
											snake_case_       :		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
											snake_case_       :		Any			       =       feature_extractor(speech_inputs[0] ,		return_tensors="""np"""							).input_features
											snake_case_       :		List[Any]			       =       feature_extractor(np_speech_inputs[0] ,		return_tensors="""np"""							).input_features
											self.assertTrue(np.allclose(a_ ,		a_ ,		atol=1e-3							)							)

											# Test batched
											snake_case_       :		Tuple			       =       feature_extractor(a_ ,		return_tensors="""np"""							).input_features
											snake_case_       :		Dict			       =       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.
											snake_case_       :		Optional[int]			       =       [floats_list((1, x)							)[0] for x in (8_00, 8_00, 8_00)]
											snake_case_       :		Union[str, Any]			       =       np.asarray(a_							)
											snake_case_       :		int			       =       feature_extractor(a_ ,		return_tensors="""np"""							).input_features
											snake_case_       :		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     __UpperCamelCase				(self							):
											snake_case_       :		str			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		Tuple			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]

											snake_case_       :		str			       =       ['longest', 'max_length', 'do_not_pad']
											snake_case_       :		Union[str, Any]			       =       [None, 16, None]
											for max_length, padding in zip(a_ ,		a_							):
																snake_case_       :		Dict			       =       feature_extractor(
																    a_ ,		padding=a_ ,		max_length=a_ ,		return_attention_mask=a_							)
																snake_case_       :		Any			       =       inputs.input_features
																snake_case_       :		Optional[Any]			       =       inputs.attention_mask
																snake_case_       :		Tuple			       =       [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     __UpperCamelCase				(self							):
											snake_case_       :		Any			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		int			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]

											snake_case_       :		Any			       =       ['longest', 'max_length', 'do_not_pad']
											snake_case_       :		Union[str, Any]			       =       [None, 16, None]
											for max_length, padding in zip(a_ ,		a_							):
																snake_case_       :		Optional[Any]			       =       feature_extractor(
																    a_ ,		max_length=a_ ,		padding=a_ ,		return_tensors="""np""" ,		return_attention_mask=a_							)
																snake_case_       :		Optional[int]			       =       inputs.input_features
																snake_case_       :		List[Any]			       =       inputs.attention_mask
																snake_case_       :		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     __UpperCamelCase				(self							):
											snake_case_       :		Dict			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		List[str]			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]
											snake_case_       :		Union[str, Any]			       =       feature_extractor(
											    a_ ,		padding="""max_length""" ,		max_length=4 ,		truncation=a_ ,		return_tensors="""np""" ,		return_attention_mask=a_ ,		)
											snake_case_       :		Optional[Any]			       =       inputs.input_features
											snake_case_       :		Dict			       =       inputs.attention_mask
											snake_case_       :		List[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]							)
											self._check_zero_mean_unit_variance(input_features[2]							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Optional[Any]			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		Optional[int]			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]
											snake_case_       :		str			       =       feature_extractor(
											    a_ ,		padding="""longest""" ,		max_length=4 ,		truncation=a_ ,		return_tensors="""np""" ,		return_attention_mask=a_ ,		)
											snake_case_       :		Dict			       =       inputs.input_features
											snake_case_       :		Optional[int]			       =       inputs.attention_mask
											snake_case_       :		Tuple			       =       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)							)

											snake_case_       :		List[str]			       =       [floats_list((1, x)							)[0] for x in range(8_00 ,		14_00 ,		2_00							)]
											snake_case_       :		Optional[Any]			       =       feature_extractor(
											    a_ ,		padding="""longest""" ,		max_length=16 ,		truncation=a_ ,		return_tensors="""np""" ,		return_attention_mask=a_ ,		)
											snake_case_       :		Tuple			       =       inputs.input_features
											snake_case_       :		Any			       =       inputs.attention_mask
											snake_case_       :		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, 6, 24)							)







						def     __UpperCamelCase				(self							):
											import torch

											snake_case_       :		int			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		Tuple			       =       np.random.rand(1_00 ,		32							).astype(np.floataa							)
											snake_case_       :		Union[str, Any]			       =       np_speech_inputs.tolist()

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







						def     __UpperCamelCase				(self ,		lowercase__							):
											from datasets import load_dataset

											snake_case_       :		Union[str, Any]			       =       load_dataset("""hf-internal-testing/librispeech_asr_dummy""" ,		"""clean""" ,		split="""validation"""							)
											# automatic decoding with librispeech
											snake_case_       :		str			       =       ds.sort("""id"""							).select(range(a_							)							)[:num_samples]['audio']

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







						def     __UpperCamelCase				(self							):
											# fmt: off
											snake_case_       :		Any			       =       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

											snake_case_       :		Union[str, Any]			       =       self._load_datasamples(1							)
											snake_case_       :		Any			       =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
											snake_case_       :		Optional[Any]			       =       feature_extractor(a_ ,		return_tensors="""pt"""							).input_features
											self.assertEquals(input_features.shape ,		(1, 5_84, 24)							)
											self.assertTrue(np.allclose(input_features[0, 0, :30] ,		a_ ,		atol=1e-4							)							)









'''simple docstring'''

import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
a							   =				'.'


if __name__ == "__main__":
	a							   =				os.path.join(REPO_PATH, 'utils/documentation_tests.txt')
	a							   =				[]
	a							   =				[]
	with open(doctest_file_path) as fp:
		for line in fp:
			a							   =				line.strip()
			a							   =				os.path.join(REPO_PATH, line)
			if not (os.path.isfile(path) or os.path.isdir(path)):
				non_existent_paths.append(line)
			all_paths.append(path)
	if len(non_existent_paths) > 0:
		a							   =				'\n'.join(non_existent_paths)
		raise ValueError(F"""`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}""")
	if all_paths != sorted(all_paths):
		raise ValueError('Files in `utils/documentation_tests.txt` are not in alphabetical order.')




