Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/constraints/form/form_point_mass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ ReactiveMP.constrain_form(pmconstraint::PointMassFormConstraint, distribution) =
# There is no need to call the optimizer on a `Distribution` object since they should have a well defined `mode`
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.


"""
default_point_mass_form_constraint_optimizer(::Type{<:VariateType}, ::Type{<:ValueSupport}, constraint::PointMassFormConstraint, distribution)

Expand Down
8 changes: 8 additions & 0 deletions test/constraints/form/form_point_mass_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@
@test mean(result.posteriors[]) p atol = 1e-1
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


@testset "`DirichletCollection` exception (mode is not defined)" begin
constraint = PointMassFormConstraint()
d = DirichletCollection(ones(3, 3))
opt = constrain_form(constraint, d)
Comment on lines +131 to +134
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/

end
end
Loading