""" Eval metrics and related

"""
import torch

class AverageMeter:
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""
    maxk = min(max(topk), output.size()[1])
    batch_size = target.size(0)
    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.reshape(1, -1).expand_as(pred))
    return [correct[:min(k, maxk)].reshape(-1).float().sum(0) * 100. / batch_size for k in topk]


def accuracy_dgdagrnn(output):
    """Computes the accuracy for dgdagrnn"""

    pred = (output > 0.5).int()
    return pred.sum(0) * 100. / pred.size(0)

def accuracy_neurosat(output, target):
    """Computes the accuracy for neurosat"""
    pred = (torch.sigmoid(output) > 0.5).int()

    correct = (pred.eq(target)).sum()
    return correct * 100. / pred.size(0)   
   