





"""simple docstring"""




def        a_				(							_lowercase       ,						_lowercase = 1000   ):
						if (not isinstance(_lowercase       ,						_lowercase   )) or (digit_position <= 0):
												raise ValueError('''Digit position must be a positive integer'''   )
						elif (not isinstance(_lowercase       ,						_lowercase   )) or (precision < 0):
												raise ValueError('''Precision must be a nonnegative integer'''   )

						# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
						# accurate
						_UpperCamelCase			:					List[str]     =    (
						    4 * _subsum(_lowercase       ,						1       ,						_lowercase   )
						    - 2 * _subsum(_lowercase       ,						4       ,						_lowercase   )
						    - _subsum(_lowercase       ,						5       ,						_lowercase   )
						    - _subsum(_lowercase       ,						6       ,						_lowercase   )
						)

						# return the first hex digit of the fractional part of the result
						return hex(int((sum_result % 1) * 16   )   )[2:]





def        a_				(							_lowercase       ,						_lowercase       ,						_lowercase   ):
						_UpperCamelCase			:					int     =    0.0
						for sum_index in range(digit_pos_to_extract + precision   ):
												_UpperCamelCase			:					str     =    8 * sum_index + denominator_addend
												if sum_index < digit_pos_to_extract:
																		# if the exponential term is an integer and we mod it by the denominator
																		# before dividing, only the integer part of the sum will change;
																		# the fractional part will not
																		_UpperCamelCase			:					Optional[int]     =    pow(
																		    16       ,						digit_pos_to_extract - 1 - sum_index       ,						_lowercase   )
												else:
																		_UpperCamelCase			:					Tuple     =    pow(16       ,						digit_pos_to_extract - 1 - sum_index   )
												total += exponential_term / denominator
						return total


if __name__ == "__main__":
						import doctest

						doctest.testmod()








"""simple docstring"""
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


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import BlipImageProcessor


class 							lowerCamelCase_(      unittest.TestCase			):





					'''simple docstring'''



					def __init__(			self ,				lowerCamelCase__ ,				lowerCamelCase__=7 ,				lowerCamelCase__=3 ,				lowerCamelCase__=1_8 ,				lowerCamelCase__=3_0 ,				lowerCamelCase__=4_0_0 ,				lowerCamelCase__=True ,				lowerCamelCase__=None ,				lowerCamelCase__=True ,				lowerCamelCase__=False ,				lowerCamelCase__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] ,				lowerCamelCase__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] ,				lowerCamelCase__=True ,				):
									_lowerCamelCase					     = size if size is not None else {'''height''': 2_0, '''width''': 2_0}
									_lowerCamelCase					     = parent
									_lowerCamelCase					     = batch_size
									_lowerCamelCase					     = num_channels
									_lowerCamelCase					     = image_size
									_lowerCamelCase					     = min_resolution
									_lowerCamelCase					     = max_resolution
									_lowerCamelCase					     = do_resize
									_lowerCamelCase					     = size
									_lowerCamelCase					     = do_normalize
									_lowerCamelCase					     = image_mean
									_lowerCamelCase					     = image_std
									_lowerCamelCase					     = do_pad
									_lowerCamelCase					     = do_convert_rgb

					def 	snake_case__					(			self      ):
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_convert_rgb": self.do_convert_rgb,
									    "do_pad": self.do_pad,
									}





					def 	snake_case__					(			self ,				lowerCamelCase__=False ,				lowerCamelCase__=False ,				lowerCamelCase__=False      ):
									assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

									if equal_resolution:
													_lowerCamelCase					     = []
													for i in range(self.batch_size      ):
																	image_inputs.append(
																	    np.random.randint(
																	        2_5_5 ,				size=(self.num_channels, self.max_resolution, self.max_resolution) ,				dtype=np.uinta      )      )
									else:
													_lowerCamelCase					     = []
													for i in range(self.batch_size      ):
																	_lowerCamelCase     ,   _lowerCamelCase					     = np.random.choice(np.arange(self.min_resolution ,				self.max_resolution      ) ,				2      )
																	image_inputs.append(np.random.randint(2_5_5 ,				size=(self.num_channels, width, height) ,				dtype=np.uinta      )      )

									if not numpify and not torchify:
													# PIL expects the channel dimension as last dimension
													_lowerCamelCase					     = [Image.fromarray(np.moveaxis(lowerCamelCase__ ,				0 ,				-1      )      ) for x in image_inputs]

									if torchify:
													_lowerCamelCase					     = [torch.from_numpy(lowerCamelCase__      ) for x in image_inputs]

									return image_inputs


@require_torch
@require_vision
class 							lowerCamelCase_(      A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				Any        =       BlipImageProcessor if is_vision_available() else None

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = BlipImageProcessingTester(self      )

					@property
					def 	snake_case__					(			self      ):
									return self.image_processor_tester.prepare_image_processor_dict()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_resize'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''size'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_normalize'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''image_mean'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''image_std'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_convert_rgb'''      )      )

					def 	snake_case__					(			self      ):
									pass

					def 	snake_case__					(			self      ):
									# Initialize image_processor
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									_lowerCamelCase					     = self.image_processor_tester.prepare_inputs(equal_resolution=lowerCamelCase__      )
									for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__ ,				Image.Image      )

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

					def 	snake_case__					(			self      ):
									# Initialize image_processor
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									# create random numpy tensors
									_lowerCamelCase					     = self.image_processor_tester.prepare_inputs(equal_resolution=lowerCamelCase__ ,				numpify=lowerCamelCase__      )
									for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__ ,				np.ndarray      )

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





					def 	snake_case__					(			self      ):
									# Initialize image_processor
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									_lowerCamelCase					     = self.image_processor_tester.prepare_inputs(equal_resolution=lowerCamelCase__ ,				torchify=lowerCamelCase__      )
									for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__ ,				torch.Tensor      )

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


@require_torch
@require_vision
class 							lowerCamelCase_(      A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				Tuple        =       BlipImageProcessor if is_vision_available() else None

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = BlipImageProcessingTester(self ,				num_channels=4      )
									_lowerCamelCase					     = 3

					@property
					def 	snake_case__					(			self      ):
									return self.image_processor_tester.prepare_image_processor_dict()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_resize'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''size'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_normalize'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''image_mean'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''image_std'''      )      )
									self.assertTrue(hasattr(lowerCamelCase__ ,				'''do_convert_rgb'''      )      )

					def 	snake_case__					(			self      ):
									pass





					def 	snake_case__					(			self      ):
									# Initialize image_processor
									_lowerCamelCase					     = self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									_lowerCamelCase					     = self.image_processor_tester.prepare_inputs(equal_resolution=lowerCamelCase__      )
									for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__ ,				Image.Image      )

									# Test not batched input
									_lowerCamelCase					     = image_processor(image_inputs[0] ,				return_tensors='''pt'''      ).pixel_values
									self.assertEqual(
									    encoded_images.shape ,				(
									        1,
									        self.expected_encoded_image_num_channels,
									        self.image_processor_tester.size['''height'''],
									        self.image_processor_tester.size['''width'''],
									    ) ,				)

									# Test batched
									_lowerCamelCase					     = image_processor(lowerCamelCase__ ,				return_tensors='''pt'''      ).pixel_values
									self.assertEqual(
									    encoded_images.shape ,				(
									        self.image_processor_tester.batch_size,
									        self.expected_encoded_image_num_channels,
									        self.image_processor_tester.size['''height'''],
									        self.image_processor_tester.size['''width'''],
									    ) ,				)
