

def stc_to_label(stc, src=None, smooth=True, connected=False, subjects_dir=None):
    "Compute a label from the non-zero sources in an stc object.\n\n    Parameters\n    ----------\n    stc : SourceEstimate\n        The source estimates.\n    src : SourceSpaces | str | None\n        The source space over which the source estimates are defined.\n        If it's a string it should the subject name (e.g. fsaverage).\n        Can be None if stc.subject is not None.\n    smooth : bool\n        Fill in vertices on the cortical surface that are not in the source\n        space based on the closest source space vertex (requires\n        src to be a SourceSpace).\n    connected : bool\n        If True a list of connected labels will be returned in each\n        hemisphere. The labels are ordered in decreasing order depending\n        of the maximum value in the stc.\n    subjects_dir : str | None\n        Path to SUBJECTS_DIR if it is not set in the environment.\n\n    Returns\n    -------\n    labels : list of Labels | list of list of Labels\n        The generated labels. If connected is False, it returns\n        a list of Labels (one per hemisphere). If no Label is available\n        in a hemisphere, None is returned. If connected is True,\n        it returns for each hemisphere a list of connected labels\n        ordered in decreasing order depending of the maximum value in the stc.\n        If no Label is available in an hemisphere, an empty list is returned.\n    "
    if (not isinstance(smooth, bool)):
        raise ValueError(('smooth should be True or False. Got %s.' % smooth))
    src = (stc.subject if (src is None) else src)
    if (src is None):
        raise ValueError('src cannot be None if stc.subject is None')
    if isinstance(src, string_types):
        subject = src
    else:
        subject = stc.subject
    if (not isinstance(stc, SourceEstimate)):
        raise ValueError('SourceEstimate should be surface source estimates')
    if isinstance(src, string_types):
        if connected:
            raise ValueError('The option to return only connected labels is only available if source spaces are provided.')
        if smooth:
            msg = 'stc_to_label with smooth=True requires src to be an instance of SourceSpace'
            raise ValueError(msg)
        subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
        surf_path_from = op.join(subjects_dir, src, 'surf')
        (rr_lh, tris_lh) = read_surface(op.join(surf_path_from, 'lh.white'))
        (rr_rh, tris_rh) = read_surface(op.join(surf_path_from, 'rh.white'))
        rr = [rr_lh, rr_rh]
        tris = [tris_lh, tris_rh]
    else:
        if (not isinstance(src, SourceSpaces)):
            raise TypeError('src must be a string or a set of source spaces')
        if (len(src) != 2):
            raise ValueError('source space should contain the 2 hemispheres')
        rr = [(1000.0 * src[0]['rr']), (1000.0 * src[1]['rr'])]
        tris = [src[0]['tris'], src[1]['tris']]
        src_conn = spatial_src_connectivity(src).tocsr()
    labels = []
    cnt = 0
    cnt_full = 0
    for (hemi_idx, (hemi, this_vertno, this_tris, this_rr)) in enumerate(zip(['lh', 'rh'], stc.vertices, tris, rr)):
        this_data = stc.data[cnt:(cnt + len(this_vertno))]
        e = mesh_edges(this_tris)
        e.data[(e.data == 2)] = 1
        n_vertices = e.shape[0]
        e = (e + sparse.eye(n_vertices, n_vertices))
        if connected:
            vertno = np.where(src[hemi_idx]['inuse'])[0]
            if (not (len(np.setdiff1d(this_vertno, vertno)) == 0)):
                raise RuntimeError('stc contains vertices not present in source space, did you morph?')
            tmp = np.zeros((len(vertno), this_data.shape[1]))
            this_vertno_idx = np.searchsorted(vertno, this_vertno)
            tmp[this_vertno_idx] = this_data
            this_data = tmp
            offset = (cnt_full + len(this_data))
            this_src_conn = src_conn[cnt_full:offset, cnt_full:offset].tocoo()
            this_data_abs_max = np.abs(this_data).max(axis=1)
            (clusters, _) = _find_clusters(this_data_abs_max, 0.0, connectivity=this_src_conn)
            cnt_full += len(this_data)
            clusters_max = np.argsort([np.max(this_data_abs_max[c]) for c in clusters])[::(- 1)]
            clusters = [clusters[k] for k in clusters_max]
            clusters = [vertno[c] for c in clusters]
        else:
            clusters = [this_vertno[np.any(this_data, axis=1)]]
        cnt += len(this_vertno)
        clusters = [c for c in clusters if (len(c) > 0)]
        if (len(clusters) == 0):
            if (not connected):
                this_labels = None
            else:
                this_labels = []
        else:
            this_labels = []
            colors = _n_colors(len(clusters))
            for (c, color) in zip(clusters, colors):
                idx_use = c
                label = Label(idx_use, this_rr[idx_use], None, hemi, 'Label from stc', subject=subject, color=color)
                if smooth:
                    label = label.fill(src)
                this_labels.append(label)
            if (not connected):
                this_labels = this_labels[0]
        labels.append(this_labels)
    return labels

dataset/ETHPy150Open mne-tools/mne-python/mne/label.py stc_to_label/original