Skip to content

Conversation

ThijsJenneskens
Copy link

Fix issue #343 by adding an exception for the DirichletCollection. Also added a test to check that mode is not being called on a DirichletCollection random variable.

@ThijsJenneskens ThijsJenneskens linked an issue Oct 7, 2025 that may be closed by this pull request
Copy link
Contributor

github-actions bot commented Oct 7, 2025

🤖 Code Formatting

Your PR still has some code formatting issues. I've updated PR #530 with the necessary formatting changes.

You can merge that PR into this branch to fix the code style check.

Alternatively, you can run make format locally and push the changes yourself.

@wouterwln
Copy link
Member

Good job! I've left some comments on the way we'd like to see the tests structured, but as a first draft this looks good!

Copy link

codecov bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.12%. Comparing base (95283b8) to head (d38b9f7).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #529   +/-   ##
=======================================
  Coverage   80.11%   80.12%           
=======================================
  Files          25       25           
  Lines        2012     2013    +1     
=======================================
+ Hits         1612     1613    +1     
  Misses        400      400           

☔ 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

@bvdmitri bvdmitri left a comment

Choose a reason for hiding this comment

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

I'm a bit confused on what this PR is doing. Could you clarify what are you trying to achieve here? That would help me to guide you.

Your PR adds a method inside of the tests, which is not allowed in a package. Also it doesn't test anything. Could you please update the PR such that all the required methods are added in the package and that you have at least one @test or @test_throws inside the @testset

end
end

Distributions.mode(d::DirichletCollection) = error("`mode` should not be called on DirichletCollection.")
Copy link
Member

Choose a reason for hiding this comment

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

You should not add new methods inside the tests

Suggested change
Distributions.mode(d::DirichletCollection) = error("`mode` should not be called on DirichletCollection.")

Copy link
Member

Choose a reason for hiding this comment

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

The reason for that is that tests are not being executed when using the package, tests are only executed by developers and not users. Thus if you attempt to add a new functionality inside tests the users will not actually get this functionality because they don't run tests

Comment on lines +131 to +134
@testset "`DirichletCollection` exception (mode is not defined)" begin
constraint = PointMassFormConstraint()
d = DirichletCollection(ones(3, 3))
opt = constrain_form(constraint, d)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a @test that tests the output?

Copy link
Member

Choose a reason for hiding this comment

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

Or if you want to test that this operation actually throws an error use @test_throws

Copy link
Member

Choose a reason for hiding this comment

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

@ThijsJenneskens you can read about the differences here https://docs.julialang.org/en/v1/stdlib/Test/

ReactiveMP.constrain_form(::PointMassFormConstraint, distribution::Distribution) = PointMass(mode(distribution))

# Except for the DirichletCollection. For this one, we will use the mean instead:
ReactiveMP.constrain_form(::PointMassFormConstraint, distribution::DirichletCollection) = PointMass(mean(distribution))
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused, so why do we need this method if we test for exception such that the mode is not defined? The method above calls the mode but this one calls the mean?

Copy link
Author

Choose a reason for hiding this comment

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

We need this method because the mode is not defined for the Dirichlet Collection. I added the test such that, if a future change does end up calling the mode on a Dirichlet Collection random variable somehow, it's clear what goes wrong.

@bvdmitri
Copy link
Member

bvdmitri commented Oct 8, 2025

If you want to add a method for the mode of DirichletCollection it should be added in ExponentialFamily.jl, not in RxInfer.jl. Is that what you wanted to do?

@ThijsJenneskens
Copy link
Author

If you want to add a method for the mode of DirichletCollection it should be added in ExponentialFamily.jl, not in RxInfer.jl. Is that what you wanted to do?

As in, use multiple dispatch to redefine the mode of a DirichletCollection to it's mean? That could be a better solution. However, maybe we want users to receive a warning if they try to call mode on a Dirichlet Collection?

@wouterwln
Copy link
Member

@ThijsJenneskens you can implement mode in ExponentialFamily, it is defined if all values of the alpha tensor are greater than 1. If this check fails, you can throw an appropriate error. Then in RxInfer you can do a similar check for the values of the alpha tensor, call mode and otherwise call mean (and throw a warning maybe). This will make a PointMassFormConstraint available for DirichletCollection. Then you can test for different values of the alpha tensor in RxInfer if constraining to a pointmass routes to the appropriate underlying calls.

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.

PointMassFormConstraint on a MatrixDirichlet marginal

3 participants