code: def test_covariances_coherence(coh, rndstate):
      """Test coherence"""
      n_channels, n_times = 3, 2048
      x = rndstate.randn(n_channels, n_times)
  
      c, freqs = coherence(x, fs=128, fmin=3, fmax=40, coh=coh)
      assert c.shape[0] == c.shape[1] == n_channels
      assert c.shape[-1] == freqs.shape[0]
      # test if coherence in [0,1]
      assert np.all((0.0 <= c) & (c <= 1.0))
  
      # test equivalence between pyriemann and scipy for ordinary coherence
      if coh == "ordinary":
          fs, window, overlap = 128, 256, 0.75
          coh_pr, freqs_pr = coherence(
              x, fs=fs, window=window, overlap=overlap
          )
          freqs_sp, coh_sp = coherence_sp(
              x[0],
              x[1],
              fs=fs,
              nperseg=window,
              noverlap=int(overlap * window),
              window=np.hanning(window),
              detrend=False,
          )
          # compare frequencies
          assert_array_almost_equal(freqs_pr, freqs_sp, 6)
          # compare coherence
          assert_array_almost_equal(coh_pr[0, 1], coh_sp, 6)
  
      if coh == "lagged":
          with pytest.warns(
              UserWarning
          ):  # not defined for DC and Nyquist bins
              coherence(x, coh=coh)
          with pytest.warns(
              UserWarning
          ):  # not defined for DC and Nyquist bins
              coherence(x, fs=64, coh=coh)
      else:
          coherence(x, coh=coh)
          coherence(x, fs=64, coh=coh)
  
      # test statistical properties of coherence between phase shifted channels
      fs, ft, n_periods = 16, 4, 20
      t = np.arange(0, n_periods, 1 / fs)
      n_times = t.shape[0]
  
      x, noise = np.empty((4, len(t))), 1e-9
      # reference channel: a pure sine + small noise (to avoid nan or inf)
      x[0] = np.sin(2 * np.pi * ft * t) + noise * rndstate.randn(
          (n_times)
      )
      # pi/4 shifted channel = pi/4 lagged phase
      x[1] = np.sin(
          2 * np.pi * ft * t + np.pi / 4
      ) + noise * rndstate.randn((n_times))
      # pi/2 shifted channel = quadrature phase
      x[2] = np.sin(
          2 * np.pi * ft * t + np.pi / 2
      ) + noise * rndstate.randn((n_times))
      # pi shifted channel = opposite phase
      x[3] = np.sin(
          2 * np.pi * ft * t + np.pi
      ) + noise * rndstate.randn((n_times))
  
      c, freqs = coherence(
          x,
          fs=fs,
          fmin=1,
          fmax=fs / 2 - 1,
          window=fs,
          overlap=0.5,
          coh=coh,
      )
      foi = freqs == ft
  
      if coh == "ordinary":
          # ord coh equal 1 between ref and all other channels
          assert_array_almost_equal(
              c[..., foi], np.ones_like(c[..., foi])
          )
  
      elif coh == "instantaneous":
          # inst coh equal 0.5 between ref and pi/4 lagged phase channels
          assert c[0, 1, foi] == pytest.approx(0.5)
          # inst coh equal 0 between ref and quadrature phase channels
          assert c[0, 2, foi] == pytest.approx(0.0)
          # inst coh equal 1 between ref and opposite phase channels
          assert c[0, 3, foi] == pytest.approx(1.0)
  
      elif coh == "lagged":
          # lagged coh equal 1 between ref and quadrature phase channels
          assert c[0, 2, foi] == pytest.approx(1.0)
          # lagged coh equal 0 between ref and opposite phase channels
          assert c[0, 3, foi] == pytest.approx(0.0, abs=1e-4)
  
      elif coh == "imaginary":
          # imag coh equal 0.5 between ref and pi/4 lagged phase channels
          assert c[0, 1, foi] == pytest.approx(0.5)
          # imag coh equal 1 between ref and quadrature phase channels
          assert c[0, 2, foi] == pytest.approx(1.0)
          # imag coh equal 0 between ref and opposite phase channels
          assert c[0, 3, foi] == pytest.approx(0.0)
  
imports: from numpy.testing import assert_array_almost_equal
  import numpy
  from scipy.linalg import block_diag
  from scipy.signal import welch, csd, coherence
  from sklearn.covariance import empirical_covariance
  import pytest
  from pyriemann.utils.covariance import covariances, covariances_EP, covariances_X, eegtocov, cross_spectrum, cospectrum, coherence, normalize, get_nondiag_weight, block_covariances, _complex_estimator
  from pyriemann.utils.test import is_real, is_hermitian, is_sym_pos_def, is_herm_pos_def
label_desc: Use image node_id 13 for calling a global function with example usage: test_covariances_coherence(coh, rndstate) without return types