





'''simple docstring'''

from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


__lowerCamelCase : str											=			logging.get_logger(__name__)

class       UpperCAmelCase (						_lowercase  ):
		UpperCAmelCase					:	Any 			=		['''audio_values''', '''audio_mask''']




		def __init__(self      :	Union[str, Any]							,			A__      :	Any=2_0_4_8							,			A__      :	Any=1							,			A__      :	List[Any]=[1_6, 1_6]							,			A__      :	Union[str, Any]=1_2_8							,			A__      :	Optional[Any]=4_4_1_0_0							,			A__      :	int=8_6							,			A__      :	str=2_0_4_8							,			A__      :	str=0.0							,			**A__      :	Optional[int]							,			)					-> Optional[Any]:
						super().__init__(
						    feature_size=A__							,			sampling_rate=A__							,			padding_value=A__							,			**A__							,			)

						lowercase         = spectrogram_length
						lowercase         = num_channels
						lowercase         = patch_size
						lowercase         = feature_size // self.patch_size[1]
						lowercase         = n_fft
						lowercase         = sampling_rate // hop_length_to_sampling_rate
						lowercase         = sampling_rate
						lowercase         = padding_value
						lowercase         = mel_filter_bank(
						    num_frequency_bins=1 + n_fft // 2							,			num_mel_filters=A__							,			min_frequency=0.0							,			max_frequency=2_2_0_5_0.0							,			sampling_rate=A__							,			norm="slaney"							,			mel_scale="slaney"							,			).T




		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	np.array				)					-> np.ndarray:
						lowercase         = spectrogram(
						    A__							,			window_function(self.n_fft							,			"hann"				)							,			frame_length=self.n_fft							,			hop_length=self.hop_length							,			power=2.0							,			mel_filters=self.mel_filters.T							,			log_mel="dB"							,			db_range=8_0.0							,			)
						lowercase         = log_spec[:, :-1]
						lowercase         = log_spec - 2_0.0
						lowercase         = np.clip(log_spec / 4_0.0							,			-2.0							,			0.0				) + 1.0
						return log_spec





		def __call__(self      :	str							,			A__      :	Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]							,			A__      :	Optional[Union[str, TensorType]] = None							,			A__      :	Optional[bool] = True							,			A__      :	Optional[int] = None							,			A__      :	bool = False							,			A__      :	bool = False							,			**A__      :	List[Any]							,			)					-> BatchFeature:

						if sampling_rate is not None:
										if sampling_rate != self.sampling_rate:
														raise ValueError(
														    "This feature extractor is set to support sampling rate"
														    f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'
														    f' with {self.sampling_rate} and not {sampling_rate}.'				)
						else:
										logger.warning(
										    "It is strongly recommended to pass the `sampling_rate` argument to this function. "
										    "Failing to do so can result in silent errors that might be hard to debug."				)

						lowercase         = isinstance(A__							,			np.ndarray				) and len(raw_speech.shape				) > 1
						if is_batched_numpy and len(raw_speech.shape				) > 2:
										raise ValueError(f'Only mono-channel audio is supported for input to {self}'				)
						lowercase         = is_batched_numpy or (
						    isinstance(A__							,			(list, tuple)				) and (isinstance(raw_speech[0]							,			(np.ndarray, tuple, list)				))
						)
						if is_batched:
										lowercase         = [np.asarray([speech]							,			dtype=np.floataa				).T for speech in raw_speech]
						elif not is_batched and not isinstance(A__							,			np.ndarray				):
										lowercase         = np.asarray(A__							,			dtype=np.floataa				)
						elif isinstance(A__							,			np.ndarray				) and raw_speech.dtype is np.dtype(np.floataa				):
										lowercase         = raw_speech.astype(np.floataa				)
						# always return batch
						if not is_batched:
										lowercase         = [np.asarray([raw_speech]				).T]

						# Convert audio signals to log mel spectrograms, truncate by time axis
						lowercase         = [
						    self._np_extract_fbank_features(waveform.squeeze()				).T[: self.spectrogram_length] for waveform in raw_speech
						]
						if isinstance(audio_features[0]							,			A__				):
										lowercase         = [np.asarray(A__							,			dtype=np.floataa				) for feature in audio_features]

						# Create audio attention mask
						lowercase         = max(
						    [ceil(feature.shape[0] / self.patch_size[0]				) * self.freq_len for feature in audio_features]				)  # The maximum number of audio patches in a batch
						if return_attention_mask:
										lowercase         = [
										    (ceil(feature.shape[0] / self.patch_size[0]				) * self.freq_len) * [1]
										    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]				) * self.freq_len) * [0]
										    for feature in audio_features
										]
										lowercase         = np.array(A__				).astype(np.floataa				)

						# convert into correct format for padding
						lowercase         = max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
						lowercase         = np.ones([len(A__				), 1, max_time_len, self.feature_size]				).astype(np.floataa				)
						lowercase         = padded_audio_features * self.padding_value
						for i in range(len(A__				)				):
										lowercase         = audio_features[i]
										lowercase         = feature

						# return as BatchFeature
						if return_attention_mask:
										lowercase         = {"audio_values": padded_audio_features, "audio_mask": audio_mask}
						else:
										lowercase         = {"audio_values": padded_audio_features}

						lowercase         = BatchFeature(data=A__							,			tensor_type=A__				)
						return encoded_inputs











'''simple docstring'''

from sklearn.metrics import fa_score, matthews_corrcoef

import datasets

from .record_evaluation import evaluate as evaluate_record


__lowerCamelCase : str											=			"\\n@article{wang2019superglue,\n  title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n  author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n  journal={arXiv preprint arXiv:1905.00537},\n  year={2019}\n}\n"

__lowerCamelCase : Optional[int]											=			"\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n"

__lowerCamelCase : int											=			"\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n    predictions: list of predictions to score. Depending on the SuperGlUE subset:\n        - for 'record': list of question-answer dictionaries with the following keys:\n            - 'idx': index of the question as specified by the dataset\n            - 'prediction_text': the predicted answer text\n        - for 'multirc': list of question-answer dictionaries with the following keys:\n            - 'idx': index of the question-answer pair as specified by the dataset\n            - 'prediction': the predicted answer label\n        - otherwise: list of predicted labels\n    references: list of reference labels. Depending on the SuperGLUE subset:\n        - for 'record': list of question-answers dictionaries with the following keys:\n            - 'idx': index of the question as specified by the dataset\n            - 'answers': list of possible answers\n        - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n    - for 'record':\n        - 'exact_match': Exact match between answer and gold answer\n        - 'f1': F1 score\n    - for 'multirc':\n        - 'exact_match': Exact match between answer and gold answer\n        - 'f1_m': Per-question macro-F1 score\n        - 'f1_a': Average F1 score over all answers\n    - for 'axb':\n        'matthews_correlation': Matthew Correlation\n    - for 'cb':\n        - 'accuracy': Accuracy\n        - 'f1': F1 score\n    - for all others:\n        - 'accuracy': Accuracy\nExamples:\n\n    >>> super_glue_metric = datasets.load_metric('super_glue', 'copa')  # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]\n    >>> predictions = [0, 1]\n    >>> references = [0, 1]\n    >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0}\n\n    >>> super_glue_metric = datasets.load_metric('super_glue', 'cb')\n    >>> predictions = [0, 1]\n    >>> references = [0, 1]\n    >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0, 'f1': 1.0}\n\n    >>> super_glue_metric = datasets.load_metric('super_glue', 'record')\n    >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}]\n    >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}]\n    >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 1.0, 'f1': 1.0}\n\n    >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc')\n    >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}]\n    >>> references = [0, 1]\n    >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0}\n\n    >>> super_glue_metric = datasets.load_metric('super_glue', 'axb')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'matthews_correlation': 1.0}\n"







def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_							):

				"""simple docstring"""






				return float((preds == labels).mean()							)







def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_="binary"							):

				"""simple docstring"""






				lowercase         = simple_accuracy(lowerCAmelCase_		,							lowerCAmelCase_							)
				lowercase         = float(fa_score(y_true=lowerCAmelCase_		,							y_pred=lowerCAmelCase_		,							average=lowerCAmelCase_							)							)
				return {
				    "accuracy": acc,
				    "f1": fa,
				}







def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_							):

				"""simple docstring"""






				lowercase         = {}
				for id_pred, label in zip(lowerCAmelCase_		,							lowerCAmelCase_							):
								lowercase         = f'{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}'
								lowercase         = id_pred["prediction"]
								if question_id in question_map:
												question_map[question_id].append((pred, label)							)
								else:
												lowercase         = [(pred, label)]
				lowercase   ,							lowercase         = [], []
				for question, preds_labels in question_map.items():
								lowercase   ,							lowercase         = zip(*lowerCAmelCase_							)
								lowercase         = fa_score(y_true=lowerCAmelCase_		,							y_pred=lowerCAmelCase_		,							average="macro"							)
								fas.append(lowerCAmelCase_							)
								lowercase         = int(sum(pred == label for pred, label in preds_labels							) == len(lowerCAmelCase_							)							)
								ems.append(lowerCAmelCase_							)
				lowercase         = float(sum(lowerCAmelCase_							) / len(lowerCAmelCase_							)							)
				lowercase         = sum(lowerCAmelCase_							) / len(lowerCAmelCase_							)
				lowercase         = float(fa_score(y_true=lowerCAmelCase_		,							y_pred=[id_pred["prediction"] for id_pred in ids_preds]							)							)
				return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a}


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




		def 				UpperCAmelCase__   (self      :	Any				)					-> str:
						if self.config_name not in [
						    "boolq",
						    "cb",
						    "copa",
						    "multirc",
						    "record",
						    "rte",
						    "wic",
						    "wsc",
						    "wsc.fixed",
						    "axb",
						    "axg",
						]:
										raise KeyError(
										    "You should supply a configuration name selected in "
										    "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]"				)
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(self._get_feature_types()				)							,			codebase_urls=[]							,			reference_urls=[]							,			format="numpy" if not self.config_name == "record" and not self.config_name == "multirc" else None							,			)




		def 				UpperCAmelCase__   (self      :	int				)					-> Optional[int]:
						if self.config_name == "record":
										return {
										    "predictions": {
										        "idx": {
										            "passage": datasets.Value("int64"				),
										            "query": datasets.Value("int64"				),
										        },
										        "prediction_text": datasets.Value("string"				),
										    },
										    "references": {
										        "idx": {
										            "passage": datasets.Value("int64"				),
										            "query": datasets.Value("int64"				),
										        },
										        "answers": datasets.Sequence(datasets.Value("string"				)				),
										    },
										}
						elif self.config_name == "multirc":
										return {
										    "predictions": {
										        "idx": {
										            "answer": datasets.Value("int64"				),
										            "paragraph": datasets.Value("int64"				),
										            "question": datasets.Value("int64"				),
										        },
										        "prediction": datasets.Value("int64"				),
										    },
										    "references": datasets.Value("int64"				),
										}
						else:
										return {
										    "predictions": datasets.Value("int64"				),
										    "references": datasets.Value("int64"				),
										}





		def 				UpperCAmelCase__   (self      :	Any							,			A__      :	int							,			A__      :	List[str]				)					-> Dict:
						if self.config_name == "axb":
										return {"matthews_correlation": matthews_corrcoef(A__							,			A__				)}
						elif self.config_name == "cb":
										return acc_and_fa(A__							,			A__							,			fa_avg="macro"				)
						elif self.config_name == "record":
										lowercase         = [
										    {
										        "qas": [
										            {"id": ref["idx"]["query"], "answers": [{"text": ans} for ans in ref["answers"]]}
										            for ref in references
										        ]
										    }
										]
										lowercase         = {pred["idx"]["query"]: pred["prediction_text"] for pred in predictions}
										return evaluate_record(A__							,			A__				)[0]
						elif self.config_name == "multirc":
										return evaluate_multirc(A__							,			A__				)
						elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]:
										return {"accuracy": simple_accuracy(A__							,			A__				)}
						else:
										raise KeyError(
										    "You should supply a configuration name selected in "
										    "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]"				)




