

"""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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      tempfile.mkdtemp()
      __magic_name__     =      SamImageProcessor()
      __magic_name__     =      SamProcessor(UpperCamelCase_       )
      processor.save_pretrained(self.tmpdirname       )







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase_       ).image_processor







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







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

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

      return image_inputs







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      SamProcessor(image_processor=self.get_image_processor()       )
      processor.save_pretrained(self.tmpdirname       )

      __magic_name__     =      self.get_image_processor(do_normalize=UpperCamelCase_ ,						padding_value=1.0       )

      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

      __magic_name__     =      SamProcessor(image_processor=UpperCamelCase_       )

      __magic_name__     =      self.prepare_image_inputs()

      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''np'''       )
      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

      __magic_name__     =      SamProcessor(image_processor=UpperCamelCase_       )
      __magic_name__     =      [torch.ones((1, 3, 5, 5)       )]

      __magic_name__     =      [[1764, 2646]]

      __magic_name__     =      [[683, 1024]]
      __magic_name__     =      processor.post_process_masks(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )
      self.assertEqual(masks[0].shape ,						(1, 3, 1764, 2646)       )

      __magic_name__     =      processor.post_process_masks(
          UpperCamelCase_ ,						torch.tensor(UpperCamelCase_       ) ,						torch.tensor(UpperCamelCase_       )       )
      self.assertEqual(masks[0].shape ,						(1, 3, 1764, 2646)       )

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

      self.assertEqual(masks[0].shape ,						(1, 3, 1764, 2646)       )

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


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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      tempfile.mkdtemp()
      __magic_name__     =      SamImageProcessor()
      __magic_name__     =      SamProcessor(UpperCamelCase_       )
      processor.save_pretrained(self.tmpdirname       )







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase_       ).image_processor







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







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

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

      return image_inputs







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      SamProcessor(image_processor=self.get_image_processor()       )
      processor.save_pretrained(self.tmpdirname       )

      __magic_name__     =      self.get_image_processor(do_normalize=UpperCamelCase_ ,						padding_value=1.0       )

      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

      __magic_name__     =      SamProcessor(image_processor=UpperCamelCase_       )

      __magic_name__     =      self.prepare_image_inputs()

      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''np'''       )
      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

      __magic_name__     =      SamProcessor(image_processor=UpperCamelCase_       )
      __magic_name__     =      [tf.ones((1, 3, 5, 5)       )]

      __magic_name__     =      [[1764, 2646]]

      __magic_name__     =      [[683, 1024]]
      __magic_name__     =      processor.post_process_masks(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						return_tensors='''tf'''       )
      self.assertEqual(masks[0].shape ,						(1, 3, 1764, 2646)       )

      __magic_name__     =      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, 1764, 2646)       )

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

      self.assertEqual(masks[0].shape ,						(1, 3, 1764, 2646)       )

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


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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      tempfile.mkdtemp()
      __magic_name__     =      SamImageProcessor()
      __magic_name__     =      SamProcessor(UpperCamelCase_       )
      processor.save_pretrained(self.tmpdirname       )







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase_       ).image_processor







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







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

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

      return image_inputs







     @is_pt_tf_cross_test
     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

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

      __magic_name__     =      [[1764, 2646]]

      __magic_name__     =      [[683, 1024]]
      __magic_name__     =      processor.post_process_masks(
          UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						return_tensors='''tf'''       )
      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_image_processor()

      __magic_name__     =      SamProcessor(image_processor=UpperCamelCase_       )

      __magic_name__     =      self.prepare_image_inputs()

      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''pt'''       )['''pixel_values'''].numpy()
      __magic_name__     =      processor(images=UpperCamelCase_ ,						return_tensors='''pt'''       )['''pixel_values'''].numpy()

      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''tf'''       )['''pixel_values'''].numpy()
      __magic_name__     =      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 collections.abc import Sequence
from queue import Queue
class        _lowercase					:







     def __init__(      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_=None ,						UpperCamelCase_=None       ):
      __magic_name__     =      start
      __magic_name__     =      end
      __magic_name__     =      val
      __magic_name__     =      (start + end) // 2
      __magic_name__     =      left
      __magic_name__     =      right







     def __repr__(      self       ):
      return f'''SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})'''


class        _lowercase					:







     def __init__(      self ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      collection
      __magic_name__     =      function
      if self.collection:
       __magic_name__     =      self._build_tree(0 ,						len(UpperCamelCase_       ) - 1       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      self._update_tree(self.root ,						UpperCamelCase_ ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      return self._query_range(self.root ,						UpperCamelCase_ ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      if start == end:
       return SegmentTreeNode(UpperCamelCase_ ,						UpperCamelCase_ ,						self.collection[start]       )
      __magic_name__     =      (start + end) // 2
      __magic_name__     =      self._build_tree(UpperCamelCase_ ,						UpperCamelCase_       )
      __magic_name__     =      self._build_tree(mid + 1 ,						UpperCamelCase_       )
      return SegmentTreeNode(UpperCamelCase_ ,						UpperCamelCase_ ,						self.fn(left.val ,						right.val       ) ,						UpperCamelCase_ ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      if node.start == i and node.end == i:
       __magic_name__     =      val
       return
      if i <= node.mid:
       self._update_tree(node.left ,						UpperCamelCase_ ,						UpperCamelCase_       )
      else:
       self._update_tree(node.right ,						UpperCamelCase_ ,						UpperCamelCase_       )
      __magic_name__     =      self.fn(node.left.val ,						node.right.val       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      if node.start == i and node.end == j:
       return node.val

      if i <= node.mid:
       if j <= node.mid:
        # range in left child tree
        return self._query_range(node.left ,						UpperCamelCase_ ,						UpperCamelCase_       )
       else:
        # range in left child tree and right child tree
        return self.fn(
            self._query_range(node.left ,						UpperCamelCase_ ,						node.mid       ) ,						self._query_range(node.right ,						node.mid + 1 ,						UpperCamelCase_       ) ,						)
      else:
       # range in right child tree
       return self._query_range(node.right ,						UpperCamelCase_ ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      if self.root is not None:
       __magic_name__     =      Queue()
       queue.put(self.root       )
       while not queue.empty():
        __magic_name__     =      queue.get()
        yield node

        if node.left is not None:
         queue.put(node.left       )

        if node.right is not None:
         queue.put(node.right       )


if __name__ == "__main__":
 import operator

 for fn in [operator.add, max, min]:
  print("*" * 50)
  __lowerCamelCase										=		SegmentTree([2, 1, 5, 3, 4], fn)
  for node in arr.traverse():
   print(node)
  print()

  arr.update(1, 5)
  for node in arr.traverse():
   print(node)
  print()

  print(arr.query_range(3, 4))  # 7
  print(arr.query_range(2, 2))  # 5
  print(arr.query_range(1, 3))  # 13
  print()

