
'''simple docstring'''


import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
					from transformers import WhisperFeatureExtractor

if is_torch_available():
					import torch

__lowercase							:      List[str]		 =   random.Random()




def lowercase_		(      _lowercase	,				_lowercase=1.0	,				_lowercase=None	,				_lowercase=None )   ->							Union[str, Any]:
					'''simple docstring'''

					if rng is None:
										lowerCamelCase_    :					Optional[Any]   =						global_rng

					lowerCamelCase_    :					Optional[Any]   =						[]
					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			):

			def __init__(self	,      A	,      A=7	,      A=4_0_0	,      A=2_0_0_0	,      A=1_0	,      A=1_6_0	,      A=8	,      A=0.0	,      A=4_0_0_0	,      A=False	,      A=True	,      ):
								lowerCamelCase_    :					Optional[Any]   =						parent
								lowerCamelCase_    :					Optional[Any]   =						batch_size
								lowerCamelCase_    :					Any   =						min_seq_length
								lowerCamelCase_    :					str   =						max_seq_length
								lowerCamelCase_    :					Optional[int]   =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								lowerCamelCase_    :					Optional[Any]   =						padding_value
								lowerCamelCase_    :					Any   =						sampling_rate
								lowerCamelCase_    :					Tuple   =						return_attention_mask
								lowerCamelCase_    :					List[str]   =						do_normalize
								lowerCamelCase_    :					Dict   =						feature_size
								lowerCamelCase_    :					Optional[int]   =						chunk_length
								lowerCamelCase_    :					Union[str, Any]   =						hop_length

			def 				UpperCAmelCase__       (self					):
								return {
								    "feature_size": self.feature_size,
								    "hop_length": self.hop_length,
								    "chunk_length": self.chunk_length,
								    "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	,      A=False	,      A=False					):
								def _flatten(A					):
													return list(itertools.chain(*_lowerCamelCase					)					)

								if equal_length:
													lowerCamelCase_    :					Optional[int]   =						[floats_list((self.max_seq_length, self.feature_size)					) for _ in range(self.batch_size					)]
								else:
													# make sure that inputs increase in size
													lowerCamelCase_    :					Dict   =						[
													    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_    :					Optional[Any]   =						[np.asarray(_lowerCamelCase					) for x in speech_inputs]
								return speech_inputs

@require_torch
@require_torchaudio
class 		__lowercase (     _lowerCAmelCase					,   unittest.TestCase			):
			lowerCamelCase	:						List[Any]							=	WhisperFeatureExtractor if is_speech_available() else None

			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					Dict   =						WhisperFeatureExtractionTester(self					)

			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					List[str]   =						self.feature_extraction_class(**self.feat_extract_dict					)

								with tempfile.TemporaryDirectory() as tmpdirname:
													lowerCamelCase_    :					Optional[Any]   =						feat_extract_first.save_pretrained(_lowerCamelCase					)[0]
													check_json_file_has_correct_format(_lowerCamelCase					)
													lowerCamelCase_    :					Tuple   =						self.feature_extraction_class.from_pretrained(_lowerCamelCase					)

								lowerCamelCase_    :					List[Any]   =						feat_extract_first.to_dict()
								lowerCamelCase_    :					Dict   =						feat_extract_second.to_dict()
								lowerCamelCase_    :					Optional[Any]   =						feat_extract_first.mel_filters
								lowerCamelCase_    :					List[Any]   =						feat_extract_second.mel_filters
								self.assertTrue(np.allclose(_lowerCamelCase	,      _lowerCamelCase					)					)
								self.assertEqual(_lowerCamelCase	,      _lowerCamelCase					)

			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					int   =						self.feature_extraction_class(**self.feat_extract_dict					)

								with tempfile.TemporaryDirectory() as tmpdirname:
													lowerCamelCase_    :					List[Any]   =						os.path.join(_lowerCamelCase	,      '''feat_extract.json'''					)
													feat_extract_first.to_json_file(_lowerCamelCase					)
													lowerCamelCase_    :					Optional[Any]   =						self.feature_extraction_class.from_json_file(_lowerCamelCase					)

								lowerCamelCase_    :					List[str]   =						feat_extract_first.to_dict()
								lowerCamelCase_    :					int   =						feat_extract_second.to_dict()
								lowerCamelCase_    :					Tuple   =						feat_extract_first.mel_filters
								lowerCamelCase_    :					List[str]   =						feat_extract_second.mel_filters
								self.assertTrue(np.allclose(_lowerCamelCase	,      _lowerCamelCase					)					)
								self.assertEqual(_lowerCamelCase	,      _lowerCamelCase					)

			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					Optional[Any]   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
								# create three inputs of length 800, 1000, and 1200
								lowerCamelCase_    :					List[Any]   =						[floats_list((1, x)					)[0] for x in range(8_0_0	,      1_4_0_0	,      2_0_0					)]
								lowerCamelCase_    :					Optional[int]   =						[np.asarray(_lowerCamelCase					) for speech_input in speech_inputs]

								# Test feature size
								lowerCamelCase_    :					Tuple   =						feature_extractor(_lowerCamelCase	,      padding='''max_length'''	,      return_tensors='''np'''					).input_features
								self.assertTrue(input_features.ndim == 3					)
								self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames					)
								self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size					)

								# Test not batched input
								lowerCamelCase_    :					Optional[int]   =						feature_extractor(speech_inputs[0]	,      return_tensors='''np'''					).input_features
								lowerCamelCase_    :					Optional[Any]   =						feature_extractor(np_speech_inputs[0]	,      return_tensors='''np'''					).input_features
								self.assertTrue(np.allclose(_lowerCamelCase	,      _lowerCamelCase	,      atol=1E-3					)					)

								# Test batched
								lowerCamelCase_    :					str   =						feature_extractor(_lowerCamelCase	,      return_tensors='''np'''					).input_features
								lowerCamelCase_    :					Optional[Any]   =						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_    :					Tuple   =						[floats_list((1, x)					)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
								lowerCamelCase_    :					str   =						np.asarray(_lowerCamelCase					)
								lowerCamelCase_    :					Union[str, Any]   =						feature_extractor(_lowerCamelCase	,      return_tensors='''np'''					).input_features
								lowerCamelCase_    :					List[Any]   =						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 truncation required
								lowerCamelCase_    :					List[Any]   =						[floats_list((1, x)					)[0] for x in range(2_0_0	,      (feature_extractor.n_samples + 5_0_0)	,      2_0_0					)]
								lowerCamelCase_    :					List[str]   =						[np.asarray(_lowerCamelCase					) for speech_input in speech_inputs]

								lowerCamelCase_    :					str   =						[x[: feature_extractor.n_samples] for x in speech_inputs]
								lowerCamelCase_    :					Union[str, Any]   =						[np.asarray(_lowerCamelCase					) for speech_input in speech_inputs_truncated]

								lowerCamelCase_    :					Tuple   =						feature_extractor(_lowerCamelCase	,      return_tensors='''np'''					).input_features
								lowerCamelCase_    :					str   =						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 				UpperCAmelCase__       (self					):
								import torch

								lowerCamelCase_    :					Optional[Any]   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
								lowerCamelCase_    :					Union[str, Any]   =						np.random.rand(1_0_0	,      3_2					).astype(np.floataa					)
								lowerCamelCase_    :					Tuple   =						np_speech_inputs.tolist()

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

			def 				UpperCAmelCase__       (self	,      A					):
								lowerCamelCase_    :					List[str]   =						load_dataset('''hf-internal-testing/librispeech_asr_dummy'''	,      '''clean'''	,      split='''validation'''					)
								# automatic decoding with librispeech
								lowerCamelCase_    :					Optional[Any]   =						ds.sort('''id'''					).select(range(_lowerCamelCase					)					)[:num_samples]['''audio''']

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

			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					Any   =						torch.tensor(
								    [
								        0.11_93, -0.09_46, -0.10_98, -0.01_96, 0.02_25, -0.06_90, -0.17_36, 0.09_51,
								        0.09_71, -0.08_17, -0.07_02, 0.01_62, 0.02_60, 0.00_17, -0.01_92, -0.16_78,
								        0.07_09, -0.18_67, -0.06_55, -0.02_74, -0.02_34, -0.18_84, -0.05_16, -0.05_54,
								        -0.02_74, -0.14_25, -0.14_23, 0.08_37, 0.03_77, -0.08_54
								    ]					)
								# fmt: on

								lowerCamelCase_    :					Optional[int]   =						self._load_datasamples(1					)
								lowerCamelCase_    :					str   =						WhisperFeatureExtractor()
								lowerCamelCase_    :					str   =						feature_extractor(_lowerCamelCase	,      return_tensors='''pt'''					).input_features
								self.assertEqual(input_features.shape	,      (1, 8_0, 3_0_0_0)					)
								self.assertTrue(torch.allclose(input_features[0, 0, :3_0]	,      _lowerCamelCase	,      atol=1E-4					)					)






			def 				UpperCAmelCase__       (self					):
								lowerCamelCase_    :					Dict   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
								lowerCamelCase_    :					Dict   =						self._load_datasamples(1					)[0]
								lowerCamelCase_    :					str   =						((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
								lowerCamelCase_    :					Union[str, Any]   =						feat_extract.zero_mean_unit_var_norm([audio]	,      attention_mask=_lowerCamelCase					)[0]

								self.assertTrue(np.all(np.mean(_lowerCamelCase					) < 1E-3					)					)
								self.assertTrue(np.all(np.abs(np.var(_lowerCamelCase					) - 1					) < 1E-3					)					)










"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import ViTImageProcessor


class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def __init__(	self :     List[str],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Union[str, Any]=13,       _lowerCamelCase :     Any=3,       _lowerCamelCase :     Optional[int]=2_24,       _lowerCamelCase :     str=30,       _lowerCamelCase :     Dict=4_00,       _lowerCamelCase :     Union[str, Any]=True,       _lowerCamelCase :     Any=None,       _lowerCamelCase :     Optional[Any]=True,       _lowerCamelCase :     Any=[0.5, 0.5, 0.5],       _lowerCamelCase :     List[str]=[0.5, 0.5, 0.5],       ):

     '''simple docstring'''
     __A									= size if size is not None else {'''height''': 18, '''width''': 18}
     __A									= parent
     __A									= batch_size
     __A									= num_channels
     __A									= image_size
     __A									= min_resolution
     __A									= max_resolution
     __A									= do_resize
     __A									= size
     __A									= do_normalize
     __A									= image_mean
     __A									= image_std






   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
     }





@require_torch
@require_vision
class 					snake_case		(							_lowerCAmelCase   ,	unittest.TestCase						):


   '''simple docstring'''

   A_						:	str						 = ViTImageProcessor if is_vision_available() else None


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= EfficientFormerImageProcessorTester(self  )


   @property
   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     return self.image_proc_tester.prepare_image_processor_dict()


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= self.image_processing_class(**self.image_processor_dict  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''image_mean'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''image_std'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''do_normalize'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''do_resize'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''size'''  )  )


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

     '''simple docstring'''
     pass


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     # Initialize image_processor
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random PIL images
     __A									= prepare_image_inputs(self.image_proc_tester,       equal_resolution=_lowerCamelCase  )
     for image in image_inputs:
       self.assertIsInstance(_lowerCamelCase,       Image.Image  )

     # Test not batched input
     __A									= image_processor(image_inputs[0],       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             1,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )

     # Test batched
     __A									= image_processor(_lowerCamelCase,       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             self.image_proc_tester.batch_size,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple  ):

     '''simple docstring'''
     # Initialize image_processor
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random numpy tensors
     __A									= prepare_image_inputs(self.image_proc_tester,       equal_resolution=_lowerCamelCase,       numpify=_lowerCamelCase  )
     for image in image_inputs:
       self.assertIsInstance(_lowerCamelCase,       np.ndarray  )

     # Test not batched input
     __A									= image_processor(image_inputs[0],       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             1,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )

     # Test batched
     __A									= image_processor(_lowerCamelCase,       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             self.image_proc_tester.batch_size,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )






   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     # Initialize image_processor
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random PyTorch tensors
     __A									= prepare_image_inputs(self.image_proc_tester,       equal_resolution=_lowerCamelCase,       torchify=_lowerCamelCase  )
     for image in image_inputs:
       self.assertIsInstance(_lowerCamelCase,       torch.Tensor  )

     # Test not batched input
     __A									= image_processor(image_inputs[0],       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             1,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )

     # Test batched
     __A									= image_processor(_lowerCamelCase,       return_tensors='''pt'''  ).pixel_values
     self.assertEqual(
         encoded_images.shape,       (
             self.image_proc_tester.batch_size,
             self.image_proc_tester.num_channels,
             self.image_proc_tester.size['''height'''],
             self.image_proc_tester.size['''width'''],
         ),       )




