Skip to content

Feat: Add model persistence (save/load methods)#492

Open
Ahmed-Zahran02 wants to merge 5 commits intodswah:mainfrom
Ahmed-Zahran02:add-model-export
Open

Feat: Add model persistence (save/load methods)#492
Ahmed-Zahran02 wants to merge 5 commits intodswah:mainfrom
Ahmed-Zahran02:add-model-export

Conversation

@Ahmed-Zahran02
Copy link
Copy Markdown

@Ahmed-Zahran02 Ahmed-Zahran02 commented Mar 4, 2026

Closes #491

Description
This PR implements model persistence for pyGAM models by adding save and load methods. This allows users to serialize trained models and restore them without retraining, addressing the need for model deployment and reuse.

from pygam import LinearGAM

# Train
gam = LinearGAM(n_splines=10).fit(X, y)

# Save
gam.save("my_gam_model.pkl")

# Load
loaded_gam = LinearGAM.load("my_gam_model.pkl")

# Verify
assert np.allclose(gam.predict(X), loaded_gam.predict(X))

I have added unit tests to verify:

  1. A model can be saved and loaded without error.
  2. The loaded model produces the exact same predictions as the original model.

Checklist

  • I have updated the docstrings for the new methods.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Copilot AI review requested due to automatic review settings March 4, 2026 09:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class model persistence APIs to pyGAM by introducing save() / load() for pickled on-disk serialization, along with to_dict() / from_dict() helpers intended for JSON-serializable export/import, plus tests covering roundtrips and versioning.

Changes:

  • Added GAM.save() / GAM.load() with optional gzip compression and serialization version metadata.
  • Added GAM.to_dict() / GAM.from_dict() for dictionary-based serialization.
  • Added a new serialization test suite covering save/load, compression, version errors, and direct pickling.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
pygam/pygam.py Implements serialization/versioning hooks and the new save/load + dict export/import APIs.
pygam/tests/test_serialization.py Adds unit tests for persistence roundtrips, compression, versioning, and dict JSON-serializability.
pygam/__init__.py Exposes SERIALIZATION_VERSION at the package level.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pygam/tests/test_serialization.py Outdated
Comment thread pygam/pygam.py
Comment thread pygam/pygam.py Outdated
Comment thread pygam/pygam.py Outdated
Comment thread pygam/pygam.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add model persistence methods (save/load functionality)

2 participants