
'''simple docstring'''







import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


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

if is_torch_available():
					import torch







def        SCREAMING_SNAKE_CASE__			(     __A    ,       __A=1.0    ,       __A=None    ,       __A=None			) ->			int:
	if rng is None:
		_snake_case     		=	global_rng

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

	return values


class 		__UpperCAmelCase   (      unittest.TestCase		):


							def __init__( self					,     lowerCAmelCase_					,     lowerCAmelCase_=7					,     lowerCAmelCase_=4_00					,     lowerCAmelCase_=20_00					,     lowerCAmelCase_=1					,     lowerCAmelCase_=0.0					,     lowerCAmelCase_=1_60_00					,     lowerCAmelCase_=True					,     lowerCAmelCase_=True					,     ):





								"""simple docstring"""






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


							def 				lowerCamelCase       ( self						):





								"""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 				lowerCamelCase       ( self					,     lowerCAmelCase_=False					,     lowerCAmelCase_=False						):





								"""simple docstring"""






								def _flatten(lowerCAmelCase_						):
									return list(itertools.chain(*lowerCAmelCase_						)						)

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

								return speech_inputs


@require_torch
@require_torchaudio
class 		__UpperCAmelCase   (      _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= ASTFeatureExtractor


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	ASTFeatureExtractionTester(self						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
								# create three inputs of length 800, 1000, and 1200
								_snake_case     		=	[floats_list((1, x)						)[0] for x in range(8_00					,     14_00					,     2_00						)]
								_snake_case     		=	[np.asarray(lowerCAmelCase_						) for speech_input in speech_inputs]

								# Test not batched input
								_snake_case     		=	feat_extract(speech_inputs[0]					,     return_tensors='np'						).input_values
								_snake_case     		=	feat_extract(np_speech_inputs[0]					,     return_tensors='np'						).input_values
								self.assertTrue(np.allclose(lowerCAmelCase_					,     lowerCAmelCase_					,     atol=1E-3						)						)

								# Test batched
								_snake_case     		=	feat_extract(lowerCAmelCase_					,     padding=lowerCAmelCase_					,     return_tensors='np'						).input_values
								_snake_case     		=	feat_extract(lowerCAmelCase_					,     padding=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.
								_snake_case     		=	[floats_list((1, x)						)[0] for x in (8_00, 8_00, 8_00)]
								_snake_case     		=	np.asarray(lowerCAmelCase_						)
								_snake_case     		=	feat_extract(lowerCAmelCase_					,     return_tensors='np'						).input_values
								_snake_case     		=	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						)						)


							@require_torch
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								import torch

								_snake_case     		=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
								_snake_case     		=	np.random.rand(1_00						).astype(np.floataa						)
								_snake_case     		=	np_speech_inputs.tolist()

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


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_						):





								"""simple docstring"""






								from datasets import load_dataset

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

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




							@require_torch
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	torch.tensor(
								    [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
								     -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
								     -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
								     -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]						)
								# fmt: on

								_snake_case     		=	self._load_datasamples(1						)
								_snake_case     		=	ASTFeatureExtractor()
								_snake_case     		=	feature_extractor(lowerCAmelCase_					,     return_tensors='pt'						).input_values
								self.assertEquals(input_values.shape					,     (1, 10_24, 1_28)						)
								self.assertTrue(torch.allclose(input_values[0, 0, :30]					,     lowerCAmelCase_					,     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 GLPNImageProcessor


class 		__UpperCAmelCase   (      unittest.TestCase		):


							def __init__( self					,     lowerCAmelCase_					,     lowerCAmelCase_=7					,     lowerCAmelCase_=3					,     lowerCAmelCase_=18					,     lowerCAmelCase_=30					,     lowerCAmelCase_=4_00					,     lowerCAmelCase_=True					,     lowerCAmelCase_=32					,     lowerCAmelCase_=True					,     ):





								"""simple docstring"""






								_snake_case     		=	parent
								_snake_case     		=	batch_size
								_snake_case     		=	num_channels
								_snake_case     		=	image_size
								_snake_case     		=	min_resolution
								_snake_case     		=	max_resolution
								_snake_case     		=	do_resize
								_snake_case     		=	size_divisor
								_snake_case     		=	do_rescale




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return {
								    "do_resize": self.do_resize,
								    "size_divisor": self.size_divisor,
								    "do_rescale": self.do_rescale,
								}


@require_torch
@require_vision
class 		__UpperCAmelCase   (      _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= GLPNImageProcessor if is_vision_available() else None


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	GLPNImageProcessingTester(self						)


							@property
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return self.image_processor_tester.prepare_image_processor_dict()


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.image_processing_class(**self.image_processor_dict						)
								self.assertTrue(hasattr(lowerCAmelCase_					,     'do_resize'						)						)
								self.assertTrue(hasattr(lowerCAmelCase_					,     'size_divisor'						)						)
								self.assertTrue(hasattr(lowerCAmelCase_					,     'resample'						)						)
								self.assertTrue(hasattr(lowerCAmelCase_					,     'do_rescale'						)						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.image_processing_class(**self.image_processor_dict						)
								# create random PIL images
								_snake_case     		=	prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase_						)
								for image in image_inputs:
									self.assertIsInstance(lowerCAmelCase_					,     Image.Image						)

								# Test not batched input (GLPNImageProcessor doesn't support batching)
								_snake_case     		=	image_processing(image_inputs[0]					,     return_tensors='pt'						).pixel_values
								self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
								self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.image_processing_class(**self.image_processor_dict						)
								# create random numpy tensors
								_snake_case     		=	prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase_					,     numpify=lowerCAmelCase_						)
								for image in image_inputs:
									self.assertIsInstance(lowerCAmelCase_					,     np.ndarray						)

								# Test not batched input (GLPNImageProcessor doesn't support batching)
								_snake_case     		=	image_processing(image_inputs[0]					,     return_tensors='pt'						).pixel_values
								self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
								self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.image_processing_class(**self.image_processor_dict						)
								# create random PyTorch tensors
								_snake_case     		=	prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase_					,     torchify=lowerCAmelCase_						)
								for image in image_inputs:
									self.assertIsInstance(lowerCAmelCase_					,     torch.Tensor						)

								# Test not batched input (GLPNImageProcessor doesn't support batching)
								_snake_case     		=	image_processing(image_inputs[0]					,     return_tensors='pt'						).pixel_values
								self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
								self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)

