code: def _assert_weight_norm(filters, G):
      """Check the result of the chosen weight normalization strategy."""
      weights, max_power_ori = (
          filters["weights"],
          filters["max_power_ori"],
      )
  
      # Make the dimensions of the weight matrix equal for both DICS (which
      # defines weights for multiple frequencies) and LCMV (which does not).
      if filters["kind"] == "LCMV":
          weights = weights[np.newaxis]
          if max_power_ori is not None:
              max_power_ori = max_power_ori[np.newaxis]
      if max_power_ori is not None:
          max_power_ori = max_power_ori[..., np.newaxis]
  
      weight_norm = filters["weight_norm"]
      inversion = filters["inversion"]
      n_channels = weights.shape[2]
  
      if inversion == "matrix":
          # Dipoles are grouped in groups with size n_orient
          n_sources = filters["n_sources"]
          n_orient = 3 if filters["is_free_ori"] else 1
      elif inversion == "single":
          # Every dipole is treated as a unique source
          n_sources = weights.shape[1]
          n_orient = 1
  
      for wi, w in enumerate(weights):
          w = w.reshape(n_sources, n_orient, n_channels)
  
          # Compute leadfield in the direction chosen during the computation of
          # the beamformer.
          if filters["pick_ori"] == "max-power":
              use_G = np.sum(
                  G * max_power_ori[wi], axis=1, keepdims=True
              )
          elif filters["pick_ori"] == "normal":
              use_G = G[:, -1:]
          else:
              use_G = G
          if inversion == "single":
              # Every dipole is treated as a unique source
              use_G = use_G.reshape(n_sources, 1, n_channels)
          assert (
              w.shape
              == use_G.shape
              == (n_sources, n_orient, n_channels)
          )
  
          # Test weight normalization scheme
          got = np.matmul(w, w.conj().swapaxes(-2, -1))
          desired = np.repeat(
              np.eye(n_orient)[np.newaxis], w.shape[0], axis=0
          )
          if n_orient == 3 and weight_norm in (
              "unit-noise-gain",
              "nai",
          ):
              # only the diagonal is correct!
              assert not np.allclose(got, desired, atol=1e-7)
              got = got.reshape(n_sources, -1)[:, :: n_orient + 1]
              desired = np.ones_like(got)
          if (
              weight_norm == "nai"
          ):  # additional scale factor, should be fixed
              atol = 1e-7 * got.flat[0]
              desired *= got.flat[0]
          else:
              atol = 1e-7
          assert_allclose(
              got, desired, atol=atol, err_msg="w @ w.conj().T = I"
          )
  
          # Check that the result here is a diagonal matrix for Sekihara
          if (
              n_orient > 1
              and weight_norm != "unit-noise-gain-invariant"
          ):
              got = w @ use_G.swapaxes(-2, -1)
              diags = np.diagonal(got, 0, -2, -1)
              want = np.apply_along_axis(np.diagflat, 1, diags)
              atol = np.mean(diags).real * 1e-12
              assert_allclose(
                  got, want, atol=atol, err_msg="G.T @ w = θI"
              )
  
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 16 for calling a global function with example usage: _assert_weight_norm(filters, G) without return types