


import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402







class lowerCAmelCase_						(			unittest.TestCase	):

	'''simple docstring'''

	def 						UpperCamelCase__					(     self ):
							snake_case_	=				0



	def 						UpperCamelCase__					(     self ):
							snake_case_	=				AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' )
							self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )



	def 						UpperCamelCase__					(     self ):
							with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case_	=				Path(__UpperCamelCase ) / '''preprocessor_config.json'''
													snake_case_	=				Path(__UpperCamelCase ) / '''config.json'''
													json.dump(
													    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		,				open(__UpperCamelCase		,				'''w''' )		,				)
													json.dump({'''model_type''': '''clip'''}		,				open(__UpperCamelCase		,				'''w''' ) )

													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase )
													self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )



	def 						UpperCamelCase__					(     self ):
							# Ensure we can load the image processor from the feature extractor config
							with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case_	=				Path(__UpperCamelCase ) / '''preprocessor_config.json'''
													snake_case_	=				Path(__UpperCamelCase ) / '''config.json'''
													json.dump(
													    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		,				open(__UpperCamelCase		,				'''w''' )		,				)
													json.dump({'''model_type''': '''clip'''}		,				open(__UpperCamelCase		,				'''w''' ) )

													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase )
													self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )



	def 						UpperCamelCase__					(     self ):
							with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case_	=				CLIPConfig()

													# Create a dummy config file with image_proceesor_type
													snake_case_	=				Path(__UpperCamelCase ) / '''preprocessor_config.json'''
													snake_case_	=				Path(__UpperCamelCase ) / '''config.json'''
													json.dump(
													    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		,				open(__UpperCamelCase		,				'''w''' )		,				)
													json.dump({'''model_type''': '''clip'''}		,				open(__UpperCamelCase		,				'''w''' ) )

													# remove image_processor_type to make sure config.json alone is enough to load image processor locally
													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase ).to_dict()

													config_dict.pop('''image_processor_type''' )
													snake_case_	=				CLIPImageProcessor(**__UpperCamelCase )

													# save in new folder
													model_config.save_pretrained(__UpperCamelCase )
													config.save_pretrained(__UpperCamelCase )

													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase )

													# make sure private variable is not incorrectly saved
													snake_case_	=				json.loads(config.to_json_string() )
													self.assertTrue('''_processor_class''' not in dict_as_saved )

							self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )



	def 						UpperCamelCase__					(     self ):
							with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case_	=				Path(__UpperCamelCase ) / '''preprocessor_config.json'''
													json.dump(
													    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		,				open(__UpperCamelCase		,				'''w''' )		,				)

													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase )
													self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )



	def 						UpperCamelCase__					(     self ):
							with self.assertRaisesRegex(
							    __UpperCamelCase		,				'''clip-base is not a local folder and is not a valid model identifier''' ):
													snake_case_	=				AutoImageProcessor.from_pretrained('''clip-base''' )



	def 						UpperCamelCase__					(     self ):
							with self.assertRaisesRegex(
							    __UpperCamelCase		,				R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ):
													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase		,				revision='''aaaaaa''' )



	def 						UpperCamelCase__					(     self ):
							with self.assertRaisesRegex(
							    __UpperCamelCase		,				'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''		,				):
													snake_case_	=				AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' )



	def 						UpperCamelCase__					(     self ):
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(__UpperCamelCase ):
													snake_case_	=				AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(__UpperCamelCase ):
													snake_case_	=				AutoImageProcessor.from_pretrained(
													    '''hf-internal-testing/test_dynamic_image_processor'''		,				trust_remote_code=__UpperCamelCase )

							snake_case_	=				AutoImageProcessor.from_pretrained(
							    '''hf-internal-testing/test_dynamic_image_processor'''		,				trust_remote_code=__UpperCamelCase )
							self.assertEqual(image_processor.__class__.__name__		,				'''NewImageProcessor''' )

							# Test image processor can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
													image_processor.save_pretrained(__UpperCamelCase )
													snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase		,				trust_remote_code=__UpperCamelCase )
							self.assertEqual(reloaded_image_processor.__class__.__name__		,				'''NewImageProcessor''' )



	def 						UpperCamelCase__					(     self ):
							try:
													AutoConfig.register('''custom'''		,				__UpperCamelCase )
													AutoImageProcessor.register(__UpperCamelCase		,				__UpperCamelCase )
													# Trying to register something existing in the Transformers library will raise an error
													with self.assertRaises(__UpperCamelCase ):
																			AutoImageProcessor.register(__UpperCamelCase		,				__UpperCamelCase )

													with tempfile.TemporaryDirectory() as tmpdirname:
																			snake_case_	=				Path(__UpperCamelCase ) / '''preprocessor_config.json'''
																			snake_case_	=				Path(__UpperCamelCase ) / '''config.json'''
																			json.dump(
																			    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		,				open(__UpperCamelCase		,				'''w''' )		,				)
																			json.dump({'''model_type''': '''clip'''}		,				open(__UpperCamelCase		,				'''w''' ) )

																			snake_case_	=				CustomImageProcessor.from_pretrained(__UpperCamelCase )

													# Now that the config is registered, it can be used as any other config with the auto-API
													with tempfile.TemporaryDirectory() as tmp_dir:
																			image_processor.save_pretrained(__UpperCamelCase )
																			snake_case_	=				AutoImageProcessor.from_pretrained(__UpperCamelCase )
																			self.assertIsInstance(__UpperCamelCase		,				__UpperCamelCase )

							finally:
													if "custom" in CONFIG_MAPPING._extra_content:
																			del CONFIG_MAPPING._extra_content["custom"]
													if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																			del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]





	def 						UpperCamelCase__					(     self ):







							class lowerCAmelCase_						(			_lowercase	):

								'''simple docstring'''

								__snake_case												=  True

							try:
													AutoConfig.register('''custom'''		,				__UpperCamelCase )
													AutoImageProcessor.register(__UpperCamelCase		,				__UpperCamelCase )
													# If remote code is not set, the default is to use local
													snake_case_	=				AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' )
													self.assertEqual(image_processor.__class__.__name__		,				'''NewImageProcessor''' )
													self.assertTrue(image_processor.is_local )

													# If remote code is disabled, we load the local one.
													snake_case_	=				AutoImageProcessor.from_pretrained(
													    '''hf-internal-testing/test_dynamic_image_processor'''		,				trust_remote_code=__UpperCamelCase )
													self.assertEqual(image_processor.__class__.__name__		,				'''NewImageProcessor''' )
													self.assertTrue(image_processor.is_local )

													# If remote is enabled, we load from the Hub
													snake_case_	=				AutoImageProcessor.from_pretrained(
													    '''hf-internal-testing/test_dynamic_image_processor'''		,				trust_remote_code=__UpperCamelCase )
													self.assertEqual(image_processor.__class__.__name__		,				'''NewImageProcessor''' )
													self.assertTrue(not hasattr(__UpperCamelCase		,				'''is_local''' ) )

							finally:
													if "custom" in CONFIG_MAPPING._extra_content:
																			del CONFIG_MAPPING._extra_content["custom"]
													if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																			del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]



import numpy as np

import datasets


UpperCAmelCase      =   """
Compute the Mahalanobis Distance

Mahalonobis distance is the distance between a point and a distribution.
And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.
It was introduced by Prof. P. C. Mahalanobis in 1936
and has been used in various statistical applications ever since
[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]
"""

UpperCAmelCase      =   """\
@article{de2000mahalanobis,
  title={The mahalanobis distance},
  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},
  journal={Chemometrics and intelligent laboratory systems},
  volume={50},
  number={1},
  pages={1--18},
  year={2000},
  publisher={Elsevier}
}
"""

UpperCAmelCase      =   """
Args:
    X: List of datapoints to be compared with the `reference_distribution`.
    reference_distribution: List of datapoints from the reference distribution we want to compare to.
Returns:
    mahalanobis: The Mahalonobis distance for each datapoint in `X`.
Examples:

    >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")
    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])
    >>> print(results)
    {'mahalanobis': array([0.5])}
"""







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

 '''simple docstring'''

 def 						UpperCamelCase__					(     self ):
       return datasets.MetricInfo(
           description=_DESCRIPTION		,				citation=_CITATION		,				inputs_description=_KWARGS_DESCRIPTION		,				features=datasets.Features(
               {
                   '''X''': datasets.Sequence(datasets.Value('''float'''		,				id='''sequence''' )		,				id='''X''' ),
               } )		,				)





 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       # convert to numpy arrays
       snake_case_	=				np.array(_UpperCAmelCase )
       snake_case_	=				np.array(_UpperCAmelCase )

       # Assert that arrays are 2D
       if len(X.shape ) != 2:
             raise ValueError('''Expected `X` to be a 2D vector''' )
       if len(reference_distribution.shape ) != 2:
             raise ValueError('''Expected `reference_distribution` to be a 2D vector''' )
       if reference_distribution.shape[0] < 2:
             raise ValueError(
                 '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' )

       # Get mahalanobis distance for each prediction
       snake_case_	=				X - np.mean(_UpperCAmelCase )
       snake_case_	=				np.cov(reference_distribution.T )
       try:
             snake_case_	=				np.linalg.inv(_UpperCAmelCase )
       except np.linalg.LinAlgError:
             snake_case_	=				np.linalg.pinv(_UpperCAmelCase )
       snake_case_	=				np.dot(_UpperCAmelCase		,				_UpperCAmelCase )
       snake_case_	=				np.dot(_UpperCAmelCase		,				X_minus_mu.T ).diagonal()

       return {"mahalanobis": mahal_dist}