code: def test_restrict_forward_to_label(tmp_path):
      """Test restriction of source space to label."""
      fwd = read_forward_solution(fname_meeg)
      fwd = convert_forward_solution(
          fwd, surf_ori=True, force_fixed=True, use_cps=True
      )
      fwd = pick_types_forward(fwd, meg=True)
  
      labels = ["Aud-lh", "Vis-rh"]
      label_lh = read_label(label_path / (labels[0] + ".label"))
      label_rh = read_label(label_path / (labels[1] + ".label"))
  
      fwd_out = restrict_forward_to_label(fwd, [label_lh, label_rh])
  
      src_sel_lh = np.intersect1d(
          fwd["src"][0]["vertno"], label_lh.vertices
      )
      src_sel_lh = np.searchsorted(fwd["src"][0]["vertno"], src_sel_lh)
      vertno_lh = fwd["src"][0]["vertno"][src_sel_lh]
  
      nuse_lh = fwd["src"][0]["nuse"]
      src_sel_rh = np.intersect1d(
          fwd["src"][1]["vertno"], label_rh.vertices
      )
      src_sel_rh = np.searchsorted(fwd["src"][1]["vertno"], src_sel_rh)
      vertno_rh = fwd["src"][1]["vertno"][src_sel_rh]
      src_sel_rh += nuse_lh
  
      assert_equal(
          fwd_out["sol"]["ncol"], len(src_sel_lh) + len(src_sel_rh)
      )
      assert_equal(fwd_out["src"][0]["nuse"], len(src_sel_lh))
      assert_equal(fwd_out["src"][1]["nuse"], len(src_sel_rh))
      assert_equal(fwd_out["src"][0]["vertno"], vertno_lh)
      assert_equal(fwd_out["src"][1]["vertno"], vertno_rh)
  
      fwd = read_forward_solution(fname_meeg)
      fwd = pick_types_forward(fwd, meg=True)
  
      labels = ["Aud-lh", "Vis-rh"]
      label_lh = read_label(label_path / (labels[0] + ".label"))
      label_rh = read_label(label_path / (labels[1] + ".label"))
  
      fwd_out = restrict_forward_to_label(fwd, [label_lh, label_rh])
  
      src_sel_lh = np.intersect1d(
          fwd["src"][0]["vertno"], label_lh.vertices
      )
      src_sel_lh = np.searchsorted(fwd["src"][0]["vertno"], src_sel_lh)
      vertno_lh = fwd["src"][0]["vertno"][src_sel_lh]
  
      nuse_lh = fwd["src"][0]["nuse"]
      src_sel_rh = np.intersect1d(
          fwd["src"][1]["vertno"], label_rh.vertices
      )
      src_sel_rh = np.searchsorted(fwd["src"][1]["vertno"], src_sel_rh)
      vertno_rh = fwd["src"][1]["vertno"][src_sel_rh]
      src_sel_rh += nuse_lh
  
      assert_equal(
          fwd_out["sol"]["ncol"],
          3 * (len(src_sel_lh) + len(src_sel_rh)),
      )
      assert_equal(fwd_out["src"][0]["nuse"], len(src_sel_lh))
      assert_equal(fwd_out["src"][1]["nuse"], len(src_sel_rh))
      assert_equal(fwd_out["src"][0]["vertno"], vertno_lh)
      assert_equal(fwd_out["src"][1]["vertno"], vertno_rh)
  
      # Test saving the restricted forward object. This only works if all fields
      # are properly accounted for.
      fname_copy = tmp_path / "copy-fwd.fif"
      write_forward_solution(fname_copy, fwd_out, overwrite=True)
      fwd_out_read = read_forward_solution(fname_copy)
      assert_forward_allclose(fwd_out, fwd_out_read)
  
imports: import gc
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal
  from mne import SourceEstimate, VectorSourceEstimate, apply_forward, apply_forward_raw, average_forward_solutions, convert_forward_solution, pick_types_forward, read_evokeds, read_forward_solution, write_forward_solution
  from mne._fiff.pick import pick_channels_forward
  from mne.channels import equalize_channels
  from mne.datasets import testing
  from mne.forward import Forward, compute_depth_prior, compute_orient_prior, is_fixed_orient, restrict_forward_to_label, restrict_forward_to_stc
  from mne.io import read_info
  from mne.label import read_label
  from mne.utils import requires_mne, run_subprocess
label_desc: Use image node_id 6 for calling a global function with example usage: test_restrict_forward_to_label(tmp_path) without return types