




import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import CLIPImageProcessor, CLIPProcessor




@require_vision
class 		__A   (      unittest.TestCase      ):

						def      A__    (		self :str			):
										'''simple docstring'''

										__magic_name__						:						str										=tempfile.mkdtemp()

										# fmt: off
										__magic_name__						:						str										=["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
										# fmt: on
										__magic_name__						:						Any										=dict(zip(__snake_case     ,			range(len(__snake_case			)			)			)			)
										__magic_name__						:						Union[str, Any]										=["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
										__magic_name__						:						Any										={"""unk_token""": """<unk>"""}

										__magic_name__						:						List[Any]										=os.path.join(self.tmpdirname     ,			VOCAB_FILES_NAMES["""vocab_file"""]			)
										__magic_name__						:						Dict										=os.path.join(self.tmpdirname     ,			VOCAB_FILES_NAMES["""merges_file"""]			)
										with open(self.vocab_file     ,			"""w"""     ,			encoding="""utf-8"""			) as fp:
														fp.write(json.dumps(__snake_case			) + """\n"""			)
										with open(self.merges_file     ,			"""w"""     ,			encoding="""utf-8"""			) as fp:
														fp.write("""\n""".join(__snake_case			)			)

										__magic_name__						:						List[str]										={
										    """do_resize""": True,
										    """size""": 20,
										    """do_center_crop""": True,
										    """crop_size""": 18,
										    """do_normalize""": True,
										    """image_mean""": [0.48145466, 0.4578275, 0.40821073],
										    """image_std""": [0.26862954, 0.26130258, 0.27577711],
										}
										__magic_name__						:						str										=os.path.join(self.tmpdirname     ,			__snake_case			)
										with open(self.image_processor_file     ,			"""w"""     ,			encoding="""utf-8"""			) as fp:
														json.dump(__snake_case     ,			__snake_case			)

						def      A__    (		self :Dict     ,			**__snake_case :Any			):
										'''simple docstring'''

										return CLIPTokenizer.from_pretrained(self.tmpdirname     ,			**__snake_case			)

						def      A__    (		self :List[Any]     ,			**__snake_case :List[Any]			):
										'''simple docstring'''

										return CLIPTokenizerFast.from_pretrained(self.tmpdirname     ,			**__snake_case			)

						def      A__    (		self :Tuple     ,			**__snake_case :List[Any]			):
										'''simple docstring'''

										return CLIPImageProcessor.from_pretrained(self.tmpdirname     ,			**__snake_case			)

						def      A__    (		self :Optional[int]			):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname			)

						def      A__    (		self :Any			):
										'''simple docstring'''

										__magic_name__						:						Dict										=[np.random.randint(2_55     ,			size=(3, 30, 4_00)     ,			dtype=np.uinta			)]

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

										return image_inputs

						def      A__    (		self :Tuple			):
										'''simple docstring'''

										__magic_name__						:						Optional[int]										=self.get_tokenizer()
										__magic_name__						:						List[Any]										=self.get_rust_tokenizer()
										__magic_name__						:						Union[str, Any]										=self.get_image_processor()

										__magic_name__						:						Optional[Any]										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)
										processor_slow.save_pretrained(self.tmpdirname			)
										__magic_name__						:						int										=CLIPProcessor.from_pretrained(self.tmpdirname     ,			use_fast=__snake_case			)

										__magic_name__						:						Optional[Any]										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)
										processor_fast.save_pretrained(self.tmpdirname			)
										__magic_name__						:						List[Any]										=CLIPProcessor.from_pretrained(self.tmpdirname			)

										self.assertEqual(processor_slow.tokenizer.get_vocab()     ,			tokenizer_slow.get_vocab()			)
										self.assertEqual(processor_fast.tokenizer.get_vocab()     ,			tokenizer_fast.get_vocab()			)
										self.assertEqual(tokenizer_slow.get_vocab()     ,			tokenizer_fast.get_vocab()			)
										self.assertIsInstance(processor_slow.tokenizer     ,			__snake_case			)
										self.assertIsInstance(processor_fast.tokenizer     ,			__snake_case			)

										self.assertEqual(processor_slow.image_processor.to_json_string()     ,			image_processor.to_json_string()			)
										self.assertEqual(processor_fast.image_processor.to_json_string()     ,			image_processor.to_json_string()			)
										self.assertIsInstance(processor_slow.image_processor     ,			__snake_case			)
										self.assertIsInstance(processor_fast.image_processor     ,			__snake_case			)

						def      A__    (		self :Tuple			):
										'''simple docstring'''

										__magic_name__						:						int										=CLIPProcessor(tokenizer=self.get_tokenizer()     ,			image_processor=self.get_image_processor()			)
										processor.save_pretrained(self.tmpdirname			)

										__magic_name__						:						Any										=self.get_tokenizer(bos_token="""(BOS)"""     ,			eos_token="""(EOS)"""			)
										__magic_name__						:						Dict										=self.get_image_processor(do_normalize=__snake_case     ,			padding_value=1.0			)

										__magic_name__						:						Tuple										=CLIPProcessor.from_pretrained(
										    self.tmpdirname     ,			bos_token="""(BOS)"""     ,			eos_token="""(EOS)"""     ,			do_normalize=__snake_case     ,			padding_value=1.0			)

										self.assertEqual(processor.tokenizer.get_vocab()     ,			tokenizer_add_kwargs.get_vocab()			)
										self.assertIsInstance(processor.tokenizer     ,			__snake_case			)

										self.assertEqual(processor.image_processor.to_json_string()     ,			image_processor_add_kwargs.to_json_string()			)
										self.assertIsInstance(processor.image_processor     ,			__snake_case			)

						def      A__    (		self :Optional[Any]			):
										'''simple docstring'''

										__magic_name__						:						List[Any]										=self.get_image_processor()
										__magic_name__						:						int										=self.get_tokenizer()

										__magic_name__						:						Dict										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)

										__magic_name__						:						int										=self.prepare_image_inputs()

										__magic_name__						:						Union[str, Any]										=image_processor(__snake_case     ,			return_tensors="""np"""			)
										__magic_name__						:						Any										=processor(images=__snake_case     ,			return_tensors="""np"""			)

										for key in input_image_proc.keys():
														self.assertAlmostEqual(input_image_proc[key].sum()     ,			input_processor[key].sum()     ,			delta=1E-2			)

						def      A__    (		self :Tuple			):
										'''simple docstring'''

										__magic_name__						:						str										=self.get_image_processor()
										__magic_name__						:						Tuple										=self.get_tokenizer()

										__magic_name__						:						List[str]										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)

										__magic_name__						:						Any										="""lower newer"""

										__magic_name__						:						Tuple										=processor(text=__snake_case			)

										__magic_name__						:						Dict										=tokenizer(__snake_case			)

										for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key]     ,			encoded_processor[key]			)

						def      A__    (		self :str			):
										'''simple docstring'''

										__magic_name__						:						str										=self.get_image_processor()
										__magic_name__						:						List[str]										=self.get_tokenizer()

										__magic_name__						:						Optional[Any]										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)

										__magic_name__						:						List[Any]										="""lower newer"""
										__magic_name__						:						Optional[Any]										=self.prepare_image_inputs()

										__magic_name__						:						Optional[Any]										=processor(text=__snake_case     ,			images=__snake_case			)

										self.assertListEqual(list(inputs.keys()			)     ,			["""input_ids""", """attention_mask""", """pixel_values"""]			)

										# test if it raises when no input is passed
										with pytest.raises(__snake_case			):
														processor()

						def      A__    (		self :Tuple			):
										'''simple docstring'''

										__magic_name__						:						Dict										=self.get_image_processor()
										__magic_name__						:						str										=self.get_tokenizer()

										__magic_name__						:						int										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)

										__magic_name__						:						Optional[Any]										=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										__magic_name__						:						Optional[Any]										=processor.batch_decode(__snake_case			)
										__magic_name__						:						Dict										=tokenizer.batch_decode(__snake_case			)

										self.assertListEqual(__snake_case     ,			__snake_case			)


						def      A__    (		self :Optional[Any]			):
										'''simple docstring'''

										__magic_name__						:						Tuple										=self.get_image_processor()
										__magic_name__						:						Any										=self.get_tokenizer()

										__magic_name__						:						List[str]										=CLIPProcessor(tokenizer=__snake_case     ,			image_processor=__snake_case			)

										__magic_name__						:						Optional[int]										="""lower newer"""
										__magic_name__						:						int										=self.prepare_image_inputs()

										__magic_name__						:						int										=processor(text=__snake_case     ,			images=__snake_case			)

										self.assertListEqual(list(inputs.keys()			)     ,			processor.model_input_names			)










from sklearn.metrics import matthews_corrcoef

import datasets


UpperCAmelCase_		:					Dict          =  "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction.  The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n"

UpperCAmelCase_		:					Any          =  "\nArgs:\n    predictions (list of int): Predicted labels, as returned by a model.\n    references (list of int): Ground truth labels.\n    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n    matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n    Example 1, a basic example with only predictions and references as inputs:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.54\n\n    Example 2, the same example as above, but also including sample weights:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.1\n\n    Example 3, the same example as above, but with sample weights that cause a negative correlation:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])\n        >>> print(round(results['matthews_correlation'], 2))\n        -0.25\n"

UpperCAmelCase_		:					Dict          =  "\\n@article{scikit-learn,\n  title={Scikit-learn: Machine Learning in {P}ython},\n  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n  journal={Journal of Machine Learning Research},\n  volume={12},\n  pages={2825--2830},\n  year={2011}\n}\n"




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,    _KWARGS_DESCRIPTION      )
class 		__A   (      datasets.Metric      ):

						def      A__    (		self :List[str]			):
										'''simple docstring'''

										return datasets.MetricInfo(
										    description=_DESCRIPTION     ,			citation=_CITATION     ,			inputs_description=_KWARGS_DESCRIPTION     ,			features=datasets.Features(
										        {
										            """predictions""": datasets.Value("""int32"""			),
										            """references""": datasets.Value("""int32"""			),
										        }			)     ,			reference_urls=[
										        """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html"""
										    ]     ,			)


						def      A__    (		self :Tuple     ,			__snake_case :str     ,			__snake_case :Tuple     ,			__snake_case :List[str]=None			):
										'''simple docstring'''

										return {
										    "matthews_correlation": float(matthews_corrcoef(__snake_case     ,			__snake_case     ,			sample_weight=__snake_case			)			),
										}




