
import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_MAPPING,
				    SegformerForImageClassification,
				    SegformerForSemanticSegmentation,
				    SegformerModel,
				)
				from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import SegformerImageProcessor





class       _snake_case					(     snake_case				):







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		List[Any]	   =     self.config_class(**self.inputs_dict		)
				self.parent.assertTrue(hasattr(_a						,   "hidden_sizes"		)		)
				self.parent.assertTrue(hasattr(_a						,   "num_attention_heads"		)		)
				self.parent.assertTrue(hasattr(_a						,   "num_encoder_blocks"		)		)







class       _snake_case					:







			def __init__(				self						,   _a						,   _a=13						,   _a=64						,   _a=3						,   _a=4						,   _a=[2, 2, 2, 2]						,   _a=[8, 4, 2, 1]						,   _a=[16, 32, 64, 128]						,   _a=[1, 4, 8, 16]						,   _a=[1, 2, 4, 8]						,   _a=True						,   _a=True						,   _a="gelu"						,   _a=0.1						,   _a=0.1						,   _a=0.02						,   _a=3						,   _a=None						,   ):
				__magic_name__		:		Union[str, Any]	   =     parent
				__magic_name__		:		Union[str, Any]	   =     batch_size
				__magic_name__		:		Dict	   =     image_size
				__magic_name__		:		Dict	   =     num_channels
				__magic_name__		:		Optional[int]	   =     num_encoder_blocks
				__magic_name__		:		int	   =     sr_ratios
				__magic_name__		:		Any	   =     depths
				__magic_name__		:		List[str]	   =     hidden_sizes
				__magic_name__		:		Optional[Any]	   =     downsampling_rates
				__magic_name__		:		Optional[Any]	   =     num_attention_heads
				__magic_name__		:		Tuple	   =     is_training
				__magic_name__		:		Tuple	   =     use_labels
				__magic_name__		:		str	   =     hidden_act
				__magic_name__		:		Tuple	   =     hidden_dropout_prob
				__magic_name__		:		Dict	   =     attention_probs_dropout_prob
				__magic_name__		:		int	   =     initializer_range
				__magic_name__		:		Tuple	   =     num_labels
				__magic_name__		:		Optional[int]	   =     scope







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Tuple	   =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

				__magic_name__		:		int	   =     None
				if self.use_labels:
					__magic_name__		:		Tuple	   =     ids_tensor([self.batch_size, self.image_size, self.image_size]						,   self.num_labels		)

				__magic_name__		:		List[Any]	   =     self.get_config()
				return config, pixel_values, labels







			def 			SCREAMING_SNAKE_CASE    (				self		):
				return SegformerConfig(
				    image_size=self.image_size						,   num_channels=self.num_channels						,   num_encoder_blocks=self.num_encoder_blocks						,   depths=self.depths						,   hidden_sizes=self.hidden_sizes						,   num_attention_heads=self.num_attention_heads						,   hidden_act=self.hidden_act						,   hidden_dropout_prob=self.hidden_dropout_prob						,   attention_probs_dropout_prob=self.attention_probs_dropout_prob						,   initializer_range=self.initializer_range						,   )







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a		):
				__magic_name__		:		Dict	   =     SegformerModel(config=_a		)
				model.to(_a		)
				model.eval()
				__magic_name__		:		Dict	   =     model(_a		)
				__magic_name__		:		str	   =     self.image_size // (self.downsampling_rates[-1] * 2)
				self.parent.assertEqual(
				    result.last_hidden_state.shape						,   (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)		)







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a		):
				__magic_name__		:		Optional[int]	   =     self.num_labels
				__magic_name__		:		Optional[Any]	   =     SegformerForSemanticSegmentation(_a		)
				model.to(_a		)
				model.eval()
				__magic_name__		:		int	   =     model(_a		)
				self.parent.assertEqual(
				    result.logits.shape						,   (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)		)
				__magic_name__		:		Tuple	   =     model(_a						,   labels=_a		)
				self.parent.assertEqual(
				    result.logits.shape						,   (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)		)
				self.parent.assertGreater(result.loss						,   0.0		)







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a		):
				__magic_name__		:		List[Any]	   =     1
				__magic_name__		:		Union[str, Any]	   =     SegformerForSemanticSegmentation(config=_a		)
				model.to(_a		)
				model.eval()
				__magic_name__		:		Dict	   =     torch.randint(0						,   1						,   (self.batch_size, self.image_size, self.image_size)		).to(_a		)
				__magic_name__		:		List[Any]	   =     model(_a						,   labels=_a		)
				self.parent.assertGreater(result.loss						,   0.0		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Any	   =     self.prepare_config_and_inputs()
				__magic_name__   ,    __magic_name__   ,    __magic_name__		:		Union[str, Any]	   =     config_and_inputs
				__magic_name__		:		List[Any]	   =     {"pixel_values": pixel_values}
				return config, inputs_dict







@require_torch
class       _snake_case					(     snake_case				,						snake_case				,						unittest.TestCase				):
			UpperCamelCase__          =    (
			    (
			        SegformerModel,
			        SegformerForSemanticSegmentation,
			        SegformerForImageClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__          =    (
			    {
			        'feature-extraction': SegformerModel,
			        'image-classification': SegformerForImageClassification,
			        'image-segmentation': SegformerForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase__          =    True
			UpperCamelCase__          =    False
			UpperCamelCase__          =    False
			UpperCamelCase__          =    False







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		List[str]	   =     SegformerModelTester(self		)
				__magic_name__		:		Tuple	   =     SegformerConfigTester(self						,   config_class=_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				self.config_tester.run_common_tests()







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Union[str, Any]	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		str	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_binary_image_segmentation(*_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Union[str, Any]	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_segmentation(*_a		)







			@unittest.skip("SegFormer does not use inputs_embeds"		)
			def 			SCREAMING_SNAKE_CASE    (				self		):
				pass







			@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"		)
			def 			SCREAMING_SNAKE_CASE    (				self		):
				pass







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__   ,    __magic_name__		:		List[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					__magic_name__		:		int	   =     model_class(_a		)
					__magic_name__		:		Tuple	   =     inspect.signature(model.forward		)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					__magic_name__		:		Dict	   =     [*signature.parameters.keys()]

					__magic_name__		:		Union[str, Any]	   =     ["pixel_values"]
					self.assertListEqual(arg_names[:1]						,   _a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__   ,    __magic_name__		:		Tuple	   =     self.model_tester.prepare_config_and_inputs_for_common()
				__magic_name__		:		List[str]	   =     True

				for model_class in self.all_model_classes:
					__magic_name__		:		int	   =     True
					__magic_name__		:		Any	   =     False
					__magic_name__		:		List[str]	   =     True
					__magic_name__		:		Union[str, Any]	   =     model_class(_a		)
					model.to(_a		)
					model.eval()
					with torch.no_grad():
						__magic_name__		:		Union[str, Any]	   =     model(**self._prepare_for_class(_a						,   _a		)		)
					__magic_name__		:		int	   =     outputs.attentions

					__magic_name__		:		List[str]	   =     sum(self.model_tester.depths		)
					self.assertEqual(len(_a		)						,   _a		)

					# check that output_attentions also work using config
					del inputs_dict["output_attentions"]
					__magic_name__		:		Tuple	   =     True
					__magic_name__		:		str	   =     model_class(_a		)
					model.to(_a		)
					model.eval()
					with torch.no_grad():
						__magic_name__		:		Optional[int]	   =     model(**self._prepare_for_class(_a						,   _a		)		)
					__magic_name__		:		Optional[int]	   =     outputs.attentions

					self.assertEqual(len(_a		)						,   _a		)

					# verify the first attentions (first block, first layer)
					__magic_name__		:		Optional[int]	   =     (self.model_tester.image_size // 4) ** 2
					__magic_name__		:		Any	   =     (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
					self.assertListEqual(
					    list(attentions[0].shape[-3:]		)						,   [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]						,   )

					# verify the last attentions (last block, last layer)
					__magic_name__		:		int	   =     (self.model_tester.image_size // 32) ** 2
					__magic_name__		:		List[str]	   =     (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
					self.assertListEqual(
					    list(attentions[-1].shape[-3:]		)						,   [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]						,   )
					__magic_name__		:		List[str]	   =     len(_a		)

					# Check attention is always last and order is fine
					__magic_name__		:		Tuple	   =     True
					__magic_name__		:		int	   =     True
					__magic_name__		:		List[Any]	   =     model_class(_a		)
					model.to(_a		)
					model.eval()
					with torch.no_grad():
						__magic_name__		:		Any	   =     model(**self._prepare_for_class(_a						,   _a		)		)

					self.assertEqual(out_len + 1						,   len(_a		)		)

					__magic_name__		:		Optional[Any]	   =     outputs.attentions

					self.assertEqual(len(_a		)						,   _a		)
					# verify the first attentions (first block, first layer)
					__magic_name__		:		Optional[Any]	   =     (self.model_tester.image_size // 4) ** 2
					__magic_name__		:		int	   =     (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
					self.assertListEqual(
					    list(self_attentions[0].shape[-3:]		)						,   [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]						,   )







			def 			SCREAMING_SNAKE_CASE    (				self		):
				def check_hidden_states_output(_a						,   _a						,   _a		):
					__magic_name__		:		Union[str, Any]	   =     model_class(_a		)
					model.to(_a		)
					model.eval()

					with torch.no_grad():
						__magic_name__		:		Union[str, Any]	   =     model(**self._prepare_for_class(_a						,   _a		)		)

					__magic_name__		:		List[str]	   =     outputs.hidden_states

					__magic_name__		:		Optional[int]	   =     self.model_tester.num_encoder_blocks
					self.assertEqual(len(_a		)						,   _a		)

					# verify the first hidden states (first block)
					self.assertListEqual(
					    list(hidden_states[0].shape[-3:]		)						,   [
					        self.model_tester.hidden_sizes[0],
					        self.model_tester.image_size // 4,
					        self.model_tester.image_size // 4,
					    ]						,   )

				__magic_name__   ,    __magic_name__		:		Optional[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					__magic_name__		:		Any	   =     True
					check_hidden_states_output(_a						,   _a						,   _a		)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					__magic_name__		:		List[str]	   =     True

					check_hidden_states_output(_a						,   _a						,   _a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				if not self.model_tester.is_training:
					return

				__magic_name__   ,    __magic_name__		:		str	   =     self.model_tester.prepare_config_and_inputs_for_common()
				__magic_name__		:		List[Any]	   =     True

				for model_class in self.all_model_classes:
					if model_class in get_values(_a		):
						continue

					__magic_name__		:		Dict	   =     model_class(_a		)
					model.to(_a		)
					model.train()
					__magic_name__		:		Any	   =     self._prepare_for_class(_a						,   _a						,   return_labels=_a		)
					__magic_name__		:		int	   =     model(**_a		).loss
					loss.backward()







			@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
			def 			SCREAMING_SNAKE_CASE    (				self		):
				pass







			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__magic_name__		:		Optional[Any]	   =     SegformerModel.from_pretrained(_a		)
					self.assertIsNotNone(_a		)







def   lowerCAmelCase_						(					)				->		Tuple:







	'''simple docstring'''


	__magic_name__		:		Optional[int]	   =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
	return image





@require_torch
class       _snake_case					(     unittest.TestCase				):







			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				# only resize + normalize
				__magic_name__		:		Optional[int]	   =     SegformerImageProcessor(
				    image_scale=(512, 512)						,   keep_ratio=_a						,   align=_a						,   do_random_crop=_a		)
				__magic_name__		:		int	   =     SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"		).to(
				    _a		)

				__magic_name__		:		List[str]	   =     prepare_img()
				__magic_name__		:		str	   =     image_processor(images=_a						,   return_tensors="pt"		)
				__magic_name__		:		Union[str, Any]	   =     encoded_inputs.pixel_values.to(_a		)

				with torch.no_grad():
					__magic_name__		:		str	   =     model(_a		)

				__magic_name__		:		Any	   =     torch.Size((1, model.config.num_labels, 128, 128)		)
				self.assertEqual(outputs.logits.shape						,   _a		)

				__magic_name__		:		Any	   =     torch.tensor(
				    [
				        [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
				        [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
				        [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
				    ]		).to(_a		)
				self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]						,   _a						,   atol=1e-4		)		)







			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				# only resize + normalize
				__magic_name__		:		Any	   =     SegformerImageProcessor(
				    image_scale=(512, 512)						,   keep_ratio=_a						,   align=_a						,   do_random_crop=_a		)
				__magic_name__		:		Union[str, Any]	   =     SegformerForSemanticSegmentation.from_pretrained(
				    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"		).to(_a		)

				__magic_name__		:		Tuple	   =     prepare_img()
				__magic_name__		:		List[Any]	   =     image_processor(images=_a						,   return_tensors="pt"		)
				__magic_name__		:		Optional[Any]	   =     encoded_inputs.pixel_values.to(_a		)

				with torch.no_grad():
					__magic_name__		:		Union[str, Any]	   =     model(_a		)

				__magic_name__		:		List[str]	   =     torch.Size((1, model.config.num_labels, 128, 128)		)
				self.assertEqual(outputs.logits.shape						,   _a		)

				__magic_name__		:		int	   =     torch.tensor(
				    [
				        [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
				        [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
				        [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
				    ]		).to(_a		)
				self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]						,   _a						,   atol=1e-1		)		)







			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				# only resize + normalize
				__magic_name__		:		Tuple	   =     SegformerImageProcessor(
				    image_scale=(512, 512)						,   keep_ratio=_a						,   align=_a						,   do_random_crop=_a		)
				__magic_name__		:		Any	   =     SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"		).to(
				    _a		)

				__magic_name__		:		Any	   =     prepare_img()
				__magic_name__		:		Tuple	   =     image_processor(images=_a						,   return_tensors="pt"		)
				__magic_name__		:		List[str]	   =     encoded_inputs.pixel_values.to(_a		)

				with torch.no_grad():
					__magic_name__		:		Optional[int]	   =     model(_a		)

				__magic_name__		:		Any	   =     outputs.logits.detach().cpu()

				__magic_name__		:		Union[str, Any]	   =     image_processor.post_process_semantic_segmentation(outputs=_a						,   target_sizes=[(500, 300)]		)
				__magic_name__		:		Dict	   =     torch.Size((500, 300)		)
				self.assertEqual(segmentation[0].shape						,   _a		)

				__magic_name__		:		Optional[int]	   =     image_processor.post_process_semantic_segmentation(outputs=_a		)
				__magic_name__		:		List[str]	   =     torch.Size((128, 128)		)
				self.assertEqual(segmentation[0].shape						,   _a		)


import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


snake_case :		Dict							      =  re.compile(R"\b(a|an|the)\b", re.UNICODE)

snake_case :		Optional[int]							      =  None






def   lowerCAmelCase_						(					)				->		Union[str, Any]:







	'''simple docstring'''


	__magic_name__		:		Any	   =     argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."   )
	parser.add_argument("data_file"      ,   metavar="data.json"      ,   help="Input data JSON file."   )
	parser.add_argument("pred_file"      ,   metavar="pred.json"      ,   help="Model predictions."   )
	parser.add_argument(
	    "--out-file"      ,   "-o"      ,   metavar="eval.json"      ,   help="Write accuracy metrics to file (default is stdout)."   )
	parser.add_argument(
	    "--na-prob-file"      ,   "-n"      ,   metavar="na_prob.json"      ,   help="Model estimates of probability of no answer."   )
	parser.add_argument(
	    "--na-prob-thresh"      ,   "-t"      ,   type=_snake_case      ,   default=1.0      ,   help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."      ,   )
	parser.add_argument(
	    "--out-image-dir"      ,   "-p"      ,   metavar="out_images"      ,   default=_snake_case      ,   help="Save precision-recall curves to directory."   )
	parser.add_argument("--verbose"      ,   "-v"      ,   action="store_true"   )
	if len(sys.argv   ) == 1:
		parser.print_help()
		sys.exit(1   )
	return parser.parse_args()






def   lowerCAmelCase_						(					_snake_case      :  Optional[Any]   )				->		Tuple:







	'''simple docstring'''


	__magic_name__		:		Optional[int]	   =     {}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				__magic_name__		:		str	   =     bool(qa["answers"]["text"]   )
	return qid_to_has_ans






def   lowerCAmelCase_						(					_snake_case      :  Union[str, Any]   )				->		Optional[Any]:







	'''simple docstring'''


	def remove_articles(_snake_case      :  List[str]   ):
		return ARTICLES_REGEX.sub(" "      ,   _snake_case   )

	def white_space_fix(_snake_case      :  Optional[int]   ):
		return " ".join(text.split()   )

	def remove_punc(_snake_case      :  Optional[int]   ):
		__magic_name__		:		Dict	   =     set(string.punctuation   )
		return "".join(ch for ch in text if ch not in exclude   )

	def lower(_snake_case      :  str   ):
		return text.lower()

	return white_space_fix(remove_articles(remove_punc(lower(_snake_case   )   )   )   )






def   lowerCAmelCase_						(					_snake_case      :  Any   )				->		Optional[Any]:







	'''simple docstring'''


	if not s:
		return []
	return normalize_answer(_snake_case   ).split()






def   lowerCAmelCase_						(					_snake_case      :  str      ,   _snake_case      :  Dict   )				->		Tuple:







	'''simple docstring'''


	return int(normalize_answer(_snake_case   ) == normalize_answer(_snake_case   )   )






def   lowerCAmelCase_						(					_snake_case      :  List[str]      ,   _snake_case      :  int   )				->		str:







	'''simple docstring'''


	__magic_name__		:		Any	   =     get_tokens(_snake_case   )
	__magic_name__		:		Optional[int]	   =     get_tokens(_snake_case   )
	__magic_name__		:		Tuple	   =     collections.Counter(_snake_case   ) & collections.Counter(_snake_case   )
	__magic_name__		:		Tuple	   =     sum(common.values()   )
	if len(_snake_case   ) == 0 or len(_snake_case   ) == 0:
		# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
		return int(gold_toks == pred_toks   )
	if num_same == 0:
		return 0
	__magic_name__		:		Dict	   =     1.0 * num_same / len(_snake_case   )
	__magic_name__		:		Optional[Any]	   =     1.0 * num_same / len(_snake_case   )
	__magic_name__		:		List[Any]	   =     (2 * precision * recall) / (precision + recall)
	return fa






def   lowerCAmelCase_						(					_snake_case      :  Optional[Any]      ,   _snake_case      :  List[Any]   )				->		List[Any]:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     {}
	__magic_name__		:		int	   =     {}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				__magic_name__		:		Union[str, Any]	   =     qa["id"]
				__magic_name__		:		Any	   =     [t for t in qa["answers"]["text"] if normalize_answer(_snake_case   )]
				if not gold_answers:
					# For unanswerable questions, only correct answer is empty string
					__magic_name__		:		Tuple	   =     [""]
				if qid not in preds:
					print(F'''Missing prediction for {qid}'''   )
					continue
				__magic_name__		:		Any	   =     preds[qid]
				# Take max over all gold answers
				__magic_name__		:		List[Any]	   =     max(compute_exact(_snake_case      ,   _snake_case   ) for a in gold_answers   )
				__magic_name__		:		int	   =     max(compute_fa(_snake_case      ,   _snake_case   ) for a in gold_answers   )
	return exact_scores, fa_scores






def   lowerCAmelCase_						(					_snake_case      :  Optional[Any]      ,   _snake_case      :  List[Any]      ,   _snake_case      :  Optional[int]      ,   _snake_case      :  Dict   )				->		Union[str, Any]:







	'''simple docstring'''


	__magic_name__		:		str	   =     {}
	for qid, s in scores.items():
		__magic_name__		:		Dict	   =     na_probs[qid] > na_prob_thresh
		if pred_na:
			__magic_name__		:		str	   =     float(not qid_to_has_ans[qid]   )
		else:
			__magic_name__		:		Optional[int]	   =     s
	return new_scores






def   lowerCAmelCase_						(					_snake_case      :  List[Any]      ,   _snake_case      :  List[str]      ,   _snake_case      :  Tuple=None   )				->		Tuple:







	'''simple docstring'''


	if not qid_list:
		__magic_name__		:		Any	   =     len(_snake_case   )
		return collections.OrderedDict(
		    [
		        ("exact", 100.0 * sum(exact_scores.values()   ) / total),
		        ("f1", 100.0 * sum(fa_scores.values()   ) / total),
		        ("total", total),
		    ]   )
	else:
		__magic_name__		:		Tuple	   =     len(_snake_case   )
		return collections.OrderedDict(
		    [
		        ("exact", 100.0 * sum(exact_scores[k] for k in qid_list   ) / total),
		        ("f1", 100.0 * sum(fa_scores[k] for k in qid_list   ) / total),
		        ("total", total),
		    ]   )






def   lowerCAmelCase_						(					_snake_case      :  Optional[int]      ,   _snake_case      :  str      ,   _snake_case      :  str   )				->		Dict:







	'''simple docstring'''


	for k in new_eval:
		__magic_name__		:		int	   =     new_eval[k]






def   lowerCAmelCase_						(					_snake_case      :  Union[str, Any]      ,   _snake_case      :  Dict      ,   _snake_case      :  Optional[Any]      ,   _snake_case      :  Union[str, Any]   )				->		str:







	'''simple docstring'''


	plt.step(_snake_case      ,   _snake_case      ,   color="b"      ,   alpha=0.2      ,   where="post"   )
	plt.fill_between(_snake_case      ,   _snake_case      ,   step="post"      ,   alpha=0.2      ,   color="b"   )
	plt.xlabel("Recall"   )
	plt.ylabel("Precision"   )
	plt.xlim([0.0, 1.05]   )
	plt.ylim([0.0, 1.05]   )
	plt.title(_snake_case   )
	plt.savefig(_snake_case   )
	plt.clf()






def   lowerCAmelCase_						(					_snake_case      :  Dict      ,   _snake_case      :  Any      ,   _snake_case      :  Optional[int]      ,   _snake_case      :  List[Any]      ,   _snake_case      :  Optional[int]=None      ,   _snake_case      :  int=None   )				->		str:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     sorted(_snake_case      ,   key=lambda _snake_case   : na_probs[k]   )
	__magic_name__		:		Optional[int]	   =     0.0
	__magic_name__		:		str	   =     1.0
	__magic_name__		:		str	   =     0.0
	__magic_name__		:		List[str]	   =     [1.0]
	__magic_name__		:		str	   =     [0.0]
	__magic_name__		:		Optional[Any]	   =     0.0
	for i, qid in enumerate(_snake_case   ):
		if qid_to_has_ans[qid]:
			true_pos += scores[qid]
		__magic_name__		:		List[str]	   =     true_pos / float(i + 1   )
		__magic_name__		:		Any	   =     true_pos / float(_snake_case   )
		if i == len(_snake_case   ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
			# i.e., if we can put a threshold after this point
			avg_prec += cur_p * (cur_r - recalls[-1])
			precisions.append(_snake_case   )
			recalls.append(_snake_case   )
	if out_image:
		plot_pr_curve(_snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case   )
	return {"ap": 100.0 * avg_prec}






def   lowerCAmelCase_						(					_snake_case      :  Tuple      ,   _snake_case      :  Optional[Any]      ,   _snake_case      :  Optional[int]      ,   _snake_case      :  Optional[Any]      ,   _snake_case      :  Any      ,   _snake_case      :  List[Any]   )				->		Union[str, Any]:







	'''simple docstring'''


	if out_image_dir and not os.path.exists(_snake_case   ):
		os.makedirs(_snake_case   )
	__magic_name__		:		Any	   =     sum(1 for v in qid_to_has_ans.values() if v   )
	if num_true_pos == 0:
		return
	__magic_name__		:		str	   =     make_precision_recall_eval(
	    _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   out_image=os.path.join(_snake_case      ,   "pr_exact.png"   )      ,   title="Precision-Recall curve for Exact Match score"      ,   )
	__magic_name__		:		Union[str, Any]	   =     make_precision_recall_eval(
	    _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   out_image=os.path.join(_snake_case      ,   "pr_f1.png"   )      ,   title="Precision-Recall curve for F1 score"      ,   )
	__magic_name__		:		str	   =     {k: float(_snake_case   ) for k, v in qid_to_has_ans.items()}
	__magic_name__		:		str	   =     make_precision_recall_eval(
	    _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   out_image=os.path.join(_snake_case      ,   "pr_oracle.png"   )      ,   title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"      ,   )
	merge_eval(_snake_case      ,   _snake_case      ,   "pr_exact"   )
	merge_eval(_snake_case      ,   _snake_case      ,   "pr_f1"   )
	merge_eval(_snake_case      ,   _snake_case      ,   "pr_oracle"   )






def   lowerCAmelCase_						(					_snake_case      :  int      ,   _snake_case      :  Optional[Any]      ,   _snake_case      :  List[str]      ,   _snake_case      :  Optional[Any]   )				->		Dict:







	'''simple docstring'''


	if not qid_list:
		return
	__magic_name__		:		Dict	   =     [na_probs[k] for k in qid_list]
	__magic_name__		:		str	   =     np.ones_like(_snake_case   ) / float(len(_snake_case   )   )
	plt.hist(_snake_case      ,   weights=_snake_case      ,   bins=20      ,   range=(0.0, 1.0)   )
	plt.xlabel("Model probability of no-answer"   )
	plt.ylabel("Proportion of dataset"   )
	plt.title(F'''Histogram of no-answer probability: {name}'''   )
	plt.savefig(os.path.join(_snake_case      ,   F'''na_prob_hist_{name}.png'''   )   )
	plt.clf()






def   lowerCAmelCase_						(					_snake_case      :  Union[str, Any]      ,   _snake_case      :  Tuple      ,   _snake_case      :  List[str]      ,   _snake_case      :  Dict   )				->		List[Any]:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]   )
	__magic_name__		:		List[str]	   =     num_no_ans
	__magic_name__		:		Dict	   =     cur_score
	__magic_name__		:		Dict	   =     0.0
	__magic_name__		:		Any	   =     sorted(_snake_case      ,   key=lambda _snake_case   : na_probs[k]   )
	for i, qid in enumerate(_snake_case   ):
		if qid not in scores:
			continue
		if qid_to_has_ans[qid]:
			__magic_name__		:		Union[str, Any]	   =     scores[qid]
		else:
			if preds[qid]:
				__magic_name__		:		List[Any]	   =     -1
			else:
				__magic_name__		:		Optional[int]	   =     0
		cur_score += diff
		if cur_score > best_score:
			__magic_name__		:		Optional[int]	   =     cur_score
			__magic_name__		:		List[Any]	   =     na_probs[qid]
	return 100.0 * best_score / len(_snake_case   ), best_thresh






def   lowerCAmelCase_						(					_snake_case      :  int      ,   _snake_case      :  str      ,   _snake_case      :  List[str]      ,   _snake_case      :  Tuple      ,   _snake_case      :  List[Any]      ,   _snake_case      :  Dict   )				->		Optional[Any]:







	'''simple docstring'''


	__magic_name__   ,    __magic_name__		:		List[str]	   =     find_best_thresh(_snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case   )
	__magic_name__   ,    __magic_name__		:		int	   =     find_best_thresh(_snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case   )
	__magic_name__		:		Optional[int]	   =     best_exact
	__magic_name__		:		List[Any]	   =     exact_thresh
	__magic_name__		:		Dict	   =     best_fa
	__magic_name__		:		Any	   =     fa_thresh






def   lowerCAmelCase_						(					)				->		int:







	'''simple docstring'''


	with open(OPTS.data_file   ) as f:
		__magic_name__		:		Optional[Any]	   =     json.load(_snake_case   )
		__magic_name__		:		List[Any]	   =     dataset_json["data"]
	with open(OPTS.pred_file   ) as f:
		__magic_name__		:		Optional[Any]	   =     json.load(_snake_case   )
	if OPTS.na_prob_file:
		with open(OPTS.na_prob_file   ) as f:
			__magic_name__		:		Any	   =     json.load(_snake_case   )
	else:
		__magic_name__		:		Any	   =     {k: 0.0 for k in preds}
	__magic_name__		:		str	   =     make_qid_to_has_ans(_snake_case   )  # maps qid to True/False
	__magic_name__		:		Tuple	   =     [k for k, v in qid_to_has_ans.items() if v]
	__magic_name__		:		Optional[Any]	   =     [k for k, v in qid_to_has_ans.items() if not v]
	__magic_name__   ,    __magic_name__		:		Union[str, Any]	   =     get_raw_scores(_snake_case      ,   _snake_case   )
	__magic_name__		:		Optional[Any]	   =     apply_no_ans_threshold(_snake_case      ,   _snake_case      ,   _snake_case      ,   OPTS.na_prob_thresh   )
	__magic_name__		:		Optional[Any]	   =     apply_no_ans_threshold(_snake_case      ,   _snake_case      ,   _snake_case      ,   OPTS.na_prob_thresh   )
	__magic_name__		:		List[Any]	   =     make_eval_dict(_snake_case      ,   _snake_case   )
	if has_ans_qids:
		__magic_name__		:		int	   =     make_eval_dict(_snake_case      ,   _snake_case      ,   qid_list=_snake_case   )
		merge_eval(_snake_case      ,   _snake_case      ,   "HasAns"   )
	if no_ans_qids:
		__magic_name__		:		List[Any]	   =     make_eval_dict(_snake_case      ,   _snake_case      ,   qid_list=_snake_case   )
		merge_eval(_snake_case      ,   _snake_case      ,   "NoAns"   )
	if OPTS.na_prob_file:
		find_all_best_thresh(_snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case   )
	if OPTS.na_prob_file and OPTS.out_image_dir:
		run_precision_recall_analysis(_snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   _snake_case      ,   OPTS.out_image_dir   )
		histogram_na_prob(_snake_case      ,   _snake_case      ,   OPTS.out_image_dir      ,   "hasAns"   )
		histogram_na_prob(_snake_case      ,   _snake_case      ,   OPTS.out_image_dir      ,   "noAns"   )
	if OPTS.out_file:
		with open(OPTS.out_file      ,   "w"   ) as f:
			json.dump(_snake_case      ,   _snake_case   )
	else:
		print(json.dumps(_snake_case      ,   indent=2   )   )


if __name__ == "__main__":
				snake_case :		int							      =  parse_args()
				if OPTS.out_image_dir:
								import matplotlib

								matplotlib.use("Agg")
								import matplotlib.pyplot as plt
				main()
