import torch


def precreate_targ_mask_encoding(nintervals):
    """Precreate 1 + 2*nintervals different target/mask encodings
    (depending whether item is a boundary, or whether each interval is
    censored or not).

    """
    nencodes = 1 + nintervals * 2
    targ_tensor = torch.zeros(nencodes, nintervals)
    mask_tensor = torch.zeros(nencodes, nintervals)
    # For bounds, leave both all-zeros
    # Next uncensored then censored ones:
    offset = 1
    for idx in range(nintervals):
        # Mark point where item suffered hazard:
        targ_tensor[idx + offset][idx] = 1
        # Include up to and incl. this one in the loss:
        mask_tensor[idx + offset][:idx+1] = 1
    offset = 1 + nintervals
    for idx in range(nintervals):
        # No hazard seen, but include survival points (leave target
        # values as zero) via the mask:
        mask_tensor[idx + offset][:idx] = 1
    return targ_tensor, mask_tensor