# 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 numpy as np
import sklearn.metrics
import matplotlib.pylab as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from .maxdiv_util import IoU
            
            # Count true positives for all thresholds
            tp = np.array([int(isTP) for a, b, score, isTP in sorted_regions]).cumsum()
            numDet = np.arange(1, len(sorted_regions) + 1)

            # Account for detections with exactly the same score
            mask = np.array([((i >= len(tp) - 1) or (sorted_regions[i][2] != sorted_regions[i+1][2])) for i in range(len(tp))])
            if not mask.all():
                tp = tp[mask]
                numDet = numDet[mask]
            
            # Compute recall and precision
            return (tp / ygt, tp / numDet if len(sorted_regions) > 0 else tp)
        
        else:
        
            # Flatten regions array after associating each region with its time series and sort them descendingly by their score
            sorted_regions = sorted((tuple(region) + (ts,) for ts, detections in enumerate(regions) for region in detections), key = lambda r: r[2], reverse = True)
        
            # Indicators for true and false positives
            tp = np.zeros(len(sorted_regions))
            fp = np.zeros(len(sorted_regions))
            mask = np.ones(len(sorted_regions), dtype = bool)
            
            # Determine for each detection if it is a true or a false positive
            region_detected = [[False] * len(gt) for gt in ygt] # prevent multiple detections of the same interval
            for i, (a, b, score, ts) in enumerate(sorted_regions):
                
                isTP, isSubsequentDetection = False, False
                for i_gt, (a_gt, b_gt) in enumerate(ygt[ts]):

# 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: maxdiv/eval.py
import numpy as np
import sklearn.metrics
import matplotlib.pylab as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from .maxdiv_util import IoU

            
            # Count true positives for all thresholds
            tp = np.array([int(isTP) for a, b, score, isTP in sorted_regions]).cumsum()
            numDet = np.arange(1, len(sorted_regions) + 1)

            # Account for detections with exactly the same score
            mask = np.array([((i >= len(tp) - 1) or (sorted_regions[i][2] != sorted_regions[i+1][2])) for i in range(len(tp))])
            if not mask.all():
                tp = tp[mask]
                numDet = numDet[mask]
            
            # Compute recall and precision
            return (tp / ygt, tp / numDet if len(sorted_regions) > 0 else tp)
        
        else:
        
            # Flatten regions array after associating each region with its time series and sort them descendingly by their score
            sorted_regions = sorted((tuple(region) + (ts,) for ts, detections in enumerate(regions) for region in detections), key = lambda r: r[2], reverse = True)
        
            # Indicators for true and false positives
            tp = np.zeros(len(sorted_regions))
            fp = np.zeros(len(sorted_regions))
            mask = np.ones(len(sorted_regions), dtype = bool)
            
            # Determine for each detection if it is a true or a false positive
            region_detected = [[False] * len(gt) for gt in ygt] # prevent multiple detections of the same interval
            for i, (a, b, score, ts) in enumerate(sorted_regions):
                
                isTP, isSubsequentDetection = False, False
                for i_gt, (a_gt, b_gt) in enumerate(ygt[ts]):

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