def f1_score(cmat):
    """ Calculate F measure from confusion matrix.

    Parameters
    ----------
    cmat : ndarray
        Confusion matrix.
        
        Assuming confusion matrix in following format:
            TN    FP
            FN    TP
        E.g. as obtained from predict(...)[0, 0, :, :]

    Returns
    -------
    f1 : float
        F measure of the given confusion matrix.
    """
    FP = cmat[0, 1]
    FN = cmat[1, 0]
    TP = cmat[1, 1]
    if TP == 0:
        return 0
    else:
        precision = TP / (TP + FP)
        recall = TP / (TP + FN)
        return 2 * precision * recall / (precision + recall)
