Skip to content

Allow saving and loading optimizer state without probes/registrations #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jameskrach
Copy link
Contributor

@jameskrach jameskrach commented Jul 10, 2025

Closes #581

Summary by CodeRabbit

  • Bug Fixes

    • Saving the optimizer state is now allowed even if no samples have been collected, without raising an error.
    • Loading an unused optimizer state no longer attempts to fit the model on empty data, preventing unnecessary errors.
  • Tests

    • Updated tests to verify that saving and loading an unused optimizer state works correctly and maintains consistent suggestions.

Copy link

coderabbitai bot commented Jul 10, 2025

Walkthrough

The changes update the BayesianOptimization class to allow saving and loading the optimizer state even when no samples have been collected. The save_state method no longer raises an error if called before any points are registered, and load_state now fits the Gaussian Process regressor only if samples exist. Corresponding tests were updated.

Changes

File(s) Change Summary
bayes_opt/bayesian_optimization.py Modified save_state to remove error when saving with no samples; load_state now fits GP only if data.
tests/test_bayesian_optimization.py Updated test to allow saving/loading with no samples and verify suggestion consistency.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Optimizer

    User->>Optimizer: Create optimizer
    User->>Optimizer: Save state (no samples)
    Optimizer-->>User: State saved (no error)
    User->>Optimizer: Load state
    Optimizer-->>User: State loaded (GP fit only if samples exist)
Loading

Assessment against linked issues

Objective Addressed Explanation
Allow serialization and deserialization of optimizer state with no registered points (#581)
Remove ValueError on save_state when no samples (#581)
Add guard to GP fitting in load_state for empty space (#581)

Poem

A bunny with code in its paws,
Hopped past some old, rigid laws.
Now, with no points to save,
The optimizer’s brave—
It saves, loads, and fits with applause!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d60b99d and 1335d9c.

📒 Files selected for processing (2)
  • bayes_opt/bayesian_optimization.py (1 hunks)
  • tests/test_bayesian_optimization.py (1 hunks)
🔇 Additional comments (2)
bayes_opt/bayesian_optimization.py (1)

434-435: LGTM! Correct implementation of conditional GP fitting.

The conditional check properly prevents fitting the Gaussian Process when no samples exist, which is essential for loading optimizer state without probes/registrations. This aligns perfectly with the PR objective.

tests/test_bayesian_optimization.py (1)

375-382: LGTM! Comprehensive test coverage for the new functionality.

The test correctly validates the new behavior where:

  1. Saving optimizer state without samples no longer raises an error
  2. Loading the saved state preserves the optimizer's suggestion consistency
  3. The functionality works as expected for both unused and used optimizers

The test coverage properly aligns with the implementation changes.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jul 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.80%. Comparing base (d60b99d) to head (1335d9c).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #582      +/-   ##
==========================================
- Coverage   97.81%   97.80%   -0.01%     
==========================================
  Files          10       10              
  Lines        1188     1186       -2     
==========================================
- Hits         1162     1160       -2     
  Misses         26       26              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@till-m till-m left a comment

Choose a reason for hiding this comment

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

LGTM! @adrianmolzon, do you want to give this a quick pass maybe?

@adrianmolzon
Copy link
Contributor

LGTM too!

@till-m till-m merged commit 0b4ad1c into bayesian-optimization:master Jul 15, 2025
15 checks passed
@jameskrach jameskrach deleted the allow-saving-state-before-registering branch July 16, 2025 03:52
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.

Enable serialization and deserialization of optimizers with no registered points
3 participants