





import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
							import torch

if is_tf_available():
							import tensorflow as tf
@require_vision
@require_torchvision
class     UpperCamelCase__					(     unittest.TestCase				):






		'''simple docstring'''






		def 						snake_case__				(							self  )       -> Optional[int]:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     tempfile.mkdtemp()
									lowercase_	:   List[Any]  				=     SamImageProcessor()
									lowercase_	:   Dict  				=     SamProcessor(snake_case__  )
									processor.save_pretrained(self.tmpdirname  )






		def 						snake_case__				(							self,   **snake_case__  )       -> List[str]:






									"""simple docstring"""
									return AutoProcessor.from_pretrained(self.tmpdirname,   **snake_case__  ).image_processor






		def 						snake_case__				(							self  )       -> Optional[Any]:






									"""simple docstring"""
									shutil.rmtree(self.tmpdirname  )






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     [np.random.randint(2_55,   size=(3, 30, 4_00),   dtype=np.uinta  )]

									lowercase_	:   Tuple  				=     [Image.fromarray(np.moveaxis(snake_case__,   0,   -1  )  ) for x in image_inputs]

									return image_inputs






		def 						snake_case__				(							self  )       -> int:






									"""simple docstring"""
									lowercase_	:   Dict  				=     SamProcessor(image_processor=self.get_image_processor()  )
									processor.save_pretrained(self.tmpdirname  )

									lowercase_	:   Optional[int]  				=     self.get_image_processor(do_normalize=snake_case__,   padding_value=1.0  )

									lowercase_	:   List[str]  				=     SamProcessor.from_pretrained(self.tmpdirname,   do_normalize=snake_case__,   padding_value=1.0  )

									self.assertEqual(processor.image_processor.to_json_string(),   image_processor_add_kwargs.to_json_string()  )
									self.assertIsInstance(processor.image_processor,   snake_case__  )






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






									"""simple docstring"""
									lowercase_	:   Tuple  				=     self.get_image_processor()

									lowercase_	:   Optional[Any]  				=     SamProcessor(image_processor=snake_case__  )

									lowercase_	:   List[str]  				=     self.prepare_image_inputs()

									lowercase_	:   Optional[int]  				=     image_processor(snake_case__,   return_tensors="""np"""  )
									lowercase_	:   Tuple  				=     processor(images=snake_case__,   return_tensors="""np"""  )

									input_feat_extract.pop("""original_sizes"""  )  # pop original_sizes as it is popped in the processor
									input_feat_extract.pop("""reshaped_input_sizes"""  )  # pop original_sizes as it is popped in the processor

									for key in input_feat_extract.keys():
																self.assertAlmostEqual(input_feat_extract[key].sum(),   input_processor[key].sum(),   delta=1E-2  )






		@require_torch
		def 						snake_case__				(							self  )       -> int:






									"""simple docstring"""
									lowercase_	:   List[str]  				=     self.get_image_processor()

									lowercase_	:   Optional[Any]  				=     SamProcessor(image_processor=snake_case__  )
									lowercase_	:   Union[str, Any]  				=     [torch.ones((1, 3, 5, 5)  )]

									lowercase_	:   Any  				=     [[17_64, 26_46]]

									lowercase_	:   Dict  				=     [[6_83, 10_24]]
									lowercase_	:   Optional[int]  				=     processor.post_process_masks(snake_case__,   snake_case__,   snake_case__  )
									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									lowercase_	:   Tuple  				=     processor.post_process_masks(
									    snake_case__,   torch.tensor(snake_case__  ),   torch.tensor(snake_case__  )  )
									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									# should also work with np
									lowercase_	:   str  				=     [np.ones((1, 3, 5, 5)  )]
									lowercase_	:   Union[str, Any]  				=     processor.post_process_masks(snake_case__,   np.array(snake_case__  ),   np.array(snake_case__  )  )

									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									lowercase_	:   Dict  				=     [[1, 0], [0, 1]]
									with self.assertRaises(snake_case__  ):
																lowercase_	:   List[Any]  				=     processor.post_process_masks(snake_case__,   np.array(snake_case__  ),   np.array(snake_case__  )  )




@require_vision
@require_tf
class     UpperCamelCase__					(     unittest.TestCase				):






		'''simple docstring'''






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






									"""simple docstring"""
									lowercase_	:   List[str]  				=     tempfile.mkdtemp()
									lowercase_	:   Any  				=     SamImageProcessor()
									lowercase_	:   Union[str, Any]  				=     SamProcessor(snake_case__  )
									processor.save_pretrained(self.tmpdirname  )






		def 						snake_case__				(							self,   **snake_case__  )       -> List[Any]:






									"""simple docstring"""
									return AutoProcessor.from_pretrained(self.tmpdirname,   **snake_case__  ).image_processor






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									shutil.rmtree(self.tmpdirname  )






		def 						snake_case__				(							self  )       -> str:






									"""simple docstring"""
									lowercase_	:   int  				=     [np.random.randint(2_55,   size=(3, 30, 4_00),   dtype=np.uinta  )]

									lowercase_	:   Optional[int]  				=     [Image.fromarray(np.moveaxis(snake_case__,   0,   -1  )  ) for x in image_inputs]

									return image_inputs






		def 						snake_case__				(							self  )       -> Tuple:






									"""simple docstring"""
									lowercase_	:   Dict  				=     SamProcessor(image_processor=self.get_image_processor()  )
									processor.save_pretrained(self.tmpdirname  )

									lowercase_	:   Optional[int]  				=     self.get_image_processor(do_normalize=snake_case__,   padding_value=1.0  )

									lowercase_	:   Optional[Any]  				=     SamProcessor.from_pretrained(self.tmpdirname,   do_normalize=snake_case__,   padding_value=1.0  )

									self.assertEqual(processor.image_processor.to_json_string(),   image_processor_add_kwargs.to_json_string()  )
									self.assertIsInstance(processor.image_processor,   snake_case__  )






		def 						snake_case__				(							self  )       -> int:






									"""simple docstring"""
									lowercase_	:   Any  				=     self.get_image_processor()

									lowercase_	:   Optional[Any]  				=     SamProcessor(image_processor=snake_case__  )

									lowercase_	:   Dict  				=     self.prepare_image_inputs()

									lowercase_	:   int  				=     image_processor(snake_case__,   return_tensors="""np"""  )
									lowercase_	:   Union[str, Any]  				=     processor(images=snake_case__,   return_tensors="""np"""  )

									input_feat_extract.pop("""original_sizes"""  )  # pop original_sizes as it is popped in the processor
									input_feat_extract.pop("""reshaped_input_sizes"""  )  # pop reshaped_input_sizes as it is popped in the processor

									for key in input_feat_extract.keys():
																self.assertAlmostEqual(input_feat_extract[key].sum(),   input_processor[key].sum(),   delta=1E-2  )






		@require_tf
		def 						snake_case__				(							self  )       -> int:






									"""simple docstring"""
									lowercase_	:   int  				=     self.get_image_processor()

									lowercase_	:   Any  				=     SamProcessor(image_processor=snake_case__  )
									lowercase_	:   Dict  				=     [tf.ones((1, 3, 5, 5)  )]

									lowercase_	:   List[str]  				=     [[17_64, 26_46]]

									lowercase_	:   Union[str, Any]  				=     [[6_83, 10_24]]
									lowercase_	:   Optional[int]  				=     processor.post_process_masks(snake_case__,   snake_case__,   snake_case__,   return_tensors="""tf"""  )
									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									lowercase_	:   Optional[Any]  				=     processor.post_process_masks(
									    snake_case__,   tf.convert_to_tensor(snake_case__  ),   tf.convert_to_tensor(snake_case__  ),   return_tensors="""tf""",   )
									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									# should also work with np
									lowercase_	:   List[str]  				=     [np.ones((1, 3, 5, 5)  )]
									lowercase_	:   Tuple  				=     processor.post_process_masks(
									    snake_case__,   np.array(snake_case__  ),   np.array(snake_case__  ),   return_tensors="""tf"""  )

									self.assertEqual(masks[0].shape,   (1, 3, 17_64, 26_46)  )

									lowercase_	:   Optional[int]  				=     [[1, 0], [0, 1]]
									with self.assertRaises(tf.errors.InvalidArgumentError  ):
																lowercase_	:   Dict  				=     processor.post_process_masks(
																    snake_case__,   np.array(snake_case__  ),   np.array(snake_case__  ),   return_tensors="""tf"""  )




@require_vision
@require_torchvision
class     UpperCamelCase__					(     unittest.TestCase				):






		'''simple docstring'''






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






									"""simple docstring"""
									lowercase_	:   Tuple  				=     tempfile.mkdtemp()
									lowercase_	:   Union[str, Any]  				=     SamImageProcessor()
									lowercase_	:   str  				=     SamProcessor(snake_case__  )
									processor.save_pretrained(self.tmpdirname  )






		def 						snake_case__				(							self,   **snake_case__  )       -> Tuple:






									"""simple docstring"""
									return AutoProcessor.from_pretrained(self.tmpdirname,   **snake_case__  ).image_processor






		def 						snake_case__				(							self  )       -> Tuple:






									"""simple docstring"""
									shutil.rmtree(self.tmpdirname  )






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   List[str]  				=     [np.random.randint(2_55,   size=(3, 30, 4_00),   dtype=np.uinta  )]

									lowercase_	:   Optional[Any]  				=     [Image.fromarray(np.moveaxis(snake_case__,   0,   -1  )  ) for x in image_inputs]

									return image_inputs






		@is_pt_tf_cross_test
		def 						snake_case__				(							self  )       -> List[str]:






									"""simple docstring"""
									lowercase_	:   Any  				=     self.get_image_processor()

									lowercase_	:   Any  				=     SamProcessor(image_processor=snake_case__  )
									lowercase_	:   int  				=     np.random.randint(0,   2,   size=(1, 3, 5, 5)  ).astype(np.floataa  )
									lowercase_	:   Optional[int]  				=     [tf.convert_to_tensor(snake_case__  )]
									lowercase_	:   List[str]  				=     [torch.tensor(snake_case__  )]

									lowercase_	:   Tuple  				=     [[17_64, 26_46]]

									lowercase_	:   Union[str, Any]  				=     [[6_83, 10_24]]
									lowercase_	:   str  				=     processor.post_process_masks(
									    snake_case__,   snake_case__,   snake_case__,   return_tensors="""tf"""  )
									lowercase_	:   List[str]  				=     processor.post_process_masks(
									    snake_case__,   snake_case__,   snake_case__,   return_tensors="""pt"""  )

									self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()  )  )






		@is_pt_tf_cross_test
		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   Tuple  				=     self.get_image_processor()

									lowercase_	:   str  				=     SamProcessor(image_processor=snake_case__  )

									lowercase_	:   Optional[int]  				=     self.prepare_image_inputs()

									lowercase_	:   Any  				=     image_processor(snake_case__,   return_tensors="""pt"""  )["""pixel_values"""].numpy()
									lowercase_	:   Optional[Any]  				=     processor(images=snake_case__,   return_tensors="""pt"""  )["""pixel_values"""].numpy()

									lowercase_	:   List[str]  				=     image_processor(snake_case__,   return_tensors="""tf"""  )["""pixel_values"""].numpy()
									lowercase_	:   Optional[Any]  				=     processor(images=snake_case__,   return_tensors="""tf"""  )["""pixel_values"""].numpy()

									self.assertTrue(np.allclose(snake_case__,   snake_case__  )  )
									self.assertTrue(np.allclose(snake_case__,   snake_case__  )  )
									self.assertTrue(np.allclose(snake_case__,   snake_case__  )  )






UpperCAmelCase_							=				8.31_44_62  # Unit - J mol-1 K-1





def  __magic_name__  (				lowercase				,       lowercase				,       lowercase				)				-> float:

							"""simple docstring"""



							if moles < 0 or kelvin < 0 or volume < 0:
														raise ValueError("""Invalid inputs. Enter positive value."""				)
							return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume





def  __magic_name__  (				lowercase				,       lowercase				,       lowercase				)				-> float:

							"""simple docstring"""



							if moles < 0 or kelvin < 0 or pressure < 0:
														raise ValueError("""Invalid inputs. Enter positive value."""				)
							return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure


if __name__ == "__main__":
							from doctest import testmod

							testmod()