






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 LevitImageProcessor


class    _lowerCAmelCase			(		unittest.TestCase      ):



				"""simple docstring"""



				def __init__(      self    :    List[Any]			,		UpperCamelCase__    :    Optional[Any]			,		UpperCamelCase__    :    int=7			,		UpperCamelCase__    :    Optional[int]=3			,		UpperCamelCase__    :    Tuple=1_8			,		UpperCamelCase__    :    Dict=3_0			,		UpperCamelCase__    :    Union[str, Any]=4_0_0			,		UpperCamelCase__    :    List[Any]=True			,		UpperCamelCase__    :    int=None			,		UpperCamelCase__    :    List[Any]=True			,		UpperCamelCase__    :    str=None			,		UpperCamelCase__    :    List[Any]=True			,		UpperCamelCase__    :    List[str]=[0.5, 0.5, 0.5]			,		UpperCamelCase__    :    List[Any]=[0.5, 0.5, 0.5]			,		):






											'''simple docstring'''







											snake_case__	  =       size if size is not None else {"""shortest_edge""": 1_8}
											snake_case__	  =       crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}
											snake_case__	  =       parent
											snake_case__	  =       batch_size
											snake_case__	  =       num_channels
											snake_case__	  =       image_size
											snake_case__	  =       min_resolution
											snake_case__	  =       max_resolution
											snake_case__	  =       do_resize
											snake_case__	  =       size
											snake_case__	  =       do_center_crop
											snake_case__	  =       crop_size
											snake_case__	  =       do_normalize
											snake_case__	  =       image_mean
											snake_case__	  =       image_std



				def 					__magic_name__							(      self    :    Any):






											'''simple docstring'''







											return {
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_normalize": self.do_normalize,
											    "do_resize": self.do_resize,
											    "do_center_crop": self.do_center_crop,
											    "size": self.size,
											    "crop_size": self.crop_size,
											}


@require_torch
@require_vision
class    _lowerCAmelCase			(		lowercase_     ,      unittest.TestCase      ):



				"""simple docstring"""



				_lowercase				:   Optional[int]      =  LevitImageProcessor if is_vision_available() else None



				def 					__magic_name__							(      self    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       LevitImageProcessingTester(self)



				@property
				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											return self.image_processor_tester.prepare_image_processor_dict()



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       self.image_processing_class(**self.image_processor_dict)
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""image_mean"""))
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""image_std"""))
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""do_normalize"""))
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""do_resize"""))
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""do_center_crop"""))
											self.assertTrue(hasattr(__lowerCAmelCase			,		"""size"""))



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											snake_case__	  =       self.image_processing_class.from_dict(self.image_processor_dict)
											self.assertEqual(image_processor.size			,		{"""shortest_edge""": 1_8})
											self.assertEqual(image_processor.crop_size			,		{"""height""": 1_8, """width""": 1_8})

											snake_case__	  =       self.image_processing_class.from_dict(self.image_processor_dict			,		size=4_2			,		crop_size=8_4)
											self.assertEqual(image_processor.size			,		{"""shortest_edge""": 4_2})
											self.assertEqual(image_processor.crop_size			,		{"""height""": 8_4, """width""": 8_4})



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											pass



				def 					__magic_name__							(      self    :    Union[str, Any]):






											'''simple docstring'''







											snake_case__	  =       self.image_processing_class(**self.image_processor_dict)
											# create random PIL images
											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
											snake_case__	  =       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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)

											# Test batched
											snake_case__	  =       image_processing(__lowerCAmelCase			,		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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.image_processing_class(**self.image_processor_dict)
											# create random numpy tensors
											snake_case__	  =       prepare_image_inputs(self.image_processor_tester			,		equal_resolution=__lowerCAmelCase			,		numpify=__lowerCAmelCase)
											for image in image_inputs:
																		self.assertIsInstance(__lowerCAmelCase			,		np.ndarray)

											# Test not batched input
											snake_case__	  =       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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)

											# Test batched
											snake_case__	  =       image_processing(__lowerCAmelCase			,		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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											snake_case__	  =       self.image_processing_class(**self.image_processor_dict)
											# create random PyTorch tensors
											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
											snake_case__	  =       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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)

											# Test batched
											snake_case__	  =       image_processing(__lowerCAmelCase			,		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.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )			,		)








from collections.abc import Callable

def _UpperCAmelCase   (	a						:	Callable[[float], float]  ,					a						:	float  ,					a						:	float ):
							snake_case__	  =       a
							snake_case__	  =       b
							if function(a ) == 0:  # one of the a or b is a root for the function
														return a
							elif function(a ) == 0:
														return b
							elif (
							    function(a ) * function(a ) > 0
							):  # if none of these are root and they are both positive or negative,
														# then this algorithm can't find the root
														raise ValueError("""could not find root in given interval.""" )
							else:
														snake_case__	  =       start + (end - start) / 2.0
														while abs(start - mid ) > 10**-7:  # until precisely equals to 10^-7
																					if function(a ) == 0:
																												return mid
																					elif function(a ) * function(a ) < 0:
																												snake_case__	  =       mid
																					else:
																												snake_case__	  =       mid
																					snake_case__	  =       start + (end - start) / 2.0
														return mid

def _UpperCAmelCase   (	a						:	float ):
							return x**3 - 2 * x - 5


if __name__ == "__main__":
	print(bisection(f, 1, 1_0_0_0))

	import doctest

	doctest.testmod()
