





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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():
      from PIL import Image

      from transformers import DonutImageProcessor





class      lowercase__    (							unittest.TestCase  ):







  def __init__(	self    ,						_lowercase    ,						_lowercase=7    ,						_lowercase=3    ,						_lowercase=18    ,						_lowercase=30    ,						_lowercase=400    ,						_lowercase=True    ,						_lowercase=None    ,						_lowercase=True    ,						_lowercase=False    ,						_lowercase=True    ,						_lowercase=True    ,						_lowercase=[0.5, 0.5, 0.5]    ,						_lowercase=[0.5, 0.5, 0.5]    ,						):
       lowerCAmelCase_					:       Optional[Any]    =		parent
       lowerCAmelCase_					:       Any    =		batch_size
       lowerCAmelCase_					:       str    =		num_channels
       lowerCAmelCase_					:       Union[str, Any]    =		image_size
       lowerCAmelCase_					:       Union[str, Any]    =		min_resolution
       lowerCAmelCase_					:       str    =		max_resolution
       lowerCAmelCase_					:       str    =		do_resize
       lowerCAmelCase_					:       List[str]    =		size if size is not None else {"""height""": 18, """width""": 20}
       lowerCAmelCase_					:       Tuple    =		do_thumbnail
       lowerCAmelCase_					:       str    =		do_align_axis
       lowerCAmelCase_					:       Optional[Any]    =		do_pad
       lowerCAmelCase_					:       List[str]    =		do_normalize
       lowerCAmelCase_					:       Tuple    =		image_mean
       lowerCAmelCase_					:       Any    =		image_std







  def 	UpperCAmelCase__		(	self						):
       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_thumbnail": self.do_thumbnail,
           "do_align_long_axis": self.do_align_axis,
           "do_pad": self.do_pad,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
       }





@require_torch
@require_vision
class      lowercase__    (							__A     ,					unittest.TestCase  ):
  __UpperCamelCase	     =  DonutImageProcessor if is_vision_available() else None







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       List[str]    =		DonutImageProcessingTester(self						)







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







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_resize"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""size"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_thumbnail"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_align_long_axis"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_pad"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_normalize"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""image_mean"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""image_std"""						)						)







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       str    =		self.image_processing_class.from_dict(self.image_processor_dict						)
       self.assertEqual(image_processor.size    ,						{"""height""": 18, """width""": 20}						)

       lowerCAmelCase_					:       str    =		self.image_processing_class.from_dict(self.image_processor_dict    ,						size=42						)
       self.assertEqual(image_processor.size    ,						{"""height""": 42, """width""": 42}						)

       # Previous config had dimensions in (width, height) order
       lowerCAmelCase_					:       Dict    =		self.image_processing_class.from_dict(self.image_processor_dict    ,						size=(42, 84)						)
       self.assertEqual(image_processor.size    ,						{"""height""": 84, """width""": 42}						)







  def 	UpperCAmelCase__		(	self						):
       pass







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       # create random PIL images
       lowerCAmelCase_					:       Optional[Any]    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						Image.Image						)

       # Test not batched input
       lowerCAmelCase_					:       int    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       Union[str, Any]    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       str    =		self.image_processing_class(**self.image_processor_dict						)
       # create random numpy tensors
       lowerCAmelCase_					:       int    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase    ,						numpify=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						np.ndarray						)

       # Test not batched input
       lowerCAmelCase_					:       Optional[Any]    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       Any    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       # create random PyTorch tensors
       lowerCAmelCase_					:       List[str]    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase    ,						torchify=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						torch.Tensor						)

       # Test not batched input
       lowerCAmelCase_					:       List[str]    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       str    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







from collections.abc import Sequence






def 							_lowerCAmelCase (  _a   :					Sequence[float]					,   _a   :					bool = False			)	->			float:
     if not arr:
          return 0

     lowerCAmelCase_					:       List[str]    =		0 if allow_empty_subarrays else float("""-inf"""			)
     lowerCAmelCase_					:       List[Any]    =		0.0
     for num in arr:
          lowerCAmelCase_					:       int    =		max(0 if allow_empty_subarrays else num					,   curr_sum + num			)
          lowerCAmelCase_					:       List[str]    =		max(_a					,   _a			)

     return max_sum


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

      testmod()

      UpperCAmelCase_					:   int 							=						[-2, 1, -3, 4, -1, 2, 1, -5, 4]
      print(F"""{max_subarray_sum(nums) = }""")
