import torch


def make_circle_masks(n, h, w):
    """
    Create circle mask on a grid of size (h, w).
    Center and radius chosen randomly from [-0.5, 0.5] and [0.1, 0.4] respectively (for grid scale of [-1, 1]).

    Parameters
    ----------
    n: int
        Number of channels to create mask for.
    h: int
        Height of image.
    w: int
        Width of image.

    Returns
    -------
    tensor
        (n, h, w)
    """
    x = torch.linspace(-1, 1, steps=w).view(1, 1, -1) # x.shape = (1, 1, w)
    y = torch.linspace(-1, 1, steps=h).view(1, -1, 1) # y.shape = (1, h, 1)
    center = torch.rand((2, n, 1, 1)) - 0.5 # center \in [-0.5, 0.5], center.shape = (2, n, 1, 1)
    r = torch.rand((n, 1, 1)) * 0.3 + 0.1 # r \in [0.1, 0.4] # r.shape = (n, 1, 1)
    x = (x - center[0]) / r # x.shape = (n, 1, w)
    y = (y - center[1]) / r # y.shape = (n, h, 1)
    mask = (x * x + y * y < 1.0).float() # mask.shape = (n, h, w)
    return mask