Skip to content

Add foundation types and renames for NN Builder refactor - #1872

Merged
janfb merged 10 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-one
Jun 15, 2026
Merged

janfb merged 10 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-one

Conversation

@satwiksps

Copy link
Copy Markdown
Collaborator

What does this PR do?

This PR sets up the groundwork for the Neural Network (NN) Builder refactor project under GSoC 2026. It doesn't add the final builders yet, but it provides a clean, reviewable foundation for the upcoming PRs.

Here are the main changes:

  1. New build_context.py File:
    Adds the core pieces needed to set up a neural network:

    • ZScoreConfig: Tracks how the user wants to preprocess data (e.g., "none", "independent", "structured").
    • ZScoreStats: Holds the calculated mean and standard deviation.
    • BuildContext: Bundles everything needed to build network (shapes, device, dtype, and z-score stats) into one place.
    • compute_z_score_stats(): A simple helper function to calculate the z-score stats from the data.
  2. Renamed ConditionalEstimatorBuilder to ConditionalEstimatorBuildFn:

    • This clarifies that this protocol is actually a function, not an object.
    • It also frees up the "Builder" name for the new classes coming in the next PR.
    • This was a simple find-and-replace across 16 trainer files.
  3. Renamed _EstimatorConfigBase to _EstimatorBuilderBase:

    • Renamed the base class and added an empty build() method to it.
    • The old from_kwargs() and to_dict() methods are untouched and work as usual.

Files Changed

  • sbi/neural_nets/build_context.py (NEW): Added ZScoreConfig, ZScoreStats, BuildContext, and compute_z_score_stats as suggested in updated plan.md
  • sbi/neural_nets/estimators/base.py: Renamed ConditionalEstimatorBuilder to ConditionalEstimatorBuildFn
  • sbi/neural_nets/net_builders/estimator_configs.py: Renamed _EstimatorConfigBase to _EstimatorBuilderBase and added an empty build() method
  • sbi/neural_nets/net_builders/vector_field_nets.py: Updated imports and docstrings
  • Trainer files (npe/, nle/, nre/, vfpe/): Updated Protocol imports across 16 files
  • tests/build_context_test.py (NEW): Added 26 passing tests to fully cover the new types, much of these codes in this file is suggested by AI, we can trim out if any tests are unnecessary

AI Usage

  • Claude Opus 4.6 (via Antigravity 2.0): Used to improve code docstrings and comments, suggest additional docstring changes, and write additional tests for build_context_test.py.
  • Grammarly: Used to refine and improve the grammar of this PR description.
  • VS Code auto-complete: Used some suggested docstrings and comments.

Does this close any issues?

N/A

Any relevant code examples, logs, or error messages?

N/A

@satwiksps
satwiksps marked this pull request as ready for review May 29, 2026 05:36
@codecov

codecov Bot commented May 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.55072% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.92%. Comparing base (f8037af) to head (e3ecd5a).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
sbi/neural_nets/build_context.py 97.56% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1872      +/-   ##
==========================================
+ Coverage   87.89%   87.92%   +0.03%     
==========================================
  Files         143      144       +1     
  Lines       13353    13403      +50     
==========================================
+ Hits        11736    11785      +49     
- Misses       1617     1618       +1     
Flag Coverage Δ
fast 81.45% <98.55%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/inference/trainers/nle/mnle.py 95.83% <100.00%> (ø)
sbi/inference/trainers/nle/nle_a.py 100.00% <ø> (ø)
sbi/inference/trainers/nle/nle_base.py 96.87% <100.00%> (ø)
sbi/inference/trainers/npe/mnpe.py 95.83% <100.00%> (ø)
sbi/inference/trainers/npe/npe_a.py 79.35% <ø> (ø)
sbi/inference/trainers/npe/npe_b.py 100.00% <ø> (ø)
sbi/inference/trainers/npe/npe_base.py 92.74% <100.00%> (ø)
sbi/inference/trainers/npe/npe_c.py 93.75% <ø> (ø)
sbi/inference/trainers/npe/npe_pfn.py 85.05% <ø> (ø)
sbi/inference/trainers/nre/bnre.py 97.22% <100.00%> (ø)
... and 11 more

@satwiksps
satwiksps force-pushed the nn-builder-refactor-pr-one branch from cd4f349 to d31f0cf Compare May 29, 2026 05:49

@janfb janfb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks a lot @satwiksps for this clean PR! it's great to receive these well-scoped PR.

As discussed in our meeting, I added a couple of comments and suggestions. Let me know if you have any questions.

Comment thread sbi/neural_nets/estimators/base.py Resolved
Comment thread sbi/neural_nets/build_context.py Resolved
Comment thread sbi/neural_nets/build_context.py Outdated
Comment on lines +132 to +133
structured = config.theta == "structured"
theta_mean, theta_std = z_standardization(theta, structured)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

to make this and the one below more readable, I suggest to make the kwarg explicit and do it in one line:

if config.theta != "none":
      theta_mean, theta_std = z_standardization(
          theta, structured_dims=(config.theta == "structured")
      )

Comment thread tests/build_context_test.py Resolved
Comment thread tests/build_context_test.py Resolved
Comment thread sbi/neural_nets/build_context.py Resolved
Comment thread sbi/neural_nets/build_context.py Resolved
Comment thread sbi/neural_nets/build_context.py Resolved
Comment thread sbi/neural_nets/estimators/base.py Resolved
@satwiksps
satwiksps changed the base branch from main to gsoc-2026 June 9, 2026 12:59

@janfb janfb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks perfect! thanks for addressing my suggestion so precisely and in separate commits 🙌 🙏

@janfb
janfb merged commit d0afd1d into sbi-dev:gsoc-2026 Jun 15, 2026
9 checks passed
Jocho-Smith pushed a commit to Jocho-Smith/sbi that referenced this pull request Jul 20, 2026
* rename ConditionalEstimatorBuilder to ConditionalEstimatorBuildFn

* add ZScoreConfig, ZScoreStats, BuildContext, and compute_z_score_stats

* rename _EstimatorConfigBase to _EstimatorBuilderBase and add build()

* add tests for builder foundation types and renames

* pre-commit fix

* rename ConditionalEstimatorBuilder in npe_pfn.py

* add warning

* some docstrings and coding style improvements

* major changes in test file

* minor docstring styling fix
janfb pushed a commit that referenced this pull request Aug 5, 2026
Ports #1872, which was reviewed and merged into the `gsoc-2026`
branch. See that pull request for the full description.

(cherry picked from commit d0afd1d)
janfb pushed a commit that referenced this pull request Aug 5, 2026
Ports #1872, which was reviewed and merged into the `gsoc-2026`
branch. See that pull request for the full description.

(cherry picked from commit d0afd1d)
@satwiksps
satwiksps deleted the nn-builder-refactor-pr-one branch August 7, 2026 10:48
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.

2 participants