Refactor checks to release ApproxKFNModel memory if needed.

This commit is contained in:
Ryan Curtin
2018-04-25 11:28:07 -04:00
parent a773966df3
commit ca17187de2
2 changed files with 27 additions and 10 deletions
@@ -189,6 +189,16 @@ static void mlpackMain()
"the reference set must be passed");
}
if (CLI::HasParam("k") && CLI::HasParam("reference") &&
((size_t) CLI::GetParam<int>("k")) >
CLI::GetParam<arma::mat>("reference").n_cols)
{
Log::Fatal << "Number of neighbors to search for ("
<< CLI::GetParam<int>("k") << ") must be less than the number of "
<< "reference points ("
<< CLI::GetParam<arma::mat>("reference").n_cols << ")." << std::endl;
}
// Do the building of a model, if necessary.
ApproxKFNModel* m;
arma::mat referenceSet; // This may be used at query time.
@@ -264,16 +274,23 @@ static void mlpackMain()
if (CLI::HasParam("exact_distances"))
{
// Check the exact distances matrix has the right dimensions.
RequireParamValue<arma::mat>("exact_distances",
[&](const arma::mat& exactDists){ return (exactDists.n_rows == k); },
true, "the number of rows in exact distances matrix must be equal to "
"k.");
RequireParamValue<arma::mat>("exact_distances",
[&](const arma::mat& exactDists){ return (exactDists.n_cols ==
referenceSet.n_cols); }, true, "the number of columns in exact "
"distances matrix must be equal to the number of columns in "
"reference set.");
exactDistances = std::move(CLI::GetParam<arma::mat>("exact_distances"));
if (exactDistances.n_rows != k)
{
delete m;
Log::Fatal << "The number of rows in the exact distances matrix ("
<< exactDistances.n_rows << " must be equal to k (" << k << ")."
<< std::endl;
}
else if (exactDistances.n_cols != referenceSet.n_cols)
{
delete m;
Log::Fatal << "The number of columns in the exact distances matrix ("
<< exactDistances.n_cols << ") must be equal to the number of "
<< "columns in the reference set (" << referenceSet.n_cols << ")."
<< std::endl;
}
}
else
{
@@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(ApproxKFNInvalidKTest)
SetInputParam("k", (int) 81); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::invalid_argument);
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}