code: def test_reg_pinv(ndim):
      """Test regularization and inversion of covariance matrix."""
      # create rank-deficient array
      a = np.array([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 1.0]])
      for _ in range(ndim - 2):
          a = a[np.newaxis]
  
      # Test if rank-deficient matrix without regularization throws
      # specific warning
      with pytest.warns(RuntimeWarning, match="deficient"):
          _reg_pinv(a, reg=0.0)
  
      # Test inversion with explicit rank
      a_inv_np = np.linalg.pinv(a, hermitian=True)
      a_inv_mne, loading_factor, rank = _reg_pinv(a, rank=2)
      assert loading_factor == 0
      assert rank == 2
      assert_allclose(a_inv_np, a_inv_mne, atol=1e-14)
  
      # Test inversion with automatic rank detection
      a_inv_mne, _, estimated_rank = _reg_pinv(a, rank=None)
      assert_allclose(a_inv_np, a_inv_mne, atol=1e-14)
      assert estimated_rank == 2
  
      # Test adding regularization
      a_inv_mne, loading_factor, estimated_rank = _reg_pinv(a, reg=2)
      # Since A has a diagonal of all ones, loading_factor should equal the
      # regularization parameter
      assert loading_factor == 2
      # The estimated rank should be that of the non-regularized matrix
      assert estimated_rank == 2
      # Test result against the NumPy version
      a_inv_np = np.linalg.pinv(
          a + loading_factor * np.eye(3), hermitian=True
      )
      assert_allclose(a_inv_np, a_inv_mne, atol=1e-14)
  
      # Test setting rcond
      a_inv_np = np.linalg.pinv(a, rcond=0.5)
      a_inv_mne, _, estimated_rank = _reg_pinv(a, rcond=0.5)
      assert_allclose(a_inv_np, a_inv_mne, atol=1e-14)
      assert estimated_rank == 1
  
      # Test inverting an all zero cov
      a_inv, loading_factor, estimated_rank = _reg_pinv(
          np.zeros((3, 3)), reg=2
      )
      assert_array_equal(a_inv, 0)
      assert loading_factor == 0
      assert estimated_rank == 0
  
imports: from copy import deepcopy
  from datetime import datetime, timezone
  from io import StringIO
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal
  from scipy import sparse
  from mne import pick_types, read_cov, read_evokeds
  from mne._fiff.pick import _picks_by_type
  from mne.epochs import make_fixed_length_epochs
  from mne.io import read_raw_fif
  from mne.time_frequency import tfr_morlet
  from mne.utils import _PCA, _apply_scaling_array, _apply_scaling_cov, _array_equal_nan, _cal_to_julian, _custom_lru_cache, _dt_to_julian, _freq_mask, _get_inst_data, _julian_to_cal, _julian_to_dt, _reg_pinv, _ReuseCycle, _time_mask, _undo_scaling_array, _undo_scaling_cov, compute_corr, create_slices, grand_average, hashfunc, numerics, object_diff, object_hash, object_size, random_permutation, sum_squared
  from mne.utils.numerics import _LRU_CACHE_MAXSIZES, _LRU_CACHES
label_desc: Use image node_id 10 for calling a global function with example usage: test_reg_pinv(ndim) without return types