


'''simple docstring'''







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:
   A   :  Union[str, Any]       		=				None


class 				__lowerCamelCase				( unittest.TestCase ):
    """simple docstring"""


    def __init__(						self    :							Optional[Any]   ,							SCREAMING_SNAKE_CASE    :							Optional[int]   ,							SCREAMING_SNAKE_CASE    :							str=7   ,							SCREAMING_SNAKE_CASE    :							List[Any]=3   ,							SCREAMING_SNAKE_CASE    :							Optional[int]=30   ,							SCREAMING_SNAKE_CASE    :							str=400   ,							SCREAMING_SNAKE_CASE    :							Dict=True   ,							SCREAMING_SNAKE_CASE    :							Optional[Any]=None   ,							SCREAMING_SNAKE_CASE    :							int=True   ,							SCREAMING_SNAKE_CASE    :							Tuple=None   ,							SCREAMING_SNAKE_CASE    :							List[str]=None   ,							SCREAMING_SNAKE_CASE    :							int=True   ,							SCREAMING_SNAKE_CASE    :							str=1 / 255   ,							SCREAMING_SNAKE_CASE    :							str=True   ,							SCREAMING_SNAKE_CASE    :							Tuple=FLAVA_IMAGE_MEAN   ,							SCREAMING_SNAKE_CASE    :							str=FLAVA_IMAGE_STD   ,							SCREAMING_SNAKE_CASE    :							Any=14   ,							SCREAMING_SNAKE_CASE    :							Optional[Any]=75   ,							SCREAMING_SNAKE_CASE    :							List[str]=None   ,							SCREAMING_SNAKE_CASE    :							List[str]=16   ,							SCREAMING_SNAKE_CASE    :							List[str]=0.3   ,							SCREAMING_SNAKE_CASE    :							Union[str, Any]=None   ,							SCREAMING_SNAKE_CASE    :							Optional[Any]=True   ,							SCREAMING_SNAKE_CASE    :							Dict=None   ,							SCREAMING_SNAKE_CASE    :							List[str]=None   ,							SCREAMING_SNAKE_CASE    :							Optional[int]=True   ,							SCREAMING_SNAKE_CASE    :							List[Any]=None   ,							SCREAMING_SNAKE_CASE    :							Optional[Any]=True   ,							SCREAMING_SNAKE_CASE    :							Optional[int]=True   ,							SCREAMING_SNAKE_CASE    :							List[str]=FLAVA_CODEBOOK_MEAN   ,							SCREAMING_SNAKE_CASE    :							str=FLAVA_CODEBOOK_STD   ,							):
           _A  :			int											=      size if size is not None else {'height': 224, 'width': 224}
           _A  :			List[str]											=      crop_size if crop_size is not None else {'height': 224, 'width': 224}
           _A  :			Dict											=      codebook_size if codebook_size is not None else {'height': 112, 'width': 112}
           _A  :			str											=      codebook_crop_size if codebook_crop_size is not None else {'height': 112, 'width': 112}

           _A  :			Optional[int]											=      parent
           _A  :			int											=      batch_size
           _A  :			Union[str, Any]											=      num_channels
           _A  :			Tuple											=      do_resize
           _A  :			int											=      do_rescale
           _A  :			Dict											=      rescale_factor
           _A  :			Any											=      min_resolution
           _A  :			Dict											=      max_resolution
           _A  :			List[Any]											=      size
           _A  :			Optional[int]											=      resample if resample is not None else PILImageResampling.BICUBIC
           _A  :			Dict											=      do_normalize
           _A  :			Union[str, Any]											=      image_mean
           _A  :			Optional[Any]											=      image_std
           _A  :			Union[str, Any]											=      do_center_crop
           _A  :			Any											=      crop_size

           _A  :			List[str]											=      input_size_patches
           _A  :			Any											=      total_mask_patches
           _A  :			Dict											=      mask_group_max_patches
           _A  :			Dict											=      mask_group_min_patches
           _A  :			Optional[Any]											=      mask_group_min_aspect_ratio
           _A  :			Dict											=      mask_group_max_aspect_ratio

           _A  :			int											=      codebook_do_resize
           _A  :			Union[str, Any]											=      codebook_size
           _A  :			str											=      codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
           _A  :			Dict											=      codebook_do_center_crop
           _A  :			List[Any]											=      codebook_crop_size
           _A  :			Optional[Any]											=      codebook_do_map_pixels
           _A  :			Dict											=      codebook_do_normalize
           _A  :			Dict											=      codebook_image_mean
           _A  :			Optional[int]											=      codebook_image_std


    def      A					(						self    :							Optional[int]):
           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      A					(						self    :							Union[str, Any]):
           return (self.size["height"], self.size["width"])


    def      A					(						self    :							Optional[Any]):
           return (
               (self.input_size_patches, self.input_size_patches)
               if not isinstance(self.input_size_patches   ,							SCREAMING_SNAKE_CASE)
               else self.input_size_patches
           )


    def      A					(						self    :							Union[str, Any]):
           return (self.codebook_size["height"], self.codebook_size["width"])




@require_torch
@require_vision
class 				__lowerCamelCase				( UpperCAmelCase_       ,      unittest.TestCase ):
    """simple docstring"""
    a									=					FlavaImageProcessor if is_vision_available() else None
    a									=					None


    def      A					(						self    :							Optional[Any]):
           _A  :			int											=      FlavaImageProcessingTester(self)


    @property
    def      A					(						self    :							List[Any]):
           return self.image_processor_tester.prepare_image_processor_dict()


    def      A					(						self    :							Any):
           _A  :			str											=      self.image_processing_class(**self.image_processor_dict)
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'image_mean'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'image_std'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'do_normalize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'do_resize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'resample'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'crop_size'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'do_center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'do_rescale'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'rescale_factor'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'masking_generator'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_do_resize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_size'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_resample'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_do_center_crop'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_crop_size'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_do_map_pixels'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_do_normalize'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_image_mean'))
           self.assertTrue(hasattr(SCREAMING_SNAKE_CASE   ,							'codebook_image_std'))


    def      A					(						self    :							List[Any]):
           _A  :			Tuple											=      self.image_processing_class.from_dict(self.image_processor_dict)
           self.assertEqual(image_processor.size   ,							{'height': 224, 'width': 224})
           self.assertEqual(image_processor.crop_size   ,							{'height': 224, 'width': 224})
           self.assertEqual(image_processor.codebook_size   ,							{'height': 112, 'width': 112})
           self.assertEqual(image_processor.codebook_crop_size   ,							{'height': 112, 'width': 112})

           _A  :			Optional[int]											=      self.image_processing_class.from_dict(
               self.image_processor_dict   ,							size=42   ,							crop_size=84   ,							codebook_size=33   ,							codebook_crop_size=66)
           self.assertEqual(image_processor.size   ,							{'height': 42, 'width': 42})
           self.assertEqual(image_processor.crop_size   ,							{'height': 84, 'width': 84})
           self.assertEqual(image_processor.codebook_size   ,							{'height': 33, 'width': 33})
           self.assertEqual(image_processor.codebook_crop_size   ,							{'height': 66, 'width': 66})


    def      A					(						self    :							Union[str, Any]):
           pass


    def      A					(						self    :							List[str]):
           # Initialize image_processing
           _A  :			str											=      self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           _A  :			int											=      prepare_image_inputs(self.image_processor_tester   ,							equal_resolution=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE   ,							PIL.Image.Image)

           # Test not batched input
           _A  :			Optional[int]											=      image_processing(image_inputs[0]   ,							return_tensors='pt')

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

           _A						,				_A  :			str											=      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
           _A  :			List[Any]											=      image_processing(SCREAMING_SNAKE_CASE   ,							return_tensors='pt')
           _A						,				_A  :			Any											=      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      A					(						self    :							str   ,							SCREAMING_SNAKE_CASE    :							Optional[Any]   ,							SCREAMING_SNAKE_CASE    :							List[str]):
           # Initialize image_processing
           _A  :			List[str]											=      self.image_processing_class(**self.image_processor_dict)
           # create random tensors
           _A  :			Any											=      prepare_image_inputs(self.image_processor_tester   ,							equal_resolution=SCREAMING_SNAKE_CASE   ,							**SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE   ,							SCREAMING_SNAKE_CASE)

           # Test not batched input
           _A  :			Optional[Any]											=      image_processing(image_inputs[0]   ,							return_tensors='pt')

           _A						,				_A  :			Any											=      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)   ,							)

           _A  :			Tuple											=      image_processing(SCREAMING_SNAKE_CASE   ,							return_image_mask=SCREAMING_SNAKE_CASE   ,							return_tensors='pt')

           _A						,				_A  :			Union[str, Any]											=      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,
               )   ,							)

           _A						,				_A  :			str											=      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
           _A  :			int											=      image_processing(SCREAMING_SNAKE_CASE   ,							return_tensors='pt').pixel_values

           _A						,				_A  :			Optional[int]											=      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
           _A  :			List[Any]											=      image_processing(SCREAMING_SNAKE_CASE   ,							return_image_mask=SCREAMING_SNAKE_CASE   ,							return_tensors='pt')

           _A						,				_A  :			int											=      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,
               )   ,							)

           _A						,				_A  :			Dict											=      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      A					(						self    :							Union[str, Any]):
           self._test_call_framework(np.ndarray   ,							prepare_kwargs={'numpify': True})


    def      A					(						self    :							str):
           self._test_call_framework(torch.Tensor   ,							prepare_kwargs={'torchify': True})


    def      A					(						self    :							Union[str, Any]):
           # Initialize image_processing
           random.seed(1234)
           _A  :			int											=      self.image_processing_class(**self.image_processor_dict)
           _A  :			List[str]											=      prepare_image_inputs(self.image_processor_tester   ,							equal_resolution=SCREAMING_SNAKE_CASE   ,							torchify=SCREAMING_SNAKE_CASE)

           # Test not batched input
           _A  :			Optional[int]											=      image_processing(image_inputs[0]   ,							return_image_mask=SCREAMING_SNAKE_CASE   ,							return_tensors='pt')
           self.assertEqual(encoded_images.bool_masked_pos.sum().item()   ,							75)


    def      A					(						self    :							Union[str, Any]):
           # Initialize image_processing
           _A  :			Dict											=      self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           _A  :			Optional[Any]											=      prepare_image_inputs(self.image_processor_tester   ,							equal_resolution=SCREAMING_SNAKE_CASE)
           for image in image_inputs:
                  self.assertIsInstance(SCREAMING_SNAKE_CASE   ,							PIL.Image.Image)

           # Test not batched input
           _A  :			List[Any]											=      image_processing(image_inputs[0]   ,							return_codebook_pixels=SCREAMING_SNAKE_CASE   ,							return_tensors='pt')
           _A						,				_A  :			Tuple											=      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
           _A  :			Union[str, Any]											=      image_processing(SCREAMING_SNAKE_CASE   ,							return_codebook_pixels=SCREAMING_SNAKE_CASE   ,							return_tensors='pt')
           _A						,				_A  :			List[Any]											=      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,
               )   ,							)










'''simple docstring'''







from unittest.mock import patch

import datasets
from datasets import Dataset






def    lowerCAmelCase__ (       ):
       _A  :			Optional[int]											=      Dataset.from_dict({'col_1': [3, 2, 0, 1]}     )

       with patch('tqdm.auto.tqdm'     ) as mock_tqdm:
              datasets.disable_progress_bar()
              dset.map(lambda lowerCamelCase     : {"col_2": x["col_1"] + 1}     )
              mock_tqdm.assert_not_called()

              mock_tqdm.reset_mock()

              datasets.enable_progress_bar()
              dset.map(lambda lowerCamelCase     : {"col_2": x["col_1"] + 1}     )
              mock_tqdm.assert_called()






