





'''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   _lowercase						(							unittest.TestCase				):


   def 					snake_case				( self					):
       A       :       Optional[int]							=						tempfile.mkdtemp()
       A       :       Optional[Any]							=						SamImageProcessor()
       A       :       List[str]							=						SamProcessor(_UpperCAmelCase					)
       processor.save_pretrained(self.tmpdirname					)


   def 					snake_case				( self    ,   **_UpperCAmelCase					):
       return AutoProcessor.from_pretrained(self.tmpdirname    ,   **_UpperCAmelCase					).image_processor


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


   def 					snake_case				( self					):
       A       :       List[Any]							=						[np.random.randint(255    ,   size=(3, 30, 400)    ,   dtype=np.uinta					)]

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

       return image_inputs


   def 					snake_case				( self					):
       A       :       Tuple							=						SamProcessor(image_processor=self.get_image_processor()					)
       processor.save_pretrained(self.tmpdirname					)

       A       :       List[str]							=						self.get_image_processor(do_normalize=_UpperCAmelCase    ,   padding_value=1.0					)

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

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


   def 					snake_case				( self					):
       A       :       Union[str, Any]							=						self.get_image_processor()

       A       :       Optional[Any]							=						SamProcessor(image_processor=_UpperCAmelCase					)

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

       A       :       Optional[Any]							=						image_processor(_UpperCAmelCase    ,   return_tensors='''np'''					)
       A       :       Tuple							=						processor(images=_UpperCAmelCase    ,   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       :       int							=						self.get_image_processor()

       A       :       int							=						SamProcessor(image_processor=_UpperCAmelCase					)
       A       :       str							=						[torch.ones((1, 3, 5, 5)					)]

       A       :       str							=						[[1_764, 2_646]]

       A       :       Optional[Any]							=						[[683, 1_024]]
       A       :       Dict							=						processor.post_process_masks(_UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase					)
       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       A       :       int							=						processor.post_process_masks(
           _UpperCAmelCase    ,   torch.tensor(_UpperCAmelCase					)    ,   torch.tensor(_UpperCAmelCase					)					)
       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       # should also work with np
       A       :       Optional[Any]							=						[np.ones((1, 3, 5, 5)					)]
       A       :       Any							=						processor.post_process_masks(_UpperCAmelCase    ,   np.array(_UpperCAmelCase					)    ,   np.array(_UpperCAmelCase					)					)

       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       A       :       Dict							=						[[1, 0], [0, 1]]
       with self.assertRaises(_UpperCAmelCase					):
           A       :       Optional[Any]							=						processor.post_process_masks(_UpperCAmelCase    ,   np.array(_UpperCAmelCase					)    ,   np.array(_UpperCAmelCase					)					)







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


   def 					snake_case				( self					):
       A       :       List[Any]							=						tempfile.mkdtemp()
       A       :       str							=						SamImageProcessor()
       A       :       Optional[Any]							=						SamProcessor(_UpperCAmelCase					)
       processor.save_pretrained(self.tmpdirname					)


   def 					snake_case				( self    ,   **_UpperCAmelCase					):
       return AutoProcessor.from_pretrained(self.tmpdirname    ,   **_UpperCAmelCase					).image_processor


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


   def 					snake_case				( self					):
       A       :       int							=						[np.random.randint(255    ,   size=(3, 30, 400)    ,   dtype=np.uinta					)]

       A       :       Union[str, Any]							=						[Image.fromarray(np.moveaxis(_UpperCAmelCase    ,   0    ,   -1					)					) for x in image_inputs]

       return image_inputs


   def 					snake_case				( self					):
       A       :       Dict							=						SamProcessor(image_processor=self.get_image_processor()					)
       processor.save_pretrained(self.tmpdirname					)

       A       :       Dict							=						self.get_image_processor(do_normalize=_UpperCAmelCase    ,   padding_value=1.0					)

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

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


   def 					snake_case				( self					):
       A       :       Any							=						self.get_image_processor()

       A       :       str							=						SamProcessor(image_processor=_UpperCAmelCase					)

       A       :       Any							=						self.prepare_image_inputs()

       A       :       Optional[int]							=						image_processor(_UpperCAmelCase    ,   return_tensors='''np'''					)
       A       :       Tuple							=						processor(images=_UpperCAmelCase    ,   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       :       Any							=						self.get_image_processor()

       A       :       Any							=						SamProcessor(image_processor=_UpperCAmelCase					)
       A       :       str							=						[tf.ones((1, 3, 5, 5)					)]

       A       :       Dict							=						[[1_764, 2_646]]

       A       :       str							=						[[683, 1_024]]
       A       :       Union[str, Any]							=						processor.post_process_masks(_UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   return_tensors='''tf'''					)
       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       A       :       Optional[Any]							=						processor.post_process_masks(
           _UpperCAmelCase    ,   tf.convert_to_tensor(_UpperCAmelCase					)    ,   tf.convert_to_tensor(_UpperCAmelCase					)    ,   return_tensors='''tf'''    ,   )
       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       # should also work with np
       A       :       int							=						[np.ones((1, 3, 5, 5)					)]
       A       :       Union[str, Any]							=						processor.post_process_masks(
           _UpperCAmelCase    ,   np.array(_UpperCAmelCase					)    ,   np.array(_UpperCAmelCase					)    ,   return_tensors='''tf'''					)

       self.assertEqual(masks[0].shape    ,   (1, 3, 1_764, 2_646)					)

       A       :       Optional[int]							=						[[1, 0], [0, 1]]
       with self.assertRaises(tf.errors.InvalidArgumentError					):
           A       :       List[str]							=						processor.post_process_masks(
               _UpperCAmelCase    ,   np.array(_UpperCAmelCase					)    ,   np.array(_UpperCAmelCase					)    ,   return_tensors='''tf'''					)







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


   def 					snake_case				( self					):
       A       :       Tuple							=						tempfile.mkdtemp()
       A       :       Optional[Any]							=						SamImageProcessor()
       A       :       Optional[int]							=						SamProcessor(_UpperCAmelCase					)
       processor.save_pretrained(self.tmpdirname					)


   def 					snake_case				( self    ,   **_UpperCAmelCase					):
       return AutoProcessor.from_pretrained(self.tmpdirname    ,   **_UpperCAmelCase					).image_processor


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


   def 					snake_case				( self					):
       A       :       Optional[Any]							=						[np.random.randint(255    ,   size=(3, 30, 400)    ,   dtype=np.uinta					)]

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

       return image_inputs


   @is_pt_tf_cross_test
   def 					snake_case				( self					):
       A       :       str							=						self.get_image_processor()

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

       A       :       Optional[int]							=						[[1_764, 2_646]]

       A       :       List[str]							=						[[683, 1_024]]
       A       :       Optional[Any]							=						processor.post_process_masks(
           _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   return_tensors='''tf'''					)
       A       :       str							=						processor.post_process_masks(
           _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   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       :       int							=						self.get_image_processor()

       A       :       Dict							=						SamProcessor(image_processor=_UpperCAmelCase					)

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

       A       :       List[Any]							=						image_processor(_UpperCAmelCase    ,   return_tensors='''pt'''					)['''pixel_values'''].numpy()
       A       :       List[str]							=						processor(images=_UpperCAmelCase    ,   return_tensors='''pt'''					)['''pixel_values'''].numpy()

       A       :       Optional[Any]							=						image_processor(_UpperCAmelCase    ,   return_tensors='''tf'''					)['''pixel_values'''].numpy()
       A       :       List[str]							=						processor(images=_UpperCAmelCase    ,   return_tensors='''tf'''					)['''pixel_values'''].numpy()

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







'''simple docstring'''






from __future__ import annotations


class   _lowercase						:


   def __init__( self    ,   _UpperCAmelCase					):
       A       :       str							=						data
       A       :       Node | None							=						None
       A       :       Node | None							=						None







def 							_lowerCamelCase(  UpperCamelCase__    : Node | None		)		->  None:  # In Order traversal of the tree
    if tree:
        display(tree.left		)
        print(tree.data		)
        display(tree.right		)
def 							_lowerCamelCase(  UpperCamelCase__    : Node | None		)		->  int:
    return 1 + max(depth_of_tree(tree.left		)    ,       depth_of_tree(tree.right		)		) if tree else 0
def 							_lowerCamelCase(  UpperCamelCase__    : Node		)		->  bool:
    if not tree:
        return True
    if tree.left and tree.right:
        return is_full_binary_tree(tree.left		) and is_full_binary_tree(tree.right		)
    else:
        return not tree.left and not tree.right
def 							_lowerCamelCase(  )		->  None:  # Main function for testing.
    A       :       Optional[int]							=						Node(1		)
    A       :       Tuple							=						Node(2		)
    A       :       Dict							=						Node(3		)
    A       :       List[str]							=						Node(4		)
    A       :       Union[str, Any]							=						Node(5		)
    A       :       str							=						Node(6		)
    A       :       Any							=						Node(7		)
    A       :       str							=						Node(8		)
    A       :       Optional[int]							=						Node(9		)

    print(is_full_binary_tree(UpperCamelCase__		)		)
    print(depth_of_tree(UpperCamelCase__		)		)
    print('''Tree is: '''		)
    display(UpperCamelCase__		)


if __name__ == "__main__":
     main()
