# Path: pysaliency/baseline_utils.py
# class ScikitLearnImageCrossValidationGenerator(object):
#     def __init__(self, stimuli, fixations):
#         self.stimuli = stimuli
#         self.fixations = fixations
# 
#     def __iter__(self):
#         for n in range(len(self.stimuli)):
#             inds = self.fixations.n == n
#             if inds.sum():
#                 yield ~inds, inds
# 
#     def __len__(self):
#         return len(self.stimuli)
# 
# class ScikitLearnImageSubjectCrossValidationGenerator(object):
#     def __init__(self, stimuli, fixations):
#         self.stimuli = stimuli
#         self.fixations = fixations
# 
#     def __iter__(self):
#         for n in range(len(self.stimuli)):
#             for s in range(self.fixations.subject_count):
#                 image_inds = self.fixations.n == n
#                 subject_inds = self.fixations.subjects == s
#                 train_inds, test_inds = image_inds & ~subject_inds, image_inds & subject_inds
#                 if test_inds.sum() == 0 or train_inds.sum() == 0:
#                     #print("Skipping")
#                     continue
#                 yield train_inds, test_inds
# 
#     def __len__(self):
#         return len(set(zip(self.fixations.n, self.fixations.subjects)))
# 
# class RegularizedKernelDensityEstimator(BaseEstimator):
#     def __init__(self, bandwidth=1.0, regularization = 1.0e-5):
#         self.bandwidth = bandwidth
#         self.regularization = regularization
# 
#     def setup(self):
#         self.kde = KernelDensity(kernel='gaussian', bandwidth=self.bandwidth)
# 
#         height, width = self.shape
#         self.uniform_density = -np.log(width*height)
# 
#         self.kde_constant = np.log(1-self.regularization)
#         self.uniform_constant = np.log(self.regularization)
# 
#     def fit(self, X):
#         self.shape = X[0, 2:4]
#         self.setup()
#         self.kde.fit(X[:, 0:2])
#         return self
# 
#     def score_samples(self, X):
#         kde_logliks = self.kde.score_samples(X[:, :2])
# 
#         logliks = np.logaddexp(
#             self.kde_constant + kde_logliks,
#             self.uniform_constant + self.uniform_density
#         )
#         return logliks
# 
#     def score(self, X):
#         return np.sum(self.score_samples(X))
# 
# def fixations_to_scikit_learn(fixations, normalize=None, keep_aspect=False, add_shape=False,
#                               add_stimulus_number=False,
#                               add_fixation_number=False,
#                               verbose=True):
#     if normalize is None:
#         xs = fixations.x
#         ys = fixations.y
#         data = [xs, ys]
#         if add_shape:
#             raise NotImplementedError()
#     else:
#         data = normalize_fixations(normalize, fixations, keep_aspect=keep_aspect, add_shape=add_shape,
#                                    verbose=verbose)
#     if add_stimulus_number:
#         data = list(data) + [fixations.n]
#     if add_fixation_number:
#         data = list(data) + [np.arange(len(fixations.n))]
#     return np.vstack(data).T.copy()


import pytest
import numpy as np
import pysaliency
from pysaliency.baseline_utils import ScikitLearnImageCrossValidationGenerator, ScikitLearnImageSubjectCrossValidationGenerator, \
    RegularizedKernelDensityEstimator, fixations_to_scikit_learn
from sklearn.model_selection import cross_val_score
        [11]]
    ts_trains = [
        [0, 200, 600],
        [100, 400],
        [50, 500, 900],
        [100]]
    ns = [0, 0, 1, 2]
    subjects = [0, 1, 1, 0]
    return pysaliency.FixationTrains.from_fixation_trains(xs_trains, ys_trains, ts_trains, ns, subjects)


@pytest.fixture
def stimuli():
    return pysaliency.Stimuli([np.random.randn(40, 40, 3),
                               np.random.randn(40, 40, 3),
                               np.random.randn(40, 40, 3)])


def _unpack_crossval(cv):
    for train_inds, test_inds in cv:
        yield list(train_inds), list(test_inds)


def unpack_crossval(cv):
    return list(_unpack_crossval(cv))


def test_image_crossvalidation(stimuli, fixation_trains):
    gsmm = GaussianSaliencyModel()


# Path: pysaliency/baseline_utils.py
# class ScikitLearnImageCrossValidationGenerator(object):
#     def __init__(self, stimuli, fixations):
#         self.stimuli = stimuli
#         self.fixations = fixations
# 
#     def __iter__(self):
#         for n in range(len(self.stimuli)):
#             inds = self.fixations.n == n
#             if inds.sum():
#                 yield ~inds, inds
# 
#     def __len__(self):
#         return len(self.stimuli)
# 
# class ScikitLearnImageSubjectCrossValidationGenerator(object):
#     def __init__(self, stimuli, fixations):
#         self.stimuli = stimuli
#         self.fixations = fixations
# 
#     def __iter__(self):
#         for n in range(len(self.stimuli)):
#             for s in range(self.fixations.subject_count):
#                 image_inds = self.fixations.n == n
#                 subject_inds = self.fixations.subjects == s
#                 train_inds, test_inds = image_inds & ~subject_inds, image_inds & subject_inds
#                 if test_inds.sum() == 0 or train_inds.sum() == 0:
#                     #print("Skipping")
#                     continue
#                 yield train_inds, test_inds
# 
#     def __len__(self):
#         return len(set(zip(self.fixations.n, self.fixations.subjects)))
# 
# class RegularizedKernelDensityEstimator(BaseEstimator):
#     def __init__(self, bandwidth=1.0, regularization = 1.0e-5):
#         self.bandwidth = bandwidth
#         self.regularization = regularization
# 
#     def setup(self):
#         self.kde = KernelDensity(kernel='gaussian', bandwidth=self.bandwidth)
# 
#         height, width = self.shape
#         self.uniform_density = -np.log(width*height)
# 
#         self.kde_constant = np.log(1-self.regularization)
#         self.uniform_constant = np.log(self.regularization)
# 
#     def fit(self, X):
#         self.shape = X[0, 2:4]
#         self.setup()
#         self.kde.fit(X[:, 0:2])
#         return self
# 
#     def score_samples(self, X):
#         kde_logliks = self.kde.score_samples(X[:, :2])
# 
#         logliks = np.logaddexp(
#             self.kde_constant + kde_logliks,
#             self.uniform_constant + self.uniform_density
#         )
#         return logliks
# 
#     def score(self, X):
#         return np.sum(self.score_samples(X))
# 
# def fixations_to_scikit_learn(fixations, normalize=None, keep_aspect=False, add_shape=False,
#                               add_stimulus_number=False,
#                               add_fixation_number=False,
#                               verbose=True):
#     if normalize is None:
#         xs = fixations.x
#         ys = fixations.y
#         data = [xs, ys]
#         if add_shape:
#             raise NotImplementedError()
#     else:
#         data = normalize_fixations(normalize, fixations, keep_aspect=keep_aspect, add_shape=add_shape,
#                                    verbose=verbose)
#     if add_stimulus_number:
#         data = list(data) + [fixations.n]
#     if add_fixation_number:
#         data = list(data) + [np.arange(len(fixations.n))]
#     return np.vstack(data).T.copy()


# Path: tests/test_crossvalidation.py
import pytest
import numpy as np
import pysaliency
from pysaliency.baseline_utils import ScikitLearnImageCrossValidationGenerator, ScikitLearnImageSubjectCrossValidationGenerator, \
    RegularizedKernelDensityEstimator, fixations_to_scikit_learn
from sklearn.model_selection import cross_val_score

        [11]]
    ts_trains = [
        [0, 200, 600],
        [100, 400],
        [50, 500, 900],
        [100]]
    ns = [0, 0, 1, 2]
    subjects = [0, 1, 1, 0]
    return pysaliency.FixationTrains.from_fixation_trains(xs_trains, ys_trains, ts_trains, ns, subjects)


@pytest.fixture
def stimuli():
    return pysaliency.Stimuli([np.random.randn(40, 40, 3),
                               np.random.randn(40, 40, 3),
                               np.random.randn(40, 40, 3)])


def _unpack_crossval(cv):
    for train_inds, test_inds in cv:
        yield list(train_inds), list(test_inds)


def unpack_crossval(cv):
    return list(_unpack_crossval(cv))


def test_image_crossvalidation(stimuli, fixation_trains):
    gsmm = GaussianSaliencyModel()


    cv = ScikitLearnImageCrossValidationGenerator(stimuli, fixation_trains)