# Path: common.py
# def latent(model, x, y, w):
#     return model.latent(x, y, w)


import numpy as np
from time import time
from pystruct.utils import objective_primal
from pystruct.learners.ssvm import BaseSSVM
from joblib import Parallel, delayed
from common import latent
    def predict_latent(self, X):
        return self.base_ssvm.predict(X)

    def score(self, X, Y):
        """Compute score as 1 - loss over whole data set.

        Returns the average accuracy (in terms of model.loss)
        over X and Y.

        Parameters
        ----------
        X : iterable
            Evaluation data.

        Y : iterable
            True labels.

        Returns
        -------
        score : float
            Average of 1 - loss over training examples.
        """
        losses = [self.model.loss(y, y_pred) / float(np.sum(y.weights))
                  for y, y_pred in zip(Y, self.predict_latent(X))]
        return 1. - np.sum(losses) / float(len(X))

    def staged_latent_objective(self, X, Y):
        for i in xrange(self.iter_done):
            w = self.w_history_[i]
            Y = Parallel(n_jobs=self.n_jobs, verbose=0, max_nbytes=1e8)(

# Path: common.py
# def latent(model, x, y, w):
#     return model.latent(x, y, w)


# Path: latent_structured_svm.py
import numpy as np
from time import time
from pystruct.utils import objective_primal
from pystruct.learners.ssvm import BaseSSVM
from joblib import Parallel, delayed
from common import latent

    def predict_latent(self, X):
        return self.base_ssvm.predict(X)

    def score(self, X, Y):
        """Compute score as 1 - loss over whole data set.

        Returns the average accuracy (in terms of model.loss)
        over X and Y.

        Parameters
        ----------
        X : iterable
            Evaluation data.

        Y : iterable
            True labels.

        Returns
        -------
        score : float
            Average of 1 - loss over training examples.
        """
        losses = [self.model.loss(y, y_pred) / float(np.sum(y.weights))
                  for y, y_pred in zip(Y, self.predict_latent(X))]
        return 1. - np.sum(losses) / float(len(X))

    def staged_latent_objective(self, X, Y):
        for i in xrange(self.iter_done):
            w = self.w_history_[i]
            Y = Parallel(n_jobs=self.n_jobs, verbose=0, max_nbytes=1e8)(

                delayed(latent)(self.model, x, y, w) for x, y in zip(X, Y))