import torch


def prep_tensor_for_vis(x):
    """Prepare tensor for visualization
    If only has one channel, concatenate to produce 3 channels
    Clone, detach and pass to cpu before clamping between 0 and 1

    Parameters
    ----------
    x: torch.FloatTensor
        Tensor with image (CHW)

    Returns
    ----------
    torch.FloatTensor
        3HW detached tensor on cpu clamped between 0 and 1
    """
    if x.shape[0] == 1:
        x = torch.cat([x] * 3, 0)
    return torch.clamp(x.clone().detach().cpu(), 0., 1.)