import numpy as np

from Orange.base import Learner, Model
from Orange.classification import LogisticRegressionLearner
from Orange.classification.base_classification import LearnerClassification
from Orange.data import Domain, ContinuousVariable, Table
from Orange.evaluation import CrossValidation
from Orange.regression import RidgeRegressionLearner
from Orange.regression.base_regression import LearnerRegression


__all__ = ['StackedLearner', 'StackedClassificationLearner',
           'StackedRegressionLearner']


class StackedModel(Model):
    def __init__(self, models, aggregate, use_prob=True):
        self.models = models
        self.aggregate = aggregate
        self.use_prob = use_prob

    def predict_storage(self, data):
        if self.use_prob:
            probs = [m(data, Model.Probs) for m in self.models]
            X = np.hstack(probs)
        else:
            pred = [m(data) for m in self.models]
            X = np.column_stack(pred)
        Y = np.repeat(np.nan, X.shape[0])
        stacked_data = data.transform(self.aggregate.domain)
        stacked_data.X = X
        stacked_data.Y = Y
        return self.aggregate(
            stacked_data, Model.ValueProbs if self.use_prob else Model.Value)


class StackedLearner(Learner):
    """
    Constructs a stacked model by fitting an aggregator
    over the results of base models.

    K-fold cross-validation is used to get predictions of the base learners
    and fit the aggregator to obtain a stacked model.

    Args:
        learners (list):
            list of `Learner`s used for base models

        aggregate (Learner):
            Learner used to fit the meta model, aggregating predictions
            of base models

        k (int):
            number of folds for cross-validation

    Returns:
        instance of StackedModel
    """

    __returns__ = StackedModel

    def __init__(self, learners, aggregate, k=5, preprocessors=None):
        super().__init__(preprocessors=preprocessors)
        self.learners = learners
        self.aggregate = aggregate
        self.k = k
        self.params = vars()

    def fit_storage(self, data):
        res = CrossValidation(data, self.learners, k=self.k)
        if data.domain.class_var.is_discrete:
            X = np.hstack(res.probabilities)
            use_prob = True
        else:
            X = res.predicted.T
            use_prob = False
        dom = Domain([ContinuousVariable('f{}'.format(i + 1))
                      for i in range(X.shape[1])],
                     data.domain.class_var)
        stacked_data = data.transform(dom)
        stacked_data.X = X
        stacked_data.Y = res.actual
        models = [l(data) for l in self.learners]
        aggregate_model = self.aggregate(stacked_data)
        return StackedModel(models, aggregate_model, use_prob=use_prob)


class StackedClassificationLearner(StackedLearner, LearnerClassification):
    """
    Subclass of StackedLearner intended for classification tasks.

    Same as the super class, but has a default
    classification-specific aggregator (`LogisticRegressionLearner`).
    """

    def __init__(self, learners, aggregate=LogisticRegressionLearner(), k=5):
        super().__init__(learners=learners, aggregate=aggregate, k=k)


class StackedRegressionLearner(StackedLearner, LearnerRegression):
    """
    Subclass of StackedLearner intended for regression tasks.

    Same as the super class, but has a default
    regression-specific aggregator (`RidgeRegressionLearner`).
    """
    def __init__(self, learners, aggregate=RidgeRegressionLearner(), k=5):
        super().__init__(learners=learners, aggregate=aggregate, k=k)


if __name__ == '__main__':
    import Orange
    iris = Table('iris')
    knn = Orange.modelling.KNNLearner()
    tree = Orange.modelling.TreeLearner()
    lr = Orange.classification.LogisticRegressionLearner()
    sl = StackedClassificationLearner([tree, knn])
    m = sl(iris[::2])
    print(m(iris[1::2], Model.Value))

    housing = Table('housing')
    sl = StackedRegressionLearner([tree, knn])
    m = sl(housing[::2])
    print(list(zip(housing[1:10:2].Y, m(housing[1:10:2], Model.Value))))
