# Path: jr/gat/classifiers.py
# class PolarRegression(BaseEstimator):
# 
#     def __init__(self, clf=None, independent=True):
#         import copy
#         if clf is None:
#             clf = LinearSVR()
#         self.clf = clf
#         if independent:
#             self.clf_cos = copy.deepcopy(clf)
#             self.clf_sin = copy.deepcopy(clf)
#         self.independent = independent
# 
#     def fit(self, X, y, sample_weight=None):
#         """
#         Fit 2 regressors cos and sin of angles y
#         Parameters
#         ----------
#         X : np.array, shape(n_trials, n_chans, n_time)
#             MEG data
#         y : list | np.array (n_trials, 2)
#             angle in radians and radius. If no radius is provided, takes r=1.
#         """
#         sample_weight = (dict() if sample_weight is None
#                          else dict(sample_weight=sample_weight))
#         if y.ndim == 1:
#             y = np.vstack((y, np.ones_like(y))).T
#         cos = np.cos(y[:, 0]) * y[:, 1]
#         sin = np.sin(y[:, 0]) * y[:, 1]
#         if self.independent:
#             self.clf_cos.fit(X, cos, **sample_weight)
#             self.clf_sin.fit(X, sin, **sample_weight)
#         else:
#             self.clf.fit(X, np.vstack((cos, sin)).T, **sample_weight)
# 
#     def predict(self, X):
#         """
#         Predict orientation from MEG data in radians
#         Parameters
#         ----------
#         X : np.array, shape(n_trials, n_chans, n_time)
#             MEG data
#         Returns
#         -------
#         predict_angle : list | np.array, shape(n_trials)
#             angle predictions in radian
#         """
#         if self.independent:
#             predict_cos = self.clf_cos.predict(X)
#             predict_sin = self.clf_sin.predict(X)
#         else:
#             predict_cossin = self.clf.predict(X)
#             predict_cos = predict_cossin[:, 0]
#             predict_sin = predict_cossin[:, 1]
#         predict_angle = np.arctan2(predict_sin, predict_cos)
#         predict_radius = np.sqrt(predict_sin ** 2 + predict_cos ** 2)
#         y_pred = np.concatenate((predict_angle.reshape([-1, 1]),
#                                  predict_radius.reshape([-1, 1])), axis=1)
#         return y_pred
# 
#     @property
#     def coef_(self,):
#         if self.independent:
#             return np.array([self.clf_cos.coef_, self.clf_sin.coef_])
#         else:
#             return self.clf.coef_
# 
# class AngularRegression(PolarRegression):
#     def predict(self, X):
#         return super(AngularRegression, self).predict(X)[:, 0]
# 
#     def score(self, X, y):
#         y_pred = self.predict(X)
#         return scorer_angle(y, y_pred)
# 
# class SVR_polar(PolarRegression):  # FIXME deprecate
# 
#     def __init__(self, clf=None, C=1, **kwargs):
#         from sklearn.preprocessing import StandardScaler
#         from sklearn.pipeline import make_pipeline
#         import warnings
#         warnings.warn('Prefer using PolarRegression(). Will be deprecated')
#         if clf is None:
#             clf = LinearSVR(C=C, **kwargs)
#         self.clf = make_pipeline(StandardScaler(), clf)
#         self.C = C
#         self.kwargs = kwargs
#         super(SVR_polar, self).__init__(clf=clf, independent=True)
# 
# class SVR_angle(SVR_polar):  # FIXME deprecate
#     def predict(self, X):
#         return super(SVR_angle, self).predict(X)[:, 0]


import numpy as np
    import matplotlib.pyplot as plt
from nose.tools import assert_true
from ..classifiers import (PolarRegression, AngularRegression,
                           SVR_polar, SVR_angle)
    from jr.meg import mat2mne
    from mne.decoding import GeneralizationAcrossTime
    from ..scorers import scorer_angle
    from sklearn.linear_model import Ridge, RidgeCV
    for trial in range(n_trial):
        X[trial, :, 1] += coefs[y[trial]].flatten()
        X[trial, :, 2] += coefs[
            y[(trial + len(angles)/2) % len(angles)]].flatten()

    # ---- export in mne structure
    events = np.array(y * 10, int)  # need integers, and avoid duplicate
    epochs = mat2mne(X, events=events)
    return epochs, angles[y]


def show_circular_data():
    epochs, angles = make_circular_data()
    coefs = list()
    sel_time = 1  # select only one time point
    for ii in np.unique(angles):
        # select each trial
        sel_y = np.where(angles == ii)[0]
        # get mean effect
        coef = np.mean(epochs._data[sel_y, :, sel_time], axis=0)
        # square image
        coef = np.reshape(coef, [np.sqrt(len(coef))] * 2)
        coefs.append()
    for coef in coefs:
        plt.matshow(coef[:, 1])
    plt.show()


def test_circular_classifiers():
    epochs, angles = make_circular_data()

# Path: jr/gat/classifiers.py
# class PolarRegression(BaseEstimator):
# 
#     def __init__(self, clf=None, independent=True):
#         import copy
#         if clf is None:
#             clf = LinearSVR()
#         self.clf = clf
#         if independent:
#             self.clf_cos = copy.deepcopy(clf)
#             self.clf_sin = copy.deepcopy(clf)
#         self.independent = independent
# 
#     def fit(self, X, y, sample_weight=None):
#         """
#         Fit 2 regressors cos and sin of angles y
#         Parameters
#         ----------
#         X : np.array, shape(n_trials, n_chans, n_time)
#             MEG data
#         y : list | np.array (n_trials, 2)
#             angle in radians and radius. If no radius is provided, takes r=1.
#         """
#         sample_weight = (dict() if sample_weight is None
#                          else dict(sample_weight=sample_weight))
#         if y.ndim == 1:
#             y = np.vstack((y, np.ones_like(y))).T
#         cos = np.cos(y[:, 0]) * y[:, 1]
#         sin = np.sin(y[:, 0]) * y[:, 1]
#         if self.independent:
#             self.clf_cos.fit(X, cos, **sample_weight)
#             self.clf_sin.fit(X, sin, **sample_weight)
#         else:
#             self.clf.fit(X, np.vstack((cos, sin)).T, **sample_weight)
# 
#     def predict(self, X):
#         """
#         Predict orientation from MEG data in radians
#         Parameters
#         ----------
#         X : np.array, shape(n_trials, n_chans, n_time)
#             MEG data
#         Returns
#         -------
#         predict_angle : list | np.array, shape(n_trials)
#             angle predictions in radian
#         """
#         if self.independent:
#             predict_cos = self.clf_cos.predict(X)
#             predict_sin = self.clf_sin.predict(X)
#         else:
#             predict_cossin = self.clf.predict(X)
#             predict_cos = predict_cossin[:, 0]
#             predict_sin = predict_cossin[:, 1]
#         predict_angle = np.arctan2(predict_sin, predict_cos)
#         predict_radius = np.sqrt(predict_sin ** 2 + predict_cos ** 2)
#         y_pred = np.concatenate((predict_angle.reshape([-1, 1]),
#                                  predict_radius.reshape([-1, 1])), axis=1)
#         return y_pred
# 
#     @property
#     def coef_(self,):
#         if self.independent:
#             return np.array([self.clf_cos.coef_, self.clf_sin.coef_])
#         else:
#             return self.clf.coef_
# 
# class AngularRegression(PolarRegression):
#     def predict(self, X):
#         return super(AngularRegression, self).predict(X)[:, 0]
# 
#     def score(self, X, y):
#         y_pred = self.predict(X)
#         return scorer_angle(y, y_pred)
# 
# class SVR_polar(PolarRegression):  # FIXME deprecate
# 
#     def __init__(self, clf=None, C=1, **kwargs):
#         from sklearn.preprocessing import StandardScaler
#         from sklearn.pipeline import make_pipeline
#         import warnings
#         warnings.warn('Prefer using PolarRegression(). Will be deprecated')
#         if clf is None:
#             clf = LinearSVR(C=C, **kwargs)
#         self.clf = make_pipeline(StandardScaler(), clf)
#         self.C = C
#         self.kwargs = kwargs
#         super(SVR_polar, self).__init__(clf=clf, independent=True)
# 
# class SVR_angle(SVR_polar):  # FIXME deprecate
#     def predict(self, X):
#         return super(SVR_angle, self).predict(X)[:, 0]


# Path: jr/gat/tests/test_clf.py
import numpy as np
    import matplotlib.pyplot as plt
from nose.tools import assert_true
from ..classifiers import (PolarRegression, AngularRegression,
                           SVR_polar, SVR_angle)
    from jr.meg import mat2mne
    from mne.decoding import GeneralizationAcrossTime
    from ..scorers import scorer_angle
    from sklearn.linear_model import Ridge, RidgeCV

    for trial in range(n_trial):
        X[trial, :, 1] += coefs[y[trial]].flatten()
        X[trial, :, 2] += coefs[
            y[(trial + len(angles)/2) % len(angles)]].flatten()

    # ---- export in mne structure
    events = np.array(y * 10, int)  # need integers, and avoid duplicate
    epochs = mat2mne(X, events=events)
    return epochs, angles[y]


def show_circular_data():
    epochs, angles = make_circular_data()
    coefs = list()
    sel_time = 1  # select only one time point
    for ii in np.unique(angles):
        # select each trial
        sel_y = np.where(angles == ii)[0]
        # get mean effect
        coef = np.mean(epochs._data[sel_y, :, sel_time], axis=0)
        # square image
        coef = np.reshape(coef, [np.sqrt(len(coef))] * 2)
        coefs.append()
    for coef in coefs:
        plt.matshow(coef[:, 1])
    plt.show()


def test_circular_classifiers():
    epochs, angles = make_circular_data()

    clf_list = [PolarRegression, AngularRegression,