code: def test_clean_confounds():
      signals, noises, confounds = generate_signals(
          n_features=41, n_confounds=5, length=45
      )
      # No signal: output must be zero.
      eps = np.finfo(np.float64).eps
      noises1 = noises.copy()
      cleaned_signals = nisignal.clean(
          noises, confounds=confounds, detrend=True, standardize=False
      )
      assert abs(cleaned_signals).max() < 100.0 * eps
      # clean should not modify inputs
      assert np.array_equal(noises, noises1)
  
      # With signal: output must be orthogonal to confounds
      cleaned_signals = nisignal.clean(
          signals + noises,
          confounds=confounds,
          detrend=False,
          standardize=True,
      )
      assert (
          abs(np.dot(confounds.T, cleaned_signals)).max() < 1000.0 * eps
      )
  
      # Same output when a constant confound is added
      confounds1 = np.hstack((np.ones((45, 1)), confounds))
      cleaned_signals1 = nisignal.clean(
          signals + noises,
          confounds=confounds1,
          detrend=False,
          standardize=True,
      )
      np.testing.assert_almost_equal(cleaned_signals1, cleaned_signals)
  
      # Test detrending. No trend should exist in the output.
      # Use confounds with a trend.
      temp = confounds.T
      temp += np.arange(confounds.shape[0])
  
      cleaned_signals = nisignal.clean(
          signals + noises,
          confounds=confounds,
          detrend=False,
          standardize=False,
      )
      coeffs = np.polyfit(
          np.arange(cleaned_signals.shape[0]), cleaned_signals, 1
      )
      assert (abs(coeffs) > 1e-3).any()  # trends remain
  
      cleaned_signals = nisignal.clean(
          signals + noises,
          confounds=confounds,
          detrend=True,
          standardize=False,
      )
      coeffs = np.polyfit(
          np.arange(cleaned_signals.shape[0]), cleaned_signals, 1
      )
      assert (abs(coeffs) < 1000.0 * eps).all()  # trend removed
  
      # Test no-op
      input_signals = 10 * signals
      cleaned_signals = nisignal.clean(
          input_signals, detrend=False, standardize=False
      )
      np.testing.assert_almost_equal(cleaned_signals, input_signals)
  
      cleaned_signals = nisignal.clean(
          input_signals, detrend=False, standardize=True
      )
      np.testing.assert_almost_equal(
          cleaned_signals.var(axis=0), np.ones(cleaned_signals.shape[1])
      )
  
      # Test with confounds read from a file. Smoke test only (result has
      # no meaning).
      current_dir = os.path.split(__file__)[0]
  
      signals, _, confounds = generate_signals(
          n_features=41, n_confounds=3, length=20
      )
      filename1 = os.path.join(current_dir, "data", "spm_confounds.txt")
      filename2 = os.path.join(
          current_dir, "data", "confounds_with_header.csv"
      )
  
      nisignal.clean(
          signals, detrend=False, standardize=False, confounds=filename1
      )
      nisignal.clean(
          signals, detrend=False, standardize=False, confounds=filename2
      )
      nisignal.clean(
          signals,
          detrend=False,
          standardize=False,
          confounds=confounds[:, 1],
      )
  
      # test with confounds as a pandas DataFrame
      confounds_df = read_csv(filename2, sep="\t")
      nisignal.clean(
          signals,
          detrend=False,
          standardize=False,
          confounds=confounds_df.values,
      )
      nisignal.clean(
          signals,
          detrend=False,
          standardize=False,
          confounds=confounds_df,
      )
  
      # test array-like signals
      list_signal = signals.tolist()
      nisignal.clean(list_signal)
  
      # Use a list containing two filenames, a 2D array and a 1D array
      nisignal.clean(
          signals,
          detrend=False,
          standardize=False,
          confounds=[
              filename1,
              confounds[:, 0:2],
              filename2,
              confounds[:, 2],
          ],
      )
  
      # Test error handling
      with pytest.raises(TypeError):
          nisignal.clean(signals, confounds=1)
      with pytest.raises(ValueError):
          nisignal.clean(signals, confounds=np.zeros(2))
      with pytest.raises(ValueError):
          nisignal.clean(signals, confounds=np.zeros((2, 2)))
      with pytest.raises(ValueError):
          nisignal.clean(signals, confounds=np.zeros((2, 3, 4)))
      with pytest.raises(ValueError):
          nisignal.clean(signals[:-1, :], confounds=filename1)
      with pytest.raises(TypeError):
          nisignal.clean(signals, confounds=[None])
      with pytest.raises(ValueError, match="t_r='None'"):
          nisignal.clean(
              signals, filter="cosine", t_r=None, high_pass=0.008
          )
      with pytest.raises(ValueError):
          # using butterworth filter here
          nisignal.clean(signals, t_r=None, low_pass=0.01)
      with pytest.raises(ValueError):
          nisignal.clean(signals, filter="not_implemented")
      with pytest.raises(ValueError):
          nisignal.clean(signals, ensure_finite=None)
      # Check warning message when no confound methods were specified,
      # but cutoff frequency provided.
      with pytest.warns(UserWarning, match="not perform filtering"):
          nisignal.clean(signals, t_r=2.5, filter=False, low_pass=0.01)
  
      # Test without standardizing that constant parts of confounds are
      # accounted for
      # passing standardize_confounds=False, detrend=False should raise warning
      warning_message = (
          r"must perform detrend and/or standardize confounds"
      )
      with pytest.warns(UserWarning, match=warning_message):
          np.testing.assert_almost_equal(
              nisignal.clean(
                  np.ones((20, 2)),
                  standardize=False,
                  confounds=np.ones(20),
                  standardize_confounds=False,
                  detrend=False,
              ).mean(),
              np.zeros((20, 2)),
          )
  
      # Test to check that confounders effects are effectively removed from
      # the signals when having a detrending and filtering operation together.
      # This did not happen originally due to a different order in which
      # these operations were being applied to the data and confounders
      # (it thus solves issue # 2730).
      signals_clean = nisignal.clean(
          signals,
          detrend=True,
          high_pass=0.01,
          standardize_confounds=True,
          standardize=True,
          confounds=confounds,
      )
      confounds_clean = nisignal.clean(
          confounds, detrend=True, high_pass=0.01, standardize=True
      )
      assert (
          abs(np.dot(confounds_clean.T, signals_clean)).max()
          < 1000.0 * eps
      )
  
imports: import os.path
  import numpy
  import pytest
  import scipy.signal
  from pandas import read_csv
  from nilearn import signal
  from nilearn.conftest import _rng
  from nilearn.signal import clean
label_desc: Use image node_id 14 for calling a global function with example usage: test_clean_confounds() without return types