made recommended changes

This commit is contained in:
NippunSharma
2021-05-05 19:00:16 +05:30
parent dbd882707b
commit c53ab38348
8 changed files with 55 additions and 24 deletions
+2 -2
View File
@@ -48,7 +48,7 @@ void AddToCLI11(const std::string& cliName,
{
using TupleType = std::tuple<T, typename ParameterType<T>::type>;
TupleType& tuple = *boost::any_cast<TupleType>(&param.value);
std::get<1>(tuple) = boost::any_cast<std::string>(value);
std::get<0>(std::get<1>(tuple)) = boost::any_cast<std::string>(value);
param.wasPassed = true;
},
param.desc.c_str());
@@ -110,7 +110,7 @@ void AddToCLI11(const std::string& cliName,
{
using TupleType = std::tuple<T, typename ParameterType<T>::type>;
TupleType& tuple = *boost::any_cast<TupleType>(&param.value);
std::get<1>(tuple) = boost::any_cast<std::string>(value);
std::get<0>(std::get<1>(tuple)) = boost::any_cast<std::string>(value);
param.wasPassed = true;
},
param.desc.c_str());
+11 -3
View File
@@ -53,8 +53,10 @@ T& GetParam(
// happens.
typedef std::tuple<T, typename ParameterType<T>::type> TupleType;
TupleType& tuple = *boost::any_cast<TupleType>(&d.value);
const std::string& value = std::get<1>(tuple);
const std::string& value = std::get<0>(std::get<1>(tuple));
T& matrix = std::get<0>(tuple);
size_t& n_rows = std::get<1>(std::get<1>(tuple));
size_t& n_cols = std::get<2>(std::get<1>(tuple));
if (d.input && !d.loaded)
{
// Call correct data::Load() function.
@@ -62,6 +64,8 @@ T& GetParam(
data::Load(value, matrix, true);
else
data::Load(value, matrix, true, !d.noTranspose);
n_rows = matrix.n_rows;
n_cols = matrix.n_cols;
d.loaded = true;
}
@@ -81,13 +85,17 @@ T& GetParam(
{
// If this is an input parameter, we need to load both the matrix and the
// dataset info.
typedef std::tuple<T, std::string> TupleType;
typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
TupleType* tuple = boost::any_cast<TupleType>(&d.value);
const std::string& value = std::get<1>(*tuple);
const std::string& value = std::get<0>(std::get<1>(*tuple));
T& t = std::get<0>(*tuple);
size_t& n_rows = std::get<1>(std::get<1>(*tuple));
size_t& n_cols = std::get<2>(std::get<1>(*tuple));
if (d.input && !d.loaded)
{
data::Load(value, std::get<1>(t), std::get<0>(t), true, !d.noTranspose);
n_rows = std::get<1>(t).n_rows;
n_cols = std::get<1>(t).n_cols;
d.loaded = true;
}
@@ -83,13 +83,14 @@ std::string GetPrintableParam(
const TupleType* tuple = boost::any_cast<TupleType>(&data.value);
std::ostringstream oss;
oss << "'" << std::get<1>(*tuple) << "'";
oss << "'" << std::get<0>(std::get<1>(*tuple)) << "'";
if (std::get<1>(*tuple) != "")
if (std::get<0>(std::get<1>(*tuple)) != "")
{
// Make sure the matrix is loaded so that we can print its size.
T& mat = GetParam<T>(const_cast<util::ParamData&>(data));
std::string matDescription = GetMatrixSize(mat);
GetParam<T>(const_cast<util::ParamData&>(data));
std::string matDescription = std::to_string(std::get<1>(std::get<1>(*tuple))) + "x";
matDescription += std::to_string(std::get<2>(std::get<1>(*tuple))) + " matrix";
oss << " (" << matDescription << ")";
}
+1 -1
View File
@@ -48,7 +48,7 @@ T& GetRawParam(
arma::mat>>::value>::type* = 0)
{
// Don't load the matrix.
typedef std::tuple<T, std::string> TupleType;
typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
T& value = std::get<0>(*boost::any_cast<TupleType>(&d.value));
return value;
}
+25 -3
View File
@@ -41,7 +41,7 @@ void InPlaceCopyInternal(
/**
* Modify the filename for any type that needs to be loaded from disk to match
* the filename of the input parameter.
* the filename of the input parameter. For matrix/datasetinfo parameter.
*
* @param d ParamData object we want to make into an in-place copy.
* @param input ParamData object whose filename we should copy.
@@ -53,12 +53,34 @@ void InPlaceCopyInternal(
const typename std::enable_if<
arma::is_arma_type<T>::value ||
std::is_same<T,
std::tuple<mlpack::data::DatasetInfo, arma::mat>>::value ||
data::HasSerialize<T>::value>::type* = 0)
std::tuple<mlpack::data::DatasetInfo, arma::mat>>::value>::type* = 0)
{
// Make the output filename the same as the input filename.
typedef std::tuple<T, typename ParameterType<T>::type> TupleType;
TupleType& tuple = *boost::any_cast<TupleType>(&d.value);
std::string& value = std::get<0>(std::get<1>(tuple));
const TupleType& inputTuple = *boost::any_cast<TupleType>(&input.value);
value = std::get<0>(std::get<1>(inputTuple));
}
/**
* Modify the filename for any type that needs to be loaded from disk to match
* the filename of the input parameter. For Serializable object.
*
* @param d ParamData object we want to make into an in-place copy.
* @param input ParamData object whose filename we should copy.
*/
template<typename T>
void InPlaceCopyInternal(
util::ParamData& d,
util::ParamData& input,
const typename std::enable_if<
data::HasSerialize<T>::value>::type* = 0)
{
// Make the output filename the same as the input filename.
typedef std::tuple<T*, typename ParameterType<T>::type> TupleType;
TupleType& tuple = *boost::any_cast<TupleType>(&d.value);
std::string& value = std::get<1>(tuple);
const TupleType& inputTuple = *boost::any_cast<TupleType>(&input.value);
@@ -53,10 +53,10 @@ void OutputParamImpl(
util::ParamData& data,
const typename boost::enable_if<arma::is_arma_type<T>>::type* /* junk */)
{
typedef std::tuple<T, std::string> TupleType;
typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
const T& output = std::get<0>(*boost::any_cast<TupleType>(&data.value));
const std::string& filename =
std::get<1>(*boost::any_cast<TupleType>(&data.value));
std::get<0>(std::get<1>(*boost::any_cast<TupleType>(&data.value)));
if (output.n_elem > 0 && filename != "")
{
@@ -95,10 +95,10 @@ void OutputParamImpl(
std::tuple<data::DatasetInfo, arma::mat>>>::type* /* junk */)
{
// Output the matrix with the mappings.
typedef std::tuple<T, std::string> TupleType;
typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
const T& tuple = std::get<0>(*boost::any_cast<TupleType>(&data.value));
const std::string& filename =
std::get<1>(*boost::any_cast<TupleType>(&data.value));
std::get<0>(std::get<1>(*boost::any_cast<TupleType>(&data.value)));
const arma::mat& matrix = std::get<1>(tuple);
// The mapping isn't taken into account. We should write a data::Save()
+4 -4
View File
@@ -53,7 +53,7 @@ struct ParameterType
template<typename eT>
struct ParameterType<arma::Col<eT>>
{
typedef std::string type;
typedef std::tuple<std::string, size_t, size_t> type;
};
/**
@@ -65,7 +65,7 @@ struct ParameterType<arma::Col<eT>>
template<typename eT>
struct ParameterType<arma::Row<eT>>
{
typedef std::string type;
typedef std::tuple<std::string, size_t, size_t> type;
};
/**
@@ -76,7 +76,7 @@ struct ParameterType<arma::Row<eT>>
template<typename eT>
struct ParameterType<arma::Mat<eT>>
{
typedef std::string type;
typedef std::tuple<std::string, size_t, size_t> type;
};
/**
@@ -86,7 +86,7 @@ template<typename eT, typename PolicyType>
struct ParameterType<std::tuple<mlpack::data::DatasetMapper<PolicyType,
std::string>, arma::Mat<eT>>>
{
typedef std::string type;
typedef std::tuple<std::string, size_t, size_t> type;
};
} // namespace cli
+3 -3
View File
@@ -51,8 +51,8 @@ void SetParam(
}
/**
* Set a matrix parameter, a matrix/dataset info parameter, or a serializable
* object. These set the filename referring to the parameter.
* Set a matrix parameter, a matrix/dataset info parameter.
* These set the filename referring to the parameter.
*/
template<typename T>
void SetParam(
@@ -65,7 +65,7 @@ void SetParam(
// We're setting the string filename.
typedef std::tuple<T, typename ParameterType<T>::type> TupleType;
TupleType& tuple = *boost::any_cast<TupleType>(&d.value);
std::get<1>(tuple) = boost::any_cast<std::string>(value);
std::get<0>(std::get<1>(tuple)) = boost::any_cast<std::string>(value);
}
/**