# 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: maxdiv/maxdiv_util.py
# def calc_gaussian_kernel(X, kernel_sigma_sq = 1.0, normalized=True):
#     """ Calculate a normalized Gaussian kernel using the columns of X """
#     # Let's first compute the kernel matrix from our squared Euclidean distances in $D$.
#     D = calc_distance_matrix(X)
#     # compute proper normalized Gaussian kernel values
#     K = np.exp(-D/(2.0*kernel_sigma_sq))
#     if normalized:
#         K = K / ((2*np.pi*kernel_sigma_sq) ** (X.shape[0] / 2))
#     return K
# 
# 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
    from sklearn.mixture import GMM
    from sklearn.mixture import GaussianMixture
from .eval import pointwiseLabelsToIntervals
from .maxdiv_util import calc_gaussian_kernel, IoU
    # sklearn < 0.18
except ImportError:


def hotellings_t(X):
    """ Global version of hotellings_t """
    
    mu = X.mean(axis = 1)
    cov = np.ma.cov(X).filled(0)
    zeromean_X = (X.T - mu).T
    if X.shape[0]==1:
        norm_X = zeromean_X/cov
        scores = zeromean_X*norm_X
        scores = scores.ravel()
    else:
        invcov = np.linalg.inv(cov)
        norm_X = np.dot(invcov, zeromean_X) if not np.ma.isMaskedArray(zeromean_X) else np.ma.dot(invcov, zeromean_X)
        scores = (norm_X * zeromean_X).sum(axis=0)
    if np.ma.isMaskedArray(X):
        scores.mask = X.mask[0,:]
    return scores


def pointwiseKDE(X, kernel_sigma_sq = 1.0):
    """ Scores every point in the time series by Kernel Density Estimation.
    
    Note: The values of X should be in range [-1,1] to avoid numerical problems.
    """
    
    # Compute kernel matrix

# 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: maxdiv/maxdiv_util.py
# def calc_gaussian_kernel(X, kernel_sigma_sq = 1.0, normalized=True):
#     """ Calculate a normalized Gaussian kernel using the columns of X """
#     # Let's first compute the kernel matrix from our squared Euclidean distances in $D$.
#     D = calc_distance_matrix(X)
#     # compute proper normalized Gaussian kernel values
#     K = np.exp(-D/(2.0*kernel_sigma_sq))
#     if normalized:
#         K = K / ((2*np.pi*kernel_sigma_sq) ** (X.shape[0] / 2))
#     return K
# 
# 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/baselines_noninterval.py
import numpy as np
    from sklearn.mixture import GMM
    from sklearn.mixture import GaussianMixture
from .eval import pointwiseLabelsToIntervals
from .maxdiv_util import calc_gaussian_kernel, IoU

    # sklearn < 0.18
except ImportError:


def hotellings_t(X):
    """ Global version of hotellings_t """
    
    mu = X.mean(axis = 1)
    cov = np.ma.cov(X).filled(0)
    zeromean_X = (X.T - mu).T
    if X.shape[0]==1:
        norm_X = zeromean_X/cov
        scores = zeromean_X*norm_X
        scores = scores.ravel()
    else:
        invcov = np.linalg.inv(cov)
        norm_X = np.dot(invcov, zeromean_X) if not np.ma.isMaskedArray(zeromean_X) else np.ma.dot(invcov, zeromean_X)
        scores = (norm_X * zeromean_X).sum(axis=0)
    if np.ma.isMaskedArray(X):
        scores.mask = X.mask[0,:]
    return scores


def pointwiseKDE(X, kernel_sigma_sq = 1.0):
    """ Scores every point in the time series by Kernel Density Estimation.
    
    Note: The values of X should be in range [-1,1] to avoid numerical problems.
    """
    
    # Compute kernel matrix

    K = calc_gaussian_kernel(X, kernel_sigma_sq, False)