Example #1
0
def linear_operator_from_masks(masks, weights=None):
    """Generates the linear operator for the group total variation Nesterov
    function from a mask for a 3D image.

    Parameters
    ----------
    masks : List of numpy arrays. The mask for each group. Each mask is an
            integer (0 or 1) or boolean numpy array or the same shape as the
            actual data. The mask does not involve any intercept variables.

    weights : List of floats. The weights account for different group sizes,
            or incorporates some prior knowledge about the importance of the
            groups. Default value is the square roots of the group sizes.
    """
    import parsimony.functions.nesterov.tv as tv

    if isinstance(masks, tuple):
        masks = list(masks)

    A = []

    G = len(masks)
    for g in xrange(G):
        mask = masks[g]

        if weights is None:
            weight = np.sqrt(np.sum(mask))
        else:
            weight = weights[g]

        # Compute group A matrix
        Ag = tv.linear_operator_from_subset_mask(mask)

        # Include the weights
        if weight != 1.0 and weight != 1:
            for A_ in Ag:
                A_ *= weight

        A += Ag

    return A
Example #2
0
def linear_operator_from_masks(masks, weights=None):
    """Generates the linear operator for the group total variation Nesterov
    function from a mask for a 3D image.

    Parameters
    ----------
    masks : List of numpy arrays. The mask for each group. Each mask is an
            integer (0 or 1) or boolean numpy array or the same shape as the
            actual data. The mask does not involve any intercept variables.

    weights : List of floats. The weights account for different group sizes,
            or incorporates some prior knowledge about the importance of the
            groups. Default value is the square roots of the group sizes.
    """
    import parsimony.functions.nesterov.tv as tv

    if isinstance(masks, tuple):
        masks = list(masks)

    A = []

    G = len(masks)
    for g in xrange(G):
        mask = masks[g]

        if weights is None:
            weight = np.sqrt(np.sum(mask))
        else:
            weight = weights[g]

        # Compute group A matrix
        Ag = tv.linear_operator_from_subset_mask(mask)

        # Include the weights
        if weight != 1.0 and weight != 1:
            for A_ in Ag:
                A_ *= weight

        A += Ag

    return A
Example #3
0
def linear_operator_from_rects(rects, shape, weights=None):
    """Generates the linear operator for the group total variation Nesterov
    function from the rectange of a 3D image.

    Parameters
    ----------
    rects : List of lists or tuples with 2-, 4- or 6-tuple elements. The shape
            of the patch of the 1D, 2D or 3D image to smooth. The elements of
            rects has the form ((x1, x2),), ((y1, y2), (x1, x2)) or ((z1, z2),
            (y1, y2), (x1, x2)), where z is the "layers", y rows and x is the
            columns and x1 means the first column to include, x2 is one beyond
            the last column to include, and similarly for y and z. The rect
            does not involve any intercept variables.

    shape : List or tuple with 1, 2 or 3 integers. The shape of the 1D, 2D or
            3D image. shape has the form (X,), (Y, X) or (Z, Y, X), where Z is
            the number of "layers", Y is the number of rows and X is the number
            of columns. The shape does not involve any intercept variables.

    weights : List of floats. The weights account for different group sizes,
            or incorporates some prior knowledge about the importance of the
            groups. Default value is the square roots of the group sizes.
    """
    import parsimony.functions.nesterov.tv as tv

    A = []
    G = len(rects)
    for g in xrange(G):
        rect = rects[g]
        if len(rect) == 1:
            rect = [(0, 1), (0, 1), rect[0]]
        elif len(rect) == 2:
            rect = [(0, 1), rect[0], rect[1]]

        while len(shape) < 3:
            shape = tuple([1] + list(shape))

        mask = np.zeros(shape, dtype=bool)
        z1 = rect[0][0]
        z2 = rect[0][1]
        y1 = rect[1][0]
        y2 = rect[1][1]
        x1 = rect[2][0]
        x2 = rect[2][1]
        mask[z1:z2, y1:y2, x1:x2] = True

        if weights is None:
            weight = np.sqrt(np.sum(mask))
        else:
            weight = weights[g]

        # Compute group A matrix
        Ag = tv.linear_operator_from_subset_mask(mask)

        # Include the weights
        if weight != 1.0 and weight != 1:
            for A_ in Ag:
                A_ *= weight

        A += Ag

    return A
Example #4
0
def linear_operator_from_rects(rects, shape, weights=None):
    """Generates the linear operator for the group total variation Nesterov
    function from the rectange of a 3D image.

    Parameters
    ----------
    rects : List of lists or tuples with 2-, 4- or 6-tuple elements. The shape
            of the patch of the 1D, 2D or 3D image to smooth. The elements of
            rects has the form ((x1, x2),), ((y1, y2), (x1, x2)) or ((z1, z2),
            (y1, y2), (x1, x2)), where z is the "layers", y rows and x is the
            columns and x1 means the first column to include, x2 is one beyond
            the last column to include, and similarly for y and z. The rect
            does not involve any intercept variables.

    shape : List or tuple with 1, 2 or 3 integers. The shape of the 1D, 2D or
            3D image. shape has the form (X,), (Y, X) or (Z, Y, X), where Z is
            the number of "layers", Y is the number of rows and X is the number
            of columns. The shape does not involve any intercept variables.

    weights : List of floats. The weights account for different group sizes,
            or incorporates some prior knowledge about the importance of the
            groups. Default value is the square roots of the group sizes.
    """
    import parsimony.functions.nesterov.tv as tv

    A = []
    G = len(rects)
    for g in xrange(G):
        rect = rects[g]
        if len(rect) == 1:
            rect = [(0, 1), (0, 1), rect[0]]
        elif len(rect) == 2:
            rect = [(0, 1), rect[0], rect[1]]

        while len(shape) < 3:
            shape = tuple([1] + list(shape))

        mask = np.zeros(shape, dtype=bool)
        z1 = rect[0][0]
        z2 = rect[0][1]
        y1 = rect[1][0]
        y2 = rect[1][1]
        x1 = rect[2][0]
        x2 = rect[2][1]
        mask[z1:z2, y1:y2, x1:x2] = True

        if weights is None:
            weight = np.sqrt(np.sum(mask))
        else:
            weight = weights[g]

        # Compute group A matrix
        Ag = tv.linear_operator_from_subset_mask(mask)

        # Include the weights
        if weight != 1.0 and weight != 1:
            for A_ in Ag:
                A_ *= weight

        A += Ag

    return A