# 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
    for i in range(numSamples):
        permDet = []
        for d in det:
            x = random.randint(0, 1)
            permDet.append({
                'ftype' : d['ftype'],
                'fnum'  : d['fnum'],
                'det1'  : d['det1' if x == 0 else 'det2'],
                'det2'  : d['det2' if x == 0 else 'det1'],
                'num_gt': d['num_gt']
            })
        stats.append(stat(permDet))
    p = (np.abs(stats) >= np.abs(obs)).sum() / numSamples
    conf = (scipy.stats.binom.ppf(0.005, n = numSamples, p = p) / numSamples, scipy.stats.binom.ppf(0.995, n = numSamples, p = p) / numSamples)
    if conf[0] > conf[1]:
        conf = (conf[1], conf[1])
    return p, conf


def apDiff(det):
    """ Computes the difference of Overall Average Precision for two sets of detections.
    
    The detections have to be given as a list of dictionaries with the keys 'det1', 'det2'
    and 'num_gt' (number of ground-truth intervals).
    
    Returns: Tuple with 3 elements: AP of the 1st detections, AP of the 2nd detections, difference of both.
    """
    
    num_gt = sum(d['num_gt'] for d in det)
    ap1 = eval.average_precision(num_gt, [d['det1'] for d in det])

# 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

    for i in range(numSamples):
        permDet = []
        for d in det:
            x = random.randint(0, 1)
            permDet.append({
                'ftype' : d['ftype'],
                'fnum'  : d['fnum'],
                'det1'  : d['det1' if x == 0 else 'det2'],
                'det2'  : d['det2' if x == 0 else 'det1'],
                'num_gt': d['num_gt']
            })
        stats.append(stat(permDet))
    p = (np.abs(stats) >= np.abs(obs)).sum() / numSamples
    conf = (scipy.stats.binom.ppf(0.005, n = numSamples, p = p) / numSamples, scipy.stats.binom.ppf(0.995, n = numSamples, p = p) / numSamples)
    if conf[0] > conf[1]:
        conf = (conf[1], conf[1])
    return p, conf


def apDiff(det):
    """ Computes the difference of Overall Average Precision for two sets of detections.
    
    The detections have to be given as a list of dictionaries with the keys 'det1', 'det2'
    and 'num_gt' (number of ground-truth intervals).
    
    Returns: Tuple with 3 elements: AP of the 1st detections, AP of the 2nd detections, difference of both.
    """
    
    num_gt = sum(d['num_gt'] for d in det)
    ap1 = eval.average_precision(num_gt, [d['det1'] for d in det])

    ap2 = eval.average_precision(num_gt, [d['det2'] for d in det])