"""simple docstring"""




import numpy as np

import datasets


__lowerCAmelCase    :							Tuple													=						'''
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/]
'''

__lowerCAmelCase    :							List[Any]													=						'''\
@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}
}
'''

__lowerCAmelCase    :							Optional[Any]													=						'''
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       a_      (     datasets.Metric			):




					def 						_SCREAMING_SNAKE_CASE						(	self						:     List[Any]     ):
							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 						_SCREAMING_SNAKE_CASE						(	self						:     Any  ,						snake_case__						:     Dict  ,						snake_case__						:     Dict     ):
							lowerCAmelCase__        =   np.array(__lowerCAmelCase     )
							lowerCAmelCase__        =   np.array(__lowerCAmelCase     )

							# 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
							lowerCAmelCase__        =   X - np.mean(__lowerCAmelCase     )
							lowerCAmelCase__        =   np.cov(reference_distribution.T     )
							try:
									lowerCAmelCase__        =   np.linalg.inv(__lowerCAmelCase     )
							except np.linalg.LinAlgError:
									lowerCAmelCase__        =   np.linalg.pinv(__lowerCAmelCase     )
							lowerCAmelCase__        =   np.dot(__lowerCAmelCase  ,						__lowerCAmelCase     )
							lowerCAmelCase__        =   np.dot(__lowerCAmelCase  ,						X_minus_mu.T     ).diagonal()

							return {"mahalanobis": mahal_dist}







"""simple docstring"""




import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class       a_      (     __UpperCamelCase ,		unittest.TestCase			):
					UpperCamelCase_ :	str			=    LayoutLMTokenizer
					UpperCamelCase_ :	List[Any]			=    LayoutLMTokenizerFast
					UpperCamelCase_ :	Dict			=    True
					UpperCamelCase_ :	Any			=    True




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Tuple     ):
							super().setUp()

							lowerCAmelCase__        =   [
							    """[UNK]""",
							    """[CLS]""",
							    """[SEP]""",
							    """want""",
							    """##want""",
							    """##ed""",
							    """wa""",
							    """un""",
							    """runn""",
							    """##ing""",
							    """,""",
							    """low""",
							    """lowest""",
							]
							lowerCAmelCase__        =   os.path.join(self.tmpdirname  ,						VOCAB_FILES_NAMES["""vocab_file"""]     )
							with open(self.vocab_file  ,						"""w"""  ,						encoding="""utf-8"""     ) as vocab_writer:
									vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]     )     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     int  ,						**snake_case__						:     Union[str, Any]     ):
							return LayoutLMTokenizer.from_pretrained(self.tmpdirname  ,						**snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     List[Any]  ,						snake_case__						:     Tuple     ):
							lowerCAmelCase__        =   """UNwant\u00E9d,running"""
							lowerCAmelCase__        =   """unwanted, running"""
							return input_text, output_text




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]     ):
							lowerCAmelCase__        =   self.tokenizer_class(self.vocab_file     )

							lowerCAmelCase__        =   tokenizer.tokenize("""UNwant\u00E9d,running"""     )
							self.assertListEqual(snake_case__  ,						["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]     )
							self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__     )  ,						[7, 4, 5, 10, 8, 9]     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     List[str]     ):
							pass




