


'''simple docstring'''


import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_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_torch_available():
				import torch

if is_datasets_available():
				from datasets import load_dataset

UpperCAmelCase_		:   Optional[int]							   =		random.Random()







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__=1.0							,      SCREAMING_SNAKE_CASE__=None							,      SCREAMING_SNAKE_CASE__=None     ):







				"""simple docstring"""





				if rng is None:
								_SCREAMING_SNAKE_CASE :       str     		=      global_rng

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

				return values





class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							def __init__(    self    ,					__snake_case    ,					__snake_case=7    ,					__snake_case=400    ,					__snake_case=2000    ,					__snake_case=2048    ,					__snake_case=128    ,					__snake_case=1    ,					__snake_case=512    ,					__snake_case=30    ,					__snake_case=4_4100    ,					):
											_SCREAMING_SNAKE_CASE :       str     		=      parent
											_SCREAMING_SNAKE_CASE :       Tuple     		=      batch_size
											_SCREAMING_SNAKE_CASE :       List[str]     		=      min_seq_length
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      max_seq_length
											_SCREAMING_SNAKE_CASE :       Dict     		=      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											_SCREAMING_SNAKE_CASE :       List[str]     		=      spectrogram_length
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      feature_size
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      num_audio_channels
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      hop_length
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      chunk_length
											_SCREAMING_SNAKE_CASE :       str     		=      sampling_rate






							def UpperCAmelCase_	(    self  ):
											return {
											    "spectrogram_length": self.spectrogram_length,
											    "feature_size": self.feature_size,
											    "num_audio_channels": self.num_audio_channels,
											    "hop_length": self.hop_length,
											    "chunk_length": self.chunk_length,
											    "sampling_rate": self.sampling_rate,
											}






							def UpperCAmelCase_	(    self    ,					__snake_case=False    ,					__snake_case=False  ):
											def _flatten(__snake_case  ):
															return list(itertools.chain(*__snake_case  )  )

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





@require_torch
@require_torchaudio
class     lowercase__  (						_snake_case     ,     unittest.TestCase					):





							'''simple docstring'''


							A_							:      List[str]      	=  TvltFeatureExtractor






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      TvltFeatureExtractionTester(self  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.feature_extraction_class(**self.feat_extract_dict  )
											self.assertTrue(hasattr(__snake_case    ,					"""spectrogram_length"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""feature_size"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""num_audio_channels"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""hop_length"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""chunk_length"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""sampling_rate"""  )  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       int     		=      self.feature_extraction_class(**self.feat_extract_dict  )

											with tempfile.TemporaryDirectory() as tmpdirname:
															_SCREAMING_SNAKE_CASE :       Tuple     		=      feat_extract_first.save_pretrained(__snake_case  )[0]
															check_json_file_has_correct_format(__snake_case  )
															_SCREAMING_SNAKE_CASE :       Optional[int]     		=      self.feature_extraction_class.from_pretrained(__snake_case  )

											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      feat_extract_first.to_dict()
											_SCREAMING_SNAKE_CASE :       Any     		=      feat_extract_second.to_dict()
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      dict_first.pop("""mel_filters"""  )
											_SCREAMING_SNAKE_CASE :       int     		=      dict_second.pop("""mel_filters"""  )
											self.assertTrue(np.allclose(__snake_case    ,					__snake_case  )  )
											self.assertEqual(__snake_case    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[str]     		=      self.feature_extraction_class(**self.feat_extract_dict  )

											with tempfile.TemporaryDirectory() as tmpdirname:
															_SCREAMING_SNAKE_CASE :       int     		=      os.path.join(__snake_case    ,					"""feat_extract.json"""  )
															feat_extract_first.to_json_file(__snake_case  )
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      self.feature_extraction_class.from_json_file(__snake_case  )

											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      feat_extract_first.to_dict()
											_SCREAMING_SNAKE_CASE :       str     		=      feat_extract_second.to_dict()
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      dict_first.pop("""mel_filters"""  )
											_SCREAMING_SNAKE_CASE :       List[str]     		=      dict_second.pop("""mel_filters"""  )
											self.assertTrue(np.allclose(__snake_case    ,					__snake_case  )  )
											self.assertEqual(__snake_case    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											# Initialize feature_extractor
											_SCREAMING_SNAKE_CASE :       List[str]     		=      self.feature_extraction_class(**self.feat_extract_dict  )

											# create three inputs of length 800, 1000, and 1200
											_SCREAMING_SNAKE_CASE :       str     		=      [floats_list((1, x)  )[0] for x in range(800    ,					1400    ,					200  )]
											_SCREAMING_SNAKE_CASE :       Any     		=      [np.asarray(__snake_case  ) for speech_input in speech_inputs]

											# Test not batched input
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      feature_extractor(np_speech_inputs[0]    ,					return_tensors="""np"""    ,					sampling_rate=4_4100  ).audio_values

											self.assertTrue(encoded_audios.ndim == 4  )
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size  )
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length  )
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels  )

											# Test batched
											_SCREAMING_SNAKE_CASE :       int     		=      feature_extractor(__snake_case    ,					return_tensors="""np"""    ,					sampling_rate=4_4100  ).audio_values

											self.assertTrue(encoded_audios.ndim == 4  )
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size  )
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length  )
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels  )

											# Test audio masking
											_SCREAMING_SNAKE_CASE :       Dict     		=      feature_extractor(
											    __snake_case    ,					return_tensors="""np"""    ,					sampling_rate=4_4100    ,					mask_audio=__snake_case  ).audio_values

											self.assertTrue(encoded_audios.ndim == 4  )
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size  )
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length  )
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels  )

											# Test 2-D numpy arrays are batched.
											_SCREAMING_SNAKE_CASE :       Any     		=      [floats_list((1, x)  )[0] for x in (800, 800, 800)]
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      np.asarray(__snake_case  )
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      feature_extractor(__snake_case    ,					return_tensors="""np"""    ,					sampling_rate=4_4100  ).audio_values
											self.assertTrue(encoded_audios.ndim == 4  )
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size  )
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length  )
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels  )






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

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






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[str]     		=      self._load_datasamples(1  )
											_SCREAMING_SNAKE_CASE :       Any     		=      TvltFeatureExtractor()
											_SCREAMING_SNAKE_CASE :       Tuple     		=      feature_extractor(__snake_case    ,					return_tensors="""pt"""  ).audio_values

											self.assertEquals(audio_values.shape    ,					(1, 1, 192, 128)  )

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]]  )
											self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]    ,					__snake_case    ,					atol=1e-4  )  )








'''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 MobileNetVaImageProcessor





class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							def __init__(    self    ,					__snake_case    ,					__snake_case=7    ,					__snake_case=3    ,					__snake_case=18    ,					__snake_case=30    ,					__snake_case=400    ,					__snake_case=True    ,					__snake_case=None    ,					__snake_case=True    ,					__snake_case=None    ,					):
											_SCREAMING_SNAKE_CASE :       Any     		=      size if size is not None else {"""shortest_edge""": 20}
											_SCREAMING_SNAKE_CASE :       int     		=      crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
											_SCREAMING_SNAKE_CASE :       Dict     		=      parent
											_SCREAMING_SNAKE_CASE :       Any     		=      batch_size
											_SCREAMING_SNAKE_CASE :       Any     		=      num_channels
											_SCREAMING_SNAKE_CASE :       Dict     		=      image_size
											_SCREAMING_SNAKE_CASE :       Any     		=      min_resolution
											_SCREAMING_SNAKE_CASE :       Any     		=      max_resolution
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      do_resize
											_SCREAMING_SNAKE_CASE :       Tuple     		=      size
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      do_center_crop
											_SCREAMING_SNAKE_CASE :       str     		=      crop_size






							def UpperCAmelCase_	(    self  ):
											return {
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "do_center_crop": self.do_center_crop,
											    "crop_size": self.crop_size,
											}





@require_torch
@require_vision
class     lowercase__  (						_snake_case     ,     unittest.TestCase					):





							'''simple docstring'''


							A_							:      Union[str, Any]      	=  MobileNetVaImageProcessor if is_vision_available() else None






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       int     		=      MobileNetVaImageProcessingTester(self  )






							@property
							def UpperCAmelCase_	(    self  ):
											return self.image_processor_tester.prepare_image_processor_dict()






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       str     		=      self.image_processing_class(**self.image_processor_dict  )
											self.assertTrue(hasattr(__snake_case    ,					"""do_resize"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""size"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""do_center_crop"""  )  )
											self.assertTrue(hasattr(__snake_case    ,					"""crop_size"""  )  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       str     		=      self.image_processing_class.from_dict(self.image_processor_dict  )
											self.assertEqual(image_processor.size    ,					{"""shortest_edge""": 20}  )
											self.assertEqual(image_processor.crop_size    ,					{"""height""": 18, """width""": 18}  )

											_SCREAMING_SNAKE_CASE :       Dict     		=      self.image_processing_class.from_dict(self.image_processor_dict    ,					size=42    ,					crop_size=84  )
											self.assertEqual(image_processor.size    ,					{"""shortest_edge""": 42}  )
											self.assertEqual(image_processor.crop_size    ,					{"""height""": 84, """width""": 84}  )






							def UpperCAmelCase_	(    self  ):
											pass






							def UpperCAmelCase_	(    self  ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE :       str     		=      self.image_processing_class(**self.image_processor_dict  )
											# create random PIL images
											_SCREAMING_SNAKE_CASE :       List[str]     		=      prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=__snake_case  )
											for image in image_inputs:
															self.assertIsInstance(__snake_case    ,					Image.Image  )

											# Test not batched input
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      image_processing(image_inputs[0]    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)

											# Test batched
											_SCREAMING_SNAKE_CASE :       int     		=      image_processing(__snake_case    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)






							def UpperCAmelCase_	(    self  ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      self.image_processing_class(**self.image_processor_dict  )
											# create random numpy tensors
											_SCREAMING_SNAKE_CASE :       Tuple     		=      prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=__snake_case    ,					numpify=__snake_case  )
											for image in image_inputs:
															self.assertIsInstance(__snake_case    ,					np.ndarray  )

											# Test not batched input
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      image_processing(image_inputs[0]    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)

											# Test batched
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      image_processing(__snake_case    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)






							def UpperCAmelCase_	(    self  ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.image_processing_class(**self.image_processor_dict  )
											# create random PyTorch tensors
											_SCREAMING_SNAKE_CASE :       str     		=      prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=__snake_case    ,					torchify=__snake_case  )
											for image in image_inputs:
															self.assertIsInstance(__snake_case    ,					torch.Tensor  )

											# Test not batched input
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      image_processing(image_inputs[0]    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)

											# Test batched
											_SCREAMING_SNAKE_CASE :       str     		=      image_processing(__snake_case    ,					return_tensors="""pt"""  ).pixel_values
											self.assertEqual(
											    encoded_images.shape    ,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )    ,					)




