code: def test_make_dics(tmp_path, _load_forward, idx, whiten):
      """Test making DICS beamformer filters."""
      pytest.importorskip("h5io")
      # We only test proper handling of parameters here. Testing the results is
      # done in test_apply_dics_timeseries and test_apply_dics_csd.
  
      fwd_free, fwd_surf, fwd_fixed, fwd_vol = _load_forward
      epochs, _, csd, _, label, vertices, source_ind = _simulate_data(
          fwd_fixed, idx
      )
      with pytest.raises(ValueError, match="several sensor types"):
          make_dics(
              epochs.info, fwd_surf, csd, label=label, pick_ori=None
          )
      if whiten:
          noise_csd, rank = _make_rand_csd(epochs.info, csd)
          assert rank == len(epochs.info["ch_names"]) == 62
      else:
          noise_csd = None
          epochs.pick(picks="grad")
  
      with pytest.raises(
          ValueError, match="Invalid value for the 'pick_ori'"
      ):
          make_dics(
              epochs.info,
              fwd_fixed,
              csd,
              pick_ori="notexistent",
              noise_csd=noise_csd,
          )
      with pytest.raises(ValueError, match="rank, if str"):
          make_dics(
              epochs.info,
              fwd_fixed,
              csd,
              rank="foo",
              noise_csd=noise_csd,
          )
      with pytest.raises(TypeError, match="rank must be"):
          make_dics(
              epochs.info, fwd_fixed, csd, rank=1.0, noise_csd=noise_csd
          )
  
      # Test if fixed forward operator is detected when picking normal
      # orientation
      with pytest.raises(
          ValueError, match="forward operator with free ori"
      ):
          make_dics(
              epochs.info,
              fwd_fixed,
              csd,
              pick_ori="normal",
              noise_csd=noise_csd,
          )
  
      # Test if non-surface oriented forward operator is detected when picking
      # normal orientation
      with pytest.raises(
          ValueError, match="oriented in surface coordinates"
      ):
          make_dics(
              epochs.info,
              fwd_free,
              csd,
              pick_ori="normal",
              noise_csd=noise_csd,
          )
  
      # Test if volume forward operator is detected when picking normal
      # orientation
      with pytest.raises(
          ValueError, match="oriented in surface coordinates"
      ):
          make_dics(
              epochs.info,
              fwd_vol,
              csd,
              pick_ori="normal",
              noise_csd=noise_csd,
          )
  
      # Test invalid combinations of parameters
      with pytest.raises(
          ValueError, match="reduce_rank cannot be used with"
      ):
          make_dics(
              epochs.info,
              fwd_free,
              csd,
              inversion="single",
              reduce_rank=True,
              noise_csd=noise_csd,
          )
      # TODO: Restore this?
      # with pytest.raises(ValueError, match='not stable with depth'):
      #     make_dics(epochs.info, fwd_free, csd, weight_norm='unit-noise-gain',
      #               inversion='single', depth=None)
  
      # Sanity checks on the returned filters
      n_freq = len(csd.frequencies)
      vertices = np.intersect1d(
          label.vertices, fwd_free["src"][0]["vertno"]
      )
      n_verts = len(vertices)
      n_orient = 3
  
      n_channels = len(epochs.ch_names)
      # Test return values
      weight_norm = "unit-noise-gain"
      inversion = "single"
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori=None,
          weight_norm=weight_norm,
          depth=None,
          real_filter=False,
          noise_csd=noise_csd,
          inversion=inversion,
      )
      assert filters["weights"].shape == (
          n_freq,
          n_verts * n_orient,
          n_channels,
      )
      assert np.iscomplexobj(filters["weights"])
      assert filters["csd"].ch_names == epochs.ch_names
      assert isinstance(filters["csd"], CrossSpectralDensity)
      assert filters["ch_names"] == epochs.ch_names
      assert_array_equal(filters["proj"], np.eye(n_channels))
      assert_array_equal(filters["vertices"][0], vertices)
      assert_array_equal(
          filters["vertices"][1], []
      )  # Label was on the LH
      assert filters["subject"] == fwd_free["src"]._subject
      assert filters["pick_ori"] is None
      assert filters["is_free_ori"]
      assert filters["inversion"] == inversion
      assert filters["weight_norm"] == weight_norm
      assert "DICS" in repr(filters)
      assert 'subject "sample"' in repr(filters)
      assert str(len(vertices)) in repr(filters)
      assert str(n_channels) in repr(filters)
      assert "rank" not in repr(filters)
      _, noise_cov = _prepare_noise_csd(
          csd, noise_csd, real_filter=False
      )
      _, _, _, _, G, _, _, _ = _prepare_beamformer_input(
          epochs.info,
          fwd_surf,
          label,
          "vector",
          combine_xyz=False,
          exp=None,
          noise_cov=noise_cov,
      )
      G.shape = (n_channels, n_verts, n_orient)
      G = G.transpose(1, 2, 0).conj()  # verts, orient, ch
      _assert_weight_norm(filters, G)
  
      inversion = "matrix"
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori=None,
          weight_norm=weight_norm,
          depth=None,
          noise_csd=noise_csd,
          inversion=inversion,
      )
      _assert_weight_norm(filters, G)
  
      weight_norm = "unit-noise-gain-invariant"
      inversion = "single"
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori=None,
          weight_norm=weight_norm,
          depth=None,
          noise_csd=noise_csd,
          inversion=inversion,
      )
      _assert_weight_norm(filters, G)
  
      # Test picking orientations. Also test weight norming under these different
      # conditions.
      weight_norm = "unit-noise-gain"
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori="normal",
          weight_norm=weight_norm,
          depth=None,
          noise_csd=noise_csd,
          inversion=inversion,
      )
      n_orient = 1
      assert filters["weights"].shape == (
          n_freq,
          n_verts * n_orient,
          n_channels,
      )
      assert not filters["is_free_ori"]
      _assert_weight_norm(filters, G)
  
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori="max-power",
          weight_norm=weight_norm,
          depth=None,
          noise_csd=noise_csd,
          inversion=inversion,
      )
      n_orient = 1
      assert filters["weights"].shape == (
          n_freq,
          n_verts * n_orient,
          n_channels,
      )
      assert not filters["is_free_ori"]
      _assert_weight_norm(filters, G)
  
      # From here on, only work on a single frequency
      csd = csd[0]
  
      # Test using a real-valued filter
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori="normal",
          real_filter=True,
          noise_csd=noise_csd,
      )
      assert not np.iscomplexobj(filters["weights"])
  
      # Test forward normalization. When inversion='single', the power of a
      # unit-noise CSD should be 1, even without weight normalization.
      if not whiten:
          csd_noise = csd.copy()
          inds = np.triu_indices(csd.n_channels)
          # Using [:, :] syntax for in-place broadcasting
          csd_noise._data[:, :] = np.eye(csd.n_channels)[inds][
              :, np.newaxis
          ]
          filters = make_dics(
              epochs.info,
              fwd_surf,
              csd_noise,
              label=label,
              weight_norm=None,
              depth=1.0,
              noise_csd=noise_csd,
              inversion="single",
          )
          w = filters["weights"][0][:3]
          assert_allclose(
              np.diag(w.dot(w.conjugate().T)), 1.0, rtol=1e-6, atol=0
          )
  
      # Test turning off both forward and weight normalization
      filters = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          weight_norm=None,
          depth=None,
          noise_csd=noise_csd,
      )
      w = filters["weights"][0][:3]
      assert not np.allclose(
          np.diag(w.dot(w.conjugate().T)), 1.0, rtol=1e-2, atol=0
      )
  
      # Test neural-activity-index weight normalization. It should be a scaled
      # version of the unit-noise-gain beamformer.
      filters_nai = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori="max-power",
          weight_norm="nai",
          depth=None,
          noise_csd=noise_csd,
      )
      w_nai = filters_nai["weights"][0]
      filters_ung = make_dics(
          epochs.info,
          fwd_surf,
          csd,
          label=label,
          pick_ori="max-power",
          weight_norm="unit-noise-gain",
          depth=None,
          noise_csd=noise_csd,
      )
      w_ung = filters_ung["weights"][0]
      assert_allclose(
          np.corrcoef(np.abs(w_nai).ravel(), np.abs(w_ung).ravel()),
          1,
          atol=1e-7,
      )
  
      # Test whether spatial filter contains src_type
      assert "src_type" in filters
  
      fname = tmp_path / "filters-dics.h5"
      filters.save(fname)
      filters_read = read_beamformer(fname)
      assert isinstance(filters, Beamformer)
      assert isinstance(filters_read, Beamformer)
      for key in [
          "tmin",
          "tmax",
      ]:  # deal with strictness of object_diff
          setattr(
              filters["csd"],
              key,
              np.float64(getattr(filters["csd"], key)),
          )
      assert object_diff(filters, filters_read) == ""
  
imports: import copy
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal, assert_array_less
  import mne
  from mne import pick_types
  from mne._fiff.constants import FIFF
  from mne._fiff.pick import pick_info
  from mne.beamformer import Beamformer, apply_dics, apply_dics_csd, apply_dics_epochs, apply_dics_tfr_epochs, make_dics, read_beamformer
  from mne.beamformer._compute_beamformer import _prepare_beamformer_input
  from mne.beamformer._dics import _prepare_noise_csd
  from mne.beamformer.tests.test_lcmv import _assert_weight_norm
  from mne.datasets import testing
  from mne.io import read_info
  from mne.proj import compute_proj_evoked, make_projector
  from mne.surface import _compute_nearest
  from mne.time_frequency import CrossSpectralDensity, EpochsTFR, csd_morlet, csd_tfr
  from mne.time_frequency.csd import _sym_mat_to_vector
  from mne.transforms import apply_trans, invert_transform
  from mne.utils import catch_logging, object_diff
label_desc: Use image node_id 5 for calling a global function with example usage: test_make_dics(tmp_path, _load_forward, idx, whiten) without return types