Skip to content

Conversation

@till-m
Copy link
Member

@till-m till-m commented Dec 27, 2025

The previous method of constructing the WrappedKernel didn't allow for combined kernels, this PR fixes that.

Summary by CodeRabbit

  • Bug Fixes

    • Improved kernel state preservation during wrapping to maintain internal configuration and transform functions correctly.
  • Tests

    • Added validation test for combined kernel compatibility with the wrapped kernel workflow.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 27, 2025

📝 Walkthrough

Walkthrough

The changes modify the wrap_kernel function in bayes_opt/parameter.py to preserve a kernel's internal state and transform function by copying attributes directly via __dict__.update, rather than reconstructing from parameters. A new test validates this wrapping behavior with composite kernels (kernel sums) in GP fitting workflows.

Changes

Cohort / File(s) Summary
Kernel Wrapping Implementation
bayes_opt/parameter.py
Modified wrap_kernel to instantiate WrappedKernel without calling __init__, copy all attributes via __dict__.update, and explicitly set _transform. Changes state preservation strategy from parameter-based reconstruction to direct attribute copying.
Kernel Wrapping Validation
tests/test_parameter.py
Added test_combined_wrapped_kernel_fit() to validate wrapped kernel behavior with composite kernels (Matern + WhiteKernel sum) in GaussianProcessRegressor fitting, complementing existing wrapped kernel tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A kernel wrapped with care so true,
State preserved in every view,
Transform intact, composites blessed,
Our wrapped kernels pass the test!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Support combined kernels' directly and specifically summarizes the main change - enabling combined kernels in the WrappedKernel construction method.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Dec 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.89%. Comparing base (c410d51) to head (350bfa5).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #597   +/-   ##
=======================================
  Coverage   97.89%   97.89%           
=======================================
  Files          10       10           
  Lines        1185     1188    +3     
=======================================
+ Hits         1160     1163    +3     
  Misses         25       25           

☔ 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

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
tests/test_parameter.py (1)

249-260: Consider adding assertions to validate kernel behavior.

The test successfully validates that combined kernels can be wrapped and fitted without errors. However, it could be more thorough by adding assertions similar to the existing test_wrapped_kernel_fit test (line 246), which verifies that the kernel's parameters changed after fitting.

🔎 Suggested assertion to validate the kernel behavior
     gp.fit(space.params, space.target)
+
+    # Verify that hyperparameters were optimized (not just using initial values)
+    assert gp.kernel_.k1.length_scale != 1e5
+    assert gp.kernel_.k2.noise_level != 1.0
bayes_opt/parameter.py (1)

492-495: Document why shallow copy and __new__ are necessary for combined kernels.

The implementation uses __new__ and __dict__.update() to preserve the internal state of combined kernels (e.g., Matern + WhiteKernel), which cannot be reconstructed from get_params() alone. This approach is necessary and works correctly with sklearn kernels, which have simple attribute structures that are safe to shallow-copy after initialization.

Add a comment explaining that this pattern is specifically required to support combined kernels and that the shallow copy is safe because sklearn kernels do not modify their attributes after construction.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c410d51 and 350bfa5.

📒 Files selected for processing (2)
  • bayes_opt/parameter.py
  • tests/test_parameter.py
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_parameter.py (2)
bayes_opt/target_space.py (3)
  • kernel_transform (340-347)
  • params (149-156)
  • target (159-166)
bayes_opt/parameter.py (5)
  • wrap_kernel (457-495)
  • kernel_transform (115-126)
  • kernel_transform (222-234)
  • kernel_transform (308-320)
  • kernel_transform (434-449)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Python 3.10 - numpy >=1.25,<2
  • GitHub Check: Python 3.12 - numpy >=1.25,<2
  • GitHub Check: Python 3.12 - numpy >=2
  • GitHub Check: Python 3.11 - numpy >=2
  • GitHub Check: Python 3.11 - numpy >=1.25,<2
  • GitHub Check: Python 3.9 - numpy >=1.25,<2
  • GitHub Check: Python 3.13 - numpy >=2
  • GitHub Check: Python 3.10 - numpy >=2
  • GitHub Check: Python 3.9 - numpy >=2

@till-m till-m merged commit ab61027 into bayesian-optimization:master Dec 27, 2025
15 checks passed
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.

1 participant