






'''simple docstring'''





import json
import os
import random
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
						from PIL import Image

						from transformers import FlavaImageProcessor, FlavaProcessor
						from transformers.models.flava.image_processing_flava import (
						    FLAVA_CODEBOOK_MEAN,
						    FLAVA_CODEBOOK_STD,
						    FLAVA_IMAGE_MEAN,
						    FLAVA_IMAGE_STD,
						)


@require_vision
class 				snake_case__     (			unittest.TestCase):




					def 						A     (       self       :						Optional[Any]     )  ->    List[str]:
								UpperCAmelCase_     :   List[Any]       =   tempfile.mkdtemp()

								# fmt: off
								UpperCAmelCase_     :   Union[str, Any]       =   ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''']
								# fmt: on
								UpperCAmelCase_     :   str       =   os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES['''vocab_file''']     )

								with open(self.vocab_file							,				'''w'''							,				encoding='''utf-8'''     ) as fp:
											fp.write(''''''.join([x + '''\n''' for x in vocab_tokens]     )     )

								UpperCAmelCase_     :   List[Any]       =   {
								    '''image_mean''': FLAVA_IMAGE_MEAN,
								    '''image_std''': FLAVA_IMAGE_STD,
								    '''do_normalize''': True,
								    '''do_resize''': True,
								    '''size''': 2_24,
								    '''do_center_crop''': True,
								    '''crop_size''': 2_24,
								    '''input_size_patches''': 14,
								    '''total_mask_patches''': 75,
								    '''mask_group_max_patches''': None,
								    '''mask_group_min_patches''': 16,
								    '''mask_group_min_aspect_ratio''': 0.3,
								    '''mask_group_max_aspect_ratio''': None,
								    '''codebook_do_resize''': True,
								    '''codebook_size''': 1_12,
								    '''codebook_do_center_crop''': True,
								    '''codebook_crop_size''': 1_12,
								    '''codebook_do_map_pixels''': True,
								    '''codebook_do_normalize''': True,
								    '''codebook_image_mean''': FLAVA_CODEBOOK_MEAN,
								    '''codebook_image_std''': FLAVA_CODEBOOK_STD,
								}

								UpperCAmelCase_     :   Union[str, Any]       =   os.path.join(self.tmpdirname							,				__lowercase     )
								with open(self.image_processor_file							,				'''w'''							,				encoding='''utf-8'''     ) as fp:
											json.dump(__lowercase							,				__lowercase     )




					def 						A     (       self       :						Optional[Any]							,				**_A       :						int     )  ->    int:
								return BertTokenizer.from_pretrained(self.tmpdirname							,				**__lowercase     )




					def 						A     (       self       :						Any							,				**_A       :						List[str]     )  ->    Optional[Any]:
								return BertTokenizerFast.from_pretrained(self.tmpdirname							,				**__lowercase     )




					def 						A     (       self       :						List[Any]							,				**_A       :						int     )  ->    Dict:
								return FlavaImageProcessor.from_pretrained(self.tmpdirname							,				**__lowercase     )




					def 						A     (       self       :						List[str]     )  ->    Tuple:
								shutil.rmtree(self.tmpdirname     )




					def 						A     (       self       :						Tuple     )  ->    List[Any]:
								UpperCAmelCase_     :   Optional[int]       =   [np.random.randint(2_55							,				size=(3, 30, 4_00)							,				dtype=np.uinta     )]

								UpperCAmelCase_     :   List[str]       =   [Image.fromarray(np.moveaxis(__lowercase							,				0							,				-1     )     ) for x in image_inputs]

								return image_inputs




					def 						A     (       self       :						Union[str, Any]     )  ->    Union[str, Any]:
								UpperCAmelCase_     :   Dict       =   self.get_tokenizer()
								UpperCAmelCase_     :   int       =   self.get_rust_tokenizer()
								UpperCAmelCase_     :   Optional[Any]       =   self.get_image_processor()

								UpperCAmelCase_     :   Any       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )
								processor_slow.save_pretrained(self.tmpdirname     )
								UpperCAmelCase_     :   Optional[int]       =   FlavaProcessor.from_pretrained(self.tmpdirname							,				use_fast=__lowercase     )

								UpperCAmelCase_     :   Any       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )
								processor_fast.save_pretrained(self.tmpdirname     )
								UpperCAmelCase_     :   str       =   FlavaProcessor.from_pretrained(self.tmpdirname     )

								self.assertEqual(processor_slow.tokenizer.get_vocab()							,				tokenizer_slow.get_vocab()     )
								self.assertEqual(processor_fast.tokenizer.get_vocab()							,				tokenizer_fast.get_vocab()     )
								self.assertEqual(tokenizer_slow.get_vocab()							,				tokenizer_fast.get_vocab()     )
								self.assertIsInstance(processor_slow.tokenizer							,				__lowercase     )
								self.assertIsInstance(processor_fast.tokenizer							,				__lowercase     )

								self.assertEqual(processor_slow.image_processor.to_json_string()							,				image_processor.to_json_string()     )
								self.assertEqual(processor_fast.image_processor.to_json_string()							,				image_processor.to_json_string()     )
								self.assertIsInstance(processor_slow.image_processor							,				__lowercase     )
								self.assertIsInstance(processor_fast.image_processor							,				__lowercase     )




					def 						A     (       self       :						Optional[int]     )  ->    List[str]:
								UpperCAmelCase_     :   Optional[Any]       =   FlavaProcessor(tokenizer=self.get_tokenizer()							,				image_processor=self.get_image_processor()     )
								processor.save_pretrained(self.tmpdirname     )

								UpperCAmelCase_     :   List[Any]       =   self.get_tokenizer(bos_token='''(BOS)'''							,				eos_token='''(EOS)'''     )
								UpperCAmelCase_     :   Optional[int]       =   self.get_image_processor(do_normalize=__lowercase							,				padding_value=1.0     )

								UpperCAmelCase_     :   Dict       =   FlavaProcessor.from_pretrained(
								    self.tmpdirname							,				bos_token='''(BOS)'''							,				eos_token='''(EOS)'''							,				do_normalize=__lowercase							,				padding_value=1.0     )

								self.assertEqual(processor.tokenizer.get_vocab()							,				tokenizer_add_kwargs.get_vocab()     )
								self.assertIsInstance(processor.tokenizer							,				__lowercase     )

								self.assertEqual(processor.image_processor.to_json_string()							,				image_processor_add_kwargs.to_json_string()     )
								self.assertIsInstance(processor.image_processor							,				__lowercase     )




					def 						A     (       self       :						Any     )  ->    Tuple:
								UpperCAmelCase_     :   Optional[Any]       =   self.get_image_processor()
								UpperCAmelCase_     :   int       =   self.get_tokenizer()

								UpperCAmelCase_     :   List[Any]       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )

								UpperCAmelCase_     :   Optional[int]       =   self.prepare_image_inputs()

								UpperCAmelCase_     :   Tuple       =   image_processor(__lowercase							,				return_tensors='''np'''     )
								UpperCAmelCase_     :   Tuple       =   processor(images=__lowercase							,				return_tensors='''np'''     )

								for key in input_feat_extract.keys():
											self.assertAlmostEqual(input_feat_extract[key].sum()							,				input_processor[key].sum()							,				delta=1e-2     )

								# With rest of the args
								random.seed(12_34     )
								UpperCAmelCase_     :   Dict       =   image_processor(
								    __lowercase							,				return_image_mask=__lowercase							,				return_codebook_pixels=__lowercase							,				return_tensors='''np'''     )
								random.seed(12_34     )
								UpperCAmelCase_     :   Any       =   processor(
								    images=__lowercase							,				return_image_mask=__lowercase							,				return_codebook_pixels=__lowercase							,				return_tensors='''np'''     )

								for key in input_feat_extract.keys():
											self.assertAlmostEqual(input_feat_extract[key].sum()							,				input_processor[key].sum()							,				delta=1e-2     )




					def 						A     (       self       :						str     )  ->    str:
								UpperCAmelCase_     :   int       =   self.get_image_processor()
								UpperCAmelCase_     :   Tuple       =   self.get_tokenizer()

								UpperCAmelCase_     :   Union[str, Any]       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )

								UpperCAmelCase_     :   int       =   '''lower newer'''

								UpperCAmelCase_     :   Dict       =   processor(text=__lowercase     )

								UpperCAmelCase_     :   Tuple       =   tokenizer(__lowercase     )

								for key in encoded_tok.keys():
											self.assertListEqual(encoded_tok[key]							,				encoded_processor[key]     )




					def 						A     (       self       :						Any     )  ->    Optional[int]:
								UpperCAmelCase_     :   Union[str, Any]       =   self.get_image_processor()
								UpperCAmelCase_     :   Tuple       =   self.get_tokenizer()

								UpperCAmelCase_     :   Optional[Any]       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )

								UpperCAmelCase_     :   Optional[Any]       =   '''lower newer'''
								UpperCAmelCase_     :   Any       =   self.prepare_image_inputs()

								UpperCAmelCase_     :   int       =   processor(text=__lowercase							,				images=__lowercase     )

								self.assertListEqual(list(inputs.keys()     )							,				['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values''']     )

								# add extra args
								UpperCAmelCase_     :   Union[str, Any]       =   processor(text=__lowercase							,				images=__lowercase							,				return_codebook_pixels=__lowercase							,				return_image_mask=__lowercase     )

								self.assertListEqual(
								    list(inputs.keys()     )							,				[
								        '''input_ids''',
								        '''token_type_ids''',
								        '''attention_mask''',
								        '''pixel_values''',
								        '''codebook_pixel_values''',
								        '''bool_masked_pos''',
								    ]							,				)

								# test if it raises when no input is passed
								with pytest.raises(__lowercase     ):
											processor()




					def 						A     (       self       :						int     )  ->    Optional[int]:
								UpperCAmelCase_     :   Optional[Any]       =   self.get_image_processor()
								UpperCAmelCase_     :   List[Any]       =   self.get_tokenizer()

								UpperCAmelCase_     :   Tuple       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )

								UpperCAmelCase_     :   Dict       =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								UpperCAmelCase_     :   List[Any]       =   processor.batch_decode(__lowercase     )
								UpperCAmelCase_     :   str       =   tokenizer.batch_decode(__lowercase     )

								self.assertListEqual(__lowercase							,				__lowercase     )





					def 						A     (       self       :						Union[str, Any]     )  ->    Optional[Any]:
								UpperCAmelCase_     :   List[Any]       =   self.get_image_processor()
								UpperCAmelCase_     :   Any       =   self.get_tokenizer()

								UpperCAmelCase_     :   Tuple       =   FlavaProcessor(tokenizer=__lowercase							,				image_processor=__lowercase     )

								UpperCAmelCase_     :   Union[str, Any]       =   '''lower newer'''
								UpperCAmelCase_     :   List[Any]       =   self.prepare_image_inputs()

								UpperCAmelCase_     :   Tuple       =   processor(text=__lowercase							,				images=__lowercase     )

								self.assertListEqual(list(inputs.keys()     )							,				processor.model_input_names     )










'''simple docstring'''





from __future__ import annotations

from collections import deque
from enum import Enum
from math import atana, degrees
from sys import maxsize


class 				snake_case__     (			UpperCamelCase):
					a_       =			1
					a_       =			2
					a_       =			3




					def __repr__(       self       :						str     )  ->    Optional[int]:
								return F"{self.__class__.__name__}.{self.name}"






def __UpperCAmelCase (			A			:	tuple[int, int]	,   A			:	int	,   A			:	int						)  -> float:
			UpperCAmelCase_  ,     UpperCAmelCase_     :   Any       =   point
			return degrees(atana(y - miny	,   x - minx						)						)
def __UpperCAmelCase (			A			:	tuple[int, int]	,   A			:	tuple[int, int]	,   A			:	tuple[int, int]						)  -> Direction:
			UpperCAmelCase_  ,     UpperCAmelCase_     :   Tuple       =   starting
			UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[Any]       =   via
			UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[int]       =   target
			UpperCAmelCase_     :   Optional[int]       =   degrees(atana(ya - ya	,   xa - xa						)						)
			via_angle %= 3_6_0
			UpperCAmelCase_     :   Dict       =   degrees(atana(ya - ya	,   xa - xa						)						)
			target_angle %= 3_6_0
			# t-
			#  \ \
			#   \ v
			#    \|
			#     s
			# via_angle is always lower than target_angle, if direction is left.
			# If they are same, it means they are on a same line of convex hull.
			if target_angle > via_angle:
						return Direction.left
			elif target_angle == via_angle:
						return Direction.straight
			else:
						return Direction.right







def __UpperCAmelCase (			A			:	list[tuple[int, int]]						)  -> list[tuple[int, int]]:

			if len(A						) <= 2:
						# There is no convex hull
						raise ValueError('''graham_scan: argument must contain more than 3 points.'''						)
			if len(A						) == 3:
						return points
			# find the lowest and the most left point
			UpperCAmelCase_     :   List[str]       =   0
			UpperCAmelCase_  ,     UpperCAmelCase_     :   List[Any]       =   maxsize, maxsize
			for i, point in enumerate(A						):
						UpperCAmelCase_     :   str       =   point[0]
						UpperCAmelCase_     :   List[Any]       =   point[1]
						if y < miny:
									UpperCAmelCase_     :   int       =   y
									UpperCAmelCase_     :   Dict       =   x
									UpperCAmelCase_     :   Any       =   i
						if y == miny and x < minx:
									UpperCAmelCase_     :   str       =   x
									UpperCAmelCase_     :   Optional[int]       =   i

    # remove the lowest and the most left point from points for preparing for sort
			points.pop(A						)

			UpperCAmelCase_     :   Optional[Any]       =   sorted(A	,   key=lambda A						: angle_comparer(A	,   A	,   A						)						)
			# This insert actually costs complexity,
			# and you should instead add (minx, miny) into stack later.
			# I'm using insert just for easy understanding.
			sorted_points.insert(0	,   (minx, miny)						)

			UpperCAmelCase_     :   deque[tuple[int, int]]       =   deque()
			stack.append(sorted_points[0]						)
			stack.append(sorted_points[1]						)
			stack.append(sorted_points[2]						)
			# In any ways, the first 3 points line are towards left.
			# Because we sort them the angle from minx, miny.
			UpperCAmelCase_     :   Any       =   Direction.left

			for i in range(3	,   len(A						)						):
						while True:
									UpperCAmelCase_     :   Any       =   stack[-2]
									UpperCAmelCase_     :   List[Any]       =   stack[-1]
									UpperCAmelCase_     :   Any       =   sorted_points[i]
									UpperCAmelCase_     :   Tuple       =   check_direction(A	,   A	,   A						)

									if next_direction == Direction.left:
												UpperCAmelCase_     :   Optional[Any]       =   Direction.left
												break
									if next_direction == Direction.straight:
												if current_direction == Direction.left:
															# We keep current_direction as left.
															# Because if the straight line keeps as straight,
															# we want to know if this straight line is towards left.
															break
												elif current_direction == Direction.right:
															# If the straight line is towards right,
															# every previous points on those straigh line is not convex hull.
															stack.pop()
									if next_direction == Direction.right:
												stack.pop()
						stack.append(sorted_points[i]						)
			return list(A						)


