# Path: maxdiv/preproc.py
# def get_available_methods():
# def local_linear_regression(X, window_size=5):
# def td(X, k = None, T = 1, opt_th = 0.05):
# def td_params(X, k = None, T = 1, opt_th = 0.05):
# def normalize_time_series(ts):
# def detect_periods(ts):
# def deseasonalize_ft(ts):
# def deseasonalize_zscore(ts, period_len):
# def detrend_linear(ts):
# def detrend_ols(ts, periods = None, linear_trend = True, linear_season_trend = False, return_model_params = False):
# def pca_projection(X, k):
# def sparse_random_projection(X, k):
#         C = np.zeros(dimension*dimension)
#         P = np.linspace(0.0, 1.0, end-start)
#         P = np.vstack( [P, np.ones(P.shape)] )
#             T = int(context_size // max_k) + 1
#             T = max(1, int(round(context_size / k)))
#     N = len(ts) if ts.ndim == 1 else ts.shape[1]
#     A = np.hstack((np.arange(0.0, float(N)).reshape((N, 1)), np.ones((N, 1))))
#     A = np.zeros((func.shape[1], num_params))
#     A[:,0] = 1
#         A[:,1] = np.arange(0.0, func.shape[1])
#     C = (X.T - X.mean(axis = 1)).T
#     R = np.ma.cov(C).filled(0)


import sys
import numpy as np
import matplotlib.pylab as plt
import datasets
from maxdiv import preproc
from maxdiv.baselines_noninterval import *

if len(sys.argv) < 3:
    print('Usage: {} <method> [<dataset>/]<extremetype> [<embedding-dimension = 3>]'.format(sys.argv[0]))
else:

    # Parse arguments
    method, ftype = sys.argv[1:3]
    embed_dim = int(sys.argv[3]) if len(sys.argv) > 3 else 3
    if method not in METHODS:
        print('Unknown method: {}\nPossible values: {}'.format(method, ', '.join(METHODS)))
        exit()
    
    # Load test data
    if '/' in ftype:
        dataset, ftype = ftype.split('/', 1)
    else:
        dataset = 'synthetic'
    try:
        data = datasets.loadDatasets(dataset)
    except:
        print('Unknown dataset: {}'.format(dataset))
        exit()
    if ftype not in data:
        print('Unknown extreme type: {}'.format(ftype))
        exit()
    
    # Detect and plot anomaly boundaries
    for func in data[ftype]:
        
        # Compute scores for each point

# Path: maxdiv/preproc.py
# def get_available_methods():
# def local_linear_regression(X, window_size=5):
# def td(X, k = None, T = 1, opt_th = 0.05):
# def td_params(X, k = None, T = 1, opt_th = 0.05):
# def normalize_time_series(ts):
# def detect_periods(ts):
# def deseasonalize_ft(ts):
# def deseasonalize_zscore(ts, period_len):
# def detrend_linear(ts):
# def detrend_ols(ts, periods = None, linear_trend = True, linear_season_trend = False, return_model_params = False):
# def pca_projection(X, k):
# def sparse_random_projection(X, k):
#         C = np.zeros(dimension*dimension)
#         P = np.linspace(0.0, 1.0, end-start)
#         P = np.vstack( [P, np.ones(P.shape)] )
#             T = int(context_size // max_k) + 1
#             T = max(1, int(round(context_size / k)))
#     N = len(ts) if ts.ndim == 1 else ts.shape[1]
#     A = np.hstack((np.arange(0.0, float(N)).reshape((N, 1)), np.ones((N, 1))))
#     A = np.zeros((func.shape[1], num_params))
#     A[:,0] = 1
#         A[:,1] = np.arange(0.0, func.shape[1])
#     C = (X.T - X.mean(axis = 1)).T
#     R = np.ma.cov(C).filled(0)


# Path: experiments/detect_anomaly_boundaries.py
import sys
import numpy as np
import matplotlib.pylab as plt
import datasets
from maxdiv import preproc
from maxdiv.baselines_noninterval import *


if len(sys.argv) < 3:
    print('Usage: {} <method> [<dataset>/]<extremetype> [<embedding-dimension = 3>]'.format(sys.argv[0]))
else:

    # Parse arguments
    method, ftype = sys.argv[1:3]
    embed_dim = int(sys.argv[3]) if len(sys.argv) > 3 else 3
    if method not in METHODS:
        print('Unknown method: {}\nPossible values: {}'.format(method, ', '.join(METHODS)))
        exit()
    
    # Load test data
    if '/' in ftype:
        dataset, ftype = ftype.split('/', 1)
    else:
        dataset = 'synthetic'
    try:
        data = datasets.loadDatasets(dataset)
    except:
        print('Unknown dataset: {}'.format(dataset))
        exit()
    if ftype not in data:
        print('Unknown extreme type: {}'.format(ftype))
        exit()
    
    # Detect and plot anomaly boundaries
    for func in data[ftype]:
        
        # Compute scores for each point

        scores = METHODS[method](preproc.td(func['ts'], embed_dim))