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

import numpy as np


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

_UpperCAmelCase :			List[Any]              =	None


def 				A	( )       ->  Optional[Any]:
		'''simple docstring'''

		UpperCamelCase          =					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=lowercase   ,	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=lowercase   ,	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 				A	( lowercase			)       ->  str:
		'''simple docstring'''

		UpperCamelCase          =					{}
		for article in dataset:
				for p in article["paragraphs"]:
						for qa in p["qas"]:
								UpperCamelCase          =					bool(qa['answers']['text']			)
		return qid_to_has_ans


def 				A	( lowercase			)       ->  Union[str, Any]:
		'''simple docstring'''

		def remove_articles(lowercase			):
				return ARTICLES_REGEX.sub(' '   ,	lowercase			)

		def white_space_fix(lowercase			):
				return " ".join(text.split()			)

		def remove_punc(lowercase			):
				UpperCamelCase          =					set(string.punctuation			)
				return "".join(ch for ch in text if ch not in exclude			)

		def lower(lowercase			):
				return text.lower()

		return white_space_fix(remove_articles(remove_punc(lower(lowercase			)			)			)			)


def 				A	( lowercase			)       ->  int:
		'''simple docstring'''

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


def 				A	( lowercase   ,	lowercase			)       ->  Any:
		'''simple docstring'''

		return int(normalize_answer(lowercase			) == normalize_answer(lowercase			)			)


def 				A	( lowercase   ,	lowercase			)       ->  List[Any]:
		'''simple docstring'''

		UpperCamelCase          =					get_tokens(lowercase			)
		UpperCamelCase          =					get_tokens(lowercase			)
		UpperCamelCase          =					collections.Counter(lowercase			) & collections.Counter(lowercase			)
		UpperCamelCase          =					sum(common.values()			)
		if len(lowercase			) == 0 or len(lowercase			) == 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
		UpperCamelCase          =					1.0 * num_same / len(lowercase			)
		UpperCamelCase          =					1.0 * num_same / len(lowercase			)
		UpperCamelCase          =					(2 * precision * recall) / (precision + recall)
		return fa


def 				A	( lowercase   ,	lowercase			)       ->  Union[str, Any]:
		'''simple docstring'''

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


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  str:
		'''simple docstring'''

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


def 				A	( lowercase   ,	lowercase   ,	lowercase=None			)       ->  Dict:
		'''simple docstring'''

		if not qid_list:
				UpperCamelCase          =					len(lowercase			)
				return collections.OrderedDict(
				    [
				        ('exact', 1_0_0.0 * sum(exact_scores.values()			) / total),
				        ('f1', 1_0_0.0 * sum(fa_scores.values()			) / total),
				        ('total', total),
				    ]			)
		else:
				UpperCamelCase          =					len(lowercase			)
				return collections.OrderedDict(
				    [
				        ('exact', 1_0_0.0 * sum(exact_scores[k] for k in qid_list			) / total),
				        ('f1', 1_0_0.0 * sum(fa_scores[k] for k in qid_list			) / total),
				        ('total', total),
				    ]			)


def 				A	( lowercase   ,	lowercase   ,	lowercase			)       ->  Tuple:
		'''simple docstring'''

		for k in new_eval:
				UpperCamelCase          =					new_eval[k]


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  Tuple:
		'''simple docstring'''

		plt.step(lowercase   ,	lowercase   ,	color='b'   ,	alpha=0.2   ,	where='post'			)
		plt.fill_between(lowercase   ,	lowercase   ,	step='post'   ,	alpha=0.2   ,	color='b'			)
		plt.xlabel('Recall'			)
		plt.ylabel('Precision'			)
		plt.xlim([0.0, 1.0_5]			)
		plt.ylim([0.0, 1.0_5]			)
		plt.title(lowercase			)
		plt.savefig(lowercase			)
		plt.clf()


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase   ,	lowercase=None   ,	lowercase=None			)       ->  Optional[int]:
		'''simple docstring'''

		UpperCamelCase          =					sorted(lowercase   ,	key=lambda lowercase			: na_probs[k]			)
		UpperCamelCase          =					0.0
		UpperCamelCase          =					1.0
		UpperCamelCase          =					0.0
		UpperCamelCase          =					[1.0]
		UpperCamelCase          =					[0.0]
		UpperCamelCase          =					0.0
		for i, qid in enumerate(lowercase			):
				if qid_to_has_ans[qid]:
						true_pos += scores[qid]
				UpperCamelCase          =					true_pos / float(i + 1			)
				UpperCamelCase          =					true_pos / float(lowercase			)
				if i == len(lowercase			) - 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(lowercase			)
						recalls.append(lowercase			)
		if out_image:
				plot_pr_curve(lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)
		return {"ap": 1_0_0.0 * avg_prec}


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  Tuple:
		'''simple docstring'''

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


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  str:
		'''simple docstring'''

		if not qid_list:
				return
		UpperCamelCase          =					[na_probs[k] for k in qid_list]
		UpperCamelCase          =					np.ones_like(lowercase			) / float(len(lowercase			)			)
		plt.hist(lowercase   ,	weights=lowercase   ,	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(lowercase   ,	f'''na_prob_hist_{name}.png'''			)			)
		plt.clf()


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  List[Any]:
		'''simple docstring'''

		UpperCamelCase          =					sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]			)
		UpperCamelCase          =					num_no_ans
		UpperCamelCase          =					cur_score
		UpperCamelCase          =					0.0
		UpperCamelCase          =					sorted(lowercase   ,	key=lambda lowercase			: na_probs[k]			)
		for i, qid in enumerate(lowercase			):
				if qid not in scores:
						continue
				if qid_to_has_ans[qid]:
						UpperCamelCase          =					scores[qid]
				else:
						if preds[qid]:
								UpperCamelCase          =					-1
						else:
								UpperCamelCase          =					0
				cur_score += diff
				if cur_score > best_score:
						UpperCamelCase          =					cur_score
						UpperCamelCase          =					na_probs[qid]
		return 1_0_0.0 * best_score / len(lowercase			), best_thresh


def 				A	( lowercase   ,	lowercase   ,	lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)       ->  List[str]:
		'''simple docstring'''

		UpperCamelCase     ,  UpperCamelCase          =					find_best_thresh(lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)
		UpperCamelCase     ,  UpperCamelCase          =					find_best_thresh(lowercase   ,	lowercase   ,	lowercase   ,	lowercase			)
		UpperCamelCase          =					best_exact
		UpperCamelCase          =					exact_thresh
		UpperCamelCase          =					best_fa
		UpperCamelCase          =					fa_thresh


def 				A	( )       ->  List[str]:
		'''simple docstring'''

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


if __name__ == "__main__":
					_UpperCAmelCase :			List[str]              =	parse_args()
					if OPTS.out_image_dir:
										import matplotlib

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


import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation


class     lowercase (		unittest.TestCase       ):



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('swish'	)

				self.assertIsInstance(A_    ,				nn.SiLU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('silu'	)

				self.assertIsInstance(A_    ,				nn.SiLU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('mish'	)

				self.assertIsInstance(A_    ,				nn.Mish	)

				self.assertEqual(act(torch.tensor(-200    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('gelu'	)

				self.assertIsInstance(A_    ,				nn.GELU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)
