code: def test_label_center_of_mass():
      """Test computing the center of mass of a label."""
      pytest.importorskip("nibabel")
      stc = read_source_estimate(stc_fname)
      stc.lh_data[:] = 0
      vertex_stc = stc.center_of_mass(
          "sample", subjects_dir=subjects_dir
      )[0]
      assert_equal(vertex_stc, 124791)
      label = Label(
          stc.vertices[1],
          pos=None,
          values=stc.rh_data.mean(axis=1),
          hemi="rh",
          subject="sample",
      )
      vertex_label = label.center_of_mass(subjects_dir=subjects_dir)
      assert_equal(vertex_label, vertex_stc)
  
      labels = read_labels_from_annot(
          "sample", parc="aparc.a2009s", subjects_dir=subjects_dir
      )
      src = read_source_spaces(src_fname)
      # Try a couple of random ones, one from left and one from right
      # Visually verified in about the right place using mne_analyze
      for label, expected in zip(
          [labels[2], labels[3], labels[-5]], [141162, 145221, 55979]
      ):
          label.values[:] = -1
          pytest.raises(
              ValueError,
              label.center_of_mass,
              subjects_dir=subjects_dir,
          )
          label.values[:] = 0
          pytest.raises(
              ValueError,
              label.center_of_mass,
              subjects_dir=subjects_dir,
          )
          label.values[:] = 1
          assert_equal(
              label.center_of_mass(subjects_dir=subjects_dir), expected
          )
          assert_equal(
              label.center_of_mass(
                  subjects_dir=subjects_dir,
                  restrict_vertices=label.vertices,
              ),
              expected,
          )
          # restrict to source space
          idx = 0 if label.hemi == "lh" else 1
          # this simple nearest version is not equivalent, but is probably
          # close enough for many labels (including the test ones):
          pos = label.pos[np.where(label.vertices == expected)[0][0]]
          pos = src[idx]["rr"][src[idx]["vertno"]] - pos
          pos = np.argmin(np.sum(pos * pos, axis=1))
          src_expected = src[idx]["vertno"][pos]
          # see if we actually get the same one
          src_restrict = np.intersect1d(
              label.vertices, src[idx]["vertno"]
          )
          assert_equal(
              label.center_of_mass(
                  subjects_dir=subjects_dir,
                  restrict_vertices=src_restrict,
              ),
              src_expected,
          )
          assert_equal(
              label.center_of_mass(
                  subjects_dir=subjects_dir, restrict_vertices=src
              ),
              src_expected,
          )
      # degenerate cases
      pytest.raises(
          ValueError,
          label.center_of_mass,
          subjects_dir=subjects_dir,
          restrict_vertices="foo",
      )
      pytest.raises(
          TypeError,
          label.center_of_mass,
          subjects_dir=subjects_dir,
          surf=1,
      )
      pytest.raises(
          OSError,
          label.center_of_mass,
          subjects_dir=subjects_dir,
          surf="foo",
      )
  
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 22 for calling a global function with example usage: test_label_center_of_mass() without return types