Exemple #1
0
def linear_operator_from_mesh(mesh_coord, mesh_triangles, mask=None, offset=0,
                              weights=None):
    """Generates the linear operator for the total variation Nesterov function
    from a mesh.

    Parameters
    ----------
    mesh_coord : Numpy array [n, 3] of float.

    mesh_triangles : Numpy array, n_triangles-by-3. The (integer) indices of
            the three nodes forming the triangle.

    mask : Numpy array (shape (n,)) of integers/boolean. Non-null values
            correspond to columns of X. Groups may be defined using different
            values in the mask. TV will be applied within groups of the same
            value in the mask.

    offset : Non-negative integer. The index of the first column, variable,
            where TV applies. This is different from penalty_start which
            define where the penalty applies. The offset defines where TV
            applies within the penalised variables.

                Example: X := [Intercept, Age, Weight, Image]. Intercept is
                not penalized, TV does not apply on Age and Weight but only on
                Image. Thus: penalty_start = 1, offset = 2 (skip Age and
                Weight).

    weights : Numpy array. The weight put on the gradient of every point.
            Default is weight 1 for each point, or equivalently, no weight. The
            weights is a numpy array of the same shape as mask.

    Returns
    -------
    out1 : List or sparse matrices. Linear operator for the total variation
           Nesterov function computed over a mesh.

    out2 : Integer. The number of compacts.

    Examples
    --------
    >>> import numpy as np
    >>> import parsimony.functions.nesterov.l1tv as tv_helper
    >>> mesh_coord = np.array([[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2]])
    >>> mesh_triangles = np.array([[0 ,1, 3], [0, 2 ,3], [2, 3, 5], [2, 4, 5]])
    >>> A = tv_helper.linear_operator_from_mesh(mesh_coord,mesh_triangles)
    """
    Atv = tv.linear_operator_from_mesh(mesh_coord=mesh_coord,
                                       mesh_triangles=mesh_triangles,
                                       mask=mask, offset=offset,
                                       weights=weights)
    num_variables = mask.sum() if not mask is None else mesh_coord.shape[0]
    Al1 = l1.linear_operator_from_variables(num_variables,
                                            penalty_start=offset)

    A = LinearOperator(Al1[0], *Atv)
    return A
Exemple #2
0
def linear_operator_from_shape(shape, num_variables, penalty_start=0):
    """Generates the linear operator for the total variation Nesterov function
    from the shape of a 3D image.

    Parameters
    ----------
    shape : List or tuple with 1, 2 or 3 elements. The shape of the 1D, 2D or
            3D image. shape has the form (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.

    num_variables : Positive integer. The total number of variables, including
            the intercept variable(s).

    penalty_start : Non-negative integer. The number of variables to exempt
            from penalisation. Equivalently, the first index to be penalised.
            Default is 0, all variables are included.
    """
    Atv = tv.linear_operator_from_shape(shape)
    Al1 = l1.linear_operator_from_variables(num_variables, penalty_start=penalty_start)

    A = LinearOperator(Al1[0], *Atv)
    return A