code: def test_write_labels_to_annot(tmp_path):
      """Test writing FreeSurfer parcellation from labels."""
      pytest.importorskip("nibabel")
      labels = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir
      )
  
      # create temporary subjects-dir skeleton
      surf_dir = subjects_dir / "sample" / "surf"
      temp_surf_dir = tmp_path / "sample" / "surf"
      os.makedirs(temp_surf_dir)
      shutil.copy(surf_dir / "lh.white", temp_surf_dir)
      shutil.copy(surf_dir / "rh.white", temp_surf_dir)
      os.makedirs(tmp_path / "sample" / "label")
  
      # test automatic filenames
      dst = tmp_path / "sample" / "label" / "%s.%s.annot"
      write_labels_to_annot(
          labels, "sample", "test1", subjects_dir=tmp_path
      )
      assert Path(str(dst) % ("lh", "test1")).exists()
      assert Path(str(dst) % ("rh", "test1")).exists()
      # lh only
      for label in labels:
          if label.hemi == "lh":
              break
      write_labels_to_annot(
          [label], "sample", "test2", subjects_dir=tmp_path
      )
      assert Path(str(dst) % ("lh", "test2")).exists()
      assert Path(str(dst) % ("rh", "test2")).exists()
      # rh only
      for label in labels:
          if label.hemi == "rh":
              break
      write_labels_to_annot(
          [label], "sample", "test3", subjects_dir=tmp_path
      )
      assert Path(str(dst) % ("lh", "test3")).exists()
      assert Path(str(dst) % ("rh", "test3")).exists()
      # label alone
      pytest.raises(
          TypeError,
          write_labels_to_annot,
          labels[0],
          "sample",
          "test4",
          subjects_dir=tmp_path,
      )
  
      # write left and right hemi labels with filenames:
      fnames = [tmp_path / (hemi + "-myparc") for hemi in ["lh", "rh"]]
      for fname in fnames:
          with pytest.warns(RuntimeWarning, match="subjects_dir"):
              write_labels_to_annot(labels, annot_fname=fname)
  
      # read it back
      labels2 = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir, annot_fname=fnames[0]
      )
      labels22 = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir, annot_fname=fnames[1]
      )
      labels2.extend(labels22)
  
      names = [label.name for label in labels2]
  
      for label in labels:
          idx = names.index(label.name)
          assert_labels_equal(label, labels2[idx])
  
      # same with label-internal colors
      for fname in fnames:
          write_labels_to_annot(
              labels,
              "sample",
              annot_fname=fname,
              overwrite=True,
              subjects_dir=subjects_dir,
          )
      labels3 = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir, annot_fname=fnames[0]
      )
      labels33 = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir, annot_fname=fnames[1]
      )
      labels3.extend(labels33)
      names3 = [label.name for label in labels3]
      for label in labels:
          idx = names3.index(label.name)
          assert_labels_equal(label, labels3[idx])
  
      # make sure we can't overwrite things
      pytest.raises(
          ValueError,
          write_labels_to_annot,
          labels,
          "sample",
          annot_fname=fnames[0],
          subjects_dir=subjects_dir,
      )
  
      # however, this works
      write_labels_to_annot(
          labels,
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
  
      # label without color
      labels_ = labels[:]
      labels_[0] = labels_[0].copy()
      labels_[0].color = None
      write_labels_to_annot(
          labels_,
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
  
      # duplicate color
      labels_[0].color = labels_[2].color
      pytest.raises(
          ValueError,
          write_labels_to_annot,
          labels_,
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
  
      # invalid color inputs
      labels_[0].color = (1.1, 1.0, 1.0, 1.0)
      pytest.raises(
          ValueError,
          write_labels_to_annot,
          labels_,
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
  
      # overlapping labels
      labels_ = labels[:]
      cuneus_lh = labels[6]
      precuneus_lh = labels[50]
      labels_.append(precuneus_lh + cuneus_lh)
      pytest.raises(
          ValueError,
          write_labels_to_annot,
          labels_,
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
  
      # unlabeled vertices
      labels_lh = [
          label for label in labels if label.name.endswith("lh")
      ]
      write_labels_to_annot(
          labels_lh[1:],
          "sample",
          annot_fname=fnames[0],
          overwrite=True,
          subjects_dir=subjects_dir,
      )
      labels_reloaded = read_labels_from_annot(
          "sample", annot_fname=fnames[0], subjects_dir=subjects_dir
      )
      assert_equal(len(labels_lh), len(labels_reloaded))
      label0 = labels_lh[0]
      label1 = labels_reloaded[-1]
      assert_equal(label1.name, "unknown-lh")
      assert np.all(np.isin(label0.vertices, label1.vertices))
  
      # unnamed labels
      labels4 = labels[:]
      labels4[0].name = None
      pytest.raises(
          ValueError,
          write_labels_to_annot,
          labels4,
          annot_fname=fnames[0],
      )
  
imports: import glob
  import os
  import pickle
  import shutil
  from itertools import product
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_array_less, assert_equal
  from scipy import sparse
  from mne import grow_labels, labels_to_stc, morph_labels, random_parcellation, read_label, read_labels_from_annot, read_source_estimate, read_source_spaces, read_surface, spatial_tris_adjacency, split_label, stc_to_label, write_labels_to_annot
  from mne.datasets import testing
  from mne.label import Label, _blend_colors, _load_vert_pos, _n_colors, _read_annot, _read_annot_cands, label_sign_flip, select_sources
  from mne.source_estimate import mesh_edges
  from mne.source_space import SourceSpaces
  from mne.surface import _mesh_borders
  from mne.utils import _record_warnings, get_subjects_dir
label_desc: Use image node_id 15 for calling a global function with example usage: test_write_labels_to_annot(tmp_path) without return types