


"""simple docstring"""




import unittest

import numpy as np
import requests

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

	from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
	__UpperCamelCase						=      False

if is_vision_available():
	from PIL import Image

	from transformers import PixaStructImageProcessor
class 					lowerCAmelCase	(					unittest.TestCase    ):




		'''simple docstring'''







		def __init__(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=7						,       lowerCAmelCase__=3						,       lowerCAmelCase__=18						,       lowerCAmelCase__=30						,       lowerCAmelCase__=400						,       lowerCAmelCase__=None						,       lowerCAmelCase__=True						,       lowerCAmelCase__=True						,       lowerCAmelCase__=None						,       )     ->							Any:
								SCREAMING_SNAKE_CASE													=					size if size is not None else {'height': 20, 'width': 20}
								SCREAMING_SNAKE_CASE													=					parent
								SCREAMING_SNAKE_CASE													=					batch_size
								SCREAMING_SNAKE_CASE													=					num_channels
								SCREAMING_SNAKE_CASE													=					image_size
								SCREAMING_SNAKE_CASE													=					min_resolution
								SCREAMING_SNAKE_CASE													=					max_resolution
								SCREAMING_SNAKE_CASE													=					size
								SCREAMING_SNAKE_CASE													=					do_normalize
								SCREAMING_SNAKE_CASE													=					do_convert_rgb
								SCREAMING_SNAKE_CASE													=					[512, 1_024, 2_048, 4_096]
								SCREAMING_SNAKE_CASE													=					patch_size if patch_size is not None else {'height': 16, 'width': 16}







		def 							__A						(	self				)     ->							str:
								return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}







		def 							__A						(	self				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE													=					'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
								SCREAMING_SNAKE_CASE													=					Image.open(requests.get(lowerCAmelCase__						,       stream=lowerCAmelCase__				).raw				).convert('RGB'				)
								return raw_image





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11	,    reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""	,    )
@require_torch
@require_vision
class 					lowerCAmelCase	(					lowerCamelCase_	,    unittest.TestCase    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     Tuple								=     PixaStructImageProcessor if is_vision_available() else None







		def 							__A						(	self				)     ->							Any:
								SCREAMING_SNAKE_CASE													=					PixaStructImageProcessingTester(self				)







		@property
		def 							__A						(	self				)     ->							Tuple:
								return self.image_processor_tester.prepare_image_processor_dict()







		def 							__A						(	self				)     ->							Union[str, Any]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_normalize'				)				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_convert_rgb'				)				)







		def 							__A						(	self				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					self.image_processor_tester.prepare_dummy_image()

								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								SCREAMING_SNAKE_CASE													=					2_048

								SCREAMING_SNAKE_CASE													=					image_processor(lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				)
								self.assertTrue(torch.allclose(inputs.flattened_patches.mean()						,       torch.tensor(0.06_06				)						,       atol=1e-3						,       rtol=1e-3				)				)







		def 							__A						(	self				)     ->							Union[str, Any]:
								# Initialize image_processor
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								# create random PIL images
								SCREAMING_SNAKE_CASE													=					prepare_image_inputs(self.image_processor_tester						,       equal_resolution=lowerCAmelCase__				)
								for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase__						,       Image.Image				)

								# Test not batched input
								SCREAMING_SNAKE_CASE													=					(
								    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
								    * self.image_processor_tester.num_channels
								) + 2

								for max_patch in self.image_processor_tester.max_patches:
														# Test not batched input
														SCREAMING_SNAKE_CASE													=					image_processor(
														    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (1, max_patch, expected_hidden_dim)						,       )

														# Test batched
														SCREAMING_SNAKE_CASE													=					image_processor(
														    lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,       )







		def 							__A						(	self				)     ->							Any:
								# Initialize image_processor
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								# create random PIL images
								SCREAMING_SNAKE_CASE													=					prepare_image_inputs(self.image_processor_tester						,       equal_resolution=lowerCAmelCase__				)
								for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase__						,       Image.Image				)

								# Test not batched input
								SCREAMING_SNAKE_CASE													=					(
								    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
								    * self.image_processor_tester.num_channels
								) + 2

								SCREAMING_SNAKE_CASE													=					True

								for max_patch in self.image_processor_tester.max_patches:
														# Test not batched input
														with self.assertRaises(lowerCAmelCase__				):
																				SCREAMING_SNAKE_CASE													=					image_processor(
																				    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches

														SCREAMING_SNAKE_CASE													=					'Hello'

														SCREAMING_SNAKE_CASE													=					image_processor(
														    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__						,       header_text=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (1, max_patch, expected_hidden_dim)						,       )

														# Test batched
														SCREAMING_SNAKE_CASE													=					image_processor(
														    lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__						,       header_text=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,       )







		def 							__A						(	self				)     ->							Tuple:
								# Initialize image_processor
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								# create random numpy tensors
								SCREAMING_SNAKE_CASE													=					prepare_image_inputs(self.image_processor_tester						,       equal_resolution=lowerCAmelCase__						,       numpify=lowerCAmelCase__				)
								for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase__						,       np.ndarray				)

								SCREAMING_SNAKE_CASE													=					(
								    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
								    * self.image_processor_tester.num_channels
								) + 2

								for max_patch in self.image_processor_tester.max_patches:
														# Test not batched input
														SCREAMING_SNAKE_CASE													=					image_processor(
														    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (1, max_patch, expected_hidden_dim)						,       )

														# Test batched
														SCREAMING_SNAKE_CASE													=					image_processor(
														    lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,       )







		def 							__A						(	self				)     ->							Any:
								# Initialize image_processor
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								# create random PyTorch tensors
								SCREAMING_SNAKE_CASE													=					prepare_image_inputs(self.image_processor_tester						,       equal_resolution=lowerCAmelCase__						,       torchify=lowerCAmelCase__				)
								for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase__						,       torch.Tensor				)

								# Test not batched input
								SCREAMING_SNAKE_CASE													=					(
								    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
								    * self.image_processor_tester.num_channels
								) + 2

								for max_patch in self.image_processor_tester.max_patches:
														# Test not batched input
														SCREAMING_SNAKE_CASE													=					image_processor(
														    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (1, max_patch, expected_hidden_dim)						,       )

														# Test batched
														SCREAMING_SNAKE_CASE													=					image_processor(
														    lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,       )





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11	,    reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""	,    )
@require_torch
@require_vision
class 					lowerCAmelCase	(					lowerCamelCase_	,    unittest.TestCase    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     Optional[Any]								=     PixaStructImageProcessor if is_vision_available() else None







		def 							__A						(	self				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					PixaStructImageProcessingTester(self						,       num_channels=4				)
								SCREAMING_SNAKE_CASE													=					3







		@property
		def 							__A						(	self				)     ->							List[str]:
								return self.image_processor_tester.prepare_image_processor_dict()







		def 							__A						(	self				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_normalize'				)				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_convert_rgb'				)				)







		def 							__A						(	self				)     ->							int:
								# Initialize image_processor
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								# create random PIL images
								SCREAMING_SNAKE_CASE													=					prepare_image_inputs(self.image_processor_tester						,       equal_resolution=lowerCAmelCase__				)
								for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase__						,       Image.Image				)

								# Test not batched input
								SCREAMING_SNAKE_CASE													=					(
								    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
								    * (self.image_processor_tester.num_channels - 1)
								) + 2

								for max_patch in self.image_processor_tester.max_patches:
														# Test not batched input
														SCREAMING_SNAKE_CASE													=					image_processor(
														    image_inputs[0]						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (1, max_patch, expected_hidden_dim)						,       )

														# Test batched
														SCREAMING_SNAKE_CASE													=					image_processor(
														    lowerCAmelCase__						,       return_tensors='pt'						,       max_patches=lowerCAmelCase__				).flattened_patches
														self.assertEqual(
														    encoded_images.shape						,       (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,       )






"""simple docstring"""




from __future__ import annotations

from collections.abc import Callable


def     lowercase			(SCREAMING_SNAKE_CASE_		:				Callable[[int | float], int | float] ,			SCREAMING_SNAKE_CASE_		:				int | float ,			SCREAMING_SNAKE_CASE_		:				int | float ,			SCREAMING_SNAKE_CASE_		:				int = 1_00 ,			)   ->						float:
						SCREAMING_SNAKE_CASE													=					x_start
						SCREAMING_SNAKE_CASE													=					fnc(SCREAMING_SNAKE_CASE_  )
						SCREAMING_SNAKE_CASE													=					0.0
						for _ in range(SCREAMING_SNAKE_CASE_  ):
												# Approximates small segments of curve as linear and solve
												# for trapezoidal area
												SCREAMING_SNAKE_CASE													=					(x_end - x_start) / steps + xa
												SCREAMING_SNAKE_CASE													=					fnc(SCREAMING_SNAKE_CASE_  )
												area += abs(fxa + fxa  ) * (xa - xa) / 2
												# Increment step
												SCREAMING_SNAKE_CASE													=					xa
												SCREAMING_SNAKE_CASE													=					fxa
						return area


if __name__ == "__main__":


	def     lowercase			(SCREAMING_SNAKE_CASE_		:				Optional[int]  )   ->						Dict:
							return x**3 + x**2

	print('''f(x) = x^3 + x^2''')
	print('''The area between the curve, x = -5, x = 5 and the x axis is:''')
	__UpperCamelCase						=      10
	while i <= 100000:
		print(f'''with {i} steps: {trapezoidal_area(f, -5, 5, i)}''')
		i *= 10


