import torch


def calc_regression_acc(predictions, targets, thld=None):
    """Calculate accuracy when treating regression as binary classification.

    Args:
        predictions (torch.Tensor): The predicted mean. A mean greater than zero
            will be considered a postive prediction.
        targets (torch.Tensor): The regression targets. Targets greater than
            zero will be considered as positive lables.
        thld (float, optional): If specified, another threshold than zero can be
            chosen to define the boundary between positive and negative values.

    Returns:
        Accuracy.
    """
    if thld is None:
        thld = 0
    labels = targets > thld
    pred_labels = predictions > thld

    return 100 * torch.sum(labels == pred_labels) / labels.shape[0]