






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_video_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import VideoMAEImageProcessor




class 							a_ (						unittest.TestCase		):


							'''simple docstring'''






							def __init__(		self			,			lowercase_			,			lowercase_=7			,			lowercase_=3			,			lowercase_=1_0			,			lowercase_=1_8			,			lowercase_=3_0			,			lowercase_=4_0_0			,			lowercase_=True			,			lowercase_=None			,			lowercase_=True			,			lowercase_=[0.5, 0.5, 0.5]			,			lowercase_=[0.5, 0.5, 0.5]			,			lowercase_=None			,			)					->					Optional[int]:
														'''simple docstring'''

														lowerCAmelCase_										=     size if size is not None else {'''shortest_edge''': 1_8}
														lowerCAmelCase_										=     crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

														lowerCAmelCase_										=     parent
														lowerCAmelCase_										=     batch_size
														lowerCAmelCase_										=     num_channels
														lowerCAmelCase_										=     num_frames
														lowerCAmelCase_										=     image_size
														lowerCAmelCase_										=     min_resolution
														lowerCAmelCase_										=     max_resolution
														lowerCAmelCase_										=     do_resize
														lowerCAmelCase_										=     size
														lowerCAmelCase_										=     do_normalize
														lowerCAmelCase_										=     image_mean
														lowerCAmelCase_										=     image_std
														lowerCAmelCase_										=     crop_size






							def      _lowercase       (		self   )					->					Optional[Any]:
														'''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,
														    "crop_size": self.crop_size,
														}




@require_torch
@require_vision
class 							a_ (						__SCREAMING_SNAKE_CASE	,	unittest.TestCase		):


							'''simple docstring'''






							__a:	Any     =    VideoMAEImageProcessor if is_vision_available() else None

							def      _lowercase       (		self   )					->					Optional[int]:
														'''simple docstring'''

														lowerCAmelCase_										=     VideoMAEImageProcessingTester(self   )

							@property
							def      _lowercase       (		self   )					->					int:
														'''simple docstring'''

														return self.image_processor_tester.prepare_image_processor_dict()

							def      _lowercase       (		self   )					->					Tuple:
														'''simple docstring'''

														lowerCAmelCase_										=     self.image_processing_class(**self.image_processor_dict   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'image_mean'   )   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'image_std'   )   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'do_normalize'   )   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'do_resize'   )   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'do_center_crop'   )   )
														self.assertTrue(hasattr(UpperCamelCase__			,			'size'   )   )

							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     self.image_processing_class.from_dict(self.image_processor_dict   )
														self.assertEqual(image_processor.size			,			{'shortest_edge': 1_8}   )
														self.assertEqual(image_processor.crop_size			,			{'height': 1_8, 'width': 1_8}   )

														lowerCAmelCase_										=     self.image_processing_class.from_dict(self.image_processor_dict			,			size=4_2			,			crop_size=8_4   )
														self.assertEqual(image_processor.size			,			{'shortest_edge': 4_2}   )
														self.assertEqual(image_processor.crop_size			,			{'height': 8_4, 'width': 8_4}   )

							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														pass

							def      _lowercase       (		self   )					->					Tuple:
														'''simple docstring'''

														lowerCAmelCase_										=     self.image_processing_class(**self.image_processor_dict   )
														# create random PIL videos
														lowerCAmelCase_										=     prepare_video_inputs(self.image_processor_tester			,			equal_resolution=UpperCamelCase__   )
														for video in video_inputs:
																					self.assertIsInstance(UpperCamelCase__			,			UpperCamelCase__   )
																					self.assertIsInstance(video[0]			,			Image.Image   )

														# Test not batched input
														lowerCAmelCase_										=     image_processing(video_inputs[0]			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        1,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)

														# Test batched
														lowerCAmelCase_										=     image_processing(UpperCamelCase__			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)

							def      _lowercase       (		self   )					->					Union[str, Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     self.image_processing_class(**self.image_processor_dict   )
														# create random numpy tensors
														lowerCAmelCase_										=     prepare_video_inputs(self.image_processor_tester			,			equal_resolution=UpperCamelCase__			,			numpify=UpperCamelCase__   )
														for video in video_inputs:
																					self.assertIsInstance(UpperCamelCase__			,			UpperCamelCase__   )
																					self.assertIsInstance(video[0]			,			np.ndarray   )

														# Test not batched input
														lowerCAmelCase_										=     image_processing(video_inputs[0]			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        1,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)

														# Test batched
														lowerCAmelCase_										=     image_processing(UpperCamelCase__			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)






							def      _lowercase       (		self   )					->					List[Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     self.image_processing_class(**self.image_processor_dict   )
														# create random PyTorch tensors
														lowerCAmelCase_										=     prepare_video_inputs(self.image_processor_tester			,			equal_resolution=UpperCamelCase__			,			torchify=UpperCamelCase__   )
														for video in video_inputs:
																					self.assertIsInstance(UpperCamelCase__			,			UpperCamelCase__   )
																					self.assertIsInstance(video[0]			,			torch.Tensor   )

														# Test not batched input
														lowerCAmelCase_										=     image_processing(video_inputs[0]			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        1,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)

														# Test batched
														lowerCAmelCase_										=     image_processing(UpperCamelCase__			,			return_tensors='pt'   ).pixel_values
														self.assertEqual(
														    encoded_videos.shape			,			(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_frames,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size['height'],
														        self.image_processor_tester.crop_size['width'],
														    )			,			)





'''simple docstring'''
import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase






@require_tokenizers
class      snake_case__    (	unittest.TestCase ):







		"""simple docstring"""

		lowerCamelCase							=		EsmTokenizer



		def 	lowerCAmelCase							(	self							:       List[Any]	)		->       Any:






				"""simple docstring"""







				super().setUp()
				snake_case   :    Optional[int]			       =		tempfile.mkdtemp()
				# fmt: off
				snake_case   :    List[str]			       =		["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
				# fmt: on
				snake_case   :    List[Any]			       =		os.path.join(self.tmpdirname   ,     VOCAB_FILES_NAMES['''vocab_file''']	)
				with open(self.vocab_file   ,     '''w'''   ,     encoding='''utf-8'''	) as vocab_writer:
						vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]	)	)



		def 	lowerCAmelCase							(	self							:       str   ,     **UpperCamelCase__							:       Union[str, Any]	)		->       List[PreTrainedTokenizerBase]:






				"""simple docstring"""







				return [self.get_tokenizer(**UpperCamelCase__	)]



		def 	lowerCAmelCase							(	self							:       Union[str, Any]   ,     **UpperCamelCase__							:       Tuple	)		->       PreTrainedTokenizer:






				"""simple docstring"""







				return self.tokenizer_class.from_pretrained(self.tmpdirname   ,     **UpperCamelCase__	)



		def 	lowerCAmelCase							(	self							:       List[str]	)		->       Tuple:






				"""simple docstring"""







				snake_case   :    str			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    Optional[int]			       =		tokenizer.tokenize('''LAGVS'''	)
				self.assertListEqual(UpperCamelCase__   ,     ['''L''', '''A''', '''G''', '''V''', '''S''']	)
				self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__	)   ,     [4, 5, 6, 7, 8]	)



		def 	lowerCAmelCase							(	self							:       List[Any]	)		->       int:






				"""simple docstring"""







				snake_case   :    Dict			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    List[Any]			       =		'''LAGVS'''
				self.assertListEqual(tokenizer.encode(UpperCamelCase__	)   ,     [0, 4, 5, 6, 7, 8, 2]	)



		def 	lowerCAmelCase							(	self							:       List[str]	)		->       int:






				"""simple docstring"""







				snake_case   :    Union[str, Any]			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    int			       =		['''LAGVS''', '''WCB''']
				snake_case   :    List[str]			       =		tokenizer(UpperCamelCase__   ,     padding=UpperCamelCase__	)['''input_ids''']

				self.assertListEqual(UpperCamelCase__   ,     [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]	)



		def 	lowerCAmelCase							(	self							:       int	)		->       List[str]:






				"""simple docstring"""







				snake_case   :    Dict			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    List[str]			       =		['''LAGVS''', '''WCB''']
				snake_case   :    Any			       =		tokenizer(UpperCamelCase__   ,     padding=UpperCamelCase__	)['''input_ids''']

				self.assertListEqual(UpperCamelCase__   ,     [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]	)



		def 	lowerCAmelCase							(	self							:       Any	)		->       int:






				"""simple docstring"""







				snake_case   :    List[str]			       =		self.get_tokenizers(fast=UpperCamelCase__	)
				for tokenizer in tokenizers:
						with self.subTest(f'{tokenizer.__class__.__name__}'	):
								snake_case   :    int			       =		'''<unk>'''
								snake_case   :    int			       =		'''<mask>'''

								snake_case   :    Any			       =		tokenizer.tokenize(UpperCamelCase__	)
								snake_case   :    Any			       =		tokenizer.tokenize(UpperCamelCase__	)

								self.assertEqual(len(UpperCamelCase__	)   ,     1	)
								self.assertEqual(len(UpperCamelCase__	)   ,     1	)
								self.assertEqual(token_a[0]   ,     UpperCamelCase__	)
								self.assertEqual(token_a[0]   ,     UpperCamelCase__	)


