



'''simple docstring'''

import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor


class 	a__     (				unittest.TestCase	):


	def __init__(self : int,					__UpperCAmelCase : Any,					__UpperCAmelCase : Dict=7,					__UpperCAmelCase : List[str]=3,					__UpperCAmelCase : str=18,					__UpperCAmelCase : Optional[int]=30,					__UpperCAmelCase : str=400,					__UpperCAmelCase : List[str]=True,					__UpperCAmelCase : Optional[Any]=None,					__UpperCAmelCase : Dict=True,					__UpperCAmelCase : List[Any]=False,					__UpperCAmelCase : str=True,					__UpperCAmelCase : List[Any]=True,					__UpperCAmelCase : List[str]=[0.5, 0.5, 0.5],					__UpperCAmelCase : int=[0.5, 0.5, 0.5],					)     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= parent
					SCREAMING_SNAKE_CASE  :						Any      					= batch_size
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= num_channels
					SCREAMING_SNAKE_CASE  :						List[Any]      					= image_size
					SCREAMING_SNAKE_CASE  :						List[Any]      					= min_resolution
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= max_resolution
					SCREAMING_SNAKE_CASE  :						Dict      					= do_resize
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= size if size is not None else {'''height''': 18, '''width''': 20}
					SCREAMING_SNAKE_CASE  :						List[str]      					= do_thumbnail
					SCREAMING_SNAKE_CASE  :						str      					= do_align_axis
					SCREAMING_SNAKE_CASE  :						int      					= do_pad
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= do_normalize
					SCREAMING_SNAKE_CASE  :						Any      					= image_mean
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= image_std






	def 				lowercase__  (self : Dict      )     ->			List[str]:

					"""simple docstring"""

					return {
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_thumbnail": self.do_thumbnail,
					    "do_align_long_axis": self.do_align_axis,
					    "do_pad": self.do_pad,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					}



@require_torch
@require_vision
class 	a__     (				_lowercase,			unittest.TestCase	):
	__magic_name__		:    Tuple	    =     DonutImageProcessor if is_vision_available() else None


	def 				lowercase__  (self : Union[str, Any]      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= DonutImageProcessingTester(self      )


	@property
	def 				lowercase__  (self : Optional[Any]      )     ->			Any:

					"""simple docstring"""

					return self.image_processor_tester.prepare_image_processor_dict()


	def 				lowercase__  (self : List[str]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= self.image_processing_class(**self.image_processor_dict      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''do_resize'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''size'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''do_thumbnail'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''do_align_long_axis'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''do_pad'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''do_normalize'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''image_mean'''      )      )
					self.assertTrue(hasattr(__UpperCAmelCase,					'''image_std'''      )      )


	def 				lowercase__  (self : str      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.image_processing_class.from_dict(self.image_processor_dict      )
					self.assertEqual(image_processor.size,					{'''height''': 18, '''width''': 20}      )

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.image_processing_class.from_dict(self.image_processor_dict,					size=42      )
					self.assertEqual(image_processor.size,					{'''height''': 42, '''width''': 42}      )

					# Previous config had dimensions in (width, height) order
					SCREAMING_SNAKE_CASE  :						Any      					= self.image_processing_class.from_dict(self.image_processor_dict,					size=(42, 84)      )
					self.assertEqual(image_processor.size,					{'''height''': 84, '''width''': 42}      )


	def 				lowercase__  (self : Dict      )     ->			Dict:

					"""simple docstring"""

					pass


	@is_flaky()
	def 				lowercase__  (self : List[str]      )     ->			Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.image_processing_class(**self.image_processor_dict      )
					# create random PIL images
					SCREAMING_SNAKE_CASE  :						List[str]      					= prepare_image_inputs(self.image_processor_tester,					equal_resolution=__UpperCAmelCase      )
					for image in image_inputs:
									self.assertIsInstance(__UpperCAmelCase,					Image.Image      )

					# Test not batched input
					SCREAMING_SNAKE_CASE  :						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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)

					# Test batched
					SCREAMING_SNAKE_CASE  :						str      					= image_processing(__UpperCAmelCase,					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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)


	@is_flaky()
	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.image_processing_class(**self.image_processor_dict      )
					# create random numpy tensors
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= prepare_image_inputs(self.image_processor_tester,					equal_resolution=__UpperCAmelCase,					numpify=__UpperCAmelCase      )
					for image in image_inputs:
									self.assertIsInstance(__UpperCAmelCase,					np.ndarray      )

					# Test not batched input
					SCREAMING_SNAKE_CASE  :						List[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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)

					# Test batched
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= image_processing(__UpperCAmelCase,					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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)






	@is_flaky()
	def 				lowercase__  (self : Union[str, Any]      )     ->			Dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.image_processing_class(**self.image_processor_dict      )
					# create random PyTorch tensors
					SCREAMING_SNAKE_CASE  :						Any      					= prepare_image_inputs(self.image_processor_tester,					equal_resolution=__UpperCAmelCase,					torchify=__UpperCAmelCase      )
					for image in image_inputs:
									self.assertIsInstance(__UpperCAmelCase,					torch.Tensor      )

					# Test not batched input
					SCREAMING_SNAKE_CASE  :						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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)

					# Test batched
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= image_processing(__UpperCAmelCase,					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['''height'''],
					        self.image_processor_tester.size['''width'''],
					    ),					)









'''simple docstring'''

import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate






def       __lowercase  (_SCREAMING_SNAKE_CASE      :List[Any]       ):
				return x + 2




class 	a__     (				unittest.TestCase	):


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Dict      					= {}
					SCREAMING_SNAKE_CASE  :						Any      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )

					SCREAMING_SNAKE_CASE  :						str      					= '''x = y'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''y''': 5}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 5, '''y''': 5}      )


	def 				lowercase__  (self : Optional[int]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= '''y = add_two(x)'''
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )

					# Won't work without the tool
					with CaptureStdout() as out:
									SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result is None
					assert "tried to execute add_two" in out.out


	def 				lowercase__  (self : Any      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Any      					= {}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )


	def 				lowercase__  (self : Optional[int]      )     ->			Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )


	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3\ny = 5'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )


	def 				lowercase__  (self : Tuple      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''text = f\'This is x: {x}.\''''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Dict      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == "This is x: 3."
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''text''': '''This is x: 3.'''}      )


	def 				lowercase__  (self : int      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''if x <= 3:\n    y = 2\nelse:\n    y = 5'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 2}      )

					SCREAMING_SNAKE_CASE  :						Any      					= {'''x''': 8}
					SCREAMING_SNAKE_CASE  :						int      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 8, '''y''': 5}      )


	def 				lowercase__  (self : Any      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''test_list = [x, add_two(x)]'''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertListEqual(__UpperCAmelCase,					[3, 5]      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= '''y = x'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 3}      )


	def 				lowercase__  (self : Optional[Any]      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_list = [x, add_two(x)]\ntest_list[1]'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )






	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 0\nfor i in range(3):\n    x = i'''
					SCREAMING_SNAKE_CASE  :						List[Any]      					= {}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{'''range''': range},					state=__UpperCAmelCase      )
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 2, '''i''': 2}      )




