


import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


a					  =     '\\n    @inproceedings{kakwani2020indicnlpsuite,\n    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n    year={2020},\n    booktitle={Findings of EMNLP},\n}\n'

a					  =     '\\n    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n'

a					  =     '\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n    predictions: list of predictions to score (as int64),\n        except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n    references: list of ground truth labels corresponding to the predictions (as int64),\n        except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n    "accuracy": Accuracy\n    "f1": F1 score\n    "precision": Precision@10\nExamples:\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\')  # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0, \'f1\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'precision@10\': 1.0}\n\n'


def 			UpperCAmelCase_				(      UpperCAmelCase__						,							UpperCAmelCase__  ):
					return float((preds == labels).mean()  )


def 			UpperCAmelCase_				(      UpperCAmelCase__						,							UpperCAmelCase__  ):
					lowercase_  							=			simple_accuracy(UpperCAmelCase__						,							UpperCAmelCase__  )
					lowercase_  							=			float(fa_score(y_true=UpperCAmelCase__						,							y_pred=UpperCAmelCase__  )  )
					return {
					    "accuracy": acc,
					    "f1": fa,
					}


def 			UpperCAmelCase_				(      UpperCAmelCase__						,							UpperCAmelCase__  ):
					lowercase_  							=			np.array(UpperCAmelCase__  )
					lowercase_  							=			np.array(UpperCAmelCase__  )
					lowercase_  							=			en_sentvecs.shape[0]

					# mean centering
					lowercase_  							=			en_sentvecs - np.mean(UpperCAmelCase__						,							axis=0  )
					lowercase_  							=			in_sentvecs - np.mean(UpperCAmelCase__						,							axis=0  )

					lowercase_  							=			cdist(UpperCAmelCase__						,							UpperCAmelCase__						,							"""cosine"""  )
					lowercase_  							=			np.array(range(UpperCAmelCase__  )  )
					lowercase_  							=			sim.argsort(axis=1  )[:, :1_0]
					lowercase_  							=			np.any(preds == actual[:, None]						,							axis=1  )
					return float(matches.mean()  )







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

					def   UpperCAmelCase__       (     self       :   List[Any]       ):






										'''simple docstring'''






										if self.config_name not in [
										    "wnli",
										    "copa",
										    "sna",
										    "csqa",
										    "wstp",
										    "inltkh",
										    "bbca",
										    "cvit-mkb-clsr",
										    "iitp-mr",
										    "iitp-pr",
										    "actsa-sc",
										    "md",
										    "wiki-ner",
										]:
															raise KeyError(
															    """You should supply a configuration name selected in """
															    """[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", """
															    """\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", """
															    """\"wiki-ner\"]"""       )
										return datasets.MetricInfo(
										    description=_DESCRIPTION							,					citation=_CITATION							,					inputs_description=_KWARGS_DESCRIPTION							,					features=datasets.Features(
										        {
										            """predictions""": datasets.Value("""int64"""       )
										            if self.config_name != """cvit-mkb-clsr"""
										            else datasets.Sequence(datasets.Value("""float32"""       )       ),
										            """references""": datasets.Value("""int64"""       )
										            if self.config_name != """cvit-mkb-clsr"""
										            else datasets.Sequence(datasets.Value("""float32"""       )       ),
										        }       )							,					codebase_urls=[]							,					reference_urls=[]							,					format="""numpy""" if self.config_name != """cvit-mkb-clsr""" else None							,					)







					def   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   Tuple       ):






										'''simple docstring'''






										if self.config_name == "cvit-mkb-clsr":
															return {"precision@10": precision_at_aa(UpperCamelCase__							,					UpperCamelCase__       )}
										elif self.config_name in ["wiki-ner"]:
															return acc_and_fa(UpperCamelCase__							,					UpperCamelCase__       )
										elif self.config_name in [
										    "wnli",
										    "copa",
										    "sna",
										    "csqa",
										    "wstp",
										    "inltkh",
										    "bbca",
										    "iitp-mr",
										    "iitp-pr",
										    "actsa-sc",
										    "md",
										]:
															return {"accuracy": simple_accuracy(UpperCamelCase__							,					UpperCamelCase__       )}
										else:
															raise KeyError(
															    """You should supply a configuration name selected in """
															    """[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", """
															    """\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", """
															    """\"wiki-ner\"]"""       )






from typing import Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format
from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images
from ...utils import TensorType, logging


a					  =     logging.get_logger(__name__)







class        UpperCamelCase__   (	__magic_name__   ):
					__SCREAMING_SNAKE_CASE       :  str						=   ['pixel_values']

					def __init__(     self       :   List[Any]							,					UpperCamelCase__       :   bool = True							,					UpperCamelCase__       :   Union[int, float] = 1 / 255							,					UpperCamelCase__       :   bool = True							,					UpperCamelCase__       :   int = 8							,					**UpperCamelCase__       :   Optional[Any]							,					):






										'''simple docstring'''






										super().__init__(**UpperCamelCase__       )

										lowercase_  							=			do_rescale
										lowercase_  							=			rescale_factor
										lowercase_  							=			do_pad
										lowercase_  							=			pad_size

					def   UpperCAmelCase__       (     self       :   int							,					UpperCamelCase__       :   np.ndarray							,					UpperCamelCase__       :   float							,					UpperCamelCase__       :   Optional[Union[str, ChannelDimension]] = None							,					**UpperCamelCase__       :   Optional[Any]       ):






										'''simple docstring'''






										return rescale(UpperCamelCase__							,					scale=UpperCamelCase__							,					data_format=UpperCamelCase__							,					**UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   List[Any]							,					UpperCamelCase__       :   np.ndarray							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Optional[Union[str, ChannelDimension]] = None       ):






										'''simple docstring'''






										lowercase_							,					lowercase_  							=			get_image_size(UpperCamelCase__       )
										lowercase_  							=			(old_height // size + 1) * size - old_height
										lowercase_  							=			(old_width // size + 1) * size - old_width

										return pad(UpperCamelCase__							,					((0, pad_height), (0, pad_width))							,					mode="""symmetric"""							,					data_format=UpperCamelCase__       )







					def   UpperCAmelCase__       (     self       :   Optional[int]							,					UpperCamelCase__       :   ImageInput							,					UpperCamelCase__       :   Optional[bool] = None							,					UpperCamelCase__       :   Optional[float] = None							,					UpperCamelCase__       :   Optional[bool] = None							,					UpperCamelCase__       :   Optional[int] = None							,					UpperCamelCase__       :   Optional[Union[str, TensorType]] = None							,					UpperCamelCase__       :   Union[str, ChannelDimension] = ChannelDimension.FIRST							,					**UpperCamelCase__       :   Dict							,					):






										'''simple docstring'''






										lowercase_  							=			do_rescale if do_rescale is not None else self.do_rescale
										lowercase_  							=			rescale_factor if rescale_factor is not None else self.rescale_factor
										lowercase_  							=			do_pad if do_pad is not None else self.do_pad
										lowercase_  							=			pad_size if pad_size is not None else self.pad_size

										lowercase_  							=			make_list_of_images(UpperCamelCase__       )

										if not valid_images(UpperCamelCase__       ):
															raise ValueError(
															    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
															    """torch.Tensor, tf.Tensor or jax.ndarray."""       )

										if do_rescale and rescale_factor is None:
															raise ValueError("""Rescale factor must be specified if do_rescale is True."""       )

										# All transformations expect numpy arrays.
										lowercase_  							=			[to_numpy_array(UpperCamelCase__       ) for image in images]

										if do_rescale:
															lowercase_  							=			[self.rescale(image=UpperCamelCase__							,					scale=UpperCamelCase__       ) for image in images]

										if do_pad:
															lowercase_  							=			[self.pad(UpperCamelCase__							,					size=UpperCamelCase__       ) for image in images]

										lowercase_  							=			[to_channel_dimension_format(UpperCamelCase__							,					UpperCamelCase__       ) for image in images]

										lowercase_  							=			{"""pixel_values""": images}
										return BatchFeature(data=UpperCamelCase__							,					tensor_type=UpperCamelCase__       )


