Skip to content
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

Streamline internal de/conditioning interface #776

Merged
merged 8 commits into from
Jan 10, 2025
Merged

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jan 9, 2025

This PR:

  • in src/contexts.jl, removes the methods AbstractPPL.condition(values, ...) in favour of directly using the ConditionContext() constructor.

    This is mainly inspired by Aqua's complaints about how we are pirating AbstractPPL.{de,}condition (see Add Aqua tests #775). I've tested locally and this PR would fix all of the condition-related type piracies. But also semantically, I think that condition is something you do to a model, not a context (and AbstractPPL's API sort of specifies that too). Since contexts are DynamicPPL-internal, I think it makes sense that we use an different, internal, function to manipulate that.

  • likewise, renames AbstractPPL.decondition(context, ...) to decondition_context(context, ...). Suggestions for other names welcome!

  • shifts the bulk of the 'figuring-out-arguments-and-coercing-to-the-right-format' stuff into a helper function, called _make_conditioning_values. This allows us to have a much more specific type for the ConditionContext struct itself, while retaining the current flexibility of the model conditioning syntax.

  • adds tests for all of the above


Going beyond the name changes, there are two changes to functionality, which are both in the new decondition_context:

  • decondition_context now checks if there are no more conditioned values left, and if so, unwraps the layer of ConditionContext. This shouldn't really affect anything, it just makes for cleaner context trees. For example:

    julia> ctx = ConditionContext(Dict(@varname(x) => 1, @varname(y) => 2))
    ConditionContext(Dict{VarName{sym, typeof(identity)} where sym, Int64}(x => 1, y => 2), DefaultContext())
    
    julia> # On current master
           decondition(ctx, @varname(x), @varname(y)) # On current master
    ConditionContext(Dict{VarName{sym, typeof(identity)} where sym, Int64}(), DefaultContext())
    
    julia> # With this PR
           decondition_context(ctx, @varname(x), @varname(y))
    DefaultContext()
  • decondition_context also makes sure to deepcopy the previous conditioned values, to avoid surprises due to mutation in BangBang.delete!!:

    julia> ctx = ConditionContext(Dict(@varname(x) => 1, @varname(y) => 2))
    ConditionContext(Dict{VarName{sym, typeof(identity)} where sym, Int64}(x => 1, y => 2), DefaultContext())
    
    julia> # On current master
           decondition(ctx, @varname(x)); decondition(ctx, @varname(y))
    ConditionContext(Dict{VarName{sym, typeof(identity)} where sym, Int64}(), DefaultContext())
    
    julia> # With this PR
           decondition_context(ctx, @varname(x)); decondition_context(ctx, @varname(y))
    ConditionContext(Dict{VarName{sym, typeof(identity)} where sym, Int64}(x => 1), DefaultContext())

@penelopeysm penelopeysm changed the title Streamline de/conditioning interface Streamline internal de/conditioning interface Jan 9, 2025
@penelopeysm penelopeysm requested a review from sunxd3 January 9, 2025 18:29
@penelopeysm penelopeysm mentioned this pull request Jan 9, 2025
@coveralls
Copy link

coveralls commented Jan 9, 2025

Pull Request Test Coverage Report for Build 12700023444

Details

  • 14 of 32 (43.75%) changed or added relevant lines in 2 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.1%) to 86.345%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/model.jl 9 10 90.0%
src/contexts.jl 5 22 22.73%
Files with Coverage Reduction New Missed Lines %
src/contexts.jl 2 30.16%
Totals Coverage Status
Change from base Build 12657521724: 0.1%
Covered Lines: 3737
Relevant Lines: 4328

💛 - Coveralls

Copy link
Member

@sunxd3 sunxd3 left a comment

Choose a reason for hiding this comment

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

This makes a ton of sense to me, I really like it.
Just some nitpicking, really.

Project.toml Show resolved Hide resolved
src/model.jl Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 87.50000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 86.26%. Comparing base (003ff2f) to head (2597193).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/contexts.jl 86.36% 3 Missing ⚠️
src/model.jl 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #776      +/-   ##
==========================================
+ Coverage   86.13%   86.26%   +0.12%     
==========================================
  Files          36       36              
  Lines        4336     4332       -4     
==========================================
+ Hits         3735     3737       +2     
+ Misses        601      595       -6     

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

@sunxd3
Copy link
Member

sunxd3 commented Jan 10, 2025

Thanks Penny, no more issue from my end

@penelopeysm penelopeysm added this pull request to the merge queue Jan 10, 2025
Merged via the queue into master with commit e673b69 Jan 10, 2025
20 checks passed
@penelopeysm penelopeysm deleted the py/condition branch January 10, 2025 11:47
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.

3 participants