import random
import unittest

import numpy as np

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

      from transformers import FlavaImageProcessor
      from transformers.image_utils import PILImageResampling
      from transformers.models.flava.image_processing_flava import (
          FLAVA_CODEBOOK_MEAN,
          FLAVA_CODEBOOK_STD,
          FLAVA_IMAGE_MEAN,
          FLAVA_IMAGE_STD,
      )
else:
      UpperCamelCase__       							=      UpperCamelCase__       							=      UpperCamelCase__       							=      UpperCamelCase__       							=      None





class   A							(       unittest.TestCase  ):


   def __init__(self  :		int ,						__UpperCAmelCase  :		int ,						__UpperCAmelCase  :		Any=7 ,						__UpperCAmelCase  :		Tuple=3 ,						__UpperCAmelCase  :		int=3_0 ,						__UpperCAmelCase  :		Union[str, Any]=4_0_0 ,						__UpperCAmelCase  :		Dict=True ,						__UpperCAmelCase  :		int=None ,						__UpperCAmelCase  :		List[str]=True ,						__UpperCAmelCase  :		Dict=None ,						__UpperCAmelCase  :		Any=None ,						__UpperCAmelCase  :		List[Any]=True ,						__UpperCAmelCase  :		Any=1 / 2_5_5 ,						__UpperCAmelCase  :		Optional[int]=True ,						__UpperCAmelCase  :		Any=FLAVA_IMAGE_MEAN ,						__UpperCAmelCase  :		str=FLAVA_IMAGE_STD ,						__UpperCAmelCase  :		Optional[int]=1_4 ,						__UpperCAmelCase  :		List[str]=7_5 ,						__UpperCAmelCase  :		Union[str, Any]=None ,						__UpperCAmelCase  :		List[Any]=1_6 ,						__UpperCAmelCase  :		Optional[Any]=0.3 ,						__UpperCAmelCase  :		Any=None ,						__UpperCAmelCase  :		Dict=True ,						__UpperCAmelCase  :		Optional[int]=None ,						__UpperCAmelCase  :		Optional[Any]=None ,						__UpperCAmelCase  :		List[str]=True ,						__UpperCAmelCase  :		Optional[int]=None ,						__UpperCAmelCase  :		Optional[int]=True ,						__UpperCAmelCase  :		Optional[int]=True ,						__UpperCAmelCase  :		List[Any]=FLAVA_CODEBOOK_MEAN ,						__UpperCAmelCase  :		Dict=FLAVA_CODEBOOK_STD ,						)		->       Optional[int]:
         """simple docstring"""



         UpperCAmelCase__			=						size if size is not None else {"height": 2_2_4, "width": 2_2_4}
         UpperCAmelCase__			=						crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
         UpperCAmelCase__			=						codebook_size if codebook_size is not None else {"height": 1_1_2, "width": 1_1_2}
         UpperCAmelCase__			=						codebook_crop_size if codebook_crop_size is not None else {"height": 1_1_2, "width": 1_1_2}

         UpperCAmelCase__			=						parent
         UpperCAmelCase__			=						batch_size
         UpperCAmelCase__			=						num_channels
         UpperCAmelCase__			=						do_resize
         UpperCAmelCase__			=						do_rescale
         UpperCAmelCase__			=						rescale_factor
         UpperCAmelCase__			=						min_resolution
         UpperCAmelCase__			=						max_resolution
         UpperCAmelCase__			=						size
         UpperCAmelCase__			=						resample if resample is not None else PILImageResampling.BICUBIC
         UpperCAmelCase__			=						do_normalize
         UpperCAmelCase__			=						image_mean
         UpperCAmelCase__			=						image_std
         UpperCAmelCase__			=						do_center_crop
         UpperCAmelCase__			=						crop_size

         UpperCAmelCase__			=						input_size_patches
         UpperCAmelCase__			=						total_mask_patches
         UpperCAmelCase__			=						mask_group_max_patches
         UpperCAmelCase__			=						mask_group_min_patches
         UpperCAmelCase__			=						mask_group_min_aspect_ratio
         UpperCAmelCase__			=						mask_group_max_aspect_ratio

         UpperCAmelCase__			=						codebook_do_resize
         UpperCAmelCase__			=						codebook_size
         UpperCAmelCase__			=						codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
         UpperCAmelCase__			=						codebook_do_center_crop
         UpperCAmelCase__			=						codebook_crop_size
         UpperCAmelCase__			=						codebook_do_map_pixels
         UpperCAmelCase__			=						codebook_do_normalize
         UpperCAmelCase__			=						codebook_image_mean
         UpperCAmelCase__			=						codebook_image_std


   def     lowercase_   (self  :		Optional[Any]	)		->       Optional[Any]:
         """simple docstring"""



         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
             "resample": self.resample,
             "do_rescale": self.do_rescale,
             "rescale_factor": self.rescale_factor,
             "do_center_crop": self.do_center_crop,
             "crop_size": self.crop_size,
             "input_size_patches": self.input_size_patches,
             "total_mask_patches": self.total_mask_patches,
             "mask_group_max_patches": self.mask_group_max_patches,
             "mask_group_min_patches": self.mask_group_min_patches,
             "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
             "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
             "codebook_do_resize": self.codebook_do_resize,
             "codebook_size": self.codebook_size,
             "codebook_resample": self.codebook_resample,
             "codebook_do_center_crop": self.codebook_do_center_crop,
             "codebook_crop_size": self.codebook_crop_size,
             "codebook_do_map_pixels": self.codebook_do_map_pixels,
             "codebook_do_normalize": self.codebook_do_normalize,
             "codebook_image_mean": self.codebook_image_mean,
             "codebook_image_std": self.codebook_image_std,
         }


   def     lowercase_   (self  :		str	)		->       Tuple:
         """simple docstring"""



         return (self.size["height"], self.size["width"])


   def     lowercase_   (self  :		Optional[Any]	)		->       int:
         """simple docstring"""



         return (
             (self.input_size_patches, self.input_size_patches)
             if not isinstance(self.input_size_patches ,						__UpperCAmelCase	)
             else self.input_size_patches
         )





   def     lowercase_   (self  :		Optional[int]	)		->       Optional[int]:
         """simple docstring"""



         return (self.codebook_size["height"], self.codebook_size["width"])





@require_torch
@require_vision
class   A							(       UpperCAmelCase_	,       unittest.TestCase  ):
   __UpperCAmelCase				:				str          =    FlavaImageProcessor if is_vision_available() else None
   __UpperCAmelCase				:				Union[str, Any]          =    None


   def     lowercase_   (self  :		Optional[Any]	)		->       List[str]:
         """simple docstring"""



         UpperCAmelCase__			=						FlavaImageProcessingTester(self	)


   @property
   def     lowercase_   (self  :		int	)		->       Optional[Any]:
         """simple docstring"""



         return self.image_processor_tester.prepare_image_processor_dict()


   def     lowercase_   (self  :		Union[str, Any]	)		->       Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__			=						self.image_processing_class(**self.image_processor_dict	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"image_mean"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"image_std"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"do_normalize"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"do_resize"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"resample"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"crop_size"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"do_center_crop"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"do_rescale"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"rescale_factor"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"masking_generator"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_do_resize"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_size"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_resample"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_do_center_crop"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_crop_size"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_do_map_pixels"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_do_normalize"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_image_mean"	)	)
         self.assertTrue(hasattr(__UpperCAmelCase ,						"codebook_image_std"	)	)


   def     lowercase_   (self  :		Optional[Any]	)		->       Dict:
         """simple docstring"""



         UpperCAmelCase__			=						self.image_processing_class.from_dict(self.image_processor_dict	)
         self.assertEqual(image_processor.size ,						{"height": 2_2_4, "width": 2_2_4}	)
         self.assertEqual(image_processor.crop_size ,						{"height": 2_2_4, "width": 2_2_4}	)
         self.assertEqual(image_processor.codebook_size ,						{"height": 1_1_2, "width": 1_1_2}	)
         self.assertEqual(image_processor.codebook_crop_size ,						{"height": 1_1_2, "width": 1_1_2}	)

         UpperCAmelCase__			=						self.image_processing_class.from_dict(
             self.image_processor_dict ,						size=4_2 ,						crop_size=8_4 ,						codebook_size=3_3 ,						codebook_crop_size=6_6	)
         self.assertEqual(image_processor.size ,						{"height": 4_2, "width": 4_2}	)
         self.assertEqual(image_processor.crop_size ,						{"height": 8_4, "width": 8_4}	)
         self.assertEqual(image_processor.codebook_size ,						{"height": 3_3, "width": 3_3}	)
         self.assertEqual(image_processor.codebook_crop_size ,						{"height": 6_6, "width": 6_6}	)


   def     lowercase_   (self  :		Tuple	)		->       Dict:
         """simple docstring"""



         pass


   def     lowercase_   (self  :		List[Any]	)		->       Any:
         """simple docstring"""



         UpperCAmelCase__			=						self.image_processing_class(**self.image_processor_dict	)
         # create random PIL images
         UpperCAmelCase__			=						prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__UpperCAmelCase	)
         for image in image_inputs:
               self.assertIsInstance(__UpperCAmelCase ,						PIL.Image.Image	)

         # Test not batched input
         UpperCAmelCase__			=						image_processing(image_inputs[0] ,						return_tensors="pt"	)

         # Test no bool masked pos
         self.assertFalse("bool_masked_pos" in encoded_images	)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()

         self.assertEqual(
             encoded_images.pixel_values.shape ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width) ,						)

         # Test batched
         UpperCAmelCase__			=						image_processing(__UpperCAmelCase ,						return_tensors="pt"	)
         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()

         # Test no bool masked pos
         self.assertFalse("bool_masked_pos" in encoded_images	)

         self.assertEqual(
             encoded_images.pixel_values.shape ,						(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             ) ,						)


   def     lowercase_   (self  :		Dict ,						__UpperCAmelCase  :		Dict ,						__UpperCAmelCase  :		Union[str, Any]	)		->       List[str]:
         """simple docstring"""



         UpperCAmelCase__			=						self.image_processing_class(**self.image_processor_dict	)
         # create random tensors
         UpperCAmelCase__			=						prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__UpperCAmelCase ,						**__UpperCAmelCase	)
         for image in image_inputs:
               self.assertIsInstance(__UpperCAmelCase ,						__UpperCAmelCase	)

         # Test not batched input
         UpperCAmelCase__			=						image_processing(image_inputs[0] ,						return_tensors="pt"	)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()
         self.assertEqual(
             encoded_images.pixel_values.shape ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width) ,						)

         UpperCAmelCase__			=						image_processing(__UpperCAmelCase ,						return_image_mask=__UpperCAmelCase ,						return_tensors="pt"	)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()
         self.assertEqual(
             encoded_images.pixel_values.shape ,						(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             ) ,						)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_mask_size()
         self.assertEqual(
             encoded_images.bool_masked_pos.shape ,						(
                 self.image_processor_tester.batch_size,
                 expected_height,
                 expected_width,
             ) ,						)

         # Test batched
         UpperCAmelCase__			=						image_processing(__UpperCAmelCase ,						return_tensors="pt"	).pixel_values

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()
         self.assertEqual(
             encoded_images.shape ,						(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             ) ,						)

         # Test masking
         UpperCAmelCase__			=						image_processing(__UpperCAmelCase ,						return_image_mask=__UpperCAmelCase ,						return_tensors="pt"	)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_image_size()
         self.assertEqual(
             encoded_images.pixel_values.shape ,						(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             ) ,						)

         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_mask_size()
         self.assertEqual(
             encoded_images.bool_masked_pos.shape ,						(
                 self.image_processor_tester.batch_size,
                 expected_height,
                 expected_width,
             ) ,						)


   def     lowercase_   (self  :		Dict	)		->       Tuple:
         """simple docstring"""



         self._test_call_framework(np.ndarray ,						prepare_kwargs={"numpify": True}	)


   def     lowercase_   (self  :		Dict	)		->       List[str]:
         """simple docstring"""



         self._test_call_framework(torch.Tensor ,						prepare_kwargs={"torchify": True}	)


   def     lowercase_   (self  :		List[str]	)		->       Dict:
         """simple docstring"""



         random.seed(1_2_3_4	)
         UpperCAmelCase__			=						self.image_processing_class(**self.image_processor_dict	)
         UpperCAmelCase__			=						prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__UpperCAmelCase ,						torchify=__UpperCAmelCase	)

         # Test not batched input
         UpperCAmelCase__			=						image_processing(image_inputs[0] ,						return_image_mask=__UpperCAmelCase ,						return_tensors="pt"	)
         self.assertEqual(encoded_images.bool_masked_pos.sum().item() ,						7_5	)





   def     lowercase_   (self  :		int	)		->       List[str]:
         """simple docstring"""



         UpperCAmelCase__			=						self.image_processing_class(**self.image_processor_dict	)
         # create random PIL images
         UpperCAmelCase__			=						prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__UpperCAmelCase	)
         for image in image_inputs:
               self.assertIsInstance(__UpperCAmelCase ,						PIL.Image.Image	)

         # Test not batched input
         UpperCAmelCase__			=						image_processing(image_inputs[0] ,						return_codebook_pixels=__UpperCAmelCase ,						return_tensors="pt"	)
         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_codebook_image_size()
         self.assertEqual(
             encoded_images.codebook_pixel_values.shape ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width) ,						)

         # Test batched
         UpperCAmelCase__			=						image_processing(__UpperCAmelCase ,						return_codebook_pixels=__UpperCAmelCase ,						return_tensors="pt"	)
         UpperCAmelCase__	,   UpperCAmelCase__			=						self.image_processor_tester.get_expected_codebook_image_size()
         self.assertEqual(
             encoded_images.codebook_pixel_values.shape ,						(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             ) ,						)




UpperCamelCase__       							=      [
    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
]




def      lowerCAmelCase_				(  __A,						__A,						__A,						__A							)  ->       bool:







      '''simple docstring'''


      return 0 <= row < rows and 0 <= col < cols




def      lowerCAmelCase_				(  __A,						__A,						__A,						__A							)  ->       int:







      '''simple docstring'''


      UpperCAmelCase__			=						len(__A							)
      UpperCAmelCase__			=						len(mat[0]							)
      if is_safe(__A,						__A,						__A,						__A							) and (row, col) not in seen and mat[row][col] == 1:
            seen.add((row, col)							)
            return (
                1
                + depth_first_search(row + 1,						__A,						__A,						__A							)
                + depth_first_search(row - 1,						__A,						__A,						__A							)
                + depth_first_search(__A,						col + 1,						__A,						__A							)
                + depth_first_search(__A,						col - 1,						__A,						__A							)
            )
      else:
            return 0




def      lowerCAmelCase_				(  __A							)  ->       int:







      '''simple docstring'''


      UpperCAmelCase__			=						set()

      UpperCAmelCase__			=						0
      for row, line in enumerate(__A							):
            for col, item in enumerate(__A							):
                  if item == 1 and (row, col) not in seen:
                        # Maximizing the area
                        UpperCAmelCase__			=						max(__A,						depth_first_search(__A,						__A,						__A,						__A							)							)
      return max_area


if __name__ == "__main__":
      import doctest

      print(find_max_area(matrix))  # Output -> 6
      doctest.testmod()



