



'''simple docstring'''






from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
						import PIL

if is_torch_available():
						import torch


UpperCamelCase__:     List[str]										=    logging.get_logger(__name__)

class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	lowerCamelCase__    				= ["""pixel_values"""]







	def __init__(		self    :  Union[str, Any]	,    __snake_case    :  bool = True	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  PILImageResampling = PILImageResampling.BILINEAR	,    __snake_case    :  bool = True	,    __snake_case    :  Union[int, float] = 1 / 255	,    __snake_case    :  bool = True	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  bool = True	,    **__snake_case    :  str	,    )    ->	None:
								super().__init__(**__snake_case	)
								UpperCAmelCase				: Union[str, Any]											=	size if size is not None else {'''shortest_edge''': 224}
								UpperCAmelCase				: Optional[int]											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								UpperCAmelCase				: Union[str, Any]											=	crop_size if crop_size is not None else {'''height''': 256, '''width''': 256}
								UpperCAmelCase				: List[Any]											=	get_size_dict(__snake_case	,    param_name='''crop_size'''	)

								UpperCAmelCase				: Optional[int]											=	do_resize
								UpperCAmelCase				: Tuple											=	size
								UpperCAmelCase				: List[str]											=	resample
								UpperCAmelCase				: List[str]											=	do_rescale
								UpperCAmelCase				: Union[str, Any]											=	rescale_factor
								UpperCAmelCase				: Optional[Any]											=	do_center_crop
								UpperCAmelCase				: str											=	crop_size
								UpperCAmelCase				: List[str]											=	do_flip_channel_order







	def   A			(		self    :  Optional[int]	,    __snake_case    :  np.ndarray	,    __snake_case    :  Dict[str, int]	,    __snake_case    :  PILImageResampling = PIL.Image.BILINEAR	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  List[str]	,    )    ->	np.ndarray:
								UpperCAmelCase				: Optional[Any]											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								if "shortest_edge" not in size:
															raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}"""	)
								UpperCAmelCase				: Dict											=	get_resize_output_image_size(__snake_case	,    size=size['''shortest_edge''']	,    default_to_square=__snake_case	)
								return resize(__snake_case	,    size=__snake_case	,    resample=__snake_case	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  Optional[int]	,    __snake_case    :  np.ndarray	,    __snake_case    :  Dict[str, int]	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  Any	,    )    ->	np.ndarray:
								UpperCAmelCase				: List[str]											=	get_size_dict(__snake_case	)
								if "height" not in size or "width" not in size:
															raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"""	)
								return center_crop(__snake_case	,    size=(size['''height'''], size['''width'''])	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  str	,    __snake_case    :  np.ndarray	,    __snake_case    :  Union[int, float]	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  int	,    )    ->	Optional[int]:
								return rescale(__snake_case	,    scale=__snake_case	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  str	,    __snake_case    :  np.ndarray	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	)    ->	np.ndarray:
								return flip_channel_order(__snake_case	,    data_format=__snake_case	)







	def   A			(		self    :  Optional[int]	,    __snake_case    :  ImageInput	,    __snake_case    :  bool = None	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  PILImageResampling = None	,    __snake_case    :  bool = None	,    __snake_case    :  float = None	,    __snake_case    :  bool = None	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  bool = None	,    __snake_case    :  Optional[Union[str, TensorType]] = None	,    __snake_case    :  ChannelDimension = ChannelDimension.FIRST	,    **__snake_case    :  Optional[int]	,    )    ->	PIL.Image.Image:
								UpperCAmelCase				: Any											=	do_resize if do_resize is not None else self.do_resize
								UpperCAmelCase				: Optional[int]											=	resample if resample is not None else self.resample
								UpperCAmelCase				: int											=	do_rescale if do_rescale is not None else self.do_rescale
								UpperCAmelCase				: Optional[int]											=	rescale_factor if rescale_factor is not None else self.rescale_factor
								UpperCAmelCase				: Tuple											=	do_center_crop if do_center_crop is not None else self.do_center_crop
								UpperCAmelCase				: Dict											=	(
								    do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
								)

								UpperCAmelCase				: int											=	size if size is not None else self.size
								UpperCAmelCase				: Any											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								UpperCAmelCase				: int											=	crop_size if crop_size is not None else self.crop_size
								UpperCAmelCase				: Optional[int]											=	get_size_dict(__snake_case	,    param_name='''crop_size'''	)

								UpperCAmelCase				: List[str]											=	make_list_of_images(__snake_case	)

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

								if do_resize and size is None:
															raise ValueError('''Size must be specified if do_resize is True.'''	)

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

								if do_center_crop and crop_size is None:
															raise ValueError('''Crop size must be specified if do_center_crop is True.'''	)

								# All transformations expect numpy arrays.
								UpperCAmelCase				: Union[str, Any]											=	[to_numpy_array(__snake_case	) for image in images]

								if do_resize:
															UpperCAmelCase				: Tuple											=	[self.resize(image=__snake_case	,    size=__snake_case	,    resample=__snake_case	) for image in images]

								if do_center_crop:
															UpperCAmelCase				: List[str]											=	[self.center_crop(image=__snake_case	,    size=__snake_case	) for image in images]

								if do_rescale:
															UpperCAmelCase				: Tuple											=	[self.rescale(image=__snake_case	,    scale=__snake_case	) for image in images]

								# the pretrained checkpoints assume images are BGR, not RGB
								if do_flip_channel_order:
															UpperCAmelCase				: List[Any]											=	[self.flip_channel_order(image=__snake_case	) for image in images]

								UpperCAmelCase				: int											=	[to_channel_dimension_format(__snake_case	,    __snake_case	) for image in images]

								UpperCAmelCase				: List[str]											=	{'''pixel_values''': images}
								return BatchFeature(data=__snake_case	,    tensor_type=__snake_case	)







	def   A			(		self    :  Tuple	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  List[Tuple] = None	)    ->	Dict:
								UpperCAmelCase				: str											=	outputs.logits

								# Resize logits and compute semantic segmentation maps
								if target_sizes is not None:
															if len(__snake_case	) != len(__snake_case	):
																						raise ValueError(
																						    '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''	)

															if is_torch_tensor(__snake_case	):
																						UpperCAmelCase				: int											=	target_sizes.numpy()

															UpperCAmelCase				: Optional[int]											=	[]

															for idx in range(len(__snake_case	)	):
																						UpperCAmelCase				: List[Any]											=	torch.nn.functional.interpolate(
																						    logits[idx].unsqueeze(dim=0	)	,    size=target_sizes[idx]	,    mode='''bilinear'''	,    align_corners=__snake_case	)
																						UpperCAmelCase				: Any											=	resized_logits[0].argmax(dim=0	)
																						semantic_segmentation.append(__snake_case	)
								else:
															UpperCAmelCase				: Any											=	logits.argmax(dim=1	)
															UpperCAmelCase				: int											=	[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]	)]

								return semantic_segmentation










'''simple docstring'''






from sklearn.metrics import fa_score, matthews_corrcoef

import datasets

from .record_evaluation import evaluate as evaluate_record


UpperCamelCase__:     int										=    "\\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"

UpperCamelCase__:     Dict										=    "\\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"

UpperCamelCase__:     str										=    "\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 				snake_case_  (      _lowerCAmelCase						:		str     ,   _lowerCAmelCase						:		Tuple						)   ->     Dict:
							return float((preds == labels).mean()						)


def 				snake_case_  (      _lowerCAmelCase						:		List[Any]     ,   _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		Dict="binary"						)   ->     Any:
							UpperCAmelCase				: str											=	simple_accuracy(_lowerCAmelCase     ,   _lowerCAmelCase						)
							UpperCAmelCase				: str											=	float(fa_score(y_true=_lowerCAmelCase     ,   y_pred=_lowerCAmelCase     ,   average=_lowerCAmelCase						)						)
							return {
							    "accuracy": acc,
							    "f1": fa,
							}


def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		str						)   ->     List[str]:
							UpperCAmelCase				: Optional[int]											=	{}
							for id_pred, label in zip(_lowerCAmelCase     ,   _lowerCAmelCase						):
														UpperCAmelCase				: Union[str, Any]											=	f"""{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}"""
														UpperCAmelCase				: List[str]											=	id_pred['''prediction''']
														if question_id in question_map:
																					question_map[question_id].append((pred, label)						)
														else:
																					UpperCAmelCase				: Union[str, Any]											=	[(pred, label)]
							UpperCAmelCase							,	UpperCAmelCase				: Dict											=	[], []
							for question, preds_labels in question_map.items():
														UpperCAmelCase							,	UpperCAmelCase				: List[Any]											=	zip(*_lowerCAmelCase						)
														UpperCAmelCase				: int											=	fa_score(y_true=_lowerCAmelCase     ,   y_pred=_lowerCAmelCase     ,   average='''macro'''						)
														fas.append(_lowerCAmelCase						)
														UpperCAmelCase				: Dict											=	int(sum(pred == label for pred, label in preds_labels						) == len(_lowerCAmelCase						)						)
														ems.append(_lowerCAmelCase						)
							UpperCAmelCase				: int											=	float(sum(_lowerCAmelCase						) / len(_lowerCAmelCase						)						)
							UpperCAmelCase				: Optional[int]											=	sum(_lowerCAmelCase						) / len(_lowerCAmelCase						)
							UpperCAmelCase				: str											=	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        SCREAMING_SNAKE_CASE(							datasets.Metric    ):







	"""simple docstring"""






	def   A			(		self    :  Optional[Any]	)    ->	Dict:
								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   A			(		self    :  Union[str, Any]	)    ->	Dict:
								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   A			(		self    :  Dict	,    __snake_case    :  str	,    __snake_case    :  str	)    ->	Optional[int]:
								if self.config_name == "axb":
															return {"matthews_correlation": matthews_corrcoef(__snake_case	,    __snake_case	)}
								elif self.config_name == "cb":
															return acc_and_fa(__snake_case	,    __snake_case	,    fa_avg='''macro'''	)
								elif self.config_name == "record":
															UpperCAmelCase				: Tuple											=	[
															    {
															        '''qas''': [
															            {'''id''': ref['''idx''']['''query'''], '''answers''': [{'''text''': ans} for ans in ref['''answers''']]}
															            for ref in references
															        ]
															    }
															]
															UpperCAmelCase				: int											=	{pred['''idx''']['''query''']: pred['''prediction_text'''] for pred in predictions}
															return evaluate_record(__snake_case	,    __snake_case	)[0]
								elif self.config_name == "multirc":
															return evaluate_multirc(__snake_case	,    __snake_case	)
								elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]:
															return {"accuracy": simple_accuracy(__snake_case	,    __snake_case	)}
								else:
															raise KeyError(
															    '''You should supply a configuration name selected in '''
															    '''["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]'''	)





