




import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)




class       UpperCAmelCase_   :




							'''simple docstring'''



							def __init__(       self							:						int							,  a							:						Optional[int]      )      ->       Union[str, Any]:
										SCREAMING_SNAKE_CASE     =       foo







							def __call__(       self							:						int      )      ->       List[str]:
										return self.foo





class       UpperCAmelCase_   ( datasets.Dataset							):




							'''simple docstring'''



							@fingerprint_transform(inplace=a      )
							def  _UpperCAmelCase  (       self							:						Tuple							,  a							:						List[Any]							,  *a							:						int							,  **a							:						Any      )      ->       int:
										return DatasetChild(self.data							,  fingerprint=a      )







							@fingerprint_transform(inplace=a      )
							def  _UpperCAmelCase  (       self							:						Optional[Any]							,  a							:						Optional[int]							,  *a							:						List[Any]							,  **a							:						Union[str, Any]      )      ->       Union[str, Any]:
										return DatasetChild(self.data							,  fingerprint=a      )





class       UpperCAmelCase_   :




							'''simple docstring'''



							def __init__(       self							:						Union[str, Any]							,  a							:						Optional[int]      )      ->       List[Any]:
										SCREAMING_SNAKE_CASE     =       callable





							def __call__(       self							:						Union[str, Any]							,  *a							:						List[Any]							,  **a							:						Any      )      ->       str:
										if self.callable is not None:
													return self.callable(*a							,  **a      )







							def __getstate__(       self							:						Optional[Any]      )      ->       Tuple:
										raise pickle.PicklingError()





class       UpperCAmelCase_   ( A							):




							'''simple docstring'''



							@require_transformers
							@pytest.mark.integration
							def  _UpperCAmelCase  (       self							:						int      )      ->       List[str]:
										from transformers import AutoTokenizer

										def encode(a							:						Dict      ):
													return tokenizer(a      )

										# TODO: add hash consistency tests across sessions
										SCREAMING_SNAKE_CASE     =       AutoTokenizer.from_pretrained("""bert-base-uncased"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(lambda a      : tokenizer(a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       AutoTokenizer.from_pretrained("""bert-base-cased"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(lambda a      : tokenizer(a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       AutoTokenizer.from_pretrained("""bert-base-uncased"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(lambda a      : tokenizer(a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							@require_transformers
							@pytest.mark.integration
							def  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       Any:
										from transformers import AutoTokenizer

										SCREAMING_SNAKE_CASE     =       AutoTokenizer.from_pretrained("""gpt2"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										tokenizer("""Hello world !"""      )  # call once to change the tokenizer's cache
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )







							@require_regex
							def  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       str:
										import regex

										SCREAMING_SNAKE_CASE     =       regex.Regex("""foo"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       regex.Regex("""bar"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       regex.Regex("""foo"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





class       UpperCAmelCase_   ( A							):




							'''simple docstring'''



							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       str:
										def func():
													return foo

										SCREAMING_SNAKE_CASE     =       [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       [1]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       str:
										def func():
													pass

										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()

										def func():
													pass

										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       Optional[int]:
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([0]      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([1]      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([0]      )      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       Any:
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([0]      ).__call__      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([1]      ).__call__      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(Foo([0]      ).__call__      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						List[str]      )      ->       str:
										SCREAMING_SNAKE_CASE     =       (
										    """co_argcount""",
										    """co_kwonlyargcount""",
										    """co_nlocals""",
										    """co_stacksize""",
										    """co_flags""",
										    """co_code""",
										    """co_consts""",
										    """co_names""",
										    """co_varnames""",
										    """co_filename""",
										    """co_name""",
										    """co_firstlineno""",
										    """co_lnotab""",
										    """co_freevars""",
										    """co_cellvars""",
										)

										def _create_code_pyaa(*a							:						Dict      ):
													if hasattr(args[-3]							,  """encode"""      ):
																SCREAMING_SNAKE_CASE     =       list(a      )
																SCREAMING_SNAKE_CASE     =       args[-3].encode()  # co_lnotab
																SCREAMING_SNAKE_CASE     =       args[-10].encode()  # co_code
													if hasattr(a							,  """co_posonlyargcount"""      ):
																if len(a      ) == 16:
																			return CodeType(*a      )
																elif len(a      ) == 15:
																			return CodeType(args[0]							,  0							,  *args[1:]      )
																return CodeType(args[0]							,  0							,  0							,  *args[1:]      )
													elif hasattr(a							,  """co_kwonlyargcount"""      ):
																if len(a      ) == 16:
																			return CodeType(args[0]							,  *args[2:]      )
																elif len(a      ) == 15:
																			return CodeType(*a      )
																return CodeType(args[0]							,  0							,  *args[1:]      )
													if len(a      ) == 16:
																return CodeType(args[0]							,  *args[3:]      )
													elif len(a      ) == 15:
																return CodeType(args[0]							,  *args[2:]      )
													return CodeType(*a      )

										def create_ipython_func(a							:						Optional[int]							,  a							:						Optional[Any]      ):
													def func():
																return returned_obj

													SCREAMING_SNAKE_CASE     =       func.__code__
													# Use _create_code from dill in order to make it work for different python versions
													if datasets.config.PY_VERSION < version.parse("""3.8"""      ):
																SCREAMING_SNAKE_CASE     =       _create_code_pyaa(
																    *[getattr(a							,  a      ) if k != """co_filename""" else co_filename for k in code_args_pyaa]      )
													else:
																SCREAMING_SNAKE_CASE     =       code.replace(co_filename=a      )
													return FunctionType(a							,  func.__globals__							,  func.__name__							,  func.__defaults__							,  func.__closure__      )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       """<ipython-input-2-e0383a102aae>""", [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       """<ipython-input-2-e0383a102aae>""", [1]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       """<ipython-input-5-713f6613acf3>""", [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       os.path.join(gettempdir()							,  """ipykernel_12345"""							,  """321456789.py"""      ), [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       os.path.join(gettempdir()							,  """ipykernel_12345"""							,  """321456789.py"""      ), [1]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       os.path.join(gettempdir()							,  """ipykernel_12345"""							,  """654123987.py"""      ), [0]
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(create_ipython_func(a							,  a      )      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )







							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       List[str]:
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       [0], [1]

										def func():
													return foo, bar

										SCREAMING_SNAKE_CASE     =       """__main__"""

										def globalvars_mocka_side_effect(a							:						Optional[Any]							,  *a							:						int							,  **a							:						Optional[int]      ):
													return {"foo": foo, "bar": bar}

										def globalvars_mocka_side_effect(a							:						Union[str, Any]							,  *a							:						int							,  **a							:						Union[str, Any]      ):
													return {"bar": bar, "foo": foo}

										with patch("""dill.detect.globalvars"""							,  side_effect=a      ) as globalvars_mocka:
													SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
													self.assertGreater(globalvars_mocka.call_count							,  0      )
										with patch("""dill.detect.globalvars"""							,  side_effect=a      ) as globalvars_mocka:
													SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
													self.assertGreater(globalvars_mocka.call_count							,  0      )
										self.assertEqual(a							,  a      )





class       UpperCAmelCase_   ( A							):




							'''simple docstring'''



							def  _UpperCAmelCase  (       self							:						str      )      ->       int:
										SCREAMING_SNAKE_CASE     =       Hasher.hash("""hello"""      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash("""hello"""      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash("""there"""      )
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						str      )      ->       Any:
										SCREAMING_SNAKE_CASE     =       Hasher.hash(Foo("""hello"""      )      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash(Foo("""hello"""      )      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash(Foo("""there"""      )      )
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						Tuple      )      ->       Tuple:
										SCREAMING_SNAKE_CASE     =       Hasher()
										for x in ["hello", Foo("""hello"""      )]:
													hasher.update(a      )
										SCREAMING_SNAKE_CASE     =       hasher.hexdigest()
										SCREAMING_SNAKE_CASE     =       Hasher()
										for x in ["hello", Foo("""hello"""      )]:
													hasher.update(a      )
										SCREAMING_SNAKE_CASE     =       hasher.hexdigest()
										SCREAMING_SNAKE_CASE     =       Hasher()
										for x in ["there", Foo("""there"""      )]:
													hasher.update(a      )
										SCREAMING_SNAKE_CASE     =       hasher.hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							def  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       Any:
										with self.assertRaises(pickle.PicklingError      ):
													Hasher.hash(UnpicklableCallable(Foo("""hello"""      )      )      )





							def  _UpperCAmelCase  (       self							:						Any      )      ->       Any:
										SCREAMING_SNAKE_CASE     =       """abc"""
										SCREAMING_SNAKE_CASE     =       [string, string]  # two strings have the same ids
										SCREAMING_SNAKE_CASE     =       [string, string]
										SCREAMING_SNAKE_CASE     =       json.loads(f"""[\"{string}\", \"{string}\"]"""      )  # two strings have different ids
										self.assertIs(obja[0]							,  a      )
										self.assertIs(obja[0]							,  obja[1]      )
										self.assertIs(obja[0]							,  a      )
										self.assertIs(obja[0]							,  obja[1]      )
										self.assertIsNot(obja[0]							,  a      )
										self.assertIsNot(obja[0]							,  obja[1]      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash(a      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash(a      )
										SCREAMING_SNAKE_CASE     =       Hasher.hash(a      )
										self.assertEqual(a							,  a      )
										self.assertEqual(a							,  a      )





							@require_tiktoken
							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       Union[str, Any]:
										import tiktoken

										SCREAMING_SNAKE_CASE     =       tiktoken.get_encoding("""gpt2"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       tiktoken.get_encoding("""r50k_base"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       tiktoken.get_encoding("""gpt2"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





							@require_torch
							def  _UpperCAmelCase  (       self							:						List[Any]      )      ->       Tuple:
										import torch

										SCREAMING_SNAKE_CASE     =       torch.tensor([1.0]      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       torch.tensor([2.0]      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       torch.tensor([1.0]      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )







							@require_spacy
							@require_spacy_model("""en_core_web_sm"""      )
							@require_spacy_model("""fr_core_news_sm"""      )
							@pytest.mark.integration
							def  _UpperCAmelCase  (       self							:						int      )      ->       Any:
										import spacy

										SCREAMING_SNAKE_CASE     =       spacy.load("""en_core_web_sm"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       spacy.load("""fr_core_news_sm"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										SCREAMING_SNAKE_CASE     =       spacy.load("""en_core_web_sm"""      )
										SCREAMING_SNAKE_CASE     =       mda(datasets.utils.py_utils.dumps(a      )      ).hexdigest()
										self.assertEqual(a							,  a      )
										self.assertNotEqual(a							,  a      )





@pytest.mark.integration
def        lowerCamelCase_     (     SCREAMING_SNAKE_CASE ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE     =       tmp_path / """dir1"""
			SCREAMING_SNAKE_CASE     =       tmp_path / """dir2"""
			dira.mkdir()
			dira.mkdir()
			SCREAMING_SNAKE_CASE     =       """script.py"""
			SCREAMING_SNAKE_CASE     =       dedent(
			    """
    from datasets.fingerprint import Hasher
    def foo():
        pass
    print(Hasher.hash(foo))
    """ )
			SCREAMING_SNAKE_CASE     =       dira / script_filename
			SCREAMING_SNAKE_CASE     =       dira / script_filename
			with script_patha.open("""w""" ) as f:
						f.write(SCREAMING_SNAKE_CASE )
			with script_patha.open("""w""" ) as f:
						f.write(SCREAMING_SNAKE_CASE )
			SCREAMING_SNAKE_CASE     =       subprocess.check_output(["""python""", str(SCREAMING_SNAKE_CASE )] )
			SCREAMING_SNAKE_CASE     =       subprocess.check_output(["""python""", str(SCREAMING_SNAKE_CASE )] )
			assert fingerprinta == fingerprinta
def        lowerCamelCase_     (     ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE     =       {"""a""": [0, 1, 2]}
			SCREAMING_SNAKE_CASE     =       DatasetChild(InMemoryTable.from_pydict(SCREAMING_SNAKE_CASE ) )
			SCREAMING_SNAKE_CASE     =       dataset.funca()._fingerprint
			assert expected_fingerprint == dataset.funca()._fingerprint
			assert expected_fingerprint != dataset.funca()._fingerprint

			with Pool(2 ) as p:
						assert expected_fingerprint == p.apply_async(dataset.funca ).get()._fingerprint
						assert expected_fingerprint != p.apply_async(dataset.funca ).get()._fingerprint
def        lowerCamelCase_     (     ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE     =       {"""a""": [0, 1, 2]}




			class       UpperCAmelCase_   ( datasets.Dataset							):




										'''simple docstring'''



										@fingerprint_transform(inplace=a      )
										def  _UpperCAmelCase  (       self							:						Dict							,  a							:						List[Any]      )      ->       int:
													return DummyDatasetChild(self.data							,  fingerprint=a      )





			SCREAMING_SNAKE_CASE     =       DummyDatasetChild(InMemoryTable.from_pydict(SCREAMING_SNAKE_CASE ) ).func()




			class       UpperCAmelCase_   ( datasets.Dataset							):




										'''simple docstring'''



										@fingerprint_transform(inplace=a							,  version="""1.0.0"""      )
										def  _UpperCAmelCase  (       self							:						List[Any]							,  a							:						List[Any]      )      ->       Optional[int]:
													return DummyDatasetChild(self.data							,  fingerprint=a      )





			SCREAMING_SNAKE_CASE     =       DummyDatasetChild(InMemoryTable.from_pydict(SCREAMING_SNAKE_CASE ) ).func()




			class       UpperCAmelCase_   ( datasets.Dataset							):




										'''simple docstring'''



										@fingerprint_transform(inplace=a							,  version="""2.0.0"""      )
										def  _UpperCAmelCase  (       self							:						Tuple							,  a							:						Union[str, Any]      )      ->       List[str]:
													return DummyDatasetChild(self.data							,  fingerprint=a      )





			SCREAMING_SNAKE_CASE     =       DummyDatasetChild(InMemoryTable.from_pydict(SCREAMING_SNAKE_CASE ) ).func()

			assert len({fingeprint_no_version, fingeprint_a, fingeprint_a} ) == 3




def        lowerCamelCase_     (     ):
			'''simple docstring'''
			# AttributeError: module 'dill._dill' has no attribute 'stack'
			SCREAMING_SNAKE_CASE     =       Hasher()
			hasher.update(lambda SCREAMING_SNAKE_CASE : x )












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    :				List[str]    						=	None




class       UpperCAmelCase_   ( unittest.TestCase							):




							'''simple docstring'''



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

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

										SCREAMING_SNAKE_CASE     =       input_size_patches
										SCREAMING_SNAKE_CASE     =       total_mask_patches
										SCREAMING_SNAKE_CASE     =       mask_group_max_patches
										SCREAMING_SNAKE_CASE     =       mask_group_min_patches
										SCREAMING_SNAKE_CASE     =       mask_group_min_aspect_ratio
										SCREAMING_SNAKE_CASE     =       mask_group_max_aspect_ratio

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





							def  _UpperCAmelCase  (       self							:						str      )      ->       str:
										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  _UpperCAmelCase  (       self							:						Dict      )      ->       List[Any]:
										return (self.size["height"], self.size["width"])





							def  _UpperCAmelCase  (       self							:						int      )      ->       int:
										return (
										    (self.input_size_patches, self.input_size_patches)
										    if not isinstance(self.input_size_patches							,  a      )
										    else self.input_size_patches
										)







							def  _UpperCAmelCase  (       self							:						str      )      ->       List[Any]:
										return (self.codebook_size["height"], self.codebook_size["width"])





@require_torch
@require_vision
class       UpperCAmelCase_   ( A		,   unittest.TestCase							):




							'''simple docstring'''



							a__    	=  FlavaImageProcessor if is_vision_available() else None
							a__    	=  None





							def  _UpperCAmelCase  (       self							:						Dict      )      ->       Any:
										SCREAMING_SNAKE_CASE     =       FlavaImageProcessingTester(self      )





							@property
							def  _UpperCAmelCase  (       self							:						Dict      )      ->       Any:
										return self.image_processor_tester.prepare_image_processor_dict()





							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       str:
										SCREAMING_SNAKE_CASE     =       self.image_processing_class(**self.image_processor_dict      )
										self.assertTrue(hasattr(a							,  """image_mean"""      )      )
										self.assertTrue(hasattr(a							,  """image_std"""      )      )
										self.assertTrue(hasattr(a							,  """do_normalize"""      )      )
										self.assertTrue(hasattr(a							,  """do_resize"""      )      )
										self.assertTrue(hasattr(a							,  """resample"""      )      )
										self.assertTrue(hasattr(a							,  """crop_size"""      )      )
										self.assertTrue(hasattr(a							,  """do_center_crop"""      )      )
										self.assertTrue(hasattr(a							,  """do_rescale"""      )      )
										self.assertTrue(hasattr(a							,  """rescale_factor"""      )      )
										self.assertTrue(hasattr(a							,  """masking_generator"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_do_resize"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_size"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_resample"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_do_center_crop"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_crop_size"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_do_map_pixels"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_do_normalize"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_image_mean"""      )      )
										self.assertTrue(hasattr(a							,  """codebook_image_std"""      )      )





							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE     =       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}      )

										SCREAMING_SNAKE_CASE     =       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  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       Optional[int]:
										pass





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

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

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

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

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

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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)							,  )

										SCREAMING_SNAKE_CASE     =       image_processing(a							,  return_image_mask=a							,  return_tensors="""pt"""      )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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,
										    )							,  )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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
										SCREAMING_SNAKE_CASE     =       image_processing(a							,  return_tensors="""pt"""      ).pixel_values

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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
										SCREAMING_SNAKE_CASE     =       image_processing(a							,  return_image_mask=a							,  return_tensors="""pt"""      )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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,
										    )							,  )

										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       Dict:
										self._test_call_framework(np.ndarray							,  prepare_kwargs={"""numpify""": True}      )





							def  _UpperCAmelCase  (       self							:						Dict      )      ->       Dict:
										self._test_call_framework(torch.Tensor							,  prepare_kwargs={"""torchify""": True}      )





							def  _UpperCAmelCase  (       self							:						str      )      ->       Optional[int]:
										# Initialize image_processing
										random.seed(1_234      )
										SCREAMING_SNAKE_CASE     =       self.image_processing_class(**self.image_processor_dict      )
										SCREAMING_SNAKE_CASE     =       prepare_image_inputs(self.image_processor_tester							,  equal_resolution=a							,  torchify=a      )

										# Test not batched input
										SCREAMING_SNAKE_CASE     =       image_processing(image_inputs[0]							,  return_image_mask=a							,  return_tensors="""pt"""      )
										self.assertEqual(encoded_images.bool_masked_pos.sum().item()							,  75      )







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

										# Test not batched input
										SCREAMING_SNAKE_CASE     =       image_processing(image_inputs[0]							,  return_codebook_pixels=a							,  return_tensors="""pt"""      )
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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
										SCREAMING_SNAKE_CASE     =       image_processing(a							,  return_codebook_pixels=a							,  return_tensors="""pt"""      )
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       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,
										    )							,  )






