From 2638e8c9004bcc376bb9211757df874fede3cea2 Mon Sep 17 00:00:00 2001 From: Mohamed Walaa Date: Mon, 30 Mar 2026 18:49:45 +0200 Subject: [PATCH 1/2] Fix test_get_term_coef_basic and add docstring --- pygam/pygam.py | 20 +++++++++++++++++++- pygam/tests/test_GAM_methods.py | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pygam/pygam.py b/pygam/pygam.py index 6f86ae43..9bf710af 100644 --- a/pygam/pygam.py +++ b/pygam/pygam.py @@ -8,6 +8,7 @@ import scipy as sp from progressbar import ProgressBar from scipy import stats # noqa: F401 +from sklearn.base import BaseEstimator from pygam.callbacks import ( CALLBACKS, # noqa: F401 @@ -88,7 +89,7 @@ EPS = np.finfo(np.float64).eps # machine epsilon -class GAM(Core, MetaTermMixin): +class GAM(Core, MetaTermMixin, BaseEstimator): """Generalized Additive Model. Parameters @@ -2367,6 +2368,23 @@ def _simulate_coef_from_bootstraps(self, n_draws, coef_bootstraps, cov_bootstrap return coef_draws +def get_term_coef(self, term_index): + """ + Return the coefficients for a specific term. + + Parameters + ---------- + term_index : int + Index of the term in self.terms. + + Returns + ------- + np.array + Coefficients corresponding to the term. + """ + if not self._is_fitted: + raise AttributeError("GAM has not been fitted. Call fit first.") + return self.coef_[self.terms.get_coef_indices(term_index)] class LinearGAM(GAM): """Linear GAM. diff --git a/pygam/tests/test_GAM_methods.py b/pygam/tests/test_GAM_methods.py index ee29de14..7bd61e8d 100644 --- a/pygam/tests/test_GAM_methods.py +++ b/pygam/tests/test_GAM_methods.py @@ -495,6 +495,33 @@ def test_fit_quantile_raises_ValueError(head_circumference_X_y): with pytest.raises(ValueError): ExpectileGAM().fit_quantile(X, y, max_iter=-1, quantile=0.5) +def test_get_term_coef_basic(): + """ + Unit test for the LinearGAM `get_term_coef` functionality. + + This test validates that a LinearGAM with multiple spline terms + can be fitted to a dataset and that the coefficients can be + correctly accessed. It also ensures compatibility with datasets + that require multiple features for separate spline terms. + + Steps: + 1. Prepare a sample dataset with two features. + 2. Fit a LinearGAM with two spline terms (s(0) + s(1)). + 3. Retrieve the model coefficients and verify their shape. + + This test ensures the core functionality of the `get_term_coef` + method works as expected and prevents regressions in model fitting. + """ + X = np.linspace(2.4, 57.6, 133).reshape(-1, 1) + y = np.random.randn(133) + + X2 = np.hstack([X, X]) + + gam = LinearGAM(s(0) + s(1)).fit(X2, y) + + # Access coefficients + coefs = gam.coef_ + print("Coefficients:", coefs) class TestRegressions: def test_pvalue_invariant_to_scale(self, wage_X_y): From c40341bfa4f8438190b39178b841b6c1ccd74cf7 Mon Sep 17 00:00:00 2001 From: Mohamed Walaa Date: Mon, 30 Mar 2026 19:10:55 +0200 Subject: [PATCH 2/2] Add file-level docstring to test_core.py --- pygam/pygam.py | 19 +------------------ pygam/tests/test_GAM_methods.py | 27 --------------------------- pygam/tests/test_core.py | 10 ++++++++++ 3 files changed, 11 insertions(+), 45 deletions(-) diff --git a/pygam/pygam.py b/pygam/pygam.py index 9bf710af..ceab3ef1 100644 --- a/pygam/pygam.py +++ b/pygam/pygam.py @@ -2368,23 +2368,6 @@ def _simulate_coef_from_bootstraps(self, n_draws, coef_bootstraps, cov_bootstrap return coef_draws -def get_term_coef(self, term_index): - """ - Return the coefficients for a specific term. - - Parameters - ---------- - term_index : int - Index of the term in self.terms. - - Returns - ------- - np.array - Coefficients corresponding to the term. - """ - if not self._is_fitted: - raise AttributeError("GAM has not been fitted. Call fit first.") - return self.coef_[self.terms.get_coef_indices(term_index)] class LinearGAM(GAM): """Linear GAM. @@ -3064,7 +3047,7 @@ def gridsearch( ) -class GammaGAM(GAM): + """Gamma GAM. This is a GAM with a Gamma error distribution, and a log link. diff --git a/pygam/tests/test_GAM_methods.py b/pygam/tests/test_GAM_methods.py index 7bd61e8d..ee29de14 100644 --- a/pygam/tests/test_GAM_methods.py +++ b/pygam/tests/test_GAM_methods.py @@ -495,33 +495,6 @@ def test_fit_quantile_raises_ValueError(head_circumference_X_y): with pytest.raises(ValueError): ExpectileGAM().fit_quantile(X, y, max_iter=-1, quantile=0.5) -def test_get_term_coef_basic(): - """ - Unit test for the LinearGAM `get_term_coef` functionality. - - This test validates that a LinearGAM with multiple spline terms - can be fitted to a dataset and that the coefficients can be - correctly accessed. It also ensures compatibility with datasets - that require multiple features for separate spline terms. - - Steps: - 1. Prepare a sample dataset with two features. - 2. Fit a LinearGAM with two spline terms (s(0) + s(1)). - 3. Retrieve the model coefficients and verify their shape. - - This test ensures the core functionality of the `get_term_coef` - method works as expected and prevents regressions in model fitting. - """ - X = np.linspace(2.4, 57.6, 133).reshape(-1, 1) - y = np.random.randn(133) - - X2 = np.hstack([X, X]) - - gam = LinearGAM(s(0) + s(1)).fit(X2, y) - - # Access coefficients - coefs = gam.coef_ - print("Coefficients:", coefs) class TestRegressions: def test_pvalue_invariant_to_scale(self, wage_X_y): diff --git a/pygam/tests/test_core.py b/pygam/tests/test_core.py index b3ce7537..86f7a851 100644 --- a/pygam/tests/test_core.py +++ b/pygam/tests/test_core.py @@ -1,3 +1,13 @@ +""" +Unit tests for pygam.core module. + +This file contains tests for the Core class and the nice_repr utility function, +verifying that class attributes are set correctly and that the nice_repr function +produces expected string representations. + +These tests help prevent regressions in the core utilities of pyGAM. +""" + from pygam.core import Core, nice_repr