# 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
                # Account for detections with exactly the same score
                if (i < len(sorted_regions) - 1) and (score == sorted_regions[i+1][2]):
                    mask[i] = False
        
            # Compute recall and precision
            tp = tp.cumsum()[mask]
            fp = fp.cumsum()[mask]
            return (tp / sum(len(gt) for gt in ygt), tp / (tp + fp) if len(sorted_regions) > 0 else tp)
    
    else:
    
        if not isinstance(ygt, list):
        
            # Count true positives for a specific threshold
            tp = sum(int(isTP) for detections in regions for a, b, score, isTP in detections if score >= th)
            
            # Compute recall and precision
            return (float(tp) / ygt, float(tp) / max(1, sum(len(detections) for detections in regions)))
        
        else:
    
            # Count true and false positives for a specific threshold
            tp, fp = 0, 0
            region_detected = [[False] * len(gt) for gt in ygt] # prevent multiple detections of the same interval
            for i in range(len(regions)):
                for a, b, score in regions[i]:
                    if (th is None) or (score >= th):
                    
                        isTP = False
                        for i_gt, (a_gt, b_gt) in enumerate(ygt[i]):

# 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

                # Account for detections with exactly the same score
                if (i < len(sorted_regions) - 1) and (score == sorted_regions[i+1][2]):
                    mask[i] = False
        
            # Compute recall and precision
            tp = tp.cumsum()[mask]
            fp = fp.cumsum()[mask]
            return (tp / sum(len(gt) for gt in ygt), tp / (tp + fp) if len(sorted_regions) > 0 else tp)
    
    else:
    
        if not isinstance(ygt, list):
        
            # Count true positives for a specific threshold
            tp = sum(int(isTP) for detections in regions for a, b, score, isTP in detections if score >= th)
            
            # Compute recall and precision
            return (float(tp) / ygt, float(tp) / max(1, sum(len(detections) for detections in regions)))
        
        else:
    
            # Count true and false positives for a specific threshold
            tp, fp = 0, 0
            region_detected = [[False] * len(gt) for gt in ygt] # prevent multiple detections of the same interval
            for i in range(len(regions)):
                for a, b, score in regions[i]:
                    if (th is None) or (score >= th):
                    
                        isTP = False
                        for i_gt, (a_gt, b_gt) in enumerate(ygt[i]):

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