# Path: maxdiv/maxdiv_util.py
# def enforce_multivariate_timeseries(X):
# def calc_distance_matrix(X, metric='sqeuclidean'):
# def calc_gaussian_kernel(X, kernel_sigma_sq = 1.0, normalized=True):
# def calc_nonstationary_gaussian_kernel(X, kernel_sigma_sq_vec):
# def IoU(start1, len1, start2, len2):
# def td_mutual_information(ts, k, T = 1):
# def context_window_size(X, th = 0.05):
# def m_estimation(A, b, k = 1.345):
# def plot_matrix_with_interval(D, a, b):
#         X = X.reshape((1, len(X)))
#     D = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(X.T, metric))
#         D = np.ma.MaskedArray(D)
#     D = calc_distance_matrix(X)
#     K = np.exp(-D/(2.0*kernel_sigma_sq))
#         K = K / ((2*np.pi*kernel_sigma_sq) ** (X.shape[0] / 2))
#     D = calc_distance_matrix(X)
#     S = np.tile(kernel_sigma_sq_vec, [n,1])
#     K = np.exp(-D/(0.5*S_sum))*(np.power(S_prod,0.25)/np.sqrt(0.5*S_sum))
#         W = np.diag(np.sqrt(w))
# 
# 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.pyplot as plt
import datasets
from maxdiv import maxdiv_util, preproc
    if func['ts'].shape[1] >= minSVDLen:
        mm[row,:] = func['ts'][0,:minSVDLen]
        rowAssoc[id] = row
        row += 1
# Perform SVD
mm_u, mm_s, mm_vt = np.linalg.svd(mm, full_matrices = False)
# Plot first right singular vectors
fig = plt.figure()
for i in range(6):
    ax = fig.add_subplot(3, 2, i + 1, title = '{}. right-singular vector for singular value {}'.format(i+1, mm_s[i]))
    ax.plot(mm_vt[i,:])
    freq = np.fft.fft(mm_vt[i,:])
    ps = (freq * freq.conj()).real
    period = ps[1:(len(ps)//2)+1].argmax() + 1
    print('Period of {}. right singular vector: {} -> {}'.format(i+1, period, float(minSVDLen) / period))
plt.show()
# Remove some leading singular values
try:
    rsRem = raw_input('Enter number of right-singular vectors to remove: ')
except:
    rsRem = input('Enter number of right-singular vectors to remove: ')
rsRem = int(rsRem)
mm_s[:rsRem] = 0.0
mm_norm = mm_u.dot(np.diag(mm_s).dot(mm_vt))
"""


# Individual De-seasonalization by DFT, Hourly Z Score and OLS
print('-- DFT, Hourly Z Score, OLS --')
for id in ids:

# Path: maxdiv/maxdiv_util.py
# def enforce_multivariate_timeseries(X):
# def calc_distance_matrix(X, metric='sqeuclidean'):
# def calc_gaussian_kernel(X, kernel_sigma_sq = 1.0, normalized=True):
# def calc_nonstationary_gaussian_kernel(X, kernel_sigma_sq_vec):
# def IoU(start1, len1, start2, len2):
# def td_mutual_information(ts, k, T = 1):
# def context_window_size(X, th = 0.05):
# def m_estimation(A, b, k = 1.345):
# def plot_matrix_with_interval(D, a, b):
#         X = X.reshape((1, len(X)))
#     D = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(X.T, metric))
#         D = np.ma.MaskedArray(D)
#     D = calc_distance_matrix(X)
#     K = np.exp(-D/(2.0*kernel_sigma_sq))
#         K = K / ((2*np.pi*kernel_sigma_sq) ** (X.shape[0] / 2))
#     D = calc_distance_matrix(X)
#     S = np.tile(kernel_sigma_sq_vec, [n,1])
#     K = np.exp(-D/(0.5*S_sum))*(np.power(S_prod,0.25)/np.sqrt(0.5*S_sum))
#         W = np.diag(np.sqrt(w))
# 
# 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/deseasonalization.py
import sys
import numpy as np
import matplotlib.pyplot as plt
import datasets
from maxdiv import maxdiv_util, preproc

    if func['ts'].shape[1] >= minSVDLen:
        mm[row,:] = func['ts'][0,:minSVDLen]
        rowAssoc[id] = row
        row += 1
# Perform SVD
mm_u, mm_s, mm_vt = np.linalg.svd(mm, full_matrices = False)
# Plot first right singular vectors
fig = plt.figure()
for i in range(6):
    ax = fig.add_subplot(3, 2, i + 1, title = '{}. right-singular vector for singular value {}'.format(i+1, mm_s[i]))
    ax.plot(mm_vt[i,:])
    freq = np.fft.fft(mm_vt[i,:])
    ps = (freq * freq.conj()).real
    period = ps[1:(len(ps)//2)+1].argmax() + 1
    print('Period of {}. right singular vector: {} -> {}'.format(i+1, period, float(minSVDLen) / period))
plt.show()
# Remove some leading singular values
try:
    rsRem = raw_input('Enter number of right-singular vectors to remove: ')
except:
    rsRem = input('Enter number of right-singular vectors to remove: ')
rsRem = int(rsRem)
mm_s[:rsRem] = 0.0
mm_norm = mm_u.dot(np.diag(mm_s).dot(mm_vt))
"""


# Individual De-seasonalization by DFT, Hourly Z Score and OLS
print('-- DFT, Hourly Z Score, OLS --')
for id in ids:

    func = preproc.normalize_time_series(data[id]['ts']).ravel()