
'''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 					a_					(unittest.TestCase      ):
       __lowerCAmelCase      :			Tuple      = EsmTokenizer




       def 							__UpperCamelCase    (       self					):
         super().setUp()
         _lowerCAmelCase    :					Tuple							=   tempfile.mkdtemp()
         # fmt: off
         _lowerCAmelCase    :					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
         _lowerCAmelCase    :					List[str]							=   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 							__UpperCamelCase    (       self				,				**snake_case_					):
         return [self.get_tokenizer(**snake_case_					)]




       def 							__UpperCamelCase    (       self				,				**snake_case_					):
         return self.tokenizer_class.from_pretrained(self.tmpdirname				,				**snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[str]							=   self.tokenizer_class(self.vocab_file					)

         _lowerCAmelCase    :					List[str]							=   tokenizer.tokenize("""LAGVS"""					)
         self.assertListEqual(snake_case_				,				["""L""", """A""", """G""", """V""", """S"""]					)
         self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_					)				,				[4, 5, 6, 7, 8]					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Tuple							=   self.tokenizer_class(self.vocab_file					)

         _lowerCAmelCase    :					Any							=   """LAGVS"""
         self.assertListEqual(tokenizer.encode(snake_case_					)				,				[0, 4, 5, 6, 7, 8, 2]					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Union[str, Any]							=   self.tokenizer_class(self.vocab_file					)

         _lowerCAmelCase    :					List[Any]							=   ["""LAGVS""", """WCB"""]
         _lowerCAmelCase    :					Dict							=   tokenizer(snake_case_				,				padding=snake_case_					)["""input_ids"""]

         self.assertListEqual(snake_case_				,				[[0, 4, 5, 6, 7, 8, 2], [0, 2_2, 2_3, 2_5, 2]]					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   self.tokenizer_class(self.vocab_file					)

         _lowerCAmelCase    :					Dict							=   ["""LAGVS""", """WCB"""]
         _lowerCAmelCase    :					str							=   tokenizer(snake_case_				,				padding=snake_case_					)["""input_ids"""]

         self.assertListEqual(snake_case_				,				[[0, 4, 5, 6, 7, 8, 2], [0, 2_2, 2_3, 2_5, 2, 1, 1]]					)







       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Union[str, Any]							=   self.get_tokenizers(fast=snake_case_					)
         for tokenizer in tokenizers:
           with self.subTest(f'{tokenizer.__class__.__name__}'					):
             _lowerCAmelCase    :					int							=   """<unk>"""
             _lowerCAmelCase    :					List[str]							=   """<mask>"""

             _lowerCAmelCase    :					Dict							=   tokenizer.tokenize(snake_case_					)
             _lowerCAmelCase    :					Optional[int]							=   tokenizer.tokenize(snake_case_					)

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




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


class 					a_					(unittest.TestCase      ):




       def __init__(       self				,				snake_case_				,				snake_case_=7				,				snake_case_=3				,				snake_case_=1_8				,				snake_case_=3_0				,				snake_case_=4_0_0				,				snake_case_=True				,				snake_case_=None				,				snake_case_=0.875				,				snake_case_=True				,				snake_case_=[0.5, 0.5, 0.5]				,				snake_case_=[0.5, 0.5, 0.5]				,				):
         _lowerCAmelCase    :					List[str]							=   size if size is not None else {"""shortest_edge""": 2_0}
         _lowerCAmelCase    :					Tuple							=   parent
         _lowerCAmelCase    :					List[str]							=   batch_size
         _lowerCAmelCase    :					List[str]							=   num_channels
         _lowerCAmelCase    :					Tuple							=   image_size
         _lowerCAmelCase    :					Union[str, Any]							=   min_resolution
         _lowerCAmelCase    :					Tuple							=   max_resolution
         _lowerCAmelCase    :					List[Any]							=   do_resize
         _lowerCAmelCase    :					Dict							=   size
         _lowerCAmelCase    :					str							=   crop_pct
         _lowerCAmelCase    :					List[str]							=   do_normalize
         _lowerCAmelCase    :					Any							=   image_mean
         _lowerCAmelCase    :					Any							=   image_std







       def 							__UpperCamelCase    (       self					):
         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_pct": self.crop_pct,
         }



@require_torch
@require_vision
class 					a_					(_a							,     unittest.TestCase      ):
       __lowerCAmelCase      :			Dict      = ConvNextImageProcessor if is_vision_available() else None




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					int							=   ConvNextImageProcessingTester(self					)




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




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   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_				,				"""crop_pct"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""do_normalize"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""image_mean"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""image_std"""					)					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Any							=   self.image_processing_class.from_dict(self.image_processor_dict					)
         self.assertEqual(image_processor.size				,				{"""shortest_edge""": 2_0}					)

         _lowerCAmelCase    :					List[Any]							=   self.image_processing_class.from_dict(self.image_processor_dict				,				size=4_2					)
         self.assertEqual(image_processor.size				,				{"""shortest_edge""": 4_2}					)




       def 							__UpperCamelCase    (       self					):
         pass




       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					str							=   self.image_processing_class(**self.image_processor_dict					)
         # create random PIL images
         _lowerCAmelCase    :					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
         _lowerCAmelCase    :					Optional[int]							=   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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)

         # Test batched
         _lowerCAmelCase    :					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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)




       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					int							=   self.image_processing_class(**self.image_processor_dict					)
         # create random numpy tensors
         _lowerCAmelCase    :					Any							=   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
         _lowerCAmelCase    :					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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)

         # Test batched
         _lowerCAmelCase    :					List[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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)







       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					Tuple							=   self.image_processing_class(**self.image_processor_dict					)
         # create random PyTorch tensors
         _lowerCAmelCase    :					List[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
         _lowerCAmelCase    :					List[str]							=   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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)

         # Test batched
         _lowerCAmelCase    :					Optional[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.size["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)


