def initialize_eigenanatomy(initmat, mask=None, initlabels=None, nreps=1, smoothing=0):
    """
    InitializeEigenanatomy is a helper function to initialize sparseDecom
    and sparseDecom2. Can be used to estimate sparseness parameters per
    eigenvector. The user then only chooses nvecs and optional
    regularization parameters.

    Arguments
    ---------
    initmat : np.ndarray or ANTsImage
        input matrix where rows provide initial vector values.
        alternatively, this can be an antsImage which contains labeled regions.

    mask : ANTsImage
        mask if available

    initlabels : list/tuple of integers
        which labels in initmat to use as initial components

    nreps : integer
        nrepetitions to use

    smoothing : float
        if using an initial label image, optionally smooth each roi

    Returns
    -------
    dict w/ the following key/value pairs:
        `initlist` : list of ANTsImage types
            initialization list(s) for sparseDecom(2)

        `mask` : ANTsImage
            mask(s) for sparseDecom(2)

        `enames` : list of strings
            string names of components for sparseDecom(2)

    Example
    -------
    >>> import ants
    >>> import numpy as np
    >>> mat = np.random.randn(4,100).astype('float32')
    >>> init = ants.initialize_eigenanatomy(mat)
    """
    if isinstance(initmat, iio.ANTsImage):
        # create initmat from each of the unique labels
        if mask is not None:
            selectvec = mask > 0
        else:
            selectvec = initmat > 0
        initmatvec = initmat[selectvec]

        if initlabels is None:
            ulabs = np.sort(np.unique(initmatvec))
            ulabs = ulabs[ulabs > 0]
        else:
            ulabs = initlabels

        nvox = len(initmatvec)
        temp = np.zeros((len(ulabs), nvox))

        for x in range(len(ulabs)):
            timg = utils.threshold_image(initmat, ulabs[x]-1e-4, ulabs[x]+1e-4)
            if smoothing > 0:
                timg = utils.smooth_image(timg, smoothing)
            temp[x,:] = timg[selectvec]
        initmat = temp

    nclasses = initmat.shape[0]
    classlabels = ['init%i'%i for i in range(nclasses)]
    initlist = []
    if mask is None:
        maskmat = np.zeros(initmat.shape)
        maskmat[0,:] = 1
        mask = core.from_numpy(maskmat.astype('float32'))

    eanatnames = ['A'] * (nclasses*nreps)
    ct = 0
    for i in range(nclasses):
        vecimg = mask.clone('float')
        initf = initmat[i,:]
        vecimg[mask==1] = initf
        for nr in range(nreps):
            initlist.append(vecimg)
            eanatnames[ct+nr-1] = str(classlabels[i])
            ct = ct + 1

    return {'initlist': initlist, 'mask': mask, 'enames': eanatnames}
def initialize_eigenanatomy ( initmat , mask = None , initlabels = None , nreps = 1 , smoothing = 0 ) : if isinstance ( initmat , iio . ANTsImage ) : if mask is not None : selectvec = mask > 0 else : selectvec = initmat > 0 initmatvec = initmat [ selectvec ] if initlabels is None : ulabs = np . sort ( np . unique ( initmatvec ) ) ulabs = ulabs [ ulabs > 0 ] else : ulabs = initlabels nvox = len ( initmatvec ) temp = np . zeros ( ( len ( ulabs ) , nvox ) ) for x in range ( len ( ulabs ) ) : timg = utils . threshold_image ( initmat , ulabs [ x ] - 1e-4 , ulabs [ x ] + 1e-4 ) if smoothing > 0 : timg = utils . smooth_image ( timg , smoothing ) temp [ x , : ] = timg [ selectvec ] initmat = temp nclasses = initmat . shape [ 0 ] classlabels = [ 'init%i' % i for i in range ( nclasses ) ] initlist = [ ] if mask is None : maskmat = np . zeros ( initmat . shape ) maskmat [ 0 , : ] = 1 mask = core . from_numpy ( maskmat . astype ( 'float32' ) ) eanatnames = [ 'A' ] * ( nclasses * nreps ) ct = 0 for i in range ( nclasses ) : vecimg = mask . clone ( 'float' ) initf = initmat [ i , : ] vecimg [ mask == 1 ] = initf for nr in range ( nreps ) : initlist . append ( vecimg ) eanatnames [ ct + nr - 1 ] = str ( classlabels [ i ] ) ct = ct + 1 return { 'initlist' : initlist , 'mask' : mask , 'enames' : eanatnames }
InitializeEigenanatomy is a helper function to initialize sparseDecom
    and sparseDecom2. Can be used to estimate sparseness parameters per
    eigenvector. The user then only chooses nvecs and optional
    regularization parameters.

    Arguments
    ---------
    initmat : np.ndarray or ANTsImage
        input matrix where rows provide initial vector values.
        alternatively, this can be an antsImage which contains labeled regions.

    mask : ANTsImage
        mask if available

    initlabels : list/tuple of integers
        which labels in initmat to use as initial components

    nreps : integer
        nrepetitions to use

    smoothing : float
        if using an initial label image, optionally smooth each roi

    Returns
    -------
    dict w/ the following key/value pairs:
        `initlist` : list of ANTsImage types
            initialization list(s) for sparseDecom(2)

        `mask` : ANTsImage
            mask(s) for sparseDecom(2)

        `enames` : list of strings
            string names of components for sparseDecom(2)

    Example
    -------
    >>> import ants
    >>> import numpy as np
    >>> mat = np.random.randn(4,100).astype('float32')
    >>> init = ants.initialize_eigenanatomy(mat)