# Path: maxdiv/eval.py
# def pointwiseLabelsToIntervals(labels):
#     """Converts an array of boolean labels for each point in a time series to a list of contiguous intervals."""
#     
#     regions = []
#     current_start = None
#     
#     for i, lbl in enumerate(labels):
#         # Begin of new interval
#         if (current_start is None) and lbl:
#             current_start = i
#         # End of current interval
#         elif (current_start is not None) and (not lbl):
#             regions.append((current_start, i))
#             current_start = None
#     
#     # Terminate trailing interval
#     if current_start is not None:
#         regions.append((current_start, len(labels)))
#     
#     return regions


import csv, json, datetime, os.path
import numpy as np
    import cPickle as pickle
    import pickle
from maxdiv.eval import pointwiseLabelsToIntervals
        elif (ds == 'synthetic_seasonal') and ('interval' in types):
            data.update(loadSyntheticTestbench(BASEPATH + '/synthetic/testcube_seasonal.pickle'))
        elif ds == 'synthetic_normal':
            data.update(loadSyntheticNormalTestbench())
        elif ds.startswith('nab_'):
            data.update(loadNabDataset(subset = ds[4:], types = types))
        elif ds.startswith('yahoo_'):
            data.update(loadYahooDataset(subset = ds[6:], types = types, minAnomalyLength = 10))
        else:
            raise ValueError('Unknown dataset: {}'.format(ds))
    
    return data


def loadSyntheticTestbench(filename = BASEPATH + '/synthetic/testcube.pickle'):
    """ Loads a synthetic test bench with different types of anomalous intervals.
    
    The testbench can be generated using `testbench.py`.
    This function will load it from the Pickle file created by that script and returns
    a dictionary in the format described in `loadDatasets()`.
    """
    
    with open(filename, 'rb') as fin:
        cube = pickle.load(fin)
        f = cube['f']
        y = cube['y']
    
    return { ftype : [{
        'ts'    : func,
        'ticks' : list(range(func.shape[1])),

# Path: maxdiv/eval.py
# def pointwiseLabelsToIntervals(labels):
#     """Converts an array of boolean labels for each point in a time series to a list of contiguous intervals."""
#     
#     regions = []
#     current_start = None
#     
#     for i, lbl in enumerate(labels):
#         # Begin of new interval
#         if (current_start is None) and lbl:
#             current_start = i
#         # End of current interval
#         elif (current_start is not None) and (not lbl):
#             regions.append((current_start, i))
#             current_start = None
#     
#     # Terminate trailing interval
#     if current_start is not None:
#         regions.append((current_start, len(labels)))
#     
#     return regions


# Path: experiments/datasets.py
import csv, json, datetime, os.path
import numpy as np
    import cPickle as pickle
    import pickle
from maxdiv.eval import pointwiseLabelsToIntervals

        elif (ds == 'synthetic_seasonal') and ('interval' in types):
            data.update(loadSyntheticTestbench(BASEPATH + '/synthetic/testcube_seasonal.pickle'))
        elif ds == 'synthetic_normal':
            data.update(loadSyntheticNormalTestbench())
        elif ds.startswith('nab_'):
            data.update(loadNabDataset(subset = ds[4:], types = types))
        elif ds.startswith('yahoo_'):
            data.update(loadYahooDataset(subset = ds[6:], types = types, minAnomalyLength = 10))
        else:
            raise ValueError('Unknown dataset: {}'.format(ds))
    
    return data


def loadSyntheticTestbench(filename = BASEPATH + '/synthetic/testcube.pickle'):
    """ Loads a synthetic test bench with different types of anomalous intervals.
    
    The testbench can be generated using `testbench.py`.
    This function will load it from the Pickle file created by that script and returns
    a dictionary in the format described in `loadDatasets()`.
    """
    
    with open(filename, 'rb') as fin:
        cube = pickle.load(fin)
        f = cube['f']
        y = cube['y']
    
    return { ftype : [{
        'ts'    : func,
        'ticks' : list(range(func.shape[1])),

        'gt'    : pointwiseLabelsToIntervals(ygt),