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

feat: add @assert length check into setmarginals! and setmessages! #366

Merged
merged 12 commits into from
Nov 29, 2023

Conversation

Nimrais
Copy link
Contributor

@Nimrais Nimrais commented Nov 28, 2023

This PR aims to resolve ReactiveBayes/RxInfer.jl#175.

@Nimrais Nimrais changed the title feat: add @assert length check into setmarginals! and setmessages feat: add @assert length check into setmarginals! and setmessages! Nov 28, 2023
Comment on lines 96 to 110
struct RepeatedValueContainer
marginal
container
_length
end

Base.length(ri::RepeatedValueContainer) = ri._length
Base.iterate(ri::RepeatedValueContainer) = iterate(ri.container)
Base.iterate(ri::RepeatedValueContainer, state) = iterate(ri.container, state)

function RepeatedValueContainer(marginal, _length)
container = Iterators.repeated(marginal, _length)
return RepeatedValueContainer(marginal, container, _length)
end

Copy link
Member

@bvdmitri bvdmitri Nov 28, 2023

Choose a reason for hiding this comment

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

Suggested change
struct RepeatedValueContainer
marginal
container
_length
end
Base.length(ri::RepeatedValueContainer) = ri._length
Base.iterate(ri::RepeatedValueContainer) = iterate(ri.container)
Base.iterate(ri::RepeatedValueContainer, state) = iterate(ri.container, state)
function RepeatedValueContainer(marginal, _length)
container = Iterators.repeated(marginal, _length)
return RepeatedValueContainer(marginal, container, _length)
end

I don't understand why is this needed? (+ it is slower than just using Iterators.repeated because all types are Any). Lets not create duplicate functionality with the Base.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bvdmitri you are right It's not needed, my reasoning about iterators length function was wrong. I checked the docs.

src/variables/variable.jl Outdated Show resolved Hide resolved
src/variables/variable.jl Outdated Show resolved Hide resolved
@@ -61,6 +62,7 @@ using Test, ReactiveMP, Rocket, BayesBase, Distributions, ExponentialFamily
activate!.(variablesmx, TestOptions())

setmarginals!(variablesmx, dist)
@test_throws AssertionError setmarginals!(variablesmx, [dist])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@test_throws AssertionError setmarginals!(variablesmx, [dist])
@test_throws AssertionError setmarginals!(variablesmx, dist[begin:end-1])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed this one. What do you mean?

Copy link
Member

Choose a reason for hiding this comment

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

@Nimrais
I just noticed that the purpose of this test might be a bit unclear (in the future), as it currently seems to throw errors for two reasons rather than solely due to a mismatched length. Specifically, since dist is a matrix and [dist] is an array of matrices, this combination isn't supported and may also throw. To effectively test the length, you can exclude the last element from dist. A straightforward way to achieve this is by using dist[begin:end-1], which will include all elements of dist except the last one and ensure that the length is equal to N - 1.

Copy link
Contributor Author

@Nimrais Nimrais Nov 28, 2023

Choose a reason for hiding this comment

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

We do not have any multivariate distributions in this test, only these ones: NormalMeanVariance(-2.0, 3.0), NormalMeanPrecision(-2.0, 3.0), PointMass(2.0).

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry, you are right, I confused myself with the variablesmx variable. The updated tests are also good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I will add it now, your concern is reasonable.

Copy link
Member

@bvdmitri bvdmitri Nov 28, 2023

Choose a reason for hiding this comment

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

As I can see you've added it here already (and this is exactly what I've ment in my previous messages). This is enough :)

Copy link

codecov bot commented Nov 28, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (178d16c) 60.20% compared to head (f9fb90b) 61.24%.
Report is 11 commits behind head on main.

❗ Current head f9fb90b differs from pull request most recent head e403fb2. Consider uploading reports for the commit e403fb2 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #366      +/-   ##
==========================================
+ Coverage   60.20%   61.24%   +1.04%     
==========================================
  Files         174      174              
  Lines        5769     5801      +32     
==========================================
+ Hits         3473     3553      +80     
+ Misses       2296     2248      -48     

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

@bvdmitri bvdmitri merged commit 58e076e into main Nov 29, 2023
4 checks passed
@bvdmitri bvdmitri deleted the add-length-assert-into-set-methods branch November 29, 2023 13:02
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.

initmarginals accept an array argument with different length
2 participants