




'''simple docstring'''







import random
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():
						import PIL

						from transformers import FlavaImageProcessor
						from transformers.image_utils import PILImageResampling
						from transformers.models.flava.image_processing_flava import (
						    FLAVA_CODEBOOK_MEAN,
						    FLAVA_CODEBOOK_STD,
						    FLAVA_IMAGE_MEAN,
						    FLAVA_IMAGE_STD,
						)
else:
						_lowerCamelCase							:						Any								=  None





class      lowerCamelCase_							(       unittest.TestCase							):




					'''simple docstring'''






					def __init__(    self,  __UpperCamelCase,  __UpperCamelCase=7,  __UpperCamelCase=3,  __UpperCamelCase=30,  __UpperCamelCase=400,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=1 / 255,  __UpperCamelCase=True,  __UpperCamelCase=FLAVA_IMAGE_MEAN,  __UpperCamelCase=FLAVA_IMAGE_STD,  __UpperCamelCase=14,  __UpperCamelCase=75,  __UpperCamelCase=None,  __UpperCamelCase=16,  __UpperCamelCase=0.3,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=FLAVA_CODEBOOK_MEAN,  __UpperCamelCase=FLAVA_CODEBOOK_STD,  ):
							_snake_case	       =       size if size is not None else {"height": 224, "width": 224}
							_snake_case	       =       crop_size if crop_size is not None else {"height": 224, "width": 224}
							_snake_case	       =       codebook_size if codebook_size is not None else {"height": 112, "width": 112}
							_snake_case	       =       codebook_crop_size if codebook_crop_size is not None else {"height": 112, "width": 112}

							_snake_case	       =       parent
							_snake_case	       =       batch_size
							_snake_case	       =       num_channels
							_snake_case	       =       do_resize
							_snake_case	       =       do_rescale
							_snake_case	       =       rescale_factor
							_snake_case	       =       min_resolution
							_snake_case	       =       max_resolution
							_snake_case	       =       size
							_snake_case	       =       resample if resample is not None else PILImageResampling.BICUBIC
							_snake_case	       =       do_normalize
							_snake_case	       =       image_mean
							_snake_case	       =       image_std
							_snake_case	       =       do_center_crop
							_snake_case	       =       crop_size

							_snake_case	       =       input_size_patches
							_snake_case	       =       total_mask_patches
							_snake_case	       =       mask_group_max_patches
							_snake_case	       =       mask_group_min_patches
							_snake_case	       =       mask_group_min_aspect_ratio
							_snake_case	       =       mask_group_max_aspect_ratio

							_snake_case	       =       codebook_do_resize
							_snake_case	       =       codebook_size
							_snake_case	       =       codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
							_snake_case	       =       codebook_do_center_crop
							_snake_case	       =       codebook_crop_size
							_snake_case	       =       codebook_do_map_pixels
							_snake_case	       =       codebook_do_normalize
							_snake_case	       =       codebook_image_mean
							_snake_case	       =       codebook_image_std






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "resample": self.resample,
							    "do_rescale": self.do_rescale,
							    "rescale_factor": self.rescale_factor,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "input_size_patches": self.input_size_patches,
							    "total_mask_patches": self.total_mask_patches,
							    "mask_group_max_patches": self.mask_group_max_patches,
							    "mask_group_min_patches": self.mask_group_min_patches,
							    "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
							    "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
							    "codebook_do_resize": self.codebook_do_resize,
							    "codebook_size": self.codebook_size,
							    "codebook_resample": self.codebook_resample,
							    "codebook_do_center_crop": self.codebook_do_center_crop,
							    "codebook_crop_size": self.codebook_crop_size,
							    "codebook_do_map_pixels": self.codebook_do_map_pixels,
							    "codebook_do_normalize": self.codebook_do_normalize,
							    "codebook_image_mean": self.codebook_image_mean,
							    "codebook_image_std": self.codebook_image_std,
							}






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return (self.size["height"], self.size["width"])






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return (
							    (self.input_size_patches, self.input_size_patches)
							    if not isinstance(self.input_size_patches,  __UpperCamelCase )
							    else self.input_size_patches
							)






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return (self.codebook_size["height"], self.codebook_size["width"])





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




					'''simple docstring'''






					lowercase__    = FlavaImageProcessor if is_vision_available() else None
					lowercase__    = None






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       FlavaImageProcessingTester(self )






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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.image_processing_class(**self.image_processor_dict )
							self.assertTrue(hasattr(__UpperCamelCase,  "image_mean" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "image_std" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "do_normalize" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "do_resize" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "resample" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "crop_size" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "do_center_crop" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "do_rescale" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "rescale_factor" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "masking_generator" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_do_resize" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_size" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_resample" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_do_center_crop" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_crop_size" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_do_map_pixels" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_do_normalize" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_image_mean" ) )
							self.assertTrue(hasattr(__UpperCamelCase,  "codebook_image_std" ) )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.image_processing_class.from_dict(self.image_processor_dict )
							self.assertEqual(image_processor.size,  {"height": 224, "width": 224} )
							self.assertEqual(image_processor.crop_size,  {"height": 224, "width": 224} )
							self.assertEqual(image_processor.codebook_size,  {"height": 112, "width": 112} )
							self.assertEqual(image_processor.codebook_crop_size,  {"height": 112, "width": 112} )

							_snake_case	       =       self.image_processing_class.from_dict(
							    self.image_processor_dict,  size=42,  crop_size=84,  codebook_size=33,  codebook_crop_size=66 )
							self.assertEqual(image_processor.size,  {"height": 42, "width": 42} )
							self.assertEqual(image_processor.crop_size,  {"height": 84, "width": 84} )
							self.assertEqual(image_processor.codebook_size,  {"height": 33, "width": 33} )
							self.assertEqual(image_processor.codebook_crop_size,  {"height": 66, "width": 66} )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							pass






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							# Initialize image_processing
							_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=__UpperCamelCase )
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  PIL.Image.Image )

							# Test not batched input
							_snake_case	       =       image_processing(image_inputs[0],  return_tensors="pt" )

							# Test no bool masked pos
							self.assertFalse("bool_masked_pos" in encoded_images )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()

							self.assertEqual(
							    encoded_images.pixel_values.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							# Test batched
							_snake_case	       =       image_processing(__UpperCamelCase,  return_tensors="pt" )
							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()

							# Test no bool masked pos
							self.assertFalse("bool_masked_pos" in encoded_images )

							self.assertEqual(
							    encoded_images.pixel_values.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )






					def 				__SCREAMING_SNAKE_CASE	(    self,  __UpperCamelCase,  __UpperCamelCase ):
							# Initialize image_processing
							_snake_case	       =       self.image_processing_class(**self.image_processor_dict )
							# create random tensors
							_snake_case	       =       prepare_image_inputs(self.image_processor_tester,  equal_resolution=__UpperCamelCase,  **__UpperCamelCase )
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  __UpperCamelCase )

							# Test not batched input
							_snake_case	       =       image_processing(image_inputs[0],  return_tensors="pt" )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							_snake_case	       =       image_processing(__UpperCamelCase,  return_image_mask=__UpperCamelCase,  return_tensors="pt" )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_mask_size()
							self.assertEqual(
							    encoded_images.bool_masked_pos.shape,  (
							        self.image_processor_tester.batch_size,
							        expected_height,
							        expected_width,
							    ),  )

							# Test batched
							_snake_case	       =       image_processing(__UpperCamelCase,  return_tensors="pt" ).pixel_values

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )

							# Test masking
							_snake_case	       =       image_processing(__UpperCamelCase,  return_image_mask=__UpperCamelCase,  return_tensors="pt" )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )

							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_mask_size()
							self.assertEqual(
							    encoded_images.bool_masked_pos.shape,  (
							        self.image_processor_tester.batch_size,
							        expected_height,
							        expected_width,
							    ),  )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self._test_call_framework(np.ndarray,  prepare_kwargs={"numpify": True} )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self._test_call_framework(torch.Tensor,  prepare_kwargs={"torchify": True} )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							# Initialize image_processing
							random.seed(1_234 )
							_snake_case	       =       self.image_processing_class(**self.image_processor_dict )
							_snake_case	       =       prepare_image_inputs(self.image_processor_tester,  equal_resolution=__UpperCamelCase,  torchify=__UpperCamelCase )

							# Test not batched input
							_snake_case	       =       image_processing(image_inputs[0],  return_image_mask=__UpperCamelCase,  return_tensors="pt" )
							self.assertEqual(encoded_images.bool_masked_pos.sum().item(),  75 )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							# Initialize image_processing
							_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=__UpperCamelCase )
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  PIL.Image.Image )

							# Test not batched input
							_snake_case	       =       image_processing(image_inputs[0],  return_codebook_pixels=__UpperCamelCase,  return_tensors="pt" )
							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_codebook_image_size()
							self.assertEqual(
							    encoded_images.codebook_pixel_values.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							# Test batched
							_snake_case	       =       image_processing(__UpperCamelCase,  return_codebook_pixels=__UpperCamelCase,  return_tensors="pt" )
							_snake_case       ,    _snake_case	       =       self.image_processor_tester.get_expected_codebook_image_size()
							self.assertEqual(
							    encoded_images.codebook_pixel_values.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )











'''simple docstring'''







from __future__ import annotations

from math import pi, pow






def 		_lowercase		(	lowercase_  ):



		'''simple docstring'''







		if side_length < 0:
				raise ValueError("vol_cube() only accepts non-negative values"  )
		return pow(lowercase_						,							3  )






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or radius < 0:
				raise ValueError("vol_spherical_cap() only accepts non-negative values"  )
		# Volume is 1/3 pi * height squared * (3 * radius - height)
		return 1 / 3 * pi * pow(lowercase_						,							2  ) * (3 * radius - height)






def 		_lowercase		(	lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if radius_a < 0 or radius_a < 0 or centers_distance < 0:
				raise ValueError("vol_spheres_intersect() only accepts non-negative values"  )
		if centers_distance == 0:
				return vol_sphere(min(lowercase_						,							lowercase_  )  )

		_snake_case	       =       (
		    (radius_a - radius_a + centers_distance)
		    * (radius_a + radius_a - centers_distance)
		    / (2 * centers_distance)
		)
		_snake_case	       =       (
		    (radius_a - radius_a + centers_distance)
		    * (radius_a + radius_a - centers_distance)
		    / (2 * centers_distance)
		)

		return vol_spherical_cap(lowercase_						,							lowercase_  ) + vol_spherical_cap(lowercase_						,							lowercase_  )






def 		_lowercase		(	lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if radius_a <= 0 or radius_a <= 0 or centers_distance < 0:
				raise ValueError(
				    "vol_spheres_union() only accepts non-negative values, non-zero radius"  )

		if centers_distance == 0:
				return vol_sphere(max(lowercase_						,							lowercase_  )  )

		return (
		    vol_sphere(lowercase_  )
		    + vol_sphere(lowercase_  )
		    - vol_spheres_intersect(lowercase_						,							lowercase_						,							lowercase_  )
		)






def 		_lowercase		(	lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if width < 0 or height < 0 or length < 0:
				raise ValueError("vol_cuboid() only accepts non-negative values"  )
		return float(width * height * length  )






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or area_of_base < 0:
				raise ValueError("vol_cone() only accepts non-negative values"  )
		return area_of_base * height / 3.0






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or radius < 0:
				raise ValueError("vol_right_circ_cone() only accepts non-negative values"  )
		return pi * pow(lowercase_						,							2  ) * height / 3.0






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or area_of_base < 0:
				raise ValueError("vol_prism() only accepts non-negative values"  )
		return float(area_of_base * height  )






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or area_of_base < 0:
				raise ValueError("vol_pyramid() only accepts non-negative values"  )
		return area_of_base * height / 3.0






def 		_lowercase		(	lowercase_  ):



		'''simple docstring'''







		if radius < 0:
				raise ValueError("vol_sphere() only accepts non-negative values"  )
		# Volume is 4/3 * pi * radius cubed
		return 4 / 3 * pi * pow(lowercase_						,							3  )






def 		_lowercase		(	lowercase_  ):



		'''simple docstring'''







		if radius < 0:
				raise ValueError("vol_hemisphere() only accepts non-negative values"  )
		# Volume is radius cubed * pi * 2/3
		return pow(lowercase_						,							3  ) * pi * 2 / 3






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if height < 0 or radius < 0:
				raise ValueError("vol_circular_cylinder() only accepts non-negative values"  )
		# Volume is radius squared * height * pi
		return pow(lowercase_						,							2  ) * height * pi






def 		_lowercase		(	lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if inner_radius < 0 or outer_radius < 0 or height < 0:
				raise ValueError(
				    "vol_hollow_circular_cylinder() only accepts non-negative values"  )
		if outer_radius <= inner_radius:
				raise ValueError("outer_radius must be greater than inner_radius"  )
		return pi * (pow(lowercase_						,							2  ) - pow(lowercase_						,							2  )) * height






def 		_lowercase		(	lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if radius_a < 0 or radius_a < 0 or height < 0:
				raise ValueError("vol_conical_frustum() only accepts non-negative values"  )
		return (
		    1
		    / 3
		    * pi
		    * height
		    * (pow(lowercase_						,							2  ) + pow(lowercase_						,							2  ) + radius_a * radius_a)
		)






def 		_lowercase		(	lowercase_						,							lowercase_  ):



		'''simple docstring'''







		if torus_radius < 0 or tube_radius < 0:
				raise ValueError("vol_torus() only accepts non-negative values"  )
		return 2 * pow(lowercase_						,							2  ) * torus_radius * pow(lowercase_						,							2  )






def 		_lowercase		(	):



		'''simple docstring'''







		print("Volumes:"  )
		print(f'''Cube: {vol_cube(2  ) = }'''  )  # = 8
		print(f'''Cuboid: {vol_cuboid(2						,							2						,							2  ) = }'''  )  # = 8
		print(f'''Cone: {vol_cone(2						,							2  ) = }'''  )  # ~= 1.33
		print(f'''Right Circular Cone: {vol_right_circ_cone(2						,							2  ) = }'''  )  # ~= 8.38
		print(f'''Prism: {vol_prism(2						,							2  ) = }'''  )  # = 4
		print(f'''Pyramid: {vol_pyramid(2						,							2  ) = }'''  )  # ~= 1.33
		print(f'''Sphere: {vol_sphere(2  ) = }'''  )  # ~= 33.5
		print(f'''Hemisphere: {vol_hemisphere(2  ) = }'''  )  # ~= 16.75
		print(f'''Circular Cylinder: {vol_circular_cylinder(2						,							2  ) = }'''  )  # ~= 25.1
		print(f'''Torus: {vol_torus(2						,							2  ) = }'''  )  # ~= 157.9
		print(f'''Conical Frustum: {vol_conical_frustum(2						,							2						,							4  ) = }'''  )  # ~= 58.6
		print(f'''Spherical cap: {vol_spherical_cap(1						,							2  ) = }'''  )  # ~= 5.24
		print(f'''Spheres intersetion: {vol_spheres_intersect(2						,							2						,							1  ) = }'''  )  # ~= 21.21
		print(f'''Spheres union: {vol_spheres_union(2						,							2						,							1  ) = }'''  )  # ~= 45.81
		print(
		    f'''Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1						,							2						,							3  ) = }'''  )  # ~= 28.3


if __name__ == "__main__":
						main()





