# Path: dsenser/xgboost/xgboostbase.py
# class XGBoostBaseSenser(object):
#     """Base sense classifier using XGBoost.
# 
#     """
# 
#     def __init__(self, a_clf=None, a_grid_search=False):
#         """Class constructor.
# 
#         Args:
#           a_clf (classifier or None):
#             classifier to use or None for default
#           a_grid_search (bool): use grid search for estimating
#             hyper-parameters
# 
#         """
#         classifier = a_clf
#         self._gs = a_grid_search
#         if a_clf is None:
#             classifier = XGBClassifier(max_depth=MAX_DEPTH,
#                                        n_estimators=NTREES,
#                                        learning_rate=ALPHA,
#                                        objective="multi:softprob")
#             self._clf = classifier
#         # latest version of XGBoost cannot deal with non-sparse feature vectors
#         self._model = Pipeline([("vect", DictVectorizer()),
#                                 ("clf", classifier)])
# 
#     def _predict(self, a_feats, a_ret, a_i):
#         """Method for predicting sense of single relation.
# 
#         Args:
#           a_feats (dict):
#             features of the input instance
#           a_ret (np.array):
#             output prediction vector
#           a_i (int):
#             row index in the output vector
# 
#         Returns:
#           void:
# 
#         Note:
#           updates ``a_ret`` in place
# 
#         """
#         ret = self._model.predict_proba(a_feats)[0]
#         if self._clf is None:
#             a_ret[a_i] += ret
#         else:
#             for i, j in enumerate(ret):
#                 a_ret[a_i][self._clf._le.inverse_transform(i)] += j


from dsenser.xgboost.xgboostbase import XGBoostBaseSenser
from mock import patch, MagicMock
from pytest import fixture
from unittest import TestCase
import numpy as np
import xgboost
import sklearn
# Imports
from __future__ import absolute_import, print_function




##################################################################
# Constants
N_Y = 5
EPS = 0.499
TRG_CLS = 1
IV = np.zeros((1, 5)) + EPS / N_Y
IV[0, TRG_CLS] = 1. - EPS


##################################################################
# Test Classes
class TestXGBoostBaseSenser(TestCase):
    @fixture(autouse=True)
    def set_ds(self):
        self.xgb = XGBoostBaseSenser()

    def test_xgb(self):
        x = [{"feat{:d}".format(x_i): 1}
             for x_i in xrange(N_Y)]
        y = [y_i for y_i in reversed(xrange(N_Y))]
        wbench = np.zeros((1, N_Y))
        self.xgb._model.fit(x, y)
        for (x_i, y_i) in zip(x, y):
            self.xgb._predict(x_i, wbench, 0)

# Path: dsenser/xgboost/xgboostbase.py
# class XGBoostBaseSenser(object):
#     """Base sense classifier using XGBoost.
# 
#     """
# 
#     def __init__(self, a_clf=None, a_grid_search=False):
#         """Class constructor.
# 
#         Args:
#           a_clf (classifier or None):
#             classifier to use or None for default
#           a_grid_search (bool): use grid search for estimating
#             hyper-parameters
# 
#         """
#         classifier = a_clf
#         self._gs = a_grid_search
#         if a_clf is None:
#             classifier = XGBClassifier(max_depth=MAX_DEPTH,
#                                        n_estimators=NTREES,
#                                        learning_rate=ALPHA,
#                                        objective="multi:softprob")
#             self._clf = classifier
#         # latest version of XGBoost cannot deal with non-sparse feature vectors
#         self._model = Pipeline([("vect", DictVectorizer()),
#                                 ("clf", classifier)])
# 
#     def _predict(self, a_feats, a_ret, a_i):
#         """Method for predicting sense of single relation.
# 
#         Args:
#           a_feats (dict):
#             features of the input instance
#           a_ret (np.array):
#             output prediction vector
#           a_i (int):
#             row index in the output vector
# 
#         Returns:
#           void:
# 
#         Note:
#           updates ``a_ret`` in place
# 
#         """
#         ret = self._model.predict_proba(a_feats)[0]
#         if self._clf is None:
#             a_ret[a_i] += ret
#         else:
#             for i, j in enumerate(ret):
#                 a_ret[a_i][self._clf._le.inverse_transform(i)] += j


# Path: tests/xgboost/test_xgb_base.py
from dsenser.xgboost.xgboostbase import XGBoostBaseSenser
from mock import patch, MagicMock
from pytest import fixture
from unittest import TestCase
import numpy as np
import xgboost
import sklearn

# Imports
from __future__ import absolute_import, print_function




##################################################################
# Constants
N_Y = 5
EPS = 0.499
TRG_CLS = 1
IV = np.zeros((1, 5)) + EPS / N_Y
IV[0, TRG_CLS] = 1. - EPS


##################################################################
# Test Classes
class TestXGBoostBaseSenser(TestCase):
    @fixture(autouse=True)
    def set_ds(self):
        self.xgb = XGBoostBaseSenser()

    def test_xgb(self):
        x = [{"feat{:d}".format(x_i): 1}
             for x_i in xrange(N_Y)]
        y = [y_i for y_i in reversed(xrange(N_Y))]
        wbench = np.zeros((1, N_Y))
        self.xgb._model.fit(x, y)
        for (x_i, y_i) in zip(x, y):
            self.xgb._predict(x_i, wbench, 0)

            assert wbench[0][y_i] >= np.max(wbench, axis=1)