# Path: maxdiv/eval.py
# def auc(ygt, regions, n = 0):
# def average_precision(ygt, regions, overlap = 0.5, plot = False):
# def recall_precision(ygt, regions, overlap = 0.5, th = None, multiAsFP = True):
# def plotDetections(func, regions, gt = [], ticks = None, export = None, silent = True, detailedvis = False):
# def show_interval(f, a, b, visborder=100, color='b', alpha=0.3, plot_function=True, border=False):
# def pointwiseLabelsToIntervals(labels):
# 
# Path: maxdiv/maxdiv_util.py
# def IoU(start1, len1, start2, len2):
#     """ Computes the intersection over union of two intervals starting at start1 and start2 with lengths len1 and len2. """
#     intersection = max(0, min(start1 + len1, start2 + len2) - max(start1, start2))
#     return float(intersection) / (len1 + len2 - intersection)


import sys
import numpy as np
import scipy.stats
import random, csv, argparse
import datasets
from maxdiv import eval
from maxdiv.maxdiv_util import IoU
    
    if addMissingFuncs:
        for ftype, hit in hits.items():
            for i, h in enumerate(hits):
                if not h:
                    print('Adding missing function: {}_{:03d}'.format(ftype, i))
                    det.append({ 'ftype' : ftype, 'fnum'  : i, 'det1'  : [], 'det2'  : [], 'num_gt': len(funcs[ftype][i]['gt']) })
    
    return det


def annotateDetections(det, gt, overlap = 0.5):
    """ Labels each detection in the given list as true or false positive according to a given list of ground-truth intervals.
    
    `det` is expected to be a list of detections as `(a, b, score)` tuples and `gt` must be a list of ground-truth intervals as
    `(a, b)` tuples.
    
    `overlap` is a threshold for the Intersection over Union (IoU) criterion. Detections must have an IoU with
    a ground-truth region greater than this threshold for being considered a true positive.
    
    This function returns a new list of detections as `(a, b, score, isTP)` tuples, where `isTP` specifies whether the detection
    is a true positive or not.
    """
    
    newDet = []
    region_detected = [False] * len(gt) # prevent multiple detections of the same interval
    for a, b, score in det:
        
        isTP = False
        for i_gt, (a_gt, b_gt) in enumerate(gt):

# Path: maxdiv/eval.py
# def auc(ygt, regions, n = 0):
# def average_precision(ygt, regions, overlap = 0.5, plot = False):
# def recall_precision(ygt, regions, overlap = 0.5, th = None, multiAsFP = True):
# def plotDetections(func, regions, gt = [], ticks = None, export = None, silent = True, detailedvis = False):
# def show_interval(f, a, b, visborder=100, color='b', alpha=0.3, plot_function=True, border=False):
# def pointwiseLabelsToIntervals(labels):
# 
# Path: maxdiv/maxdiv_util.py
# def IoU(start1, len1, start2, len2):
#     """ Computes the intersection over union of two intervals starting at start1 and start2 with lengths len1 and len2. """
#     intersection = max(0, min(start1 + len1, start2 + len2) - max(start1, start2))
#     return float(intersection) / (len1 + len2 - intersection)


# Path: tools/test_significance.py
import sys
import numpy as np
import scipy.stats
import random, csv, argparse
import datasets
from maxdiv import eval
from maxdiv.maxdiv_util import IoU

    
    if addMissingFuncs:
        for ftype, hit in hits.items():
            for i, h in enumerate(hits):
                if not h:
                    print('Adding missing function: {}_{:03d}'.format(ftype, i))
                    det.append({ 'ftype' : ftype, 'fnum'  : i, 'det1'  : [], 'det2'  : [], 'num_gt': len(funcs[ftype][i]['gt']) })
    
    return det


def annotateDetections(det, gt, overlap = 0.5):
    """ Labels each detection in the given list as true or false positive according to a given list of ground-truth intervals.
    
    `det` is expected to be a list of detections as `(a, b, score)` tuples and `gt` must be a list of ground-truth intervals as
    `(a, b)` tuples.
    
    `overlap` is a threshold for the Intersection over Union (IoU) criterion. Detections must have an IoU with
    a ground-truth region greater than this threshold for being considered a true positive.
    
    This function returns a new list of detections as `(a, b, score, isTP)` tuples, where `isTP` specifies whether the detection
    is a true positive or not.
    """
    
    newDet = []
    region_detected = [False] * len(gt) # prevent multiple detections of the same interval
    for a, b, score in det:
        
        isTP = False
        for i_gt, (a_gt, b_gt) in enumerate(gt):

            if (not region_detected[i_gt]) and (IoU(a, b - a, a_gt, b_gt - a_gt) >= overlap):