# 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, os.path
import datetime, csv
import scipy.io
from maxdiv.maxdiv_util import IoU
	'day_step': 1
    }

def loadTensor(matfile, tensorvar):
    """ load a simple tensor from a MATLAB file """
    tensor_data = scipy.io.loadmat(matfile)
    if tensorvar in tensor_data:
        tensor = tensor_data[tensorvar]
    else:
        raise Exception('Variable {} not present in {}\nAvailable variables: {}\n'.format(tensorvar,
	    matfile, tensor_data.keys()))
    sys.stderr.write ("Tensor shape: {}\n".format(tensor.shape))
    return tensor.reshape(tensor.shape + (1, 1))


# Historic event data

def loadHistoricEvents(filename = '../coastdat/historic_storms.csv'):
    """ load historic events from a CSV file """
    with open(filename) as eventfile:
        events = list(csv.DictReader(eventfile, delimiter = '\t'))
    for event in events:
        # historic events are only temporal right now
        event['START_DATE'] = datetime.datetime.strptime(event['START_DATE'], '%Y-%m-%d').date()
        event['END_DATE'] = datetime.datetime.strptime(event['END_DATE'], '%Y-%m-%d').date()
    return events

def matchDetectionWithEvent(detection, historic_events, year_offs):
    base_date = datetime.date(year_offs, 1, 1)
    # we ignore day_step here, since it should cancel out

# 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: experiments/SLP/utils.py
import sys, os.path
import datetime, csv
import scipy.io
from maxdiv.maxdiv_util import IoU

	'day_step': 1
    }

def loadTensor(matfile, tensorvar):
    """ load a simple tensor from a MATLAB file """
    tensor_data = scipy.io.loadmat(matfile)
    if tensorvar in tensor_data:
        tensor = tensor_data[tensorvar]
    else:
        raise Exception('Variable {} not present in {}\nAvailable variables: {}\n'.format(tensorvar,
	    matfile, tensor_data.keys()))
    sys.stderr.write ("Tensor shape: {}\n".format(tensor.shape))
    return tensor.reshape(tensor.shape + (1, 1))


# Historic event data

def loadHistoricEvents(filename = '../coastdat/historic_storms.csv'):
    """ load historic events from a CSV file """
    with open(filename) as eventfile:
        events = list(csv.DictReader(eventfile, delimiter = '\t'))
    for event in events:
        # historic events are only temporal right now
        event['START_DATE'] = datetime.datetime.strptime(event['START_DATE'], '%Y-%m-%d').date()
        event['END_DATE'] = datetime.datetime.strptime(event['END_DATE'], '%Y-%m-%d').date()
    return events

def matchDetectionWithEvent(detection, historic_events, year_offs):
    base_date = datetime.date(year_offs, 1, 1)
    # we ignore day_step here, since it should cancel out

    maxOverlap = max(((event, IoU(