forked from probsys/hierarchical-irm
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor Relation<DistributionType> to Relation<ValueType> #70
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89322d9
Refactor Relation<DistributionType> to Relation<ValueType>
ThomasColthurst e9f38b3
Fix bugs, add typename_playground
ThomasColthurst ea954fb
Fix more build errors
ThomasColthurst bd7b17d
All non-integration tests build
ThomasColthurst 4e4c6d6
Integration test fixes
ThomasColthurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
// Copyright 2024 | ||
// See LICENSE.txt | ||
|
||
#include "relation_variant.hh" | ||
|
||
#include <cassert> | ||
#include <type_traits> | ||
|
||
#include "distributions/beta_bernoulli.hh" | ||
#include "distributions/bigram.hh" | ||
#include "distributions/dirichlet_categorical.hh" | ||
#include "distributions/normal.hh" | ||
#include "domain.hh" | ||
#include "relation.hh" | ||
#include "relation_variant.hh" | ||
|
||
|
||
RelationVariant relation_from_spec(const std::string& name, | ||
const DistributionSpec& dist_spec, | ||
std::vector<Domain*>& domains) { | ||
switch (dist_spec.distribution) { | ||
case DistributionEnum::bernoulli: | ||
return new Relation<BetaBernoulli>(name, dist_spec, domains); | ||
case DistributionEnum::bigram: | ||
return new Relation<Bigram>(name, dist_spec, domains); | ||
case DistributionEnum::categorical: | ||
return new Relation<DirichletCategorical>(name, dist_spec, domains); | ||
case DistributionEnum::normal: | ||
return new Relation<Normal>(name, dist_spec, domains); | ||
default: | ||
assert(false && "Unsupported distribution enum value."); | ||
} | ||
DistributionVariant dv = cluster_prior_from_spec(dist_spec); | ||
|
||
RelationVariant rv; | ||
|
||
// We want to go from dv to its SampleType. This only takes five steps: | ||
// 1. To go from the DistributionVariant dv to the underlying | ||
// Distribution pointer, we use a std::visit. | ||
// 2. To get the type of the Distribution, we use decltype(*v). | ||
// 3. But that turns out to be of type DistributionName&, so we need to use | ||
// a std::remove_reference_t to get rid of the &. | ||
// 4. You would think that we could now just access ::SampleType and be done, | ||
// but no -- that expression is sufficiently complicated that the C++ | ||
// parser gets confused and throws an error about "dependent-name ... is | ||
// parsed as a non-type". Luckily the same error message also gives the | ||
// fix: just add a typename to the beginning. | ||
// 5. With that, we can finally access the SampleType and use it to construct | ||
// the right kind of Relation. | ||
std::visit( | ||
[&](const auto& v) { | ||
rv = new Relation<typename | ||
std::remove_reference_t<decltype(*v)>::SampleType>( | ||
name, dist_spec, domains); | ||
}, dv); | ||
|
||
return rv; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Apache License, Version 2.0, refer to LICENSE.txt | ||
|
||
#define BOOST_TEST_MODULE test relation_variant | ||
|
||
#include "relation_variant.hh" | ||
|
||
#include <boost/test/included/unit_test.hpp> | ||
namespace tt = boost::test_tools; | ||
|
||
BOOST_AUTO_TEST_CASE(test_relation_variant) { | ||
std::vector<Domain *> domains; | ||
domains.push_back(new Domain("D1")); | ||
RelationVariant rv = relation_from_spec( | ||
"r1", parse_distribution_spec("bernoulli"), domains); | ||
Relation<bool>* rb = std::get<Relation<bool>*>(rv); | ||
BOOST_TEST(rb->name == "r1"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the answer is no, but is there any chance this could cause problems once
DistributionVariant
containsDistribution
subclasses with the sameValueType
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. The only way the code here could go wrong is if relation_from_spec somehow returned the wrong type of Relation for a DistributionSpec -- for example, if it returned a
Relation<bool>
for a dist_spec that when passed to cluster_prior_from_spec returned aDistribution<double>
.But if that happened, lots of other things would go wrong, too.