

def split_label(label, parts=2, subject=None, subjects_dir=None, freesurfer=False):
    "Split a Label into two or more parts\n\n    Parameters\n    ----------\n    label : Label | str\n        Label which is to be split (Label object or path to a label file).\n    parts : int >= 2 | tuple of str\n        A sequence of strings specifying label names for the new labels (from\n        posterior to anterior), or the number of new labels to create (default\n        is 2). If a number is specified, names of the new labels will be the\n        input label's name with div1, div2 etc. appended.\n    subject : None | str\n        Subject which this label belongs to (needed to locate surface file;\n        should only be specified if it is not specified in the label).\n    subjects_dir : None | str\n        Path to SUBJECTS_DIR if it is not set in the environment.\n    freesurfer : bool\n        By default (``False``) ``split_label`` uses an algorithm that is\n        slightly optimized for performance and numerical precision. Set\n        ``freesurfer`` to ``True`` in order to replicate label splits from\n        FreeSurfer's ``mris_divide_parcellation``.\n\n    Returns\n    -------\n    labels : list of Label (len = n_parts)\n        The labels, starting from the lowest to the highest end of the\n        projection axis.\n\n    Notes\n    -----\n    Works by finding the label's principal eigen-axis on the spherical surface,\n    projecting all label vertex coordinates onto this axis and dividing them at\n    regular spatial intervals.\n    "
    if isinstance(label, BiHemiLabel):
        raise TypeError('Can only split labels restricted to one hemisphere.')
    elif isinstance(label, string_types):
        label = read_label(label)
    if np.isscalar(parts):
        n_parts = int(parts)
        if label.name.endswith(('lh', 'rh')):
            basename = label.name[:(- 3)]
            name_ext = label.name[(- 3):]
        else:
            basename = label.name
            name_ext = ''
        name_pattern = ('%s_div%%i%s' % (basename, name_ext))
        names = tuple(((name_pattern % i) for i in range(1, (n_parts + 1))))
    else:
        names = parts
        n_parts = len(names)
    if (n_parts < 2):
        raise ValueError(("Can't split label into %i parts" % n_parts))
    subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
    if ((label.subject is None) and (subject is None)):
        raise ValueError('The subject needs to be specified.')
    elif (subject is None):
        subject = label.subject
    elif (label.subject is None):
        pass
    elif (subject != label.subject):
        raise ValueError(('The label specifies a different subject (%r) from the subject parameter (%r).' % label.subject), subject)
    surf_fname = '.'.join((label.hemi, 'sphere'))
    surf_path = os.path.join(subjects_dir, subject, 'surf', surf_fname)
    (surface_points, surface_tris) = read_surface(surf_path)
    points = surface_points[label.vertices]
    center = np.mean(points, axis=0)
    centered_points = (points - center)
    if freesurfer:
        distance = np.sqrt(np.sum((centered_points ** 2), axis=1))
        i_closest = np.argmin(distance)
        closest_vertex = label.vertices[i_closest]
        idx = np.any((surface_tris == closest_vertex), axis=1)
        tris_for_normal = surface_tris[idx]
        r1 = surface_points[tris_for_normal[:, 0], :]
        r2 = surface_points[tris_for_normal[:, 1], :]
        r3 = surface_points[tris_for_normal[:, 2], :]
        tri_normals = fast_cross_3d((r2 - r1), (r3 - r1))
        normal = np.mean(tri_normals, axis=0)
        normal /= linalg.norm(normal)
    else:
        normal = (center / linalg.norm(center))
    (q, _) = linalg.qr(normal[:, np.newaxis])
    tangent_u = q[:, 1:]
    m_obs = np.dot(centered_points, tangent_u)
    m_cov = np.dot(m_obs.T, m_obs)
    (w, vr) = linalg.eig(m_cov)
    i = np.argmax(w)
    eigendir = vr[:, i]
    axis = np.dot(tangent_u, eigendir)
    if (axis[1] < 0):
        axis *= (- 1)
    proj = np.dot(points, axis)
    proj -= proj.min()
    proj /= (proj.max() / n_parts)
    mark = (proj // 1)
    mark[(mark == n_parts)] = (n_parts - 1)
    if (label.color is None):
        colors = ((None,) * n_parts)
    else:
        colors = _split_colors(label.color, n_parts)
    labels = []
    for (i, name, color) in zip(range(n_parts), names, colors):
        idx = (mark == i)
        vert = label.vertices[idx]
        pos = label.pos[idx]
        values = label.values[idx]
        hemi = label.hemi
        comment = label.comment
        lbl = Label(vert, pos, values, hemi, comment, name, None, subject, color)
        labels.append(lbl)
    return labels

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