# Path: crowdastro/crowd/util.py
# def balanced_accuracy(y_true, y_pred):
#     """Computes the balanced accuracy of a predictor.
# 
#     y_true: (n_examples,) array of true labels.
#     y_pred: (n_examples,) (masked) array of predicted labels.
#     -> float or None (if the balanced accuracy isn't defined).
#     """
#     if hasattr(y_pred, 'mask') and not isinstance(y_pred.mask, bool):
#         cm = sklearn.metrics.confusion_matrix(
#                 y_true[~y_pred.mask], y_pred[~y_pred.mask]).astype(float)
#     else:
#         cm = sklearn.metrics.confusion_matrix(y_true, y_pred).astype(float)
# 
#     tp = cm[1, 1]
#     n, p = cm.sum(axis=1)
#     tn = cm[0, 0]
#     if not n or not p:
#         return None
# 
#     ba = (tp / p + tn / n) / 2
#     return ba


import numpy
import sklearn.metrics
from crowdastro.crowd.util import balanced_accuracy
        """Adds labels from an oracle.

        indices: Indices of data points to label.
        labels: Labels from the oracle.
        """
        for index, label in zip(indices, labels):
            self.add_label(index, label, retrain=False)
        if retrain:
            self.retrain()

    def train(self):
        """Trains the classifier."""
        self.classifier = self.Classifier(**self.classifier_params)
        self.classifier.fit(self.pool[~self.labels.mask],
                            self.labels[~self.labels.mask])

    def retrain(self):
        """Retrains the classifier."""
        # TODO(MatthewJA): Not sure if we should use warm starts here, so for
        # now I won't.
        self.train()

    def score(self, test_xs, test_ts):
        """Finds cross-entropy error on test data."""
        return sklearn.metrics.log_loss(
            test_ts,
            self.classifier.predict(test_xs))

    def ba(self, test_xs, test_ts):
        """Finds balanced accuracy on test data."""

# Path: crowdastro/crowd/util.py
# def balanced_accuracy(y_true, y_pred):
#     """Computes the balanced accuracy of a predictor.
# 
#     y_true: (n_examples,) array of true labels.
#     y_pred: (n_examples,) (masked) array of predicted labels.
#     -> float or None (if the balanced accuracy isn't defined).
#     """
#     if hasattr(y_pred, 'mask') and not isinstance(y_pred.mask, bool):
#         cm = sklearn.metrics.confusion_matrix(
#                 y_true[~y_pred.mask], y_pred[~y_pred.mask]).astype(float)
#     else:
#         cm = sklearn.metrics.confusion_matrix(y_true, y_pred).astype(float)
# 
#     tp = cm[1, 1]
#     n, p = cm.sum(axis=1)
#     tn = cm[0, 0]
#     if not n or not p:
#         return None
# 
#     ba = (tp / p + tn / n) / 2
#     return ba


# Path: crowdastro/active_learning/sampler.py
import numpy
import sklearn.metrics
from crowdastro.crowd.util import balanced_accuracy

        """Adds labels from an oracle.

        indices: Indices of data points to label.
        labels: Labels from the oracle.
        """
        for index, label in zip(indices, labels):
            self.add_label(index, label, retrain=False)
        if retrain:
            self.retrain()

    def train(self):
        """Trains the classifier."""
        self.classifier = self.Classifier(**self.classifier_params)
        self.classifier.fit(self.pool[~self.labels.mask],
                            self.labels[~self.labels.mask])

    def retrain(self):
        """Retrains the classifier."""
        # TODO(MatthewJA): Not sure if we should use warm starts here, so for
        # now I won't.
        self.train()

    def score(self, test_xs, test_ts):
        """Finds cross-entropy error on test data."""
        return sklearn.metrics.log_loss(
            test_ts,
            self.classifier.predict(test_xs))

    def ba(self, test_xs, test_ts):
        """Finds balanced accuracy on test data."""

        return balanced_accuracy(test_ts, self.classifier.predict(test_xs))