



'''simple docstring'''





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     ):




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(a__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**a__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(a__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     def 		snake_case_		(				self):
         A__		 =					SamProcessor(image_processor=self.get_image_processor())
         processor.save_pretrained(self.tmpdirname)

         A__		 =					self.get_image_processor(do_normalize=a__ ,						padding_value=1.0)

         A__		 =					SamProcessor.from_pretrained(self.tmpdirname ,						do_normalize=a__ ,						padding_value=1.0)

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




     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(a__ ,						return_tensors='''np''')
         A__		 =					processor(images=a__ ,						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):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)
         A__		 =					[torch.ones((1, 3, 5, 5))]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(a__ ,						a__ ,						a__)
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					processor.post_process_masks(
             a__ ,						torch.tensor(a__) ,						torch.tensor(a__))
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         # should also work with np
         A__		 =					[np.ones((1, 3, 5, 5))]
         A__		 =					processor.post_process_masks(a__ ,						np.array(a__) ,						np.array(a__))

         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					[[1, 0], [0, 1]]
         with self.assertRaises(a__):
             A__		 =					processor.post_process_masks(a__ ,						np.array(a__) ,						np.array(a__))







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




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(a__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**a__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(a__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     def 		snake_case_		(				self):
         A__		 =					SamProcessor(image_processor=self.get_image_processor())
         processor.save_pretrained(self.tmpdirname)

         A__		 =					self.get_image_processor(do_normalize=a__ ,						padding_value=1.0)

         A__		 =					SamProcessor.from_pretrained(self.tmpdirname ,						do_normalize=a__ ,						padding_value=1.0)

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




     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(a__ ,						return_tensors='''np''')
         A__		 =					processor(images=a__ ,						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):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)
         A__		 =					[tf.ones((1, 3, 5, 5))]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(a__ ,						a__ ,						a__ ,						return_tensors='''tf''')
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					processor.post_process_masks(
             a__ ,						tf.convert_to_tensor(a__) ,						tf.convert_to_tensor(a__) ,						return_tensors='''tf''' ,						)
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         # should also work with np
         A__		 =					[np.ones((1, 3, 5, 5))]
         A__		 =					processor.post_process_masks(
             a__ ,						np.array(a__) ,						np.array(a__) ,						return_tensors='''tf''')

         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError):
             A__		 =					processor.post_process_masks(
                 a__ ,						np.array(a__) ,						np.array(a__) ,						return_tensors='''tf''')







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




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(a__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**a__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(a__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     @is_pt_tf_cross_test
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)
         A__		 =					np.random.randint(0 ,						2 ,						size=(1, 3, 5, 5)).astype(np.floataa)
         A__		 =					[tf.convert_to_tensor(a__)]
         A__		 =					[torch.tensor(a__)]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(
             a__ ,						a__ ,						a__ ,						return_tensors='''tf''')
         A__		 =					processor.post_process_masks(
             a__ ,						a__ ,						a__ ,						return_tensors='''pt''')

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




     @is_pt_tf_cross_test
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=a__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(a__ ,						return_tensors='''pt''')['''pixel_values'''].numpy()
         A__		 =					processor(images=a__ ,						return_tensors='''pt''')['''pixel_values'''].numpy()

         A__		 =					image_processor(a__ ,						return_tensors='''tf''')['''pixel_values'''].numpy()
         A__		 =					processor(images=a__ ,						return_tensors='''tf''')['''pixel_values'''].numpy()

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





def 		lowerCAmelCase__    (					UpperCamelCase_				:       Optional[int] ,       UpperCamelCase_				:       List[str] ,       UpperCamelCase_				:       List[Any] ,       UpperCamelCase_				:       str	)->  int:
    if height >= 1:
        move_tower(height - 1 ,       UpperCamelCase_ ,       UpperCamelCase_ ,       UpperCamelCase_	)
        move_disk(UpperCamelCase_ ,       UpperCamelCase_	)
        move_tower(height - 1 ,       UpperCamelCase_ ,       UpperCamelCase_ ,       UpperCamelCase_	)



def 		lowerCAmelCase__    (					UpperCamelCase_				:       Optional[int] ,       UpperCamelCase_				:       List[Any]	)->  str:
    print('''moving disk from''' ,       UpperCamelCase_ ,       '''to''' ,       UpperCamelCase_	)



def 		lowerCAmelCase__    (					)->  Tuple:
    A__		 =					int(input('''Height of hanoi: '''	).strip()	)
    move_tower(UpperCamelCase_ ,       '''A''' ,       '''B''' ,       '''C'''	)


if __name__ == "__main__":
   main()
