code: def test_read_labels_from_annot(tmp_path):
      """Test reading labels from FreeSurfer parcellation."""
      pytest.importorskip("nibabel")
      # test some invalid inputs
      pytest.raises(
          ValueError,
          read_labels_from_annot,
          "sample",
          hemi="bla",
          subjects_dir=subjects_dir,
      )
      pytest.raises(
          ValueError,
          read_labels_from_annot,
          "sample",
          annot_fname="bla.annot",
          subjects_dir=subjects_dir,
      )
      with pytest.raises(OSError, match="does not exist"):
          _read_annot_cands("foo")
      with pytest.raises(OSError, match="no candidate"):
          _read_annot(str(tmp_path))
  
      # read labels using hemi specification
      labels_lh = read_labels_from_annot(
          "sample", hemi="lh", subjects_dir=subjects_dir
      )
      for label in labels_lh:
          assert label.name.endswith("-lh")
          assert label.hemi == "lh"
          assert label.color is not None
  
      # read labels using annot_fname
      annot_fname = subjects_dir / "sample" / "label" / "rh.aparc.annot"
      labels_rh = read_labels_from_annot(
          "sample", annot_fname=annot_fname, subjects_dir=subjects_dir
      )
      for label in labels_rh:
          assert label.name.endswith("-rh")
          assert label.hemi == "rh"
          assert label.color is not None
  
      # combine the lh, rh, labels and sort them
      labels_lhrh = list()
      labels_lhrh.extend(labels_lh)
      labels_lhrh.extend(labels_rh)
  
      names = [label.name for label in labels_lhrh]
      labels_lhrh = [
          label for (name, label) in sorted(zip(names, labels_lhrh))
      ]
  
      # read all labels at once
      labels_both = read_labels_from_annot(
          "sample", subjects_dir=subjects_dir
      )
  
      # we have the same result
      _assert_labels_equal(labels_lhrh, labels_both)
  
      # aparc has 68 cortical labels
      assert len(labels_both) == 68
  
      # test regexp
      label = read_labels_from_annot(
          "sample",
          parc="aparc.a2009s",
          regexp="Angu",
          subjects_dir=subjects_dir,
      )[0]
      assert label.name == "G_pariet_inf-Angular-lh"
      # silly, but real regexp:
      label = read_labels_from_annot(
          "sample",
          "aparc.a2009s",
          regexp=".*-.{4,}_.{3,3}-L",
          subjects_dir=subjects_dir,
      )[0]
      assert label.name == "G_oc-temp_med-Lingual-lh"
      with pytest.raises(RuntimeError, match="did not match any of"):
          read_labels_from_annot(
              "sample",
              parc="aparc",
              annot_fname=annot_fname,
              regexp="foo",
              subjects_dir=subjects_dir,
          )
  
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 13 for calling a global function with example usage: test_read_labels_from_annot(tmp_path) without return types