code: def test_make_lcmv_bem(tmp_path, reg, proj, kind):
      """Test LCMV with evoked data and single trials."""
      pytest.importorskip("h5io")
      (
          raw,
          epochs,
          evoked,
          data_cov,
          noise_cov,
          label,
          forward,
          forward_surf_ori,
          forward_fixed,
          forward_vol,
      ) = _get_data(proj=proj)
  
      if kind == "surface":
          fwd = forward
      else:
          fwd = forward_vol
          assert kind == "volume"
  
      filters = make_lcmv(
          evoked.info, fwd, data_cov, reg=reg, noise_cov=noise_cov
      )
      stc = apply_lcmv(evoked, filters)
      stc.crop(0.02, None)
  
      # Smoke test for label= support for surfaces only
      label = mne.read_label(fname_label)
      if kind == "volume":
          ctx = pytest.raises(ValueError, match="volume source space")
      else:
          ctx = nullcontext()
      with ctx:
          make_lcmv(
              evoked.info,
              fwd,
              data_cov,
              reg=reg,
              noise_cov=noise_cov,
              label=label,
          )
  
      stc_pow = np.sum(np.abs(stc.data), axis=1)
      idx = np.argmax(stc_pow)
      max_stc = stc.data[idx]
      tmax = stc.times[np.argmax(max_stc)]
  
      assert 0.08 < tmax < 0.15, tmax
      assert 0.9 < np.max(max_stc) < 3.5, np.max(max_stc)
  
      if kind == "surface":
          # Test picking normal orientation (surface source space only).
          filters = make_lcmv(
              evoked.info,
              forward_surf_ori,
              data_cov,
              reg=reg,
              noise_cov=noise_cov,
              pick_ori="normal",
              weight_norm=None,
          )
          stc_normal = apply_lcmv(evoked, filters)
          stc_normal.crop(0.02, None)
  
          stc_pow = np.sum(np.abs(stc_normal.data), axis=1)
          idx = np.argmax(stc_pow)
          max_stc = stc_normal.data[idx]
          tmax = stc_normal.times[np.argmax(max_stc)]
  
          lower = 0.04 if proj else 0.025
          assert lower < tmax < 0.14, tmax
          lower = 3e-7 if proj else 2e-7
          assert lower < np.max(max_stc) < 3e-6, np.max(max_stc)
  
          # No weight normalization was applied, so the amplitude of normal
          # orientation results should always be smaller than free
          # orientation results.
          assert (np.abs(stc_normal.data) <= stc.data).all()
  
      # Test picking source orientation maximizing output source power
      filters = make_lcmv(
          evoked.info,
          fwd,
          data_cov,
          reg=reg,
          noise_cov=noise_cov,
          pick_ori="max-power",
      )
      stc_max_power = apply_lcmv(evoked, filters)
      stc_max_power.crop(0.02, None)
      stc_pow = np.sum(np.abs(stc_max_power.data), axis=1)
      idx = np.argmax(stc_pow)
      max_stc = np.abs(stc_max_power.data[idx])
      tmax = stc.times[np.argmax(max_stc)]
  
      lower = 0.08 if proj else 0.04
      assert lower < tmax < 0.15, tmax
      assert 0.8 < np.max(max_stc) < 3.0, np.max(max_stc)
  
      stc_max_power.data[:, :] = np.abs(stc_max_power.data)
  
      if kind == "surface":
          # Maximum output source power orientation results should be
          # similar to free orientation results in areas with channel
          # coverage
          label = mne.read_label(fname_label)
          mean_stc = stc.extract_label_time_course(
              label, fwd["src"], mode="mean"
          )
          mean_stc_max_pow = stc_max_power.extract_label_time_course(
              label, fwd["src"], mode="mean"
          )
          assert_array_less(np.abs(mean_stc - mean_stc_max_pow), 1.0)
  
      # Test if spatial filter contains src_type
      assert filters["src_type"] == kind
  
      # __repr__
      assert len(evoked.ch_names) == 22
      assert len(evoked.info["projs"]) == (3 if proj else 0)
      assert len(evoked.info["bads"]) == 2
      rank = 17 if proj else 20
      assert "LCMV" in repr(filters)
      assert "unknown subject" not in repr(filters)
      assert f'{fwd["nsource"]} vert' in repr(filters)
      assert "20 ch" in repr(filters)
      assert "rank %s" % rank in repr(filters)
  
      # I/O
      fname = tmp_path / "filters.h5"
      with pytest.warns(RuntimeWarning, match="-lcmv.h5"):
          filters.save(fname)
      filters_read = read_beamformer(fname)
      assert isinstance(filters, Beamformer)
      assert isinstance(filters_read, Beamformer)
      # deal with object_diff strictness
      filters_read["rank"] = int(filters_read["rank"])
      filters["rank"] = int(filters["rank"])
      assert object_diff(filters, filters_read) == ""
  
      if kind != "surface":
          return
  
      # Test if fixed forward operator is detected when picking normal or
      # max-power orientation
      pytest.raises(
          ValueError,
          make_lcmv,
          evoked.info,
          forward_fixed,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
          pick_ori="normal",
      )
      pytest.raises(
          ValueError,
          make_lcmv,
          evoked.info,
          forward_fixed,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
          pick_ori="max-power",
      )
  
      # Test if non-surface oriented forward operator is detected when picking
      # normal orientation
      pytest.raises(
          ValueError,
          make_lcmv,
          evoked.info,
          forward,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
          pick_ori="normal",
      )
  
      # Test if volume forward operator is detected when picking normal
      # orientation
      pytest.raises(
          ValueError,
          make_lcmv,
          evoked.info,
          forward_vol,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
          pick_ori="normal",
      )
  
      # Test if missing of noise covariance matrix is detected when more than
      # one channel type is present in the data
      pytest.raises(
          ValueError,
          make_lcmv,
          evoked.info,
          forward_vol,
          data_cov=data_cov,
          reg=0.01,
          noise_cov=None,
          pick_ori="max-power",
      )
  
      # Test if wrong channel selection is detected in application of filter
      evoked_ch = deepcopy(evoked)
      evoked_ch.pick(evoked_ch.ch_names[1:])
      filters = make_lcmv(
          evoked.info,
          forward_vol,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
      )
  
      # Test if discrepancies in channel selection of data and fwd model are
      # handled correctly in apply_lcmv
      # make filter with data where first channel was removed
      filters = make_lcmv(
          evoked_ch.info,
          forward_vol,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
      )
      # applying that filter to the full data set should automatically exclude
      # this channel from the data
      # also test here that no warnings are thrown - implemented to check whether
      # src should not be None warning occurs
      stc = apply_lcmv(evoked, filters)
  
      # the result should be equal to applying this filter to a dataset without
      # this channel:
      stc_ch = apply_lcmv(evoked_ch, filters)
      assert_array_almost_equal(stc.data, stc_ch.data)
  
      # Test if non-matching SSP projection is detected in application of filter
      if proj:
          raw_proj = raw.copy().del_proj()
          with pytest.raises(
              ValueError, match="do not match the projections"
          ):
              apply_lcmv_raw(raw_proj, filters)
  
      # Test apply_lcmv_raw
      use_raw = raw.copy().crop(0, 1)
      stc = apply_lcmv_raw(use_raw, filters)
      assert_allclose(stc.times, use_raw.times)
      assert_array_equal(
          stc.vertices[0], forward_vol["src"][0]["vertno"]
      )
  
      # Test if spatial filter contains src_type
      assert "src_type" in filters
  
      # check whether a filters object without src_type throws expected warning
      del filters["src_type"]  # emulate 0.16 behaviour to cause warning
      with pytest.warns(
          RuntimeWarning,
          match="spatial filter does not contain " "src_type",
      ):
          apply_lcmv(evoked, filters)
  
      # Now test single trial using fixed orientation forward solution
      # so we can compare it to the evoked solution
      filters = make_lcmv(
          epochs.info,
          forward_fixed,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
      )
      stcs = apply_lcmv_epochs(epochs, filters)
      stcs_ = apply_lcmv_epochs(epochs, filters, return_generator=True)
      assert_array_equal(stcs[0].data, next(stcs_).data)
  
      epochs.drop_bad()
      assert len(epochs.events) == len(stcs)
  
      # average the single trial estimates
      stc_avg = np.zeros_like(stcs[0].data)
      for this_stc in stcs:
          stc_avg += this_stc.data
      stc_avg /= len(stcs)
  
      # compare it to the solution using evoked with fixed orientation
      filters = make_lcmv(
          evoked.info,
          forward_fixed,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
      )
      stc_fixed = apply_lcmv(evoked, filters)
      assert_array_almost_equal(stc_avg, stc_fixed.data)
  
      # use a label so we have few source vertices and delayed computation is
      # not used
      filters = make_lcmv(
          epochs.info,
          forward_fixed,
          data_cov,
          reg=0.01,
          noise_cov=noise_cov,
          label=label,
      )
      stcs_label = apply_lcmv_epochs(epochs, filters)
  
      assert_array_almost_equal(
          stcs_label[0].data, stcs[0].in_label(label).data
      )
  
      # Test condition where the filters weights are zero. There should not be
      # any divide-by-zero errors
      zero_cov = data_cov.copy()
      zero_cov["data"][:] = 0
      filters = make_lcmv(
          epochs.info,
          forward_fixed,
          zero_cov,
          reg=0.01,
          noise_cov=noise_cov,
      )
      assert_array_equal(filters["weights"], 0)
  
      # Test condition where one channel type is picked
      # (avoid "grad data rank (13) did not match the noise rank (None)")
      data_cov_grad = pick_channels_cov(
          data_cov,
          [
              ch_name
              for ch_name in epochs.info["ch_names"]
              if ch_name.endswith(("2", "3"))
          ],
          ordered=False,
      )
      assert len(data_cov_grad["names"]) > 4
      make_lcmv(
          epochs.info,
          forward_fixed,
          data_cov_grad,
          reg=0.01,
          noise_cov=noise_cov,
      )
  
imports: from contextlib import nullcontext
  from copy import deepcopy
  from inspect import signature
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_array_less
  from scipy import linalg
  from scipy.spatial.distance import cdist
  import mne
  from mne import EvokedArray, VolSourceEstimate, VolVectorSourceEstimate, compute_rank, convert_forward_solution, pick_channels_cov, read_forward_solution, read_vectorview_selection
  from mne._fiff.compensator import set_current_comp
  from mne._fiff.constants import FIFF
  from mne.beamformer import Beamformer, apply_lcmv, apply_lcmv_cov, apply_lcmv_epochs, apply_lcmv_raw, make_dics, make_lcmv, read_beamformer
  from mne.beamformer._compute_beamformer import _prepare_beamformer_input
  from mne.datasets import testing
  from mne.minimum_norm import apply_inverse, make_inverse_operator
  from mne.minimum_norm.tests.test_inverse import _assert_free_ori_match
  from mne.simulation import simulate_evoked
  from mne.transforms import apply_trans, invert_transform
  from mne.utils import _record_warnings, catch_logging, object_diff
label_desc: Use image node_id 4 for calling a global function with example usage: test_make_lcmv_bem(tmp_path, reg, proj, kind) without return types