Skip to content

Commit

Permalink
BlockIndependent: Fix unique description
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 3, 2024
1 parent a632806 commit 59216ae
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 39 deletions.
11 changes: 1 addition & 10 deletions lib/src/Uncertainty/Distribution/BlockIndependentCopula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,7 @@ void BlockIndependentCopula::setCopulaCollection(const DistributionCollection &
isAlreadyComputedCovariance_ = false;
// One MUST set the dimension BEFORE the description, else an error occurs
setDimension(dimension);

// avoid description warning with identical entries
Description test(description);
Description::const_iterator it = std::unique(test.begin(), test.end());
if (it != test.end())
{
description = Description::BuildDefault(dimension_, "X");
}
setDescription(description);

setDescription(DeduplicateDecription(description));
computeRange();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,7 @@ void BlockIndependentDistribution::setDistributionCollection(const DistributionC
isAlreadyComputedCovariance_ = false;
// One MUST set the dimension BEFORE the description, else an error occurs
setDimension(dimension);

// avoid description warning with identical entries
Description test(description);
Description::const_iterator it = std::unique(test.begin(), test.end());
if (it != test.end())
{
description = Description::BuildDefault(dimension_, "X");
}
setDescription(description);

setDescription(DeduplicateDecription(description));
computeRange();
}

Expand Down
20 changes: 1 addition & 19 deletions lib/src/Uncertainty/Distribution/JointDistribution.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -294,25 +294,7 @@ void JointDistribution::setDistributionCollection(const DistributionCollection &
distributionCollection_ = coll;
isAlreadyComputedMean_ = false;
isAlreadyComputedCovariance_ = false;

// avoid description warning with identical entries
std::map<String, UnsignedInteger> occurrence;
UnsignedInteger idx = 0;
for (UnsignedInteger i = 0; i < description.getSize(); ++ i)
{
const String currentName(description[i]);
++ occurrence[currentName];
if (occurrence[currentName] > 1)
{
while (occurrence.find(OSS() << "X" << idx) != occurrence.end())
++ idx;
const String newName(OSS() << "X" << idx);
++ occurrence[newName]; // avoid duplicates with new ones too
description[i] = newName;
}
}
setDescription(description);

setDescription(DeduplicateDecription(description));
setRange(Interval(lowerBound, upperBound, finiteLowerBound, finiteUpperBound));
}

Expand Down
23 changes: 23 additions & 0 deletions lib/src/Uncertainty/Model/DistributionImplementation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4813,6 +4813,29 @@ UnsignedInteger DistributionImplementation::getParameterDimension() const
return getParameter().getSize();
}

/* Filter identical entries */
Description DistributionImplementation::DeduplicateDecription(const Description & description)
{
std::map<String, UnsignedInteger> occurrence;
UnsignedInteger idx = 0;
Description result(description);
for (UnsignedInteger i = 0; i < result.getSize(); ++ i)
{
const String currentName(result[i]);
++ occurrence[currentName];
if (occurrence[currentName] > 1)
{
// replace duplicate with the first free "XN" name
while (occurrence.find(OSS() << "X" << idx) != occurrence.end())
++ idx;
const String newName(OSS() << "X" << idx);
++ occurrence[newName]; // avoid duplicates with new ones too
result[i] = newName;
}
}
return result;
}

/* Description accessor */
void DistributionImplementation::setDescription(const Description & description)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ public:
void setQuantileEpsilon(const Scalar quantileEpsilon);

protected:
/** Filter identical entries */
static Description DeduplicateDecription(const Description & description);

/** Draw the PDF of a discrete distribution */
virtual Graph drawDiscretePDF(const Scalar xMin,
Expand Down

0 comments on commit 59216ae

Please sign in to comment.