

@verbose
def write_labels_to_annot(labels, subject=None, parc=None, overwrite=False, subjects_dir=None, annot_fname=None, colormap='hsv', hemi='both', verbose=None):
    'Create a FreeSurfer annotation from a list of labels\n\n    Parameters\n    ----------\n    labels : list with instances of mne.Label\n        The labels to create a parcellation from.\n    subject : str | None\n        The subject for which to write the parcellation for.\n    parc : str | None\n        The parcellation name to use.\n    overwrite : bool\n        Overwrite files if they already exist.\n    subjects_dir : string, or None\n        Path to SUBJECTS_DIR if it is not set in the environment.\n    annot_fname : str | None\n        Filename of the .annot file. If not None, only this file is written\n        and \'parc\' and \'subject\' are ignored.\n    colormap : str\n        Colormap to use to generate label colors for labels that do not\n        have a color specified.\n    hemi : \'both\' | \'lh\' | \'rh\'\n        The hemisphere(s) for which to write \\*.annot files (only applies if\n        annot_fname is not specified; default is \'both\').\n    verbose : bool, str, int, or None\n        If not None, override default verbose level (see mne.verbose).\n\n    Notes\n    -----\n    Vertices that are not covered by any of the labels are assigned to a label\n    named "unknown".\n    '
    logger.info('Writing labels to parcellation..')
    subjects_dir = get_subjects_dir(subjects_dir)
    (annot_fname, hemis) = _get_annot_fname(annot_fname, subject, hemi, labels, subjects_dir)
    if (not overwrite):
        for fname in annot_fname:
            if op.exists(fname):
                raise ValueError(('File %s exists. Use "overwrite=True" to overwrite it' % fname))
    to_save = []
    duplicate_colors = []
    invalid_colors = []
    overlap = []
    no_color = ((- 1), (- 1), (- 1), (- 1))
    no_color_rgb = ((- 1), (- 1), (- 1))
    for (hemi, fname) in zip(hemis, annot_fname):
        hemi_labels = [label for label in labels if (label.hemi == hemi)]
        n_hemi_labels = len(hemi_labels)
        if (n_hemi_labels == 0):
            ctab = np.empty((0, 4), dtype=np.int32)
            ctab_rgb = ctab[:, :3]
        else:
            hemi_labels.sort(key=(lambda label: label.name))
            hemi_colors = [(no_color if (label.color is None) else tuple((int(round((255 * i))) for i in label.color))) for label in hemi_labels]
            ctab = np.array(hemi_colors, dtype=np.int32)
            ctab_rgb = ctab[:, :3]
            labels_by_color = defaultdict(list)
            for (label, color) in zip(hemi_labels, ctab_rgb):
                labels_by_color[tuple(color)].append(label.name)
            for (color, names) in labels_by_color.items():
                if (color == no_color_rgb):
                    continue
                if (color == (0, 0, 0)):
                    warn('At least one label contains a color with, "r=0, g=0, b=0" value. Some FreeSurfer tools may fail to read the parcellation')
                if any(((i > 255) for i in color)):
                    msg = ('%s: %s (%s)' % (color, ', '.join(names), hemi))
                    invalid_colors.append(msg)
                if (len(names) > 1):
                    msg = ('%s: %s (%s)' % (color, ', '.join(names), hemi))
                    duplicate_colors.append(msg)
            if labels_by_color[no_color_rgb]:
                default_colors = _n_colors(n_hemi_labels, bytes_=True, cmap=colormap)
                safe_color_i = 0
                for i in xrange(n_hemi_labels):
                    if (ctab[(i, 0)] == (- 1)):
                        color = default_colors[i]
                        while np.any(np.all((color[:3] == ctab_rgb), 1)):
                            color = default_colors[safe_color_i]
                            safe_color_i += 1
                        ctab[i] = color
        if ((subject is not None) and (subjects_dir is not None)):
            fpath = os.path.join(subjects_dir, subject, 'surf', ('%s.white' % hemi))
            (points, _) = read_surface(fpath)
            n_vertices = len(points)
        else:
            if (len(hemi_labels) > 0):
                max_vert = max((np.max(label.vertices) for label in hemi_labels))
                n_vertices = (max_vert + 1)
            else:
                n_vertices = 1
            warn('Number of vertices in the surface could not be verified because the surface file could not be found; specify subject and subjects_dir parameters.')
        annot = np.empty(n_vertices, dtype=np.int)
        annot[:] = (- 1)
        annot_id_coding = np.array((1, (2 ** 8), (2 ** 16)))
        annot_ids = list(np.sum((ctab_rgb * annot_id_coding), axis=1))
        for (label, annot_id) in zip(hemi_labels, annot_ids):
            if np.any((annot[label.vertices] != (- 1))):
                other_ids = set(annot[label.vertices])
                other_ids.discard((- 1))
                other_indices = (annot_ids.index(i) for i in other_ids)
                other_names = (hemi_labels[i].name for i in other_indices)
                other_repr = ', '.join(other_names)
                msg = ('%s: %s overlaps %s' % (hemi, label.name, other_repr))
                overlap.append(msg)
            annot[label.vertices] = annot_id
        hemi_names = [label.name for label in hemi_labels]
        if (None in hemi_names):
            msg = ('Found %i labels with no name. Writing annotation filerequires all labels named' % hemi_names.count(None))
            raise ValueError(msg)
        unlabeled = (annot == (- 1))
        if np.any(unlabeled):
            msg = ("Assigning %i unlabeled vertices to 'unknown-%s'" % (unlabeled.sum(), hemi))
            logger.info(msg)
            for i in range(1, 257):
                if (not np.any(np.all(((i, i, i) == ctab_rgb), 1))):
                    break
            if (i < 256):
                color = (i, i, i, 0)
            else:
                err = "Need one free shade of gray for 'unknown' label. Please modify your label colors, or assign the unlabeled vertices to another label."
                raise ValueError(err)
            annot_id = np.sum((annot_id_coding * color[:3]))
            annot[unlabeled] = annot_id
            ctab = np.vstack((ctab, color))
            hemi_names.append('unknown')
        ctab[:, 3] = (255 - ctab[:, 3])
        hemi_names = [(name[:(- 3)] if name.endswith(hemi) else name) for name in hemi_names]
        to_save.append((fname, annot, ctab, hemi_names))
    issues = []
    if duplicate_colors:
        msg = 'Some labels have the same color values (all labels in one hemisphere must have a unique color):'
        duplicate_colors.insert(0, msg)
        issues.append(os.linesep.join(duplicate_colors))
    if invalid_colors:
        msg = 'Some labels have invalid color values (all colors should be RGBA tuples with values between 0 and 1)'
        invalid_colors.insert(0, msg)
        issues.append(os.linesep.join(invalid_colors))
    if overlap:
        msg = 'Some labels occupy vertices that are also occupied by one or more other labels. Each vertex can only be occupied by a single label in *.annot files.'
        overlap.insert(0, msg)
        issues.append(os.linesep.join(overlap))
    if issues:
        raise ValueError('\n\n'.join(issues))
    for (fname, annot, ctab, hemi_names) in to_save:
        logger.info(('   writing %d labels to %s' % (len(hemi_names), fname)))
        _write_annot(fname, annot, ctab, hemi_names)
    logger.info('[done]')

dataset/ETHPy150Open mne-tools/mne-python/mne/label.py write_labels_to_annot/VarMisuse@75/593 `parc`->`labels`