
import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


SCREAMING_SNAKE_CASE__							=				random.Random()

if is_torch_available():
							import torch




def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Union[str, Any]    ,							SCREAMING_SNAKE_CASE       :      str=1.0    ,							SCREAMING_SNAKE_CASE       :      Optional[Any]=None    ,							SCREAMING_SNAKE_CASE       :      Union[str, Any]=None			)     ->						Union[str, Any]:
				if rng is None:
								__lowercase								=					global_rng

				__lowercase								=					[]
				for batch_idx in range(shape[0]			):
								values.append([]			)
								for _ in range(shape[1]			):
												values[-1].append(rng.random() * scale			)

				return values
class 	A__   (						unittest.TestCase				):





							def __init__( self  :   Union[str, Any]     , _UpperCAmelCase  :   Dict     , _UpperCAmelCase  :   Tuple=7     , _UpperCAmelCase  :   Dict=4_00     , _UpperCAmelCase  :   Optional[int]=20_00     , _UpperCAmelCase  :   Any=1     , _UpperCAmelCase  :   List[str]=0.0     , _UpperCAmelCase  :   Union[str, Any]=1_60_00     , _UpperCAmelCase  :   Tuple=True     , _UpperCAmelCase  :   Union[str, Any]=True     , )						->		List[Any]:



											"""simple docstring"""


											__lowercase								=					parent
											__lowercase								=					batch_size
											__lowercase								=					min_seq_length
											__lowercase								=					max_seq_length
											__lowercase								=					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											__lowercase								=					feature_size
											__lowercase								=					padding_value
											__lowercase								=					sampling_rate
											__lowercase								=					return_attention_mask
											__lowercase								=					do_normalize





							def 			a__ ( self  :   Optional[int]				)						->		Dict:



											"""simple docstring"""


											return {
											    "feature_size": self.feature_size,
											    "padding_value": self.padding_value,
											    "sampling_rate": self.sampling_rate,
											    "return_attention_mask": self.return_attention_mask,
											    "do_normalize": self.do_normalize,
											}





							def 			a__ ( self  :   str     , _UpperCAmelCase  :   List[str]=False     , _UpperCAmelCase  :   int=False				)						->		Any:



											"""simple docstring"""


											def _flatten(_UpperCAmelCase  :   Union[str, Any]				):
															return list(itertools.chain(*_SCREAMING_SNAKE_CASE				)				)

											if equal_length:
															__lowercase								=					floats_list((self.batch_size, self.max_seq_length)				)
											else:
															# make sure that inputs increase in size
															__lowercase								=					[
															    _flatten(floats_list((x, self.feature_size)				)				)
															    for x in range(self.min_seq_length     , self.max_seq_length     , self.seq_length_diff				)
															]

											if numpify:
															__lowercase								=					[np.asarray(_SCREAMING_SNAKE_CASE				) for x in speech_inputs]

											return speech_inputs






@require_torch
@require_torchaudio
class 	A__   (						__SCREAMING_SNAKE_CASE    ,						unittest.TestCase				):
							lowerCAmelCase__		:						int    			=							ASTFeatureExtractor





							def 			a__ ( self  :   int				)						->		List[str]:



											"""simple docstring"""


											__lowercase								=					ASTFeatureExtractionTester(self				)





							def 			a__ ( self  :   Optional[int]				)						->		Optional[int]:



											"""simple docstring"""


											__lowercase								=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
											# create three inputs of length 800, 1000, and 1200
											__lowercase								=					[floats_list((1, x)				)[0] for x in range(8_00     , 14_00     , 2_00				)]
											__lowercase								=					[np.asarray(_SCREAMING_SNAKE_CASE				) for speech_input in speech_inputs]

											# Test not batched input
											__lowercase								=					feat_extract(speech_inputs[0]     , return_tensors='np'				).input_values
											__lowercase								=					feat_extract(np_speech_inputs[0]     , return_tensors='np'				).input_values
											self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE     , _SCREAMING_SNAKE_CASE     , atol=1e-3				)				)

											# Test batched
											__lowercase								=					feat_extract(_SCREAMING_SNAKE_CASE     , padding=_SCREAMING_SNAKE_CASE     , return_tensors='np'				).input_values
											__lowercase								=					feat_extract(_SCREAMING_SNAKE_CASE     , padding=_SCREAMING_SNAKE_CASE     , return_tensors='np'				).input_values
											for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE     , _SCREAMING_SNAKE_CASE				):
															self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE     , _SCREAMING_SNAKE_CASE     , atol=1e-3				)				)

											# Test 2-D numpy arrays are batched.
											__lowercase								=					[floats_list((1, x)				)[0] for x in (8_00, 8_00, 8_00)]
											__lowercase								=					np.asarray(_SCREAMING_SNAKE_CASE				)
											__lowercase								=					feat_extract(_SCREAMING_SNAKE_CASE     , return_tensors='np'				).input_values
											__lowercase								=					feat_extract(_SCREAMING_SNAKE_CASE     , return_tensors='np'				).input_values
											for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE     , _SCREAMING_SNAKE_CASE				):
															self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE     , _SCREAMING_SNAKE_CASE     , atol=1e-3				)				)





							@require_torch
							def 			a__ ( self  :   Dict				)						->		Tuple:



											"""simple docstring"""


											import torch

											__lowercase								=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
											__lowercase								=					np.random.rand(1_00				).astype(np.floataa				)
											__lowercase								=					np_speech_inputs.tolist()

											for inputs in [py_speech_inputs, np_speech_inputs]:
															__lowercase								=					feature_extractor.pad([{'input_values': inputs}]     , return_tensors='np'				)
															self.assertTrue(np_processed.input_values.dtype == np.floataa				)
															__lowercase								=					feature_extractor.pad([{'input_values': inputs}]     , return_tensors='pt'				)
															self.assertTrue(pt_processed.input_values.dtype == torch.floataa				)





							def 			a__ ( self  :   Dict     , _UpperCAmelCase  :   Optional[int]				)						->		int:



											"""simple docstring"""


											from datasets import load_dataset

											__lowercase								=					load_dataset('hf-internal-testing/librispeech_asr_dummy'     , 'clean'     , split='validation'				)
											# automatic decoding with librispeech
											__lowercase								=					ds.sort('id'				).select(range(_SCREAMING_SNAKE_CASE				)				)[:num_samples]["audio"]

											return [x["array"] for x in speech_samples]





							@require_torch
							def 			a__ ( self  :   Dict				)						->		Dict:



											"""simple docstring"""


											__lowercase								=					torch.tensor(
											    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
											     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
											     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
											     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]				)
											# fmt: on

											__lowercase								=					self._load_datasamples(1				)
											__lowercase								=					ASTFeatureExtractor()
											__lowercase								=					feature_extractor(_SCREAMING_SNAKE_CASE     , return_tensors='pt'				).input_values
											self.assertEquals(input_values.shape     , (1, 10_24, 1_28)				)
											self.assertTrue(torch.allclose(input_values[0, 0, :30]     , _SCREAMING_SNAKE_CASE     , atol=1e-4				)				)










"""simple docstring"""






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

import datasets


__snake_case							:   Optional[int]      =    '\\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'

__snake_case							:   str      =    '\\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'

__snake_case							:   str      =    '\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 			_lowercase				(  __snake_case					,__snake_case	)						->   Union[str, Any]:
			return float((preds == labels).mean()	)


def 			_lowercase				(  __snake_case					,__snake_case	)						->   str:
			__lowerCAmelCase      :  str												=       simple_accuracy(__snake_case					,__snake_case	)
			__lowerCAmelCase      :  Any												=       float(fa_score(y_true=__snake_case					,y_pred=__snake_case	)	)
			return {
			    "accuracy": acc,
			    "f1": fa,
			}


def 			_lowercase				(  __snake_case					,__snake_case	)						->   int:
			__lowerCAmelCase      :  Union[str, Any]												=       np.array(__snake_case	)
			__lowerCAmelCase      :  Tuple												=       np.array(__snake_case	)
			__lowerCAmelCase      :  List[Any]												=       en_sentvecs.shape[0]

			# mean centering
			__lowerCAmelCase      :  Union[str, Any]												=       en_sentvecs - np.mean(__snake_case					,axis=0	)
			__lowerCAmelCase      :  int												=       in_sentvecs - np.mean(__snake_case					,axis=0	)

			__lowerCAmelCase      :  Optional[Any]												=       cdist(__snake_case					,__snake_case					,"cosine"	)
			__lowerCAmelCase      :  int												=       np.array(range(__snake_case	)	)
			__lowerCAmelCase      :  int												=       sim.argsort(axis=1	)[:, :10]
			__lowerCAmelCase      :  Optional[Any]												=       np.any(preds == actual[:, None]					,axis=1	)
			return float(matches.mean()	)





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




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       str:

				"""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 							_SCREAMING_SNAKE_CASE   (     self:		List[str]       ,			_SCREAMING_SNAKE_CASE:		int       ,			_SCREAMING_SNAKE_CASE:		Optional[Any])				->       int:

				"""simple docstring"""

				if self.config_name == "cvit-mkb-clsr":
							return {"precision@10": precision_at_aa(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)}
				elif self.config_name in ["wiki-ner"]:
							return acc_and_fa(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				elif self.config_name in [
				    "wnli",
				    "copa",
				    "sna",
				    "csqa",
				    "wstp",
				    "inltkh",
				    "bbca",
				    "iitp-mr",
				    "iitp-pr",
				    "actsa-sc",
				    "md",
				]:
							return {"accuracy": simple_accuracy(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)}
				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\"]")