

"""simple docstring"""


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

import numpy as np


a					:Optional[int]    = re.compile(r"\b(a|an|the)\b", re.UNICODE)

a					:List[str]    = None




def  _lowercase					(    )       ->				Optional[Any]:
      SCREAMING_SNAKE_CASE__   :      int      		=	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=__lowerCAmelCase  ,					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=__lowerCAmelCase  ,					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  _lowercase					(    __lowerCAmelCase							)       ->				Optional[Any]:
      SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	{}
      for article in dataset:
            for p in article["paragraphs"]:
                  for qa in p["qas"]:
                        SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	bool(qa["""answers"""]["""text"""]							)
      return qid_to_has_ans




def  _lowercase					(    __lowerCAmelCase							)       ->				str:

      def remove_articles(__lowerCAmelCase							):
            return ARTICLES_REGEX.sub(""" """  ,					__lowerCAmelCase							)

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

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

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

      return white_space_fix(remove_articles(remove_punc(lower(__lowerCAmelCase							)							)							)							)




def  _lowercase					(    __lowerCAmelCase							)       ->				Any:
      if not s:
            return []
      return normalize_answer(__lowerCAmelCase							).split()




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase							)       ->				Dict:
      return int(normalize_answer(__lowerCAmelCase							) == normalize_answer(__lowerCAmelCase							)							)




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase							)       ->				Union[str, Any]:
      SCREAMING_SNAKE_CASE__   :      List[Any]      		=	get_tokens(__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      List[Any]      		=	get_tokens(__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      List[Any]      		=	collections.Counter(__lowerCAmelCase							) & collections.Counter(__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      List[str]      		=	sum(common.values()							)
      if len(__lowerCAmelCase							) == 0 or len(__lowerCAmelCase							) == 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
      SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	1.0 * num_same / len(__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      Tuple      		=	1.0 * num_same / len(__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	(2 * precision * recall) / (precision + recall)
      return fa




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




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)       ->				int:
      SCREAMING_SNAKE_CASE__   :      str      		=	{}
      for qid, s in scores.items():
            SCREAMING_SNAKE_CASE__   :      Any      		=	na_probs[qid] > na_prob_thresh
            if pred_na:
                  SCREAMING_SNAKE_CASE__   :      List[str]      		=	float(not qid_to_has_ans[qid]							)
            else:
                  SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	s
      return new_scores




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase=None							)       ->				Union[str, Any]:
      if not qid_list:
            SCREAMING_SNAKE_CASE__   :      int      		=	len(__lowerCAmelCase							)
            return collections.OrderedDict(
                [
                    ("""exact""", 100.0 * sum(exact_scores.values()							) / total),
                    ("""f1""", 100.0 * sum(fa_scores.values()							) / total),
                    ("""total""", total),
                ]							)
      else:
            SCREAMING_SNAKE_CASE__   :      str      		=	len(__lowerCAmelCase							)
            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  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)       ->				Dict:
      for k in new_eval:
            SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	new_eval[k]




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)       ->				Dict:
      plt.step(__lowerCAmelCase  ,					__lowerCAmelCase  ,					color="""b"""  ,					alpha=0.2  ,					where="""post"""							)
      plt.fill_between(__lowerCAmelCase  ,					__lowerCAmelCase  ,					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(__lowerCAmelCase							)
      plt.savefig(__lowerCAmelCase							)
      plt.clf()




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase=None  ,					__lowerCAmelCase=None							)       ->				str:
      SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	sorted(__lowerCAmelCase  ,					key=lambda __lowerCAmelCase							: na_probs[k]							)
      SCREAMING_SNAKE_CASE__   :      List[Any]      		=	0.0
      SCREAMING_SNAKE_CASE__   :      List[Any]      		=	1.0
      SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	0.0
      SCREAMING_SNAKE_CASE__   :      List[str]      		=	[1.0]
      SCREAMING_SNAKE_CASE__   :      List[str]      		=	[0.0]
      SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	0.0
      for i, qid in enumerate(__lowerCAmelCase							):
            if qid_to_has_ans[qid]:
                  true_pos += scores[qid]
            SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	true_pos / float(i + 1							)
            SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	true_pos / float(__lowerCAmelCase							)
            if i == len(__lowerCAmelCase							) - 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(__lowerCAmelCase							)
                  recalls.append(__lowerCAmelCase							)
      if out_image:
            plot_pr_curve(__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)
      return {"ap": 100.0 * avg_prec}




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




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)       ->				List[str]:
      if not qid_list:
            return
      SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	[na_probs[k] for k in qid_list]
      SCREAMING_SNAKE_CASE__   :      Any      		=	np.ones_like(__lowerCAmelCase							) / float(len(__lowerCAmelCase							)							)
      plt.hist(__lowerCAmelCase  ,					weights=__lowerCAmelCase  ,					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(__lowerCAmelCase  ,					F'''na_prob_hist_{name}.png'''							)							)
      plt.clf()




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




def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)       ->				int:
      SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Dict      		=	find_best_thresh(__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      List[Any]      		=	find_best_thresh(__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase  ,					__lowerCAmelCase							)
      SCREAMING_SNAKE_CASE__   :      str      		=	best_exact
      SCREAMING_SNAKE_CASE__   :      Any      		=	exact_thresh
      SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	best_fa
      SCREAMING_SNAKE_CASE__   :      List[str]      		=	fa_thresh




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


if __name__ == "__main__":
  a					:Dict    = parse_args()
  if OPTS.out_image_dir:
    import matplotlib

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







"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


a					:List[Any]    = logging.get_logger(__name__)

a					:Optional[int]    = {
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}

class      __a							(UpperCamelCase_   ,	UpperCamelCase_):






     '''simple docstring'''
     _SCREAMING_SNAKE_CASE						:Any       = """focalnet"""

     def __init__(						self	,    _a=224	,    _a=4	,    _a=3	,    _a=96	,    _a=False	,    _a=[192, 384, 768, 768]	,    _a=[2, 2, 6, 2]	,    _a=[2, 2, 2, 2]	,    _a=[3, 3, 3, 3]	,    _a="gelu"	,    _a=4.0	,    _a=0.0	,    _a=0.1	,    _a=False	,    _a=1E-4	,    _a=False	,    _a=False	,    _a=False	,    _a=0.02	,    _a=1E-5	,    _a=32	,    _a=None	,    _a=None	,    **_a	,    )  ->      Optional[Any]:


           """simple docstring"""


           super().__init__(**_a  )

           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	image_size
           SCREAMING_SNAKE_CASE__   :      str      		=	patch_size
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	num_channels
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	embed_dim
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	use_conv_embed
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	hidden_sizes
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	depths
           SCREAMING_SNAKE_CASE__   :      Any      		=	focal_levels
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	focal_windows
           SCREAMING_SNAKE_CASE__   :      Any      		=	hidden_act
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	mlp_ratio
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	hidden_dropout_prob
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	drop_path_rate
           SCREAMING_SNAKE_CASE__   :      str      		=	use_layerscale
           SCREAMING_SNAKE_CASE__   :      int      		=	layerscale_value
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	use_post_layernorm
           SCREAMING_SNAKE_CASE__   :      Any      		=	use_post_layernorm_in_modulation
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	normalize_modulator
           SCREAMING_SNAKE_CASE__   :      str      		=	initializer_range
           SCREAMING_SNAKE_CASE__   :      Any      		=	layer_norm_eps
           SCREAMING_SNAKE_CASE__   :      Any      		=	encoder_stride
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	["""stem"""] + [f'''stage{idx}''' for idx in range(1	,    len(self.depths  ) + 1  )]
           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Dict      		=	get_aligned_output_features_output_indices(
               out_features=_a	,    out_indices=_a	,    stage_names=self.stage_names  )




