code: def test_get_coef():
      """Test getting linear coefficients (filters/patterns) from estimators."""
      from sklearn import svm
      from sklearn.base import (
          BaseEstimator,
          TransformerMixin,
          is_classifier,
          is_regressor,
      )
      from sklearn.linear_model import Ridge
      from sklearn.model_selection import GridSearchCV
      from sklearn.pipeline import make_pipeline
      from sklearn.preprocessing import StandardScaler
  
      lm_classification = LinearModel()
      assert is_classifier(lm_classification)
  
      lm_regression = LinearModel(Ridge())
      assert is_regressor(lm_regression)
  
      parameters = {"kernel": ["linear"], "C": [1, 10]}
      lm_gs_classification = LinearModel(
          GridSearchCV(
              svm.SVC(), parameters, cv=2, refit=True, n_jobs=None
          )
      )
      assert is_classifier(lm_gs_classification)
  
      lm_gs_regression = LinearModel(
          GridSearchCV(
              svm.SVR(), parameters, cv=2, refit=True, n_jobs=None
          )
      )
      assert is_regressor(lm_gs_regression)
  
      # Define a classifier, an invertible transformer and an non-invertible one.
  
      class Clf(BaseEstimator):
          def fit(self, X, y):
              return self
  
      class NoInv(TransformerMixin):
          def fit(self, X, y):
              return self
  
          def transform(self, X):
              return X
  
      class Inv(NoInv):
          def inverse_transform(self, X):
              return X
  
      X, y, A = _make_data(n_samples=1000, n_features=3, n_targets=1)
  
      # I. Test inverse function
  
      # Check that we retrieve the right number of inverse functions even if
      # there are nested pipelines
      good_estimators = [
          (1, make_pipeline(Inv(), Clf())),
          (2, make_pipeline(Inv(), Inv(), Clf())),
          (3, make_pipeline(Inv(), make_pipeline(Inv(), Inv()), Clf())),
      ]
  
      for expected_n, est in good_estimators:
          est.fit(X, y)
          assert expected_n == len(_get_inverse_funcs(est))
  
      bad_estimators = [
          Clf(),  # no preprocessing
          Inv(),  # final estimator isn't classifier
          make_pipeline(NoInv(), Clf()),  # first step isn't invertible
          make_pipeline(
              Inv(), make_pipeline(Inv(), NoInv()), Clf()
          ),  # nested step isn't invertible
      ]
      for est in bad_estimators:
          est.fit(X, y)
          invs = _get_inverse_funcs(est)
          assert_equal(invs, list())
  
      # II. Test get coef for classification/regression estimators and pipelines
      rng = np.random.RandomState(0)
      for clf in (
          lm_regression,
          lm_gs_classification,
          make_pipeline(StandardScaler(), lm_classification),
          make_pipeline(StandardScaler(), lm_gs_regression),
      ):
          # generate some categorical/continuous data
          # according to the type of estimator.
          if is_classifier(clf):
              n, n_features = 1000, 3
              X = rng.rand(n, n_features)
              y = np.arange(n) % 2
          else:
              X, y, A = _make_data(
                  n_samples=1000, n_features=3, n_targets=1
              )
              y = np.ravel(y)
  
          clf.fit(X, y)
  
          # Retrieve final linear model
          filters = get_coef(clf, "filters_", False)
          if hasattr(clf, "steps"):
              if hasattr(clf.steps[-1][-1].model, "best_estimator_"):
                  # Linear Model with GridSearchCV
                  coefs = clf.steps[-1][-1].model.best_estimator_.coef_
              else:
                  # Standard Linear Model
                  coefs = clf.steps[-1][-1].model.coef_
          else:
              if hasattr(clf.model, "best_estimator_"):
                  # Linear Model with GridSearchCV
                  coefs = clf.model.best_estimator_.coef_
              else:
                  # Standard Linear Model
                  coefs = clf.model.coef_
          if coefs.ndim == 2 and coefs.shape[0] == 1:
              coefs = coefs[0]
          assert_array_equal(filters, coefs)
          patterns = get_coef(clf, "patterns_", False)
          assert filters[0] != patterns[0]
          n_chans = X.shape[1]
          assert_array_equal(
              filters.shape, patterns.shape, [n_chans, n_chans]
          )
  
      # Inverse transform linear model
      filters_inv = get_coef(clf, "filters_", True)
      assert filters[0] != filters_inv[0]
      patterns_inv = get_coef(clf, "patterns_", True)
      assert patterns[0] != patterns_inv[0]
  
imports: import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_array_less, assert_equal
  from mne import EpochsArray, create_info
  from mne.decoding import GeneralizingEstimator, Scaler, TransformerMixin, Vectorizer
  from mne.decoding.base import BaseEstimator, LinearModel, _get_inverse_funcs, cross_val_multiscore, get_coef
  from mne.decoding.search_light import SlidingEstimator
label_desc: Use image node_id 2 for calling a global function with example usage: test_get_coef() and returns: self, self, X, X