import torch


def process_targets(preds, targets, target_name, process, new_name=None,
                    skip_missing=False):
    """
    Applies some postprocessing to the given targets and stores them under
    the same name or a new name. `process` can currently only be `"sigmoid"`.
    If `skip_missing` is true-ish, exits silently if the target is missing.
    """
    try:
        data = targets[target_name]
    except KeyError:
        if skip_missing:
            return 0
        else:
            raise
    if process == 'sigmoid':
        data = torch.sigmoid(data)
    else:
        raise ValueError("Unsupported process %r" % process)
    if new_name is None:
        new_name = target_name
    targets[new_name] = data
    return 0