




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  SCREAMING_SNAKE_CASE     (	)  ->							Union[str, Any]:
    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=__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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Union[str, Any]				)  ->							int:
    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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Optional[int]				)  ->							Union[str, Any]:

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

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

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

    def lower(__UpperCamelCase      :						Optional[int]				):
        return text.lower()

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




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




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Optional[int]	,					__UpperCamelCase      :						List[str]				)  ->							str:
    return int(normalize_answer(__UpperCamelCase				) == normalize_answer(__UpperCamelCase				)				)




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Dict	,					__UpperCamelCase      :						List[Any]				)  ->							List[str]:
    UpperCAmelCase_										=					get_tokens(__UpperCamelCase				)
    UpperCAmelCase_										=					get_tokens(__UpperCamelCase				)
    UpperCAmelCase_										=					collections.Counter(__UpperCamelCase				) & collections.Counter(__UpperCamelCase				)
    UpperCAmelCase_										=					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
    UpperCAmelCase_										=					1.0 * num_same / len(__UpperCamelCase				)
    UpperCAmelCase_										=					1.0 * num_same / len(__UpperCamelCase				)
    UpperCAmelCase_										=					(2 * precision * recall) / (precision + recall)
    return fa




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Any	,					__UpperCamelCase      :						Optional[Any]				)  ->							List[str]:
    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(__UpperCamelCase				)]
                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(__UpperCamelCase	,					__UpperCamelCase				) for a in gold_answers				)
                UpperCAmelCase_										=					max(compute_fa(__UpperCamelCase	,					__UpperCamelCase				) for a in gold_answers				)
    return exact_scores, fa_scores




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




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						int	,					__UpperCamelCase      :						List[str]	,					__UpperCamelCase      :						Any	,					__UpperCamelCase      :						int				)  ->							Union[str, 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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Union[str, Any]	,					__UpperCamelCase      :						List[str]	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						str	,					__UpperCamelCase      :						str=None	,					__UpperCamelCase      :						Optional[Any]=None				)  ->							Union[str, Any]:
    UpperCAmelCase_										=					sorted(__UpperCamelCase	,					key=lambda __UpperCamelCase				: 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(__UpperCamelCase				):
        if qid_to_has_ans[qid]:
            true_pos += scores[qid]
        UpperCAmelCase_										=					true_pos / float(i + 1				)
        UpperCAmelCase_										=					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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Dict	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						Optional[int]	,					__UpperCamelCase      :						Any	,					__UpperCamelCase      :						Any				)  ->							List[Any]:
    if out_image_dir and not os.path.exists(__UpperCamelCase				):
        os.makedirs(__UpperCamelCase				)
    UpperCAmelCase_										=					sum(1 for v in qid_to_has_ans.values() if v				)
    if num_true_pos == 0:
        return
    UpperCAmelCase_										=					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'''	,					)
    UpperCAmelCase_										=					make_precision_recall_eval(
        __UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					out_image=os.path.join(__UpperCamelCase	,					'''pr_f1.png'''				)	,					title='''Precision-Recall curve for F1 score'''	,					)
    UpperCAmelCase_										=					{k: float(__UpperCamelCase				) for k, v in qid_to_has_ans.items()}
    UpperCAmelCase_										=					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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						List[Any]	,					__UpperCamelCase      :						Union[str, Any]	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						Optional[int]				)  ->							Dict:
    if not qid_list:
        return
    UpperCAmelCase_										=					[na_probs[k] for k in qid_list]
    UpperCAmelCase_										=					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  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						Union[str, Any]	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						List[Any]	,					__UpperCamelCase      :						List[Any]				)  ->							Optional[int]:
    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(__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]:
            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 100.0 * best_score / len(__UpperCamelCase				), best_thresh




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						List[str]	,					__UpperCamelCase      :						Dict	,					__UpperCamelCase      :						List[Any]	,					__UpperCamelCase      :						int	,					__UpperCamelCase      :						Union[str, Any]	,					__UpperCamelCase      :						Optional[int]				)  ->							Union[str, Any]:
    UpperCAmelCase_			,							UpperCAmelCase_										=					find_best_thresh(__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase				)
    UpperCAmelCase_			,							UpperCAmelCase_										=					find_best_thresh(__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase				)
    UpperCAmelCase_										=					best_exact
    UpperCAmelCase_										=					exact_thresh
    UpperCAmelCase_										=					best_fa
    UpperCAmelCase_										=					fa_thresh




def  SCREAMING_SNAKE_CASE     (	)  ->							int:
    with open(OPTS.data_file				) as f:
        UpperCAmelCase_										=					json.load(__UpperCamelCase				)
        UpperCAmelCase_										=					dataset_json['''data''']
    with open(OPTS.pred_file				) as f:
        UpperCAmelCase_										=					json.load(__UpperCamelCase				)
    if OPTS.na_prob_file:
        with open(OPTS.na_prob_file				) as f:
            UpperCAmelCase_										=					json.load(__UpperCamelCase				)
    else:
        UpperCAmelCase_										=					{k: 0.0 for k in preds}
    UpperCAmelCase_										=					make_qid_to_has_ans(__UpperCamelCase				)  # 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(__UpperCamelCase	,					__UpperCamelCase				)
    UpperCAmelCase_										=					apply_no_ans_threshold(__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					OPTS.na_prob_thresh				)
    UpperCAmelCase_										=					apply_no_ans_threshold(__UpperCamelCase	,					__UpperCamelCase	,					__UpperCamelCase	,					OPTS.na_prob_thresh				)
    UpperCAmelCase_										=					make_eval_dict(__UpperCamelCase	,					__UpperCamelCase				)
    if has_ans_qids:
        UpperCAmelCase_										=					make_eval_dict(__UpperCamelCase	,					__UpperCamelCase	,					qid_list=__UpperCamelCase				)
        merge_eval(__UpperCamelCase	,					__UpperCamelCase	,					'''HasAns'''				)
    if no_ans_qids:
        UpperCAmelCase_										=					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()











from __future__ import annotations

import os
from collections.abc import Mapping

_lowerCamelCase      			=						tuple[int, int]



class       a					:

     '''simple docstring'''





     def __init__(     self       :      str ,   __snake_case       :      set[int] ,   __snake_case       :      Mapping[EdgeT, int]    ):
         UpperCAmelCase_										=					vertices
         UpperCAmelCase_										=					{
             (min(__snake_case    ), max(__snake_case    )): weight for edge, weight in edges.items()
         }
     def   lowerCamelCase_       (     self       :      Any ,   __snake_case       :      EdgeT ,   __snake_case       :      int    ):
         self.vertices.add(edge[0]    )
         self.vertices.add(edge[1]    )
         UpperCAmelCase_										=					weight




     def   lowerCamelCase_       (     self       :      Union[str, Any]    ):
         UpperCAmelCase_										=					Graph({min(self.vertices    )} ,   {}    )
         UpperCAmelCase_										=					42
         UpperCAmelCase_										=					42
         UpperCAmelCase_										=					42
         UpperCAmelCase_										=					42

         while len(subgraph.vertices    ) < len(self.vertices    ):
             UpperCAmelCase_										=					max(self.edges.values()    ) + 1
             for edge, weight in self.edges.items():
                 if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices):
                     if weight < min_weight:
                         UpperCAmelCase_										=					edge
                         UpperCAmelCase_										=					weight

             subgraph.add_edge(__snake_case ,   __snake_case    )

         return subgraph




def  SCREAMING_SNAKE_CASE     (	__UpperCamelCase      :						str = "p107_network.txt"				)  ->							int:
    UpperCAmelCase_										=					os.path.abspath(os.path.dirname(__UpperCamelCase				)				)
    UpperCAmelCase_										=					os.path.join(__UpperCamelCase	,					__UpperCamelCase				)
    UpperCAmelCase_										=					{}
    UpperCAmelCase_										=					42
    UpperCAmelCase_										=					42
    UpperCAmelCase_										=					42

    with open(__UpperCamelCase				) as f:
        UpperCAmelCase_										=					f.read().strip().split('''\n'''				)

    UpperCAmelCase_										=					[line.split(''','''				) for line in data]

    for edgea in range(1	,					len(__UpperCamelCase				)				):
        for edgea in range(__UpperCamelCase				):
            if adjaceny_matrix[edgea][edgea] != "-":
                UpperCAmelCase_										=					int(adjaceny_matrix[edgea][edgea]				)

    UpperCAmelCase_										=					Graph(set(range(len(__UpperCamelCase				)				)				)	,					__UpperCamelCase				)

    UpperCAmelCase_										=					graph.prims_algorithm()

    UpperCAmelCase_										=					sum(graph.edges.values()				)
    UpperCAmelCase_										=					sum(subgraph.edges.values()				)

    return initial_total - optimal_total


if __name__ == "__main__":
 print(F"{solution() = }")





