import math
from collections import OrderedDict

from .. import numpy
from ..util import use_np

from ..base import numeric_types, string_types
from .. import ndarray, npx
from .. import registry

def check_label_shapes(labels, preds, wrap=False, shape=False):
    """Helper function for checking shape of label and prediction

    Parameters
    ----------
    labels : list of `NDArray`
        The labels of the data.

    preds : list of `NDArray`
        Predicted values.

    wrap : boolean
        If True, wrap labels/preds in a list if they are single NDArray

    shape : boolean
        If True, check the shape of labels and preds;
        Otherwise only check their length.
    """
    if not shape:
        label_shape, pred_shape = len(labels), len(preds)
    else:
        label_shape, pred_shape = labels.shape, preds.shape

    if label_shape != pred_shape:
        raise ValueError("Shape of labels {} does not match shape of "
                         "predictions {}".format(label_shape, pred_shape))

    if wrap:
        if isinstance(labels, ndarray.ndarray.NDArray):
            labels = [labels]
        if isinstance(preds, ndarray.ndarray.NDArray):
            preds = [preds]

    return labels, preds

class EvalMetric(object):
    """Base class for all evaluation metrics.

    .. note::

        This is a base class that provides common metric interfaces.
        One should not use this class directly, but instead create new metric
        classes that extend it.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    """
    def __init__(self, name, output_names=None,
                 label_names=None, **kwargs):
        self.name = str(name)
        self.output_names = output_names
        self.label_names = label_names
        self._kwargs = kwargs
        self.reset()

    def __str__(self):
        return "EvalMetric: {}".format(dict(self.get_name_value()))

    def get_config(self):
        """Save configurations of metric. Can be recreated
        from configs with metric.create(``**config``)
        """
        config = self._kwargs.copy()
        config.update({
            'metric': self.__class__.__name__,
            'name': self.name,
            'output_names': self.output_names,
            'label_names': self.label_names})
        return config

    def update_dict(self, label, pred):
        """Update the internal evaluation with named label and pred

        Parameters
        ----------
        labels : OrderedDict of str -> NDArray
            name to array mapping for labels.

        preds : OrderedDict of str -> NDArray
            name to array mapping of predicted outputs.
        """
        if self.output_names is not None:
            pred = [pred[name] for name in self.output_names]
        else:
            pred = list(pred.values())

        if self.label_names is not None:
            label = [label[name] for name in self.label_names]
        else:
            label = list(label.values())

        self.update(label, pred)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        raise NotImplementedError()

    def reset(self):
        """Resets the internal evaluation result to initial state."""
        self.num_inst = 0
        self.sum_metric = 0.0

    def get(self):
        """Gets the current evaluation result.

        Returns
        -------
        names : list of str
           Name of the metrics.
        values : list of float
           Value of the evaluations.
        """
        if self.num_inst == 0:
            return (self.name, float('nan'))
        else:
            res = self.sum_metric / self.num_inst
            if isinstance(res, numpy.ndarray) and len(res.shape) == 0:
                # currently calling ' c = mxnet.numpy.array([1,2,3]).sum() ' would get
                # ' array(6.) ', a ndarray with shape ()
                # In this case, returning a 'float' in .get() is more explicit.
                res = res.item()
            return (self.name, res)

    def get_name_value(self):
        """Returns zipped name and value pairs.

        Returns
        -------
        list of tuples
            A (name, value) tuple list.
        """
        name, value = self.get()
        if not isinstance(name, list):
            name = [name]
        if not isinstance(value, list):
            value = [value]
        return list(zip(name, value))

# pylint: disable=invalid-name
register = registry.get_register_func(EvalMetric, 'metric')
alias = registry.get_alias_func(EvalMetric, 'metric')
_create = registry.get_create_func(EvalMetric, 'metric')
# pylint: enable=invalid-name

def create(metric, *args, **kwargs):
    """Creates evaluation metric from metric names or instances of EvalMetric
    or a custom metric function.

    Parameters
    ----------
    metric : str or callable
        Specifies the metric to create.
        This argument must be one of the below:

        - Name of a metric.
        - An instance of `EvalMetric`.
        - A list, each element of which is a metric or a metric name.
        - An evaluation function that computes custom metric for a given batch of
          labels and predictions.
    *args : list
        Additional arguments to metric constructor.
        Only used when metric is str.
    **kwargs : dict
        Additional arguments to metric constructor.
        Only used when metric is str

    Examples
    --------
    >>> def custom_metric(label, pred):
    ...     return np.mean(np.abs(label - pred))
    ...
    >>> metric1 = mx.gluon.metric.create('acc')
    >>> metric2 = mx.gluon.metric.create(custom_metric)
    >>> metric3 = mx.gluon.metric.create([metric1, metric2, 'rmse'])
    """
    if callable(metric):
        return CustomMetric(metric, *args, **kwargs)
    elif isinstance(metric, list):
        composite_metric = CompositeEvalMetric()
        for child_metric in metric:
            composite_metric.add(create(child_metric, *args, **kwargs))
        return composite_metric

    return _create(metric, *args, **kwargs)

@register
@alias('composite')
class CompositeEvalMetric(EvalMetric):
    """Manages multiple evaluation metrics.

    Parameters
    ----------
    metrics : list of EvalMetric
        List of child metrics.
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0, 1, 1])]
    >>> eval_metrics_1 = mx.gluon.metric.Accuracy()
    >>> eval_metrics_2 = mx.gluon.metric.F1()
    >>> eval_metrics = mx.gluon.metric.CompositeEvalMetric()
    >>> for child_metric in [eval_metrics_1, eval_metrics_2]:
    >>>     eval_metrics.add(child_metric)
    >>> eval_metrics.update(labels = labels, preds = predicts)
    >>> eval_metrics.get()
    (['accuracy', 'f1'], [0.6666666666666666, 0.8])
    """

    def __init__(self, metrics=None, name='composite',
                 output_names=None, label_names=None):
        super(CompositeEvalMetric, self).__init__(
            name, output_names=output_names, label_names=label_names)
        if metrics is None:
            metrics = []
        self.metrics = [create(i) for i in metrics]

    def add(self, metric):
        """Adds a child metric.

        Parameters
        ----------
        metric
            A metric instance.
        """
        self.metrics.append(create(metric))

    def get_metric(self, index):
        """Returns a child metric.

        Parameters
        ----------
        index : int
            Index of child metric in the list of metrics.
        """
        try:
            return self.metrics[index]
        except IndexError:
            return ValueError("Metric index {} is out of range 0 and {}".format(
                index, len(self.metrics)))

    def update_dict(self, labels, preds): # pylint: disable=arguments-differ
        if self.label_names is not None:
            labels = OrderedDict([i for i in labels.items()
                                  if i[0] in self.label_names])
        if self.output_names is not None:
            preds = OrderedDict([i for i in preds.items()
                                 if i[0] in self.output_names])

        for metric in self.metrics:
            metric.update_dict(labels, preds)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        for metric in self.metrics:
            metric.update(labels, preds)

    def reset(self):
        """Resets the internal evaluation result to initial state."""
        try:
            for metric in self.metrics:
                metric.reset()
        except AttributeError:
            pass

    def get(self):
        """Returns the current evaluation result.

        Returns
        -------
        names : list of str
           Name of the metrics.
        values : list of float
           Value of the evaluations.
        """
        names = []
        values = []
        for metric in self.metrics:
            name, value = metric.get()
            if isinstance(name, string_types):
                name = [name]
            if isinstance(value, numeric_types):
                value = [value]
            names.extend(name)
            values.extend(value)
        return (names, values)

    def get_config(self):
        config = super(CompositeEvalMetric, self).get_config()
        config.update({'metrics': [i.get_config() for i in self.metrics]})
        return config

########################
# CLASSIFICATION METRICS
########################

@register
@alias('acc')
@use_np
class Accuracy(EvalMetric):
    """Computes accuracy classification score.

    The accuracy score is defined as

    .. math::

        \\text{accuracy}(y, \\hat{y}) = \\frac{1}{n} \\sum_{i=0}^{n-1}
        \\text{1}(\\hat{y_i} == y_i)

    Parameters
    ----------
    axis : int, default=1
        The axis that represents classes
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0, 1, 1])]
    >>> acc = mx.gluon.metric.Accuracy()
    >>> acc.update(preds = predicts, labels = labels)
    >>> acc.get()
    ('accuracy', 0.6666666666666666)
    """
    def __init__(self, axis=1, name='accuracy',
                 output_names=None, label_names=None):
        super(Accuracy, self).__init__(
            name, axis=axis,
            output_names=output_names, label_names=label_names)
        self.axis = axis

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data with class indices as values, one per sample.

        preds : list of `NDArray`
            Prediction values for samples. Each prediction value can either be the class index,
            or a vector of likelihoods for all classes.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred_label in zip(labels, preds):
            pred_label = pred_label.as_np_ndarray().to_device(label.device)
            label = label.as_np_ndarray()
            if pred_label.shape != label.shape:
                pred_label = pred_label.argmax(axis=self.axis)
            pred_label = pred_label.astype('int32')
            label = label.astype('int32')
            # flatten before checking shapes to avoid shape miss match
            label = label.reshape(-1)
            pred_label = pred_label.reshape(-1)

            check_label_shapes(label, pred_label)

            num_correct = (pred_label == label).sum().astype('float64')
            self.sum_metric += num_correct
            self.num_inst += len(pred_label)

@register
@alias('top_k_accuracy', 'top_k_acc')
@use_np
class TopKAccuracy(EvalMetric):
    """Computes top k predictions accuracy.

    `TopKAccuracy` differs from Accuracy in that it considers the prediction
    to be ``True`` as long as the ground truth label is in the top K
    predicated labels.

    If `top_k` = ``1``, then `TopKAccuracy` is identical to `Accuracy`.

    Parameters
    ----------
    top_k : int
        Whether targets are in top k predictions.
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> np.random.seed(999)
    >>> top_k = 3
    >>> labels = [mx.np.array([2, 6, 9, 2, 3, 4, 7, 8, 9, 6])]
    >>> predicts = [mx.np.array(np.random.rand(10, 10))]
    >>> acc = mx.gluon.metric.TopKAccuracy(top_k=top_k)
    >>> acc.update(labels, predicts)
    >>> acc.get()
    ('top_k_accuracy', 0.3)
    """

    def __init__(self, top_k=1, name='top_k_accuracy',
                 output_names=None, label_names=None):
        super(TopKAccuracy, self).__init__(
            name, top_k=top_k,
            output_names=output_names, label_names=label_names)
        self.top_k = top_k
        assert(self.top_k > 1), 'Please use Accuracy if top_k is no more than 1'
        self.name += f'_{self.top_k}'

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred_label in zip(labels, preds):
            assert(len(pred_label.shape) <= 2), 'Predictions should be no more than 2 dims'
            # Using argpartition here instead of argsort is safe because
            # we do not care about the order of top k elements. It is
            # much faster, which is important since that computation is
            # single-threaded due to Python GIL.
            pred_label = pred_label.as_np_ndarray().to_device(label.device).astype('float32')
            pred_label = numpy.argpartition(pred_label, -self.top_k).to_device(label.device)
            label = label.as_np_ndarray().astype('int32')
            check_label_shapes(label, pred_label)
            num_samples = pred_label.shape[0]
            num_dims = len(pred_label.shape)
            if num_dims == 1:
                num_correct = (pred_label.reshape(-1) == label.reshape(-1)).sum()
                self.sum_metric += num_correct.astype('float64')
            elif num_dims == 2:
                num_classes = pred_label.shape[1]
                top_k = min(num_classes, self.top_k)
                for j in range(top_k):
                    num_correct = (pred_label[:, num_classes - 1 - j].reshape(-1) == label.reshape(-1)).sum()
                    self.sum_metric += num_correct.astype('float64')
            self.num_inst += num_samples

def predict_with_threshold(pred, threshold=0.5):
    """Do thresholding of predictions in binary and multilabel cases.

    Parameters
    ----------
    preds : ndarray
        predictions in shape of (batch_size, ...) or (batch_size, ..., num_categories)

    preds : float or ndarray
        threshold（s) in shape of float or (num_categories)
    """
    if isinstance(threshold, float):
        return pred > threshold
    elif isinstance(threshold, (numpy.ndarray, ndarray.ndarray.NDArray)):
        num_classes = pred.shape[-1]
        assert threshold.shape[-1] == num_classes, \
                f"shape mismatch: {pred.shape[-1]} vs. {threshold.shape[-1]}"
        return pred > threshold
    else:
        raise ValueError("{} is a wrong type for threshold!".format(type(threshold)))

def one_hot(idx, num):
    return (numpy.arange(num).astype(idx) == idx[:, None]).astype('int32')

@use_np
class _ClassificationMetrics(object):
    """Private container class for classification metric statistics.

    True/false positive and true/false negative counts are sufficient statistics for various classification metrics.
    This class provides the machinery to track those statistics across mini-batches of
    (label, prediction) pairs.

    Parameters
    ----------
    class_type : str, default "binary"
        "binary": f1 for binary classification.
        "multiclass": f1 for multiclassification problem.
        "multilabel": f1 for multilabel classification.
    beta : float, default 1
        weight of precision in harmonic mean.
    threshold : float, default 0.5
        threshold for deciding whether the predictions are positive or negative.

    """

    def __init__(self, class_type="binary", threshold=0.5, beta=1):
        self.class_type = class_type
        self.threshold = threshold
        self.beta = beta
        self.reset_stats()

    def _set(self, num, device):
        if self.num_classes is None:
            self.num_classes = num
            self.true_positives = numpy.zeros(num, dtype='float64').to_device(device)
            self.false_negatives = numpy.zeros(num, dtype='float64').to_device(device)
            self.false_positives = numpy.zeros(num, dtype='float64').to_device(device)
            self.true_negatives = numpy.zeros(num, dtype='float64').to_device(device)
        else:
            assert self.num_classes == num, \
                "Input number of classes has changed from {} to {}".format(self.num_classes, num)

    def update_stats(self, label, pred):
        """Update various binary classification counts for a single (label, pred) pair.

        Parameters
        ----------
        label : `NDArray`
            The labels of the data.

        pred : `NDArray`
            Predicted values.
        """
        pred = pred.as_np_ndarray().to_device(label.device)
        label = label.as_np_ndarray().astype('int32')
        if self.class_type == "binary":
            self._set(1, label.device)
            if label.max() > 1:
                raise ValueError("Wrong label for binary classification.")
            if pred.shape == label.shape:
                pass
            elif pred.shape[-1] > 2:
                raise ValueError("The shape of prediction {} is wrong for binary classification.".format(pred.shape))
            elif pred.shape[-1] == 2:
                pred = pred.reshape(-1, 2)[:, 1]
            pred_label = predict_with_threshold(pred, self.threshold).reshape(-1)
            label = label.reshape(-1)

        elif self.class_type == "multiclass":
            num = pred.shape[-1]
            self._set(num, label.device)
            assert label.max() < num, "pred contains fewer classes than label!"
            pred_label = one_hot(pred.argmax(axis=-1).reshape(-1), num)
            label = one_hot(label.reshape(-1), num)

        elif self.class_type == "multilabel":
            num = pred.shape[-1]
            self._set(num, label.device)
            assert pred.shape == label.shape, \
                "The shape of label should be same as that of prediction for multilabel classification."
            pred_label = predict_with_threshold(pred, self.threshold).reshape(-1, num)
            label = label.reshape(-1, num)
        else:
            raise ValueError(
                "Wrong class_type {}! Only supports ['binary', 'multiclass', 'multilabel']".format(self.class_type))

        check_label_shapes(label, pred_label)

        pred_true = (pred_label == 1)
        pred_false = (pred_label == 0)
        label_true = (label == 1)
        label_false = (label == 0)

        true_pos = (pred_true * label_true).sum(0)
        false_pos = (pred_true * label_false).sum(0)
        false_neg = (pred_false * label_true).sum(0)
        true_neg = (pred_false * label_false).sum(0)
        self.true_positives += true_pos
        self.false_positives += false_pos
        self.false_negatives += false_neg
        self.true_negatives += true_neg

    @property
    def precision(self):
        if self.num_classes is not None:
            return self.true_positives / numpy.maximum(self.true_positives + self.false_positives, 1e-12)
        else:
            return 0.

    @property
    def micro_precision(self):
        if self.num_classes is not None:
            return self.true_positives.sum() / \
                numpy.maximum(self.true_positives.sum() + self.false_positives.sum(), 1e-12)
        else:
            return 0.

    @property
    def recall(self):
        if self.num_classes is not None:
            return self.true_positives / numpy.maximum(self.true_positives + self.false_negatives, 1e-12)
        else:
            return 0.

    @property
    def micro_recall(self):
        if self.num_classes is not None:
            return self.true_positives.sum() / \
                numpy.maximum(self.true_positives.sum() + self.false_negatives.sum(), 1e-12)
        else:
            return 0.

    @property
    def fscore(self):
        return (1 + self.beta ** 2) * self.precision * self.recall / \
            numpy.maximum(self.beta ** 2 * self.precision + self.recall, 1e-12)

    @property
    def micro_fscore(self):
        if self.micro_precision + self.micro_recall > 0:
            return (1 + self.beta ** 2) * self.micro_precision * self.micro_recall / \
                (self.beta ** 2 * self.micro_precision + self.micro_recall)
        else:
            return 0.

    def binary_matthewscc(self):
        """Calculate the Matthew's Correlation Coefficent"""
        if not self.total_examples:
            return 0.

        true_pos = float(self.true_positives)
        false_pos = float(self.false_positives)
        false_neg = float(self.false_negatives)
        true_neg = float(self.true_negatives)

        terms = [(true_pos + false_pos),
                 (true_pos + false_neg),
                 (true_neg + false_pos),
                 (true_neg + false_neg)]
        denom = 1.
        for t in filter(lambda t: t != 0., terms):
            denom *= t
        return ((true_pos * true_neg) - (false_pos * false_neg)) / math.sqrt(denom)

    @property
    def total_examples(self):
        if self.num_classes is None:
            return 0
        return int(self.false_negatives[0] + self.false_positives[0] + \
               self.true_negatives[0] + self.true_positives[0])

    def reset_stats(self):
        self.num_classes = None
        self.true_positives = None
        self.false_negatives = None
        self.false_positives = None
        self.true_negatives = None

@register
@use_np
class F1(EvalMetric):
    """Computes the F1 score of a binary classification problem.

    The F1 score is equivalent to harmonic mean of the precision and recall,
    where the best value is 1.0 and the worst value is 0.0. The formula for F1 score is::

        F1 = 2 * (precision * recall) / (precision + recall)

    The formula for precision and recall is::

        precision = true_positives / (true_positives + false_positives)
        recall    = true_positives / (true_positives + false_negatives)

    .. note::

        This F1 score only supports binary classification.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    class_type : str, default "binary"
        "binary": f1 for binary classification.
        "multiclass": f1 for multiclassification problem.
        "multilabel": f1 for multilabel classification.
    threshold : float, default 0.5
        threshold for postive confidence value.
    average : str, default 'micro'
        Strategy to be used for aggregating across mini-batches.
            "macro": Calculate metrics for each label and return unweighted mean of f1.
            "micro": Calculate metrics globally by counting the total TP, FN and FP.
            None: Return f1 scores for each class (numpy.ndarray) .

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0., 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0., 1., 1.])]
    >>> f1 = mx.gluon.metric.F1()
    >>> f1.update(preds = predicts, labels = labels)
    >>> f1.get()
    ('f1', 0.8)
    """

    def __init__(self, name='f1',
                 output_names=None, label_names=None, class_type="binary", threshold=0.5, average="micro"):
        self.average = average
        self.metrics = _ClassificationMetrics(class_type=class_type, threshold=threshold)
        EvalMetric.__init__(self, name=name,
                            output_names=output_names, label_names=label_names)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            self.metrics.update_stats(label, pred)

        if self.average == "micro":
            self.sum_metric = self.metrics.micro_fscore * self.metrics.total_examples
        elif self.average == "macro":
            self.sum_metric = self.metrics.fscore.mean() * self.metrics.total_examples
        else:
            self.sum_metric = self.metrics.fscore * self.metrics.total_examples
        self.num_inst = self.metrics.total_examples

    def reset(self):
        """Resets the internal evaluation result to initial state."""
        self.sum_metric = 0.
        self.num_inst = 0
        self.metrics.reset_stats()

@register
@use_np
class Fbeta(F1):
    """Computes the Fbeta score of a binary classification problem.

    The Fbeta score is equivalent to harmonic mean of the precision and recall,
    where the best value is 1.0 and the worst value is 0.0. The formula for Fbeta score is::

        Fbeta = (1 + beta ** 2) * (precision * recall) / (beta ** 2 * precision + recall)

    The formula for precision and recall is::

        precision = true_positives / (true_positives + false_positives)
        recall    = true_positives / (true_positives + false_negatives)

    .. note::

        This Fbeta score only supports binary classification.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    class_type : str, default "binary"
        "binary": f1 for binary classification.
        "multiclass": f1 for multiclassification problem.
        "multilabel": f1 for multilabel classification.
    beta : float, default 1
        weight of precision in harmonic mean.
    threshold : float, default 0.5
        threshold for postive confidence value.
    average : str, default 'micro'
        Strategy to be used for aggregating across mini-batches.
            "macro": Calculate metrics for each label and return unweighted mean of f1.
            "micro": Calculate metrics globally by counting the total TP, FN and FP.
            None: Return f1 scores for each class.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0., 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0., 1., 1.])]
    >>> fbeta = mx.gluon.metric.Fbeta(beta=2)
    >>> fbeta.update(preds = predicts, labels = labels)
    >>> fbeta.get()
    ('fbeta', 0.9090909090909091)
    """

    def __init__(self, name='fbeta',
                 output_names=None, label_names=None, class_type="binary", beta=1, threshold=0.5, average="micro"):
        super(Fbeta, self).__init__(
            name=name, output_names=output_names, label_names=label_names,
            class_type=class_type, threshold=threshold, average=average)
        self.metrics = _ClassificationMetrics(class_type=class_type, threshold=threshold, beta=beta)

@register
@use_np
class BinaryAccuracy(EvalMetric):
    """Computes the accuracy of a binary or multilabel classification problem.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    threshold : float or ndarray, default 0.5
        threshold for deciding whether the predictions are positive or negative.

    Examples
    --------
    >>> predicts = [mx.np.array([0.7, 1, 0.55])]
    >>> labels   = [mx.np.array([0., 1., 0.])]
    >>> bacc = mx.gluon.metric.BinaryAccuracy(threshold=0.6)
    >>> bacc.update(preds = predicts, labels = labels)
    >>> bacc.get()
    ('binary_accuracy', 0.6666666666666666)
    """

    def __init__(self, name='binary_accuracy',
                 output_names=None, label_names=None, threshold=0.5):
        self.threshold = threshold
        EvalMetric.__init__(self, name=name,
                            output_names=output_names, label_names=label_names)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            Each label denotes positive/negative for each class.

        preds : list of `NDArray`
            Each prediction value is a confidence value of being positive for each class.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred_label in zip(labels, preds):
            pred_label = predict_with_threshold(pred_label, self.threshold)

            pred_label = pred_label.as_np_ndarray().astype('int32').to_device(label.device)
            label = label.as_np_ndarray().astype('int32')
            # flatten before checking shapes to avoid shape miss match
            label = label.reshape(-1)
            pred_label = pred_label.reshape(-1)

            check_label_shapes(label, pred_label)

            num_correct = (pred_label == label).sum().astype('float64')
            self.sum_metric += num_correct
            self.num_inst += len(pred_label)

@register
@use_np
class MCC(EvalMetric):
    """Computes the Matthews Correlation Coefficient of a binary classification problem.

    While slower to compute than F1 the MCC can give insight that F1 or Accuracy cannot.
    For instance, if the network always predicts the same result
    then the MCC will immeadiately show this. The MCC is also symetric with respect
    to positive and negative categorization, however, there needs to be both
    positive and negative examples in the labels or it will always return 0.
    MCC of 0 is uncorrelated, 1 is completely correlated, and -1 is negatively correlated.

    .. math::

        \\text{MCC} = \\frac{ TP \\times TN - FP \\times FN }
        {\\sqrt{ (TP + FP) ( TP + FN ) ( TN + FP ) ( TN + FN ) } }

    where 0 terms in the denominator are replaced by 1.

    .. note::

        This version of MCC only supports binary classification.  See PCC.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> # In this example the network almost always predicts positive
    >>> false_positives = 1000
    >>> false_negatives = 1
    >>> true_positives = 10000
    >>> true_negatives = 1
    >>> predicts = [mx.np.array(
        [[.3, .7]]*false_positives +
        [[.7, .3]]*true_negatives +
        [[.7, .3]]*false_negatives +
        [[.3, .7]]*true_positives
    )]
    >>> labels  = [mx.np.array(
        [0.]*(false_positives + true_negatives) +
        [1.]*(false_negatives + true_positives)
    )]
    >>> f1 = mx.gluon.metric.F1()
    >>> f1.update(preds = predicts, labels = labels)
    >>> mcc = mx.gluon.metric.MCC()
    >>> mcc.update(preds = predicts, labels = labels)
    >>> f1.get()
    ('f1', 0.95233560306652054)
    >>> mcc.get()
    ('mcc', 0.01917751877733392)
    """

    def __init__(self, name='mcc',
                 output_names=None, label_names=None):
        self._metrics = _ClassificationMetrics()
        EvalMetric.__init__(self, name=name,
                            output_names=output_names, label_names=label_names)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            self._metrics.update_stats(label, pred)

        self.sum_metric = self._metrics.binary_matthewscc() * self._metrics.total_examples
        self.num_inst = self._metrics.total_examples

    def reset(self):
        """Resets the internal evaluation result to initial state."""
        self.sum_metric = 0.
        self.num_inst = 0.
        self._metrics.reset_stats()

####################
# REGRESSION METRICS
####################

@register
@use_np
class MAE(EvalMetric):
    """Computes Mean Absolute Error (MAE) loss.

    The mean absolute error is given by

    .. math::

        \\frac{\\sum_i^n |y_i - \\hat{y}_i|}{n}

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([3, -0.5, 2, 7])]
    >>> labels = [mx.np.array([2.5, 0.0, 2, 8])]
    >>> mean_absolute_error = mx.gluon.metric.MAE()
    >>> mean_absolute_error.update(labels = labels, preds = predicts)
    >>> mean_absolute_error.get()
    ('mae', 0.5)
    """

    def __init__(self, name='mae',
                 output_names=None, label_names=None):
        super(MAE, self).__init__(
            name, output_names=output_names, label_names=label_names)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            label = label.as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)

            num_inst = label.shape[0]
            mae = numpy.abs(label - pred).reshape(num_inst, -1).mean(axis=-1).sum()

            self.sum_metric += mae
            self.num_inst += num_inst

@register
@use_np
class MSE(EvalMetric):
    """Computes Mean Squared Error (MSE) loss.

    The mean squared error is given by

    .. math::

        \\frac{\\sum_i^n (y_i - \\hat{y}_i)^2}{n}

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([3, -0.5, 2, 7])]
    >>> labels = [mx.np.array([2.5, 0.0, 2, 8])]
    >>> mean_squared_error = mx.gluon.metric.MSE()
    >>> mean_squared_error.update(labels = labels, preds = predicts)
    >>> mean_squared_error.get()
    ('mse', 0.375)
    """
    def __init__(self, name='mse',
                 output_names=None, label_names=None):
        super(MSE, self).__init__(
            name, output_names=output_names, label_names=label_names)

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            label = label.as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)

            num_inst = label.shape[0]
            mse = ((label - pred)**2.0).reshape(num_inst, -1).mean(axis=-1).sum()

            self.sum_metric += mse
            self.num_inst += num_inst

@register
@use_np
class RMSE(MSE):
    """Computes Root Mean Squred Error (RMSE) loss.

    The root mean squared error is given by

    .. math::

        \\sqrt{\\frac{\\sum_i^n (y_i - \\hat{y}_i)^2}{n}}

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([3, -0.5, 2, 7])]
    >>> labels = [mx.np.array([2.5, 0.0, 2, 8])]
    >>> root_mean_squared_error = mx.gluon.metric.RMSE()
    >>> root_mean_squared_error.update(labels = labels, preds = predicts)
    >>> root_mean_squared_error.get()
    ('rmse', 0.612372457981)
    """
    def __init__(self, name='rmse',
                 output_names=None, label_names=None):
        super(RMSE, self).__init__(
            name, output_names=output_names, label_names=label_names)

    def get(self):
        if self.num_inst == 0:
            return (self.name, float('nan'))
        else:
            return (self.name, math.sqrt(self.sum_metric / self.num_inst))

@register
@use_np
class MeanPairwiseDistance(EvalMetric):
    """Computes Mean Pairwise Distance.

    The mean pairwise distance is given by

    .. math::

        \\sqrt{\\frac{(\\sum_i^n (y_i - \\hat{y}_i)^p)^\\frac{1}{p}}{n}}

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    p : float, default 2
        calculating distance using the p-norm

    Examples
    --------
    >>> predicts = [mx.np.array([[1., 2.], [3., 4.]])]
    >>> labels = [mx.np.array([[1., 0.], [4., 2.]])]
    >>> mpd = mx.gluon.metric.MeanPairwiseDistance()
    >>> mpd.update(labels = labels, preds = predicts)
    >>> mpd.get()
    ('mpd', 2.1180338859558105)
    """
    def __init__(self, name='mpd',
                 output_names=None, label_names=None, p=2):
        super(MeanPairwiseDistance, self).__init__(
            name, output_names=output_names, label_names=label_names)
        self.p = p

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            label = label.as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)

            label = label.reshape(label.shape[0], -1)
            pred = pred.reshape(pred.shape[0], -1)

            dis = (((label - pred) ** self.p).sum(axis=-1)) ** (1./self.p)
            dis = dis.sum()
            num_inst = label.shape[0]

            self.sum_metric += dis
            self.num_inst += num_inst

@register
@use_np
class MeanCosineSimilarity(EvalMetric):
    r"""Computes Mean Cosine Similarity.

    The mean cosine similarity is given by

    .. math::

        cos_sim(label, pred) = \frac{{label}.{pred}}{max(||label||.||pred||, eps)}

    Calculation happens on the last dimension of label and pred.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    eps : float, default 1e-8
        small vale to avoid division by zero.

    Examples
    --------
    >>> predicts = [mx.np.array([[1., 0.], [1., 1.]])]
    >>> labels = [mx.np.array([[3., 4.], [2., 2.]])]
    >>> mcs = mx.gluon.metric.MeanCosineSimilarity()
    >>> mcs.update(labels = labels, preds = predicts)
    >>> mcs.get()
    ('cos_sim', 0.8)
    """
    def __init__(self, name='cos_sim',
                 output_names=None, label_names=None, eps=1e-8):
        super(MeanCosineSimilarity, self).__init__(
            name, output_names=output_names, label_names=label_names)
        self.eps = eps

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        for label, pred in zip(labels, preds):
            label = label.as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)

            if len(label.shape) == 1:
                label = label.reshape(1, label.shape[0])
            if len(pred.shape) == 1:
                pred = pred.reshape(1, pred.shape[0])

            sim = (label * pred).sum(axis=-1)
            n_p = numpy.linalg.norm(pred, axis=-1)
            n_l = numpy.linalg.norm(label, axis=-1)
            sim = sim / numpy.maximum(n_l * n_p, self.eps)
            sim = sim.sum()
            num_inst = len(label.reshape(-1, label.shape[-1])) # numpy.prod(label.shape[:-1]) is not supported
            self.sum_metric += sim
            self.num_inst += num_inst

@register
@alias('ce')
@use_np
class CrossEntropy(EvalMetric):
    """Computes Cross Entropy loss.

    The cross entropy over a batch of sample size :math:`N` is given by

    .. math::

       -\\sum_{n=1}^{N}\\sum_{k=1}^{K}t_{nk}\\log (y_{nk}),

    where :math:`t_{nk}=1` if and only if sample :math:`n` belongs to class :math:`k`.
    :math:`y_{nk}` denotes the probability of sample :math:`n` belonging to
    class :math:`k`.

    Parameters
    ----------
    eps : float, default 1e-12
        Use small constant for the case that predicted value is 0.
    ignore_label : int or None, default None
        Index of invalid label to ignore when
        counting. By default, sets to -1.
        If set to `None`, it will include all entries.
    axis : int, default -1
        The axis from prediction that was used to
        compute softmax. By default use the last axis.
    from_logits : boolean, default False
        Whether `pred` is expected to be a logits tensor.
        By default, we assume that `pred` encodes a probability distribution.
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0, 1, 1])]
    >>> ce = mx.gluon.metric.CrossEntropy()
    >>> ce.update(labels, predicts)
    >>> ce.get()
    ('cross-entropy', 0.57159948348999023)
    """
    def __init__(self, eps=1e-12, ignore_label=None, axis=-1, from_logits=False,
                 name='cross-entropy', output_names=None, label_names=None):
        super(CrossEntropy, self).__init__(
            name, output_names=output_names, label_names=label_names)
        self.ignore_label = ignore_label
        self.axis = axis
        self.from_logits = from_logits
        self.eps = eps

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        loss = 0.
        num = 0
        for label, pred in zip(labels, preds):
            assert label.size == pred.size/pred.shape[-1], \
                f"shape mismatch: {label.shape} vs. {pred.shape}"
            label = label.reshape((label.size,))
            if self.from_logits:
                pred = npx.softmax(pred, axis=self.axis)
            pred = npx.pick(pred.to_device(label.device), label.astype(dtype='int32'), axis=self.axis)
            if self.ignore_label is not None:
                ignore = (label == self.ignore_label).astype(pred.dtype)
                num -= ignore.sum()
                pred = pred * (1 - ignore) + ignore
            loss -= numpy.log(numpy.maximum(self.eps, pred)).sum()
            num += pred.size
        self.sum_metric += loss
        self.num_inst += num

@register
@use_np
class Perplexity(CrossEntropy):
    """Computes perplexity.

    Perplexity is a measurement of how well a probability distribution
    or model predicts a sample. A low perplexity indicates the model
    is good at predicting the sample.

    The perplexity of a model q is defined as

    .. math::

        b^{\\big(-\\frac{1}{N} \\sum_{i=1}^N \\log_b q(x_i) \\big)}
        = \\exp \\big(-\\frac{1}{N} \\sum_{i=1}^N \\log q(x_i)\\big)

    where we let `b = e`.

    :math:`q(x_i)` is the predicted value of its ground truth
    label on sample :math:`x_i`.

    For example, we have three samples :math:`x_1, x_2, x_3` and their labels
    are :math:`[0, 1, 1]`.
    Suppose our model predicts :math:`q(x_1) = p(y_1 = 0 | x_1) = 0.3`
    and :math:`q(x_2) = 1.0`,
    :math:`q(x_3) = 0.6`. The perplexity of model q is
    :math:`exp\\big(-(\\log 0.3 + \\log 1.0 + \\log 0.6) / 3\\big) = 1.77109762852`.

    Parameters
    ----------
    eps : float, default 1e-12
        Use small constant for the case that predicted value is 0.
    ignore_label : int or None, default None
        Index of invalid label to ignore when
        counting. By default, sets to -1.
        If set to `None`, it will include all entries.
    axis : int (default -1)
        The axis from prediction that was used to
        compute softmax. By default use the last axis.
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([0, 1, 1])]
    >>> perp = mx.gluon.metric.Perplexity(ignore_label=None)
    >>> perp.update(labels, predicts)
    >>> perp.get()
    ('Perplexity', 1.7710976285155853)
    """
    def __init__(self, eps=1e-12, ignore_label=None, axis=-1, from_logits=False,
                 name='perplexity', output_names=None, label_names=None):
        super(Perplexity, self).__init__(
            eps=eps, ignore_label=ignore_label, axis=axis, from_logits=from_logits,
            name=name, output_names=output_names, label_names=label_names)

    def get(self):
        if self.num_inst == 0:
            return (self.name, float('nan'))
        else:
            return (self.name, math.exp(self.sum_metric/self.num_inst))

@register
@alias('pearsonr')
@use_np
class PearsonCorrelation(EvalMetric):
    """Computes Pearson correlation.

    The pearson correlation is given by

    .. math::

        \\frac{cov(y, \\hat{y})}{\\sigma{y}\\sigma{\\hat{y}}}

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
    >>> labels   = [mx.np.array([[1, 0], [0, 1], [0, 1]])]
    >>> pr = mx.gluon.metric.PearsonCorrelation()
    >>> pr.update(labels, predicts)
    >>> pr.get()
    ('pearsonr', 0.42163704544016178)
    """
    def __init__(self, name='pearsonr',
                 output_names=None, label_names=None):
        super(PearsonCorrelation, self).__init__(
            name, output_names=output_names, label_names=label_names)
        self.reset()

    def reset(self):
        self._sse_p = 0
        self._mean_p = 0
        self._sse_l = 0
        self._mean_l = 0
        self._pred_nums = 0
        self._label_nums = 0
        self._conv = 0

        self.num_inst = 0
        self.sum_metric = 0.0

    def update_variance(self, new_values, *aggregate):
        #Welford's online algorithm for variance update
        count, mean, m_2 = aggregate
        count += len(new_values)
        delta = new_values - mean
        mean += numpy.sum(delta / count)
        delta_2 = new_values - mean
        m_2 += numpy.sum(delta * delta_2)
        return count, mean, m_2

    def update_cov(self, label, pred):
        self._conv = self._conv + numpy.sum((label - self._mean_l) * (pred - self._mean_p))

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.
        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)
        for label, pred in zip(labels, preds):
            check_label_shapes(label, pred, False, True)
            label = label.as_np_ndarray().reshape(-1).astype(numpy.float64)
            pred = pred.as_np_ndarray().to_device(label.device).reshape(-1).astype(numpy.float64)

            self.num_inst += 1
            self._label_nums, self._mean_l, self._sse_l = \
                self.update_variance(label, self._label_nums, self._mean_l, self._sse_l)
            self.update_cov(label, pred)
            self._pred_nums, self._mean_p, self._sse_p = \
                self.update_variance(pred, self._pred_nums, self._mean_p, self._sse_p)

    def get(self):
        if self.num_inst == 0:
            return (self.name, float('nan'))

        n = self._label_nums
        pearsonr = self._conv / ((n-1) * numpy.sqrt(self._sse_p / (n - 1)) * numpy.sqrt(self._sse_l / (n - 1)))
        return (self.name, float(pearsonr))

@register
@use_np
class PCC(EvalMetric):
    """PCC is a multiclass equivalent for the Matthews correlation coefficient derived
    from a discrete solution to the Pearson correlation coefficient.

    .. math::

        \\text{PCC} = \\frac {\\sum _{k}\\sum _{l}\\sum _{m}C_{kk}C_{lm}-C_{kl}C_{mk}}
        {{\\sqrt {\\sum _{k}(\\sum _{l}C_{kl})(\\sum _{k'|k'\\neq k}\\sum _{l'}C_{k'l'})}}
         {\\sqrt {\\sum _{k}(\\sum _{l}C_{lk})(\\sum _{k'|k'\\neq k}\\sum _{l'}C_{l'k'})}}}

    defined in terms of a K x K confusion matrix C.

    When there are more than two labels the PCC will no longer range between -1 and +1.
    Instead the minimum value will be between -1 and 0 depending on the true distribution.
    The maximum value is always +1.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> # In this example the network almost always predicts positive
    >>> false_positives = 1000
    >>> false_negatives = 1
    >>> true_positives = 10000
    >>> true_negatives = 1
    >>> predicts = [mx.np.array(
        [[.3, .7]]*false_positives +
        [[.7, .3]]*true_negatives +
        [[.7, .3]]*false_negatives +
        [[.3, .7]]*true_positives
    )]
    >>> labels  = [mx.np.array(
        [0]*(false_positives + true_negatives) +
        [1]*(false_negatives + true_positives)
    )]
    >>> f1 = mx.gluon.metric.F1()
    >>> f1.update(preds = predicts, labels = labels)
    >>> pcc = mx.gluon.metric.PCC()
    >>> pcc.update(preds = predicts, labels = labels)
    >>> f1.get()
    ('f1', 0.95233560306652054)
    >>> pcc.get()
    ('pcc', 0.01917751877733392)
    """
    def __init__(self, name='pcc',
                 output_names=None, label_names=None):
        self.k = 2
        super(PCC, self).__init__(
            name=name, output_names=output_names, label_names=label_names)

    def _grow(self, inc):
        self.lcm = numpy.pad(
            self.lcm, ((0, inc), (0, inc)), 'constant', constant_values=(0))
        self.k += inc

    def _calc_mcc(self, cmat):
        n = cmat.sum()
        x = cmat.sum(axis=1)
        y = cmat.sum(axis=0)
        cov_xx = numpy.sum(x * (n - x))
        cov_yy = numpy.sum(y * (n - y))
        if cov_xx == 0 or cov_yy == 0:
            return float('nan')
        # i = cmat.diagonal() # mxnet.numpy.ndarray.diagonal() is currently not available.
        i = cmat[numpy.arange(self.k), numpy.arange(self.k)]
        cov_xy = numpy.sum(i * n - x * y)
        return cov_xy / (cov_xx * cov_yy) ** 0.5

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        labels, preds = check_label_shapes(labels, preds, True)

        # update the confusion matrix
        for label, pred in zip(labels, preds):
            label = label.astype('int32', copy=False).as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)
            if pred.shape != label.shape:
                pred = pred.argmax(axis=1).astype(label, copy=False)
            else:
                pred = pred.astype('int32', copy=False)
            n = int(max(pred.max(), label.max()))
            if n >= self.k:
                self._grow(n + 1 - self.k)
            bcm = numpy.zeros((self.k, self.k), dtype='float64')
            for i, j in zip(pred, label):
                bcm[i, j] += 1
            self.lcm += bcm
        self.num_inst += 1

    @property
    def sum_metric(self):
        return self._calc_mcc(self.lcm) * self.num_inst

    def reset(self):
        """Resets the internal evaluation result to initial state."""
        self.num_inst = 0.
        self.lcm = numpy.zeros((self.k, self.k), dtype='float64')

@register
@use_np
class Loss(EvalMetric):
    """Dummy metric for directly printing loss.

    Parameters
    ----------
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.
    """
    def __init__(self, name='loss',
                 output_names=None, label_names=None):
        super(Loss, self).__init__(
            name, output_names=output_names, label_names=label_names)

    def update(self, _, preds):

        if isinstance(preds, ndarray.ndarray.NDArray):
            preds = [preds]

        for pred in preds:
            loss = pred.sum().item()
            self.sum_metric += loss
            self.num_inst += pred.size

@register
class Torch(Loss):
    """Dummy metric for torch criterions."""
    def __init__(self, name='torch',
                 output_names=None, label_names=None):
        super(Torch, self).__init__(
            name, output_names=output_names, label_names=label_names)

@register
@use_np
class CustomMetric(EvalMetric):
    """Computes a customized evaluation metric.

    The `feval` function can return a `tuple` of (sum_metric, num_inst) or return
    an `int` sum_metric.

    Parameters
    ----------
    feval : callable(label, pred)
        Customized evaluation function.
    name : str, optional
        The name of the metric. (the default is None).
    allow_extra_outputs : bool, optional
        If true, the prediction outputs can have extra outputs.
        This is useful in RNN, where the states are also produced
        in outputs for forwarding. (the default is False).
    name : str
        Name of this metric instance for display.
    output_names : list of str, or None
        Name of predictions that should be used when updating with update_dict.
        By default include all predictions.
    label_names : list of str, or None
        Name of labels that should be used when updating with update_dict.
        By default include all labels.

    Examples
    --------
    >>> predicts = [mx.np.array(np.array([3, -0.5, 2, 7]).reshape(4,1))]
    >>> labels = [mx.np.array(np.array([2.5, 0.0, 2, 8]).reshape(4,1))]
    >>> feval = lambda x, y : (x + y).mean()
    >>> eval_metrics = mx.gluon.metric.CustomMetric(feval=feval)
    >>> eval_metrics.update(labels, predicts)
    >>> eval_metrics.get()
    ('custom(<lambda>)', 6.0)
    """
    def __init__(self, feval, name=None, allow_extra_outputs=False,
                 output_names=None, label_names=None):
        if name is None:
            name = feval.__name__
            if name.find('<') != -1:
                name = f'custom({name})'
        super(CustomMetric, self).__init__(
            name, feval=feval,
            allow_extra_outputs=allow_extra_outputs,
            output_names=output_names, label_names=label_names)
        self._feval = feval
        self._allow_extra_outputs = allow_extra_outputs

    def update(self, labels, preds):
        """Updates the internal evaluation result.

        Parameters
        ----------
        labels : list of `NDArray`
            The labels of the data.

        preds : list of `NDArray`
            Predicted values.
        """
        if not self._allow_extra_outputs:
            labels, preds = check_label_shapes(labels, preds, True)

        for pred, label in zip(preds, labels):
            label = label.as_np_ndarray()
            pred = pred.as_np_ndarray().to_device(label.device)

            reval = self._feval(label, pred)
            if isinstance(reval, tuple):
                (sum_metric, num_inst) = reval
                self.sum_metric += sum_metric
                self.num_inst += num_inst
            else:
                self.sum_metric += reval
                self.num_inst += 1

    def get_config(self):
        raise NotImplementedError("CustomMetric cannot be serialized")

# pylint: disable=invalid-name
def np(numpy_feval, name=None, allow_extra_outputs=False):
    """Creates a custom evaluation metric that receives its inputs as numpy arrays.

    Parameters
    ----------
    numpy_feval : callable(label, pred)
        Custom evaluation function that receives labels and predictions for a minibatch
        as numpy arrays and returns the corresponding custom metric as a floating point number.
    name : str, optional
        Name of the custom metric.
    allow_extra_outputs : bool, optional
        Whether prediction output is allowed to have extra outputs. This is useful in cases
        like RNN where states are also part of output which can then be fed back to the RNN
        in the next step. By default, extra outputs are not allowed.

    Returns
    -------
    float
        Custom metric corresponding to the provided labels and predictions.

    Example
    -------
    >>> def custom_metric(label, pred):
    ...     return np.mean(np.abs(label-pred))
    ...
    >>> metric = mx.gluon.metric.np(custom_metric)
    """
    def feval(label, pred):
        """Internal eval function."""
        return numpy_feval(label, pred)
    feval.__name__ = numpy_feval.__name__
    return CustomMetric(feval, name, allow_extra_outputs)
# pylint: enable=invalid-name


import logging
import math
import numpy as np
import portfolio as ptf
import w

def dF_dW_numeric(portfolio, F__, w__, dw, wParams):
    dF_dW_ = np.empty([portfolio.iMax, portfolio.jLen])
    F_base = w.eval_w(F__[0], portfolio.x___[1], w__, wParams)
    J = 0
    for i in range(portfolio.iMax):
        for j in range(portfolio.jLen_[i]):
            w__[i][j] += dw
            F_ = w.eval_w(F__[0], portfolio.x___[1], w__, wParams)
            w__[i][j] -= dw
            dF_dW_[:, J] = (F_ - F_base) / dw
            J += 1
    return dF_dW_

def dF_dW_init_ES(portfolio, F__, w__, dw):
    dF_dW_ = np.empty([portfolio.iMax, portfolio.jLen])
    for n in range(portfolio.iMax):
        J = 0
        for i in range(portfolio.iMax):
            for j in range(portfolio.jLen_[i]):
                m = 2.0 / 9 if (n == i) else -1.0 / 9
#                dF_dW_[n, J] = m * portfolio.x___[1, i, j]
                dF_dW_[n, J] = m * portfolio.x___[1][i, j]
                J += 1
    return dF_dW_

def run_epoch(portfolio, W_, alpha, wParams):
    w__ = portfolio.split(W_)
    F__ = np.empty([portfolio.tMax, portfolio.iMax])
    F__[0] = w.init_f(portfolio.iMax)
    dFdWMethod = wParams['dFdW']
    if (dFdWMethod == 'numeric'):
        dF_dW_ = dF_dW_numeric(portfolio, F__, w__, 0.01, wParams)
    elif (dFdWMethod == 'es'):
        dF_dW_ = dF_dW_init_ES(portfolio, F__, w__, 0.01)
    else:
        raise InputError("dFdWMethod %s unknown" % dFdWMethod)

    #logging.debug("dFdW %s" % dF_dW_)

    #logging.debug("W_ %s" % W_)
    sumR = sumR2 = 0
    for t in range(1, portfolio.tMax):
        x__ = portfolio.x___[t] # (asset x feature)
        r_ = portfolio.r__[t] # (asset)
        F__[t] = w.eval_w(F__[t - 1], x__, w__, wParams)
        dF_ = F__[t] - F__[t - 1]
        R = np.dot(F__[t - 1], r_) - np.dot(portfolio.c_, np.absolute(dF_))
        sumR += R
        sumR2 += R * R
        A = sumR / t
        B = sumR2 / t
        BmA2 = B - A * A

        dRdF_ = -portfolio.c_ * np.sign(dF_)
        dRdF_1 = r_ + portfolio.c_ * np.sign(dF_)
        dSdW_a = np.dot(dRdF_1, dF_dW_)

        for n in range(portfolio.iMax):
            J = 0
            for i in range(portfolio.iMax):
                for j in range(portfolio.jLen_[i]):
                    x = x__[n][j] if (n == i) else 0
                    dF_dW_[n, J] = F__[t, n] * (1 - F__[t, n]) * (x + w__[n][-1] * dF_dW_[n, J])
                    J += 1

        #logging.debug("x__ %s" % x__)
        #logging.debug("dFdW %s" % dF_dW_)
        #logging.debug("dRdF %s" % dRdF_)
        #logging.debug("dRdF_1 %s" % dRdF_1)
        dSdW_b = np.dot(dRdF_, dF_dW_)
        c = 0 if (BmA2 == 0) else (B - A * R) / math.pow(BmA2, 1.5)
        dSdW_ = c * (dSdW_a + dSdW_b)
        #logging.debug("dSdW_ %s" % dSdW_)
        W_ += alpha * c * (dSdW_a + dSdW_b)
        #logging.debug("W_ %s" % W_)
    return W_


from MVA import *
from report_generator import *
from math import sqrt, ceil, floor

def split_edges_k(m_parameters, upper_bound, rand, k=1):
    """
        INCR Algorithm. Split clique tree edges
    """
    dis_set = UnionFind()
    [dis_set[i] for i in range(m_parameters.num_maximal_cliques)]
    loops = 0
    while m_parameters.edges_list and m_parameters.num_edges < upper_bound:
        loops += 1
        (x, y, sep, omega), index = rand.next_element(m_parameters.edges_list)
        i = dis_set[x]
        j = dis_set[y]

        x_sep, y_sep = m_parameters.cliques[i] - set(sep), m_parameters.cliques[j] - set(sep)

        if len(x_sep) == 0 or len(y_sep) == 0:
            raise Exception("Not valid clique tree")
        elif len(x_sep) <= k or len(y_sep) <= k:
            # merge {x,y}
            edges_added = len(x_sep) * len(y_sep)
            dis_set.union(x, y)
            m_parameters.cliques[i].update(m_parameters.cliques[j])
            m_parameters.cardinality_array[i] += len(y_sep)
            m_parameters.cliques[j] = set()
            m_parameters.cardinality_array[j] = 0
            m_parameters.num_edges += edges_added
            m_parameters.num_maximal_cliques -= 1
            # delete old edge
            del m_parameters.edges_list[index]
        elif len(x_sep) <= k:
            # merge {x,z}
            ylen = rand.next_random(1, len(y_sep))
            y_random = list(y_sep)[0:ylen]

            m_parameters.cliques[i].update(y_random)
            m_parameters.cardinality_array[i] += ylen
            # update the edge min-seperator
            m_parameters.edges_list[index] = (x, y, y_random + sep, omega + ylen)

            # update num of edges
            m_parameters.num_edges += ylen * len(x_sep)
        elif len(y_sep) <= k:
            # merge {y,z}
            xlen = rand.next_random(1, len(x_sep))
            x_random = list(x_sep)[0:xlen]

            m_parameters.cliques[j].update(x_random)
            m_parameters.cardinality_array[j] += xlen
            # update the edge min-seperator
            m_parameters.edges_list[index] = (x, y, x_random + sep, omega + xlen)

            # update num of edges
            m_parameters.num_edges += xlen * len(y_sep)
        else:
            # make new z node
            xlen = rand.next_random(1, len(x_sep))
            ylen = rand.next_random(1, len(y_sep))

            x_random = list(x_sep)[0:xlen]
            y_random = list(y_sep)[0:ylen]

            z = set(x_random + y_random + sep)
            edges_added = xlen * ylen
            # print(str(edges_added) + " / " + str(len(x_sep) * len(y_sep)))

            # add node to list
            m_parameters.cliques.append(z)
            m_parameters.cardinality_array.append(len(z))

            # add x-z edge
            m_parameters.edges_list.append((x, len(m_parameters.cliques) - 1, x_random + sep, omega + edges_added))
            # add y-z edge
            m_parameters.edges_list.append((y, len(m_parameters.cliques) - 1, y_random + sep, omega + edges_added))

            m_parameters.num_maximal_cliques += 1
            # update num of edges
            m_parameters.num_edges += edges_added

            # delete old edge
            del m_parameters.edges_list[index]

    return loops

def init_k_tree_incr(num_vertices, k, rand):
    ct_tree = MVAParameters(0, 0, [], [], [])
    ct_tree.cliques.append(set([i for i in range(k + 1)]))
    ct_tree.cardinality_array.append(k + 1)
    ct_tree.num_maximal_cliques = 1
    ct_tree.num_edges = int(k * (k + 1) / 2)

    root = ct_tree.cliques[0]

    for u in range(1, num_vertices - k):
        i = rand.next_random(0, len(ct_tree.cliques))
        y = rand.next_random(0, len(ct_tree.cliques[i]))

        sep = [x for ii, x in enumerate(ct_tree.cliques[i]) if ii != y]

        ct_tree.cliques.append(set(sep + [u + k]))
        ct_tree.cardinality_array.append(k + 1)
        ct_tree.num_maximal_cliques += 1
        ct_tree.num_edges += k
        new_node = ct_tree.cliques[-1]
        ct_tree.edges_list.append((i, len(ct_tree.cliques) - 1, sep, k))

    return ct_tree

def Run_INCR(num_vertices, edge_density, algorithm_name, k, init_tree=None):
    """
        Initialize and run the MVA algorithm
    """

    edges_bound = int(edge_density * (num_vertices * (num_vertices - 1) / 2))
    k = max(1, k * edges_bound)
    runner = runner_factory(num_vertices, algorithm_name, None, edges_bound=edges_bound, edge_density=edge_density, k=k)

    randomizer = Randomizer(2 * num_vertices, runner["Parameters"]["seed"])
    with Timer("t_expand_cliques", runner["Times"]):
        if init_tree == "ktree":
            ktree_k = 1 / 2 * (2 * num_vertices - 1 - sqrt(((2 * num_vertices - 1) * (2 * num_vertices - 1)) - (8 * edges_bound)))
            ktree_k = int(floor(ktree_k))
            k_edges = (num_vertices - ktree_k - 1) * ktree_k + (ktree_k * (ktree_k + 1) / 2)
            p_mva = init_k_tree_incr(runner["Parameters"]["n"], ktree_k,  randomizer)
            print("- Init with " + str(ktree_k) + "-tree:")
        elif init_tree == "tree":
            p_mva = expand_tree(runner["Parameters"]["n"], randomizer)
            print("- Expand tree:")
        else:
            p_mva = expand_cliques(runner["Parameters"]["n"], randomizer)
            print("- Expand cliques:")

    print(p_mva)

    with Timer("t_split_edges", runner["Times"]):
        loops = split_edges_k(p_mva, runner["Parameters"]["edges_bound"], randomizer, k)
        print("- Split edges:")
    runner["Stats"]["total"] = runner["Times"]["t_split_edges"] + runner["Times"]["t_expand_cliques"]
    runner["Stats"]["loops%"] = loops / edges_bound
    print("    loops:", runner["Stats"]["loops%"])
    print(p_mva)

    return calculate_mva_statistics(p_mva, runner, randomizer, num_vertices)

NUM_VERTICES = [
    50,
    100,
    500,
    1000,
    2500,
    5000,
    10000,  # 50000, 100000, 500000, 1000000
]
EDGES_DENSITY = [0.1, 0.33, 0.5, 0.75, 0.99]

NAME = "INCR_KTREE_k_1"
if __name__ == '__main__':
    mva_data = []
    for num in NUM_VERTICES:
        for edge_density in EDGES_DENSITY:
            Runners = []
            for _ in range(10):
                Runners.append(Run_INCR(num, edge_density, NAME, 0, "ktree"))

            # filename = "Results/" + NAME + "/Run_{}_{}_{}.yml".format(num, edge_density, datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))

            # if not os.path.isdir(os.path.dirname(filename)):
            #     os.makedirs(os.path.dirname(filename))

            # with io.open(filename, 'w') as file:
            #     print_statistics(Runners, file)

            print("Done")
            mva_data.append(merge_runners(Runners))

    run_reports_data(NAME, mva_data)


import pandas as pd
from datetime import timedelta

""" Reading the data """

df = pd.read_excel("Assignment_Timecard.xlsx")

""" Preprocessing """

# Extracting day from datetime
df["Day"] = df["Time"].map(lambda time: time.day)

# Time difference between start of current shift and end of last shift
df["Shift Difference"] = df["Time"] - df["Time Out"].shift(1)

# Recalculating shift time as Timecard Hours aren't accurate enough
df["Shift Time"] = df["Time Out"] - df["Time"]

""" Task 1 """

df1 = df

# Removing consecutive same days (only considering one shift per day)
df1 = df1[df1["Day"] != df1["Day"].shift(-1)]

# If the difference between current day and the day exactly 7 rows above is 7, then the employee must've worked for 7 consecutive days
df1 = df1[df1["Day"] - df1["Day"].shift(7) == 7]

# Extracting only the name and the position
df1 = df1.loc[:, ["Employee Name", "Position ID"]].drop_duplicates()

print("\n\nEmployees who have worked for 7 consecutive days:\n")
print(df1)

""" Task 2 """

df2 = df

# Considering only the shifts done on the same day as valid (removing false positives)
df2 = df2[df2["Day"] == df2["Day"].shift(1)]

# Only considering and rows with shift difference more than 1 hour
df2 = df2[df2["Shift Difference"] > timedelta(hours=1)]

# Only considering and rows with shift difference less than 10 hours
df2 = df2[df2["Shift Difference"] < timedelta(hours=10)]

# Extracting only the name and the position
df2 = df2.loc[:, ["Employee Name", "Position ID"]].drop_duplicates()

print(
    "\n\nEmployees who have less than 10 hours of time between shifts but greater than 1 hour:\n"
)
print(df2)

""" Task 3 """

df3 = df

# Keeping only the rows with shift time greater than 14 hours
df3 = df3[df3["Shift Time"] > timedelta(hours=14)]

# Extracting only the name and the position
df3 = df3.loc[:, ["Employee Name", "Position ID"]].drop_duplicates()

print("\n\nEmployees who have worked for more than 14 hours in a single shift:\n")
print(df3)
print("\n\n")

# Output stored in output.txt using the command "python script.py > output.txt"


from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="https://www.google.com/policies/terms/",
      contact=openapi.Contact(email="contact@snippets.local"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

urlpatterns = [

    url(r'^backend/swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    url(r'^backend/swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),

    path("backend/admin/", admin.site.urls),

    path('backend/api/auth/', include('djoser.urls')),
    path('backend/api/auth/', include('djoser.urls.jwt')),

]
urlpatterns += [url(r'^silk/', include('silk.urls', namespace='silk'))]

if bool(settings.DEBUG):
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import streamlit as st
import pickle
import pandas as pd

def recommend(movie):
    index = movies[movies['title'] == movie].index[0]
    distances = sorted(list(enumerate(similarity[index])),reverse=True,key = lambda x: x[1])
    recommended_movies=[]
    for i in distances[1:6]:
         recommended_movies.append(movies.iloc[i[0]].title)
    return recommended_movies

similarity=pickle.load(open('similarity.pkl','rb'))
movie_dict=pickle.load(open('movie_dict.pkl','rb'))
movies=pd.DataFrame(movie_dict)
st.title("Movie Recommender")

option = st.selectbox(
    'Enter the movie on which you want recommendations',
    movies['title'].values)

if st.button('Recommend'):
    recommendation=recommend(option)
    for i in recommendation:
        st.write(i)


import glob
import json
import os
from copy import deepcopy

import cv2
import numpy as np
import reconstruction
import torch
from constants import AUGMENTED_VERTICES_NAMES, COCO_VERTICES_NAME, MODEL_FOLDER
from PIL import Image
from tqdm import tqdm
from video_scene import VideoScene

class DatasetGenerator:
    def __init__(
        self,
        data_folder: str,
        samples_per_video: int = 8,
        # sample_rate: int = 0.3,
        method="align_3d",
        output_path="infinity_dataset_combined",
        shift=0,
        split=0.8,
        infinity_version="v0.1.0",
    ):
        self.data_folder = data_folder
        self.samples_per_video = samples_per_video
        self.method = method
        self.output_path_train = os.path.join(output_path, "train")
        self.output_path_test = os.path.join(output_path, "test")
        self.shift = shift
        self.split = split
        if infinity_version == "v0.1.0":
            self.video_name = "video"
            self.labels_name = "labels"
            self.segmentation_name = "segmentation"
        else:
            self.video_name = "video.rgb"
            self.labels_name = "video.rgb"
            self.segmentation_name = "video.rgb"
        self.video_paths = sorted(
            glob.glob(os.path.join(data_folder, f"*/{self.video_name}.mp4"))
        )
        self.data_dict_train = {
            "infos": {},
            "images": [],
            "annotations": [],
            "categories": [],
        }

        self.data_dict_train["categories"] = [
            {
                "id": 0,
                "augmented_keypoints": AUGMENTED_VERTICES_NAMES,
                "coco_keypoints": COCO_VERTICES_NAME,
            }
        ]
        self.data_dict_test = deepcopy(self.data_dict_train)
        self.total_source_images = 0
        self.total_missing_bboxes = 0
        self.total_error_reconstruction = 0

        if not os.path.exists(os.path.join(self.output_path_train, "images")):
            os.makedirs(os.path.join(self.output_path_train, "images"))
        if not os.path.exists(os.path.join(self.output_path_test, "images")):
            os.makedirs(os.path.join(self.output_path_test, "images"))

    def save_image_annotation(
        self, annotation_dict, img, video_scene, index_frame, mode: str = "train"
    ):
        if mode == "train":
            self.data_dict_train["annotations"].append(annotation_dict)
            img_name = f"{len(self.data_dict_train['images'])}.png"
            img_path = os.path.join(self.output_path_train, "images", img_name)
            Image.fromarray(cv2.cvtColor(np.uint8(img), cv2.COLOR_BGR2RGB)).save(
                img_path
            )
            image_dict = {
                "id": len(self.data_dict_train["images"]),
                "width": video_scene.dims[0],
                "height": video_scene.dims[1],
                "frame_number": index_frame,
                "img_path": img_path,
            }
            self.data_dict_train["images"].append(image_dict)
        elif mode == "test":
            self.data_dict_test["annotations"].append(annotation_dict)
            img_name = f"{len(self.data_dict_test['images'])}.png"
            img_path = os.path.join(self.output_path_test, "images", img_name)
            Image.fromarray(cv2.cvtColor(np.uint8(img), cv2.COLOR_BGR2RGB)).save(
                img_path
            )
            image_dict = {
                "id": len(self.data_dict_test["images"]),
                "width": video_scene.dims[0],
                "height": video_scene.dims[1],
                "frame_number": index_frame,
                "img_path": img_path,
            }
            self.data_dict_test["images"].append(image_dict)

    def generate_dataset(self):
        train_size = int(len(self.video_paths) * self.split)
        mode = "train"
        for index_video, video_path in enumerate(tqdm(self.video_paths)):
            if index_video == train_size:
                mode = "test"
            path = os.path.dirname(video_path)
            video_scene = VideoScene(
                path_to_example=path,
                video_name=self.video_name,
                labels_name=self.labels_name,
                segmentation_name=self.segmentation_name,
            )
            indices_to_sample = list(
                set(
                    np.linspace(
                        0,
                        video_scene.nb_frames - 1,
                        self.samples_per_video,
                        dtype=np.int32,
                    )
                )
            )
            indices_to_sample = [index.item() for index in indices_to_sample]
            for index_frame in indices_to_sample:
                (
                    img,
                    ann,
                    groundtruth_landmarks,
                    coco_landmarks,
                    success,
                ) = self.get_grountruth_landmarks(video_scene, index_frame)

                self.total_source_images += 1
                if not success:
                    self.total_error_reconstruction += 1
                    continue
                if "bbox" not in ann:
                    self.total_missing_bboxes
                    continue

                annotation_dict = self.generate_annotation_dict(ann, mode=mode)
                annotation_dict["keypoints"] = groundtruth_landmarks
                annotation_dict["coco_keypoints"] = coco_landmarks
                self.save_image_annotation(
                    annotation_dict, img, video_scene, index_frame, mode=mode
                )

        with open(
            os.path.join(self.output_path_train, "annotations.json"),
            "w",
            encoding="utf-8",
        ) as f:
            json.dump(self.data_dict_train, f, ensure_ascii=False, indent=4)
        with open(
            os.path.join(self.output_path_test, "annotations.json"),
            "w",
            encoding="utf-8",
        ) as f:
            json.dump(self.data_dict_test, f, ensure_ascii=False, indent=4)

        print("total source images: ", self.total_source_images)
        print("total missing bboxes: ", self.total_missing_bboxes)
        print("total error reconstruction: ", self.total_error_reconstruction)

    def generate_annotation_dict(self, ann: dict, mode: str = "train"):
        annotation_dict = {}
        if mode == "train":
            annotation_dict["image_id"] = len(self.data_dict_train["images"])
        else:
            annotation_dict["image_id"] = len(self.data_dict_test["images"])
        annotation_dict["id"] = annotation_dict["image_id"]
        annotation_dict["category_id"] = 0
        annotation_dict["bbox"] = ann["bbox"]
        annotation_dict["percent_in_fov"] = ann["percent_in_fov"]
        annotation_dict["percent_occlusion"] = ann["percent_occlusion"]
        annotation_dict["iscrowd"] = 0

        return annotation_dict

    def get_grountruth_landmarks(self, video_scene: VideoScene, index_frame):
        img, ann, infos = video_scene.load_frame(index_frame)
        coco_landmarks = ann["keypoints"]
        gender = infos["avatar_presenting_gender"]
        betas = torch.tensor(infos["avatar_betas"], dtype=torch.float32).unsqueeze(0)
        poses = reconstruction.get_poses(ann)
        smplx_model = reconstruction.get_smplx_model(MODEL_FOLDER, gender, betas, poses)
        vertices, joints = reconstruction.get_vertices_and_joints(smplx_model, betas)
        augmented_vertices = reconstruction.get_augmented_vertices(vertices)
        projected_vertices = video_scene.compute_2d_projection(
            joints, augmented_vertices, method=self.method
        )
        if np.isnan(projected_vertices).any():
            return img, ann, {}, {}, False
        groundtruth_landmarks = {
            name: {"x": point[0], "y": point[1]}
            for name, point in zip(AUGMENTED_VERTICES_NAMES, projected_vertices)
        }

        # check if each landmark is out of frame (visible) or not:
        for name, point in groundtruth_landmarks.items():
            if (
                point["x"] < 0
                or point["y"] < 0
                or point["x"] > video_scene.dims[0]
                or point["y"] > video_scene.dims[1]
            ):
                groundtruth_landmarks[name]["v"] = 0
            else:
                groundtruth_landmarks[name]["v"] = 1

        return img, ann, groundtruth_landmarks, coco_landmarks, True

if __name__ == "__main__":
    dataset_generator = DatasetGenerator(
        data_folder="synthetic_finetuning/data/large_job_infinity_combined",
        method="align_3d",
        output_path="old_infinity_dataset_combined",
        samples_per_video=5,
        infinity_version="old",
    )
    dataset_generator.generate_dataset()


#!/usr/bin/env python3
import deploy.penguinPi as ppi
import time

if __name__ == "__main__":
    print("test left motor")
    ppi.set_velocity(10,0)
    time.sleep(3)
    print("test right motor")
    ppi.set_velocity(0,10)
    time.sleep(3)
    print("stop")
    ppi.set_velocity(0,0)
    print("capture image")
    image = ppi.get_image()
    print("image size %d by %d" % (image.shape[0],image.shape[1]))


from os import truncate

aantal = 0
herhalen = "y"
repeat = True
TotaalBakjes = 0
totaalHoorntjes = 0
TotaleBolletjes = 0
particulier = "...."

#------Zakelijk------#
Liters_ijs = 0
Btw = 6
prijsPerLiter = 9.80
#-----------------------#

Besteldesmakenlijst = []
BestaandeSmaken = ["aardbei", "a", "chocolade", "c", "vanille", "v"]

toppings = []
aantalToppings = 0
bestaandeToppings = ["sl", "slagroom", "sp", "sprinkels", "c", "caramel"]
toppingKosten = 0.00

prijsPerBakje = 0.75
prijsPerHoorntje = 1.25
prijsPerBolletje = 0.95

print("Welkom bij Papi Gelato.")

def WrongAnswer():
    print("Sorry dat is geen optie die we aanbieden...")

def AantalLiter():
    global Liters_ijs
    Liters_ijs = int(input("Hoeveel liter ijs wilt U bestellen?: "))

def topping():

        toppingKeuze = input("Wat voor topping wilt u: (G) Geen, (Sl) Slagroom, (Sp) Sprinkels of (C) Caramel Saus?: ").lower()
        if toppingKeuze == "g":
            print("")

        elif toppingKeuze in bestaandeToppings:

            toppings.append(toppingKeuze)
            global aantalToppings
            global toppingKosten
            aantalToppings += 1

            if toppingKeuze == "sl" or "slagroom":

                toppingKosten += 0.50
            elif toppingKeuze == "sp" or "sprinkels":

                toppingKosten += (0.30 * aantal)
            elif toppingKeuze == "c" or "caramel":
                if bakjeOfHoorntje == "bakje":

                    toppingKosten += 0.90
                elif bakjeOfHoorntje == "hoorntje":

                    toppingKosten += 0.60

        else:
            WrongAnswer()

def ParticulierSmaak():
    print("Welke smaak wilt u hebben?")
    print("Aardbei(A), Chocolade(C) of Vanille(V)")
    smaak = input("Welke smaak ijs wilt U?")

def smaak():
    print("Welke smaak wilt u hebben?")
    print("Aardbei(A), Chocolade(C) of Vanille(V)")
    returnsInLoop = 1
    while returnsInLoop != aantal + 1:
        smaak = input("Welke smaak wilt U hebben voor uw "+ str(returnsInLoop) + "e bolletje?").lower()
        if smaak in BestaandeSmaken:
            returnsInLoop += 1
            Besteldesmakenlijst.append(smaak)

        else:
            WrongAnswer()

def aantalbolletjes():
    repeater = "enabled"
    while repeater == "enabled":
        aantalBolletjesijs = int(input("Hoe veel bolletjes ijs wil je?: "))
        if aantal > 0:
            repeater = "disabled"
            return aantalBolletjesijs
        else:
            WrongAnswer()

def WatVoorVerpakking():
    if aantal <= 3:
        waarin = keuzeBakjeOfHoorntje()

    elif aantal > 3 and aantal < 8:
        print("Dan krijgt u van mij een bakje met", aantal, "bolletjes.")
        waarin = "bakje"

    elif aantal > 8:
        print("Sorry, maar zulke grote bakjes hebben wij niet.")
        waarin = "ERROR"

    else:
        WrongAnswer()
        waarin = "ERROR"

    return waarin

def keuzeBakjeOfHoorntje():
    soort = input("Wilt U een hoorntje of een bakje?(H of B): ").lower()
    if soort == "b":
        soort = "bakje"
    elif soort == "h":
        soort = "hoorntje"
    return soort

def nogEenKeer():
    smaak()
    topping()
    print("Hier is uw", bakjeOfHoorntje, "met", aantal, "bolletje(s)")
    overnieuw = input("Wilt u nog wat bestellen?(Y/N): ").lower()
    return overnieuw

while repeat == True:
    Soort = input("Bent u Particulier (P) of zakelijk (Z)?: ").lower()
    if Soort == "p" or Soort == "particulier":
        herhalen = "y"
        particulier = "ja"
        repeat = False
    elif Soort == "z" or Soort =="zakelijk":
        particulier = "nee"
        herhalen = "n"
        repeat = False
    else:
        WrongAnswer()
        repeat = True

while herhalen == "y":

    aantal = aantalbolletjes()

    bakjeOfHoorntje = WatVoorVerpakking()
    if bakjeOfHoorntje != "ERROR":
        herhalen = nogEenKeer()
        TotaleBolletjes += aantal
        if bakjeOfHoorntje == "bakje":

            TotaalBakjes = TotaalBakjes + 1
        elif bakjeOfHoorntje == "hoorntje":
            totaalHoorntjes = totaalHoorntjes + 1
        else:
            WrongAnswer()
    else:

        print("")

if particulier == "nee":
    Litersijs = AantalLiter()
    ParticulierSmaak()

if particulier == "ja":
    print('---------["Papi Gelato"]---------')
    print("")
    if TotaleBolletjes >= 1:
        print("Bolletje(s)    "+ str(TotaleBolletjes)+ " X " + "€" + str(round(float(TotaleBolletjes) * prijsPerBolletje,2)))

    if totaalHoorntjes >= 1:
        print("Hoorntje(s)    "+ str(totaalHoorntjes)+ " X " + "€" + str(round(float(totaalHoorntjes) * prijsPerHoorntje,2)))

    if TotaalBakjes >= 1:
        print("Bakje(s)       "+ str(TotaalBakjes)+ " X " + "€" + str(round(float(TotaalBakjes) * prijsPerBakje,2)))

    if aantalToppings >= 1:
        print("topping(s)     "+ str(aantalToppings)+ " X " + "€" + str(round(toppingKosten,2)))

    print("                   ----- +")
    totaalprijs = float(totaalHoorntjes) * prijsPerHoorntje + float(TotaalBakjes) * prijsPerBakje + TotaleBolletjes * prijsPerBolletje
    totaal2 = round(float(totaalprijs),2)

    print("Totaal"+ "              "+ "€" + str(totaal2))
else:
    print('---------["Papi Gelato"]---------')
    print("")
    totaalprijs = float(Liters_ijs) * float(prijsPerLiter)
    totaalprijs2 = round(float(totaalprijs),2)
    print("Liter   "+ str(Liters_ijs), " X "+ str(prijsPerLiter)+ " = "+"€"+ str(totaalprijs2))
    print("                -------- +")
    btwprijs = round(float(totaalprijs/100 * Btw),2)
    print("Totaal           = €"+ str(totaalprijs2))
    print(f'BTW               {round(totaalprijs2 / 106 * Btw, 2)}€')


#
# @lc app=leetcode.cn id=146 lang=python3
#
# [146] LRU 缓存机制
#

# @lc code=start
from collections import OrderedDict
class LRUCache:

    def __init__(self, capacity: int):
        self.dic = OrderedDict()
        self.capacity = capacity

    def get(self, key: int) -> int:
        if key not in self.dic:
            return -1
        val = self.dic[key]
        self.dic.move_to_end(key)
        return val

    def put(self, key: int, value: int) -> None:
        if key in self.dic:
            self.dic.move_to_end(key)
        self.dic[key] = value
        if len(self.dic) > self.capacity:
            oldest = next(iter(self.dic))
            del self.dic[oldest]

# linked list version
class node(object):
    def __init__(self,data=0,key=0,prev=None,next=None):
        self.data = data
        self.next = next
        self.prev = prev
        self.key = key

class LRUCache(object):

    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.cache = dict()
        self.head = node()
        self.tail = node()
        self.head.next = self.tail
        self.tail.prev = self.head
        self.capacity = capacity

    def get(self, key):
        """
        :type key: int
        :rtype: int
        """
        if key not in self.cache:
            return -1

        node_read = self.cache[key]
        self.move_to_head(node_read)

        return node_read.data

    def put(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: None
        """
        if key in self.cache:
            node_read = self.cache[key]
            node_read.data = value
            self.move_to_head(node_read)
        else:
            node_insert = node(value, key)
            if len(self.cache) < self.capacity:
                self.add_to_head(node_insert)
            else:
                self.move_tail()
                self.add_to_head(node_insert)

    def add_to_head(self,node):
        node.next = self.head.next
        node.prev = self.head
        self.head.next.prev = node
        self.head.next = node
        self.cache[node.key] = node

    def move_to_head(self,node):
        node.prev.next = node.next
        node.next.prev = node.prev
        self.add_to_head(node)
    def move_tail(self):
        node_delete = self.tail.prev
        self.tail.prev = node_delete.prev
        node_delete.prev.next = self.tail
        del self.cache[node_delete.key]
        del node_delete
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
# @lc code=end

a = LRUCache(2)
a.put(1,1)
a.put(2,2)
a.get(1)
a.put(3,3)
a.get(2)
a.put(4,4)
a.get(1)
a.get(3)
a.get(4)


"""Provides Additional Extensions for Snowpark"""

from .dataframe_extensions import *
from .functions_extensions import *
from .session_builder_extensions import *
from .types_extensions import *
from .column_extensions import *

rows_limit = 50

def get_display_html() -> None:
    import inspect
    for frame in inspect.getouterframes(inspect.currentframe()):
        global_names = set(frame.frame.f_globals)
        # Use multiple functions to reduce risk of mismatch
        if all(v in global_names for v in ["displayHTML", "display", "spark"]):
            return frame.frame.f_globals["displayHTML"]
    raise Exception("Unable to detect displayHTML function")

def load_ipython_extension(ipython):
    def instructions():
        inst = """
import pandas as pd
from snowflake.snowpark import Session
import snowpark_extensions
from snowflake.snowpark.functions import col, lit
from snowflake.snowpark import functions as F
        """
        return inst
    error_message_template="""
Error: @error
"""
    output_cell_output = None
    try:
        output_cell_output = get_display_html()
        print("displayHTML detected")
    except:
        from IPython.display import display, HTML
        output_cell_output = lambda x: display(HTML(x))

    def snowflake_dataframe_formatter(df):
        # Format the dataframe as a table using pandas
        from snowflake.snowpark.exceptions import SnowparkSQLException
        try:
            count = df.count()
            if count == 0:
                print(f"0 rows were returned.")
            elif count == 1:
                r=df.collect()
                import pandas as pd
                pandas_df = pd.DataFrame.from_records([x.as_dict() for x in r])
                return pandas_df.to_html()
            elif count > rows_limit:
                print(f"There are {count} rows. Showing only {rows_limit} ")
                return df.limit(rows_limit).to_pandas().to_html()
            else:
                return df.to_pandas().to_html()
        except SnowparkSQLException as sce:
            error_msg = sce.message
            formatted = error_message_template.replace("@error", error_msg)
            return formatted
        except Exception as ex:
            error_message = str(ex)
            return f"
{error_message}
"

    # Register the display hook
    from snowflake.snowpark import DataFrame
    get_ipython().display_formatter.formatters['text/html'].for_type(DataFrame, snowflake_dataframe_formatter)

    from IPython.core.magic import (Magics, magics_class, cell_magic)
    @magics_class
    class SnowparkMagics(Magics):
        def __init__(self, shell):
            super(SnowparkMagics, self).__init__(shell)
        @cell_magic
        def sql(self, line, cell):
            if "session" in self.shell.user_ns:
                session = self.shell.user_ns['session']
                from jinja2 import Template
                t = Template(cell)
                res = t.render(self.shell.user_ns)
                name = None
                if line and line.strip():
                    name = line.strip().split(" ")[0]
                df = session.sql(res)
                if name:
                    self.shell.user_ns[name] = df
                else:
                    self.shell.user_ns["__df"] = df
                    return df
            else:
                return "No session was found. You can setup one by running: session = Session.builder.from_env().getOrCreate()"
    magics = SnowparkMagics(ipython)
    ipython.register_magics(magics)
    compiled_file = "prep_log.py"
    code = compile(instructions(), compiled_file, 'exec')
    async def main():
        await ipython.run_code(code,{})
    import nest_asyncio
    nest_asyncio.apply()
    import asyncio
    asyncio.get_event_loop().run_until_complete(main())


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 17:05:36 2020

@author: Leena
"""

import random
import tkinter as tk

all_maps = ["ASCENT", "BIND", "ICE BOX", "HAVEN", "SPLIT"]

num = len(all_maps)

sel = random.choice(all_maps)

text_1 = ("Official map for the next match is \n\n" + sel)

master = tk.Tk()
master.geometry("500x500")

def openNewWindow():
    newWindow = tk.Toplevel(master)

    def destroy_all():
        newWindow.destroy()
        master.destroy()

    newWindow.title("MAP SELECTION")
    greeting = tk.Label(newWindow, text=text_1)
    greeting.config(font=("Arial",44))
    greeting.pack(pady = 10)

    btn = tk.Button(newWindow,
             text ="OK",
             command = destroy_all)

    btn.pack(pady = 10)
# a button widget which will open a
# new window on button click
btn = tk.Button(master,
             text ="Click to generate VALORANT MAP",
             command = openNewWindow)

btn.pack(pady = 10)

# mainloop, runs infinitely
tk.mainloop()