

"""simple docstring"""






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

import numpy as np


__lowerCamelCase      						=		re.compile(r"\b(a|an|the)\b", re.UNICODE)

__lowerCamelCase      						=		None





def 	lowercase      (   ) ->      List[Any]:
 __magic_name__     =      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=__UpperCamelCase      ,   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=__UpperCamelCase      ,   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      (   __UpperCamelCase						) ->      List[str]:
 __magic_name__     =      {}
 for article in dataset:
  for p in article["paragraphs"]:
   for qa in p["qas"]:
    __magic_name__     =      bool(qa['''answers''']['''text''']						)
 return qid_to_has_ans





def 	lowercase      (   __UpperCamelCase						) ->      Tuple:

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

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

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

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

 return white_space_fix(remove_articles(remove_punc(lower(__UpperCamelCase						)						)						)						)





def 	lowercase      (   __UpperCamelCase						) ->      Optional[int]:
 if not s:
  return []
 return normalize_answer(__UpperCamelCase						).split()





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase						) ->      Tuple:
 return int(normalize_answer(__UpperCamelCase						) == normalize_answer(__UpperCamelCase						)						)





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





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





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						) ->      Tuple:
 __magic_name__     =      {}
 for qid, s in scores.items():
  __magic_name__     =      na_probs[qid] > na_prob_thresh
  if pred_na:
   __magic_name__     =      float(not qid_to_has_ans[qid]						)
  else:
   __magic_name__     =      s
 return new_scores





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase=None						) ->      str:
 if not qid_list:
  __magic_name__     =      len(__UpperCamelCase						)
  return collections.OrderedDict(
      [
          ('''exact''', 100.0 * sum(exact_scores.values()						) / total),
          ('''f1''', 100.0 * sum(fa_scores.values()						) / total),
          ('''total''', total),
      ]						)
 else:
  __magic_name__     =      len(__UpperCamelCase						)
  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      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						) ->      Optional[Any]:
 for k in new_eval:
  __magic_name__     =      new_eval[k]





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						) ->      Optional[Any]:
 plt.step(__UpperCamelCase      ,   __UpperCamelCase      ,   color='''b'''      ,   alpha=0.2      ,   where='''post'''						)
 plt.fill_between(__UpperCamelCase      ,   __UpperCamelCase      ,   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(__UpperCamelCase						)
 plt.savefig(__UpperCamelCase						)
 plt.clf()





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





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





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





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





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						) ->      Optional[int]:
 __magic_name__   ,						__magic_name__     =      find_best_thresh(__UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						)
 __magic_name__   ,						__magic_name__     =      find_best_thresh(__UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						)
 __magic_name__     =      best_exact
 __magic_name__     =      exact_thresh
 __magic_name__     =      best_fa
 __magic_name__     =      fa_thresh





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


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

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




"""simple docstring"""






import torch

from diffusers import EulerDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest
class        _lowercase					(     __UpperCAmelCase     ):
     _lowerCamelCase     =       (EulerDiscreteScheduler,)
     _lowerCamelCase     =       10







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      __magic_name__     =      {
          '''num_train_timesteps''': 1100,
          '''beta_start''': 0.0_0_0_1,
          '''beta_end''': 0.0_2,
          '''beta_schedule''': '''linear''',
      }

      config.update(**UpperCamelCase_       )
      return config







     def 	lowerCAmelCase__    (      self       ):
      for timesteps in [10, 50, 100, 1000]:
       self.check_over_configs(num_train_timesteps=UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] ,						[0.0_0_0_2, 0.0_0_2, 0.0_2]       ):
       self.check_over_configs(beta_start=UpperCamelCase_ ,						beta_end=UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      for schedule in ["linear", "scaled_linear"]:
       self.check_over_configs(beta_schedule=UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      for prediction_type in ["epsilon", "v_prediction"]:
       self.check_over_configs(prediction_type=UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.scheduler_classes[0]
      __magic_name__     =      self.get_scheduler_config()
      __magic_name__     =      scheduler_class(**UpperCamelCase_       )

      scheduler.set_timesteps(self.num_inference_steps       )

      __magic_name__     =      torch.manual_seed(0       )

      __magic_name__     =      self.dummy_model()
      __magic_name__     =      self.dummy_sample_deter * scheduler.init_noise_sigma
      __magic_name__     =      sample.to(UpperCamelCase_       )

      for i, t in enumerate(scheduler.timesteps       ):
       __magic_name__     =      scheduler.scale_model_input(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      model(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      scheduler.step(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						generator=UpperCamelCase_       )
       __magic_name__     =      output.prev_sample

      __magic_name__     =      torch.sum(torch.abs(UpperCamelCase_       )       )
      __magic_name__     =      torch.mean(torch.abs(UpperCamelCase_       )       )

      assert abs(result_sum.item() - 1_0.0_8_0_7       ) < 1E-2
      assert abs(result_mean.item() - 0.0_1_3_1       ) < 1E-3







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.scheduler_classes[0]
      __magic_name__     =      self.get_scheduler_config(prediction_type='''v_prediction'''       )
      __magic_name__     =      scheduler_class(**UpperCamelCase_       )

      scheduler.set_timesteps(self.num_inference_steps       )

      __magic_name__     =      torch.manual_seed(0       )

      __magic_name__     =      self.dummy_model()
      __magic_name__     =      self.dummy_sample_deter * scheduler.init_noise_sigma
      __magic_name__     =      sample.to(UpperCamelCase_       )

      for i, t in enumerate(scheduler.timesteps       ):
       __magic_name__     =      scheduler.scale_model_input(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      model(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      scheduler.step(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						generator=UpperCamelCase_       )
       __magic_name__     =      output.prev_sample

      __magic_name__     =      torch.sum(torch.abs(UpperCamelCase_       )       )
      __magic_name__     =      torch.mean(torch.abs(UpperCamelCase_       )       )

      assert abs(result_sum.item() - 0.0_0_0_2       ) < 1E-2
      assert abs(result_mean.item() - 2.2_6_7_6E-0_6       ) < 1E-3







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.scheduler_classes[0]
      __magic_name__     =      self.get_scheduler_config()
      __magic_name__     =      scheduler_class(**UpperCamelCase_       )

      scheduler.set_timesteps(self.num_inference_steps ,						device=UpperCamelCase_       )

      __magic_name__     =      torch.manual_seed(0       )

      __magic_name__     =      self.dummy_model()
      __magic_name__     =      self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
      __magic_name__     =      sample.to(UpperCamelCase_       )

      for t in scheduler.timesteps:
       __magic_name__     =      scheduler.scale_model_input(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      model(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      scheduler.step(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						generator=UpperCamelCase_       )
       __magic_name__     =      output.prev_sample

      __magic_name__     =      torch.sum(torch.abs(UpperCamelCase_       )       )
      __magic_name__     =      torch.mean(torch.abs(UpperCamelCase_       )       )

      assert abs(result_sum.item() - 1_0.0_8_0_7       ) < 1E-2
      assert abs(result_mean.item() - 0.0_1_3_1       ) < 1E-3







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.scheduler_classes[0]
      __magic_name__     =      self.get_scheduler_config()
      __magic_name__     =      scheduler_class(**UpperCamelCase_ ,						use_karras_sigmas=UpperCamelCase_       )

      scheduler.set_timesteps(self.num_inference_steps ,						device=UpperCamelCase_       )

      __magic_name__     =      torch.manual_seed(0       )

      __magic_name__     =      self.dummy_model()
      __magic_name__     =      self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
      __magic_name__     =      sample.to(UpperCamelCase_       )

      for t in scheduler.timesteps:
       __magic_name__     =      scheduler.scale_model_input(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      model(UpperCamelCase_ ,						UpperCamelCase_       )

       __magic_name__     =      scheduler.step(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						generator=UpperCamelCase_       )
       __magic_name__     =      output.prev_sample

      __magic_name__     =      torch.sum(torch.abs(UpperCamelCase_       )       )
      __magic_name__     =      torch.mean(torch.abs(UpperCamelCase_       )       )

      assert abs(result_sum.item() - 1_2_4.5_2_2_9_9_4_9_9_5_1_1_7_1_9       ) < 1E-2
      assert abs(result_mean.item() - 0.1_6_2_1_3_9_3_2_6_3_3_3_9_9_9_6_3       ) < 1E-3

