code: def _restrict_K_to_lbls(labels, K, noise_norm, vertno, pick_ori):
      """Use labels to choose desired sources in the kernel."""
      verts_to_use = [[], []]
      # create mask for K by compiling original vertices from vertno in labels
      for ii in range(len(labels)):
          lab = labels[ii]
          # handle BiHemi labels; ok so long as no overlap w/ single hemi labels
          if lab.hemi == "both":
              l_verts = np.intersect1d(vertno[0], lab.lh.vertices)
              r_verts = np.intersect1d(
                  vertno[1], lab.rh.vertices
              )  # output sorted
              verts_to_use[0] += list(l_verts)
              verts_to_use[1] += list(r_verts)
          else:
              hidx = 0 if lab.hemi == "lh" else 1
              verts = np.intersect1d(vertno[hidx], lab.vertices)
              verts_to_use[hidx] += list(verts)
  
      # check that we don't have overlapping vertices in our labels
      for ii in range(2):
          if len(np.unique(verts_to_use[ii])) != len(verts_to_use[ii]):
              raise RuntimeError(
                  "Labels cannot have overlapping vertices. "
                  "Please select labels with unique vertices "
                  "and try again."
              )
  
      # turn original vertex numbers from vertno into indices for K
      K_mask = np.searchsorted(vertno[0], verts_to_use[0])
      r_kmask = np.searchsorted(vertno[1], verts_to_use[1]) + len(
          vertno[0]
      )
      K_mask = np.hstack((K_mask, r_kmask))
  
      # record which original vertices are at each index in out_K
      hemis = ("lh", "rh")
      ki_keys = [
          (hemis[hi], verts_to_use[hi][ii])
          for hi in range(2)
          for ii in range(len(verts_to_use[hi]))
      ]
      ki_vals = list(range(len(K_mask)))
      k_idxs = dict(zip(ki_keys, ki_vals))
  
      # mask K, handling the orientation issue
      len_allverts = len(vertno[0]) + len(vertno[1])
      if len(K) == len_allverts:
          assert pick_ori == "normal"
          out_K = K[K_mask]
      else:
          # here, K = [x0, y0, z0, x1, y1, z1 ...]
          # we need to drop x, y and z of unused vertices
          assert not pick_ori == "normal", pick_ori
          assert len(K) == 3 * len_allverts, (len(K), len_allverts)
          out_len = len(K_mask) * 3
          out_K = K[0:out_len]  # get the correct-shaped array
          for di in range(3):
              K_pick = K[di::3]
              out_K[di::3] = K_pick[
                  K_mask
              ]  # set correct values for out
  
      out_vertno = verts_to_use
      if noise_norm is not None:
          out_nn = noise_norm[K_mask]
      else:
          out_nn = None
  
      return out_K, out_nn, out_vertno, k_idxs
  
imports: import numpy
  from .._fiff.constants import FIFF
  from .._fiff.pick import pick_info
  from ..baseline import _log_rescale, rescale
  from ..epochs import Epochs
  from ..event import make_fixed_length_events
  from ..evoked import EvokedArray
  from ..fixes import _safe_svd
  from ..label import BiHemiLabel, Label
  from ..parallel import parallel_func
  from ..source_estimate import _make_stc
  from ..time_frequency.multitaper import _compute_mt_params, _mt_spectra, _psd_from_mt, _psd_from_mt_adaptive
  from ..time_frequency.tfr import cwt, morlet
  from ..utils import ProgressBar, _check_option, _pl, _validate_type, logger, verbose
  from .inverse import INVERSE_METHODS, _assemble_kernel, _check_or_prepare, _check_ori, _pick_channels_inverse_operator, _subject_from_inverse, combine_xyz
label_desc: Use image node_id 1 for calling a global function with example usage: _restrict_K_to_lbls(labels, K, noise_norm, vertno, pick_ori) and returns: out_K,  out_nn,  out_vertno,  k_idxs