




import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin







class     __lowerCAmelCase (       __magic_name__			,				unittest.TestCase ):

			"""simple docstring"""







			snake_case_   		=       CTRLTokenizer
			snake_case_   		=       False
			snake_case_   		=       False


			def       lowercase_    (      self							)    ->      Any:



							'''simple docstring'''
							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							__lowerCamelCase		       =    ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>']
							__lowerCamelCase		       =    dict(zip(lowerCamelCase__			,						range(len(lowerCamelCase__							)							)							)							)
							__lowerCamelCase		       =    ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', '']
							__lowerCamelCase		       =    {'unk_token': '<unk>'}

							__lowerCamelCase		       =    os.path.join(self.tmpdirname			,						VOCAB_FILES_NAMES['vocab_file']							)
							__lowerCamelCase		       =    os.path.join(self.tmpdirname			,						VOCAB_FILES_NAMES['merges_file']							)
							with open(self.vocab_file			,						'w'			,						encoding='utf-8'							) as fp:
											fp.write(json.dumps(lowerCamelCase__							) + '\n'							)
							with open(self.merges_file			,						'w'			,						encoding='utf-8'							) as fp:
											fp.write('\n'.join(lowerCamelCase__							)							)


			def       lowercase_    (      self			,						**lowerCamelCase__							)    ->      Tuple:



							'''simple docstring'''
							kwargs.update(self.special_tokens_map							)
							return CTRLTokenizer.from_pretrained(self.tmpdirname			,						**lowerCamelCase__							)


			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      int:



							'''simple docstring'''
							__lowerCamelCase		       =    'adapt react readapt apt'
							__lowerCamelCase		       =    'adapt react readapt apt'
							return input_text, output_text






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



							'''simple docstring'''
							__lowerCamelCase		       =    CTRLTokenizer(self.vocab_file			,						self.merges_file			,						**self.special_tokens_map							)
							__lowerCamelCase		       =    'adapt react readapt apt'
							__lowerCamelCase		       =    'adapt re@@ a@@ c@@ t re@@ adapt apt'.split()
							__lowerCamelCase		       =    tokenizer.tokenize(lowerCamelCase__							)
							self.assertListEqual(lowerCamelCase__			,						lowerCamelCase__							)

							__lowerCamelCase		       =    tokens + [tokenizer.unk_token]

							__lowerCamelCase		       =    [0, 1, 2, 4, 5, 1, 0, 3, 6]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__							)			,						lowerCamelCase__							)









import numpy as np
from PIL import Image



def _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      if arr.shape[0] != arr.shape[1]:
            raise ValueError('''The input array is not a square matrix''' )
      __A          =				0
      __A          =				0
      __A          =				0
      __A          =				0

      # compute the shape of the output matrix
      __A          =				(arr.shape[0] - size) // stride + 1
      # initialize the output matrix with zeros of shape maxpool_shape
      __A          =				np.zeros((maxpool_shape, maxpool_shape) )

      while i < arr.shape[0]:
            if i + size > arr.shape[0]:
                  # if the end of the matrix is reached, break
                  break
            while j < arr.shape[1]:
                  # if the end of the matrix is reached, break
                  if j + size > arr.shape[1]:
                        break
                  # compute the maximum of the pooling matrix
                  __A          =				np.max(arr[i : i + size, j : j + size] )
                  # shift the pooling matrix by stride of column pixels
                  j += stride
                  mat_j += 1

            # shift the pooling matrix by stride of row pixels
            i += stride
            mat_i += 1

            # reset the column index to 0
            __A          =				0
            __A          =				0

      return updated_arr



def _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      if arr.shape[0] != arr.shape[1]:
            raise ValueError('''The input array is not a square matrix''' )
      __A          =				0
      __A          =				0
      __A          =				0
      __A          =				0

      # compute the shape of the output matrix
      __A          =				(arr.shape[0] - size) // stride + 1
      # initialize the output matrix with zeros of shape avgpool_shape
      __A          =				np.zeros((avgpool_shape, avgpool_shape) )

      while i < arr.shape[0]:
            # if the end of the matrix is reached, break
            if i + size > arr.shape[0]:
                  break
            while j < arr.shape[1]:
                  # if the end of the matrix is reached, break
                  if j + size > arr.shape[1]:
                        break
                  # compute the average of the pooling matrix
                  __A          =				int(np.average(arr[i : i + size, j : j + size] ) )
                  # shift the pooling matrix by stride of column pixels
                  j += stride
                  mat_j += 1

            # shift the pooling matrix by stride of row pixels
            i += stride
            mat_i += 1
            # reset the column index to 0
            __A          =				0
            __A          =				0

      return updated_arr


# Main Function
if __name__ == "__main__":
       from doctest import testmod

       testmod(name='avgpooling', verbose=True)

       # Loading the image
       snake_case__					:							Optional[Any]													=     Image.open('path_to_image')

       # Converting the image to numpy array and maxpooling, displaying the result
       # Ensure that the image is a square matrix

       Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

       # Converting the image to numpy array and averagepooling, displaying the result
       # Ensure that the image is a square matrix

       Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()





