diff --git a/src/mlpack/bindings/cli/add_to_cli11.hpp b/src/mlpack/bindings/cli/add_to_cli11.hpp index f4ae48a608..ceb03c64e2 100644 --- a/src/mlpack/bindings/cli/add_to_cli11.hpp +++ b/src/mlpack/bindings/cli/add_to_cli11.hpp @@ -48,7 +48,7 @@ void AddToCLI11(const std::string& cliName, { using TupleType = std::tuple::type>; TupleType& tuple = *boost::any_cast(¶m.value); - std::get<1>(tuple) = boost::any_cast(value); + std::get<0>(std::get<1>(tuple)) = boost::any_cast(value); param.wasPassed = true; }, param.desc.c_str()); @@ -110,7 +110,7 @@ void AddToCLI11(const std::string& cliName, { using TupleType = std::tuple::type>; TupleType& tuple = *boost::any_cast(¶m.value); - std::get<1>(tuple) = boost::any_cast(value); + std::get<0>(std::get<1>(tuple)) = boost::any_cast(value); param.wasPassed = true; }, param.desc.c_str()); diff --git a/src/mlpack/bindings/cli/get_param.hpp b/src/mlpack/bindings/cli/get_param.hpp index eaaa813d61..d401e0e554 100644 --- a/src/mlpack/bindings/cli/get_param.hpp +++ b/src/mlpack/bindings/cli/get_param.hpp @@ -53,8 +53,10 @@ T& GetParam( // happens. typedef std::tuple::type> TupleType; TupleType& tuple = *boost::any_cast(&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 TupleType; + typedef std::tuple> TupleType; TupleType* tuple = boost::any_cast(&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; } diff --git a/src/mlpack/bindings/cli/get_printable_param_impl.hpp b/src/mlpack/bindings/cli/get_printable_param_impl.hpp index 9e2584f6b5..9898c49ee6 100644 --- a/src/mlpack/bindings/cli/get_printable_param_impl.hpp +++ b/src/mlpack/bindings/cli/get_printable_param_impl.hpp @@ -83,13 +83,15 @@ std::string GetPrintableParam( const TupleType* tuple = boost::any_cast(&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(const_cast(data)); - std::string matDescription = GetMatrixSize(mat); + GetParam(const_cast(data)); + std::string matDescription = + std::to_string(std::get<2>(std::get<1>(*tuple))) + "x" + + std::to_string(std::get<1>(std::get<1>(*tuple))) + " matrix"; oss << " (" << matDescription << ")"; } diff --git a/src/mlpack/bindings/cli/get_raw_param.hpp b/src/mlpack/bindings/cli/get_raw_param.hpp index 35d544f08b..46c3956a1f 100644 --- a/src/mlpack/bindings/cli/get_raw_param.hpp +++ b/src/mlpack/bindings/cli/get_raw_param.hpp @@ -48,7 +48,7 @@ T& GetRawParam( arma::mat>>::value>::type* = 0) { // Don't load the matrix. - typedef std::tuple TupleType; + typedef std::tuple> TupleType; T& value = std::get<0>(*boost::any_cast(&d.value)); return value; } diff --git a/src/mlpack/bindings/cli/in_place_copy.hpp b/src/mlpack/bindings/cli/in_place_copy.hpp index ca0d7667f5..d3ed1c9521 100644 --- a/src/mlpack/bindings/cli/in_place_copy.hpp +++ b/src/mlpack/bindings/cli/in_place_copy.hpp @@ -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 a 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,35 @@ void InPlaceCopyInternal( const typename std::enable_if< arma::is_arma_type::value || std::is_same>::value || - data::HasSerialize::value>::type* = 0) + std::tuple>::value + >::type* = 0) { // Make the output filename the same as the input filename. typedef std::tuple::type> TupleType; TupleType& tuple = *boost::any_cast(&d.value); + std::string& value = std::get<0>(std::get<1>(tuple)); + + const TupleType& inputTuple = *boost::any_cast(&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 objects. + * + * @param d ParamData object we want to make into an in-place copy. + * @param input ParamData object whose filename we should copy. + */ +template +void InPlaceCopyInternal( + util::ParamData& d, + util::ParamData& input, + const typename std::enable_if< + data::HasSerialize::value>::type* = 0) +{ + // Make the output filename the same as the input filename. + typedef std::tuple::type> TupleType; + TupleType& tuple = *boost::any_cast(&d.value); std::string& value = std::get<1>(tuple); const TupleType& inputTuple = *boost::any_cast(&input.value); diff --git a/src/mlpack/bindings/cli/output_param_impl.hpp b/src/mlpack/bindings/cli/output_param_impl.hpp index c4dcfddbf4..ab2f1e8822 100644 --- a/src/mlpack/bindings/cli/output_param_impl.hpp +++ b/src/mlpack/bindings/cli/output_param_impl.hpp @@ -53,10 +53,10 @@ void OutputParamImpl( util::ParamData& data, const typename boost::enable_if>::type* /* junk */) { - typedef std::tuple TupleType; + typedef std::tuple> TupleType; const T& output = std::get<0>(*boost::any_cast(&data.value)); const std::string& filename = - std::get<1>(*boost::any_cast(&data.value)); + std::get<0>(std::get<1>(*boost::any_cast(&data.value))); if (output.n_elem > 0 && filename != "") { @@ -95,10 +95,10 @@ void OutputParamImpl( std::tuple>>::type* /* junk */) { // Output the matrix with the mappings. - typedef std::tuple TupleType; + typedef std::tuple> TupleType; const T& tuple = std::get<0>(*boost::any_cast(&data.value)); const std::string& filename = - std::get<1>(*boost::any_cast(&data.value)); + std::get<0>(std::get<1>(*boost::any_cast(&data.value))); const arma::mat& matrix = std::get<1>(tuple); // The mapping isn't taken into account. We should write a data::Save() diff --git a/src/mlpack/bindings/cli/parameter_type.hpp b/src/mlpack/bindings/cli/parameter_type.hpp index ddc289ae32..036240b375 100644 --- a/src/mlpack/bindings/cli/parameter_type.hpp +++ b/src/mlpack/bindings/cli/parameter_type.hpp @@ -53,7 +53,7 @@ struct ParameterType template struct ParameterType> { - typedef std::string type; + typedef std::tuple type; }; /** @@ -65,7 +65,7 @@ struct ParameterType> template struct ParameterType> { - typedef std::string type; + typedef std::tuple type; }; /** @@ -76,7 +76,7 @@ struct ParameterType> template struct ParameterType> { - typedef std::string type; + typedef std::tuple type; }; /** @@ -86,7 +86,7 @@ template struct ParameterType, arma::Mat>> { - typedef std::string type; + typedef std::tuple type; }; } // namespace cli diff --git a/src/mlpack/bindings/cli/set_param.hpp b/src/mlpack/bindings/cli/set_param.hpp index 8295b51242..f800fe0553 100644 --- a/src/mlpack/bindings/cli/set_param.hpp +++ b/src/mlpack/bindings/cli/set_param.hpp @@ -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 void SetParam( @@ -65,7 +65,7 @@ void SetParam( // We're setting the string filename. typedef std::tuple::type> TupleType; TupleType& tuple = *boost::any_cast(&d.value); - std::get<1>(tuple) = boost::any_cast(value); + std::get<0>(std::get<1>(tuple)) = boost::any_cast(value); } /** diff --git a/src/mlpack/tests/cli_binding_test.cpp b/src/mlpack/tests/cli_binding_test.cpp index 391a0bfe1a..76cf6a652d 100644 --- a/src/mlpack/tests/cli_binding_test.cpp +++ b/src/mlpack/tests/cli_binding_test.cpp @@ -82,7 +82,9 @@ TEST_CASE("GetParamLoadedMatTest", "[CLIOptionTest]") // Create value. string filename = "hello.csv"; arma::mat m(5, 5, arma::fill::ones); - tuple tuple = make_tuple(m, filename); + typedef std::tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple tuple = make_tuple(m, testTuple); d.value = boost::any(tuple); // Mark it as already loaded. d.input = true; @@ -106,7 +108,9 @@ TEST_CASE("GetParamUnloadedMatTest", "[CLIOptionTest]") arma::mat test(5, 5, arma::fill::ones); data::Save("test.csv", test); arma::mat m; - tuple tuple = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple tuple = make_tuple(m, testTuple); d.value = boost::any(tuple); // Make sure it is not loaded yet. d.input = true; @@ -132,7 +136,9 @@ TEST_CASE("GetParamUmatTest", "[CLIOptionTest]") // Create value. string filename = "hello.csv"; arma::Mat m(5, 5, arma::fill::ones); - tuple, string> tuple = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple, TupleType> tuple = make_tuple(m, testTuple); d.value = boost::any(tuple); // Mark it as already loaded. d.input = true; @@ -157,7 +163,9 @@ TEST_CASE("GetParamUnloadedUmatTest", "[CLIOptionTest]") arma::Mat test(5, 5, arma::fill::ones); data::Save("test.csv", test); arma::Mat m; - tuple, string> tuple = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple, TupleType> tuple = make_tuple(m, testTuple); d.value = boost::any(tuple); // Make sure it is not loaded yet. d.input = true; @@ -199,8 +207,10 @@ TEST_CASE("GetParamDatasetInfoMatTest", "[CLIOptionTest]") data::DatasetInfo dd; arma::mat m; + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; tuple tuple1 = make_tuple(dd, m); - tuple tuple2 = make_tuple(tuple1, filename); + tuple tuple2 = make_tuple(tuple1, testTuple); d.value = boost::any(tuple2); // Make sure it is not loaded yet. @@ -274,7 +284,9 @@ TEST_CASE("RawParamMatTest", "[CLIOptionTest]") // Create value. string filename = "hello.csv"; arma::mat m(5, 5, arma::fill::ones); - tuple tuple = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple tuple = make_tuple(m, testTuple); d.value = boost::any(tuple); d.input = true; d.loaded = false; @@ -322,9 +334,10 @@ TEST_CASE("GetRawParamDatasetInfoTest", "[CLIOptionTest]") // Create tuples. data::DatasetInfo dd(3); arma::mat m(3, 3, arma::fill::randu); - + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; tuple tuple1 = make_tuple(dd, m); - tuple tuple2 = make_tuple(tuple1, filename); + tuple tuple2 = make_tuple(tuple1, testTuple); d.value = boost::any(tuple2); // Make sure it is not loaded yet. @@ -350,7 +363,9 @@ TEST_CASE("OutputParamMatTest", "[CLIOptionTest]") // Create value. string filename = "test.csv"; arma::mat m(3, 3, arma::fill::randu); - tuple t = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple t = make_tuple(m, testTuple); d.value = boost::any(t); d.input = false; @@ -376,7 +391,9 @@ TEST_CASE("OutputParamUmatTest", "[CLIOptionTest]") // Create value. string filename = "test.csv"; arma::Mat m(3, 3, arma::fill::randu); - tuple, string> t = make_tuple(m, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple, TupleType> t = make_tuple(m, testTuple); d.value = boost::any(t); d.input = false; @@ -467,7 +484,9 @@ TEST_CASE("SetParamMatrixTest", "[CLIOptionTest]") // Create initial value. string filename = "hello.csv"; arma::mat m(5, 5, arma::fill::randu); - d.value = boost::any(make_tuple(m, filename)); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + d.value = boost::any(make_tuple(m, testTuple)); // Get a new string. string newFilename = "new.csv"; @@ -477,9 +496,9 @@ TEST_CASE("SetParamMatrixTest", "[CLIOptionTest]") (void*) NULL); // Make sure the change went through. - tuple& t = - *boost::any_cast>(&d.value); - REQUIRE(get<1>(t) == "new.csv"); + tuple& t = + *boost::any_cast>(&d.value); + REQUIRE(get<0>(get<1>(t)) == "new.csv"); } // Test that calling SetParam on a model sets the string correctly. @@ -517,8 +536,10 @@ TEST_CASE("SetParamDatasetInfoMatTest", "[CLIOptionTest]") string filename = "test.csv"; arma::mat m(3, 3, arma::fill::randu); DatasetInfo di(3); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; tuple t1 = make_tuple(di, m); - tuple, string> t2 = make_tuple(t1, filename); + tuple, TupleType> t2 = make_tuple(t1, testTuple); d.value = boost::any(t2); d.noTranspose = false; @@ -530,10 +551,10 @@ TEST_CASE("SetParamDatasetInfoMatTest", "[CLIOptionTest]") (const void*) &a2, (void*) NULL); // Check that the name is right. - tuple, string>& t3 = - *boost::any_cast, string>>(&d.value); + tuple, TupleType>& t3 = + *boost::any_cast, TupleType>>(&d.value); - REQUIRE(get<1>(t3) == "new_filename.csv"); + REQUIRE(get<0>(get<1>(t3)) == "new_filename.csv"); } // Test that GetAllocatedMemory() will properly return NULL for a non-model @@ -556,7 +577,9 @@ TEST_CASE("GetAllocatedMemoryNonModelTest", "[CLIOptionTest]") // Also test with a matrix type. arma::mat test(10, 10, arma::fill::ones); string filename = "test.csv"; - tuple t = make_tuple(test, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple t = make_tuple(test, testTuple); d.value = boost::any(t); result = (void*) 1; @@ -602,7 +625,9 @@ TEST_CASE("DeleteAllocatedMemoryNonModelTest", "[CLIOptionTest]") arma::mat test(10, 10, arma::fill::ones); string filename = "test.csv"; - tuple t = make_tuple(test, filename); + typedef tuple TupleType; + TupleType testTuple{filename, 0, 0}; + tuple t = make_tuple(test, testTuple); d.value = boost::any(t); DeleteAllocatedMemory((util::ParamData&) d, diff --git a/src/mlpack/tests/io_test.cpp b/src/mlpack/tests/io_test.cpp index 26319a8d79..725ea25222 100644 --- a/src/mlpack/tests/io_test.cpp +++ b/src/mlpack/tests/io_test.cpp @@ -974,7 +974,7 @@ TEST_CASE_METHOD(IOTestDestroyer, "UnmappedParamTest", // Now check that we can get unmapped parameters. REQUIRE(IO::GetPrintableParam("matrix") == - "'test_data_3_1000.csv' (3x1000 matrix)"); + "'test_data_3_1000.csv' (1000x3 matrix)"); // This will have size 0x0 since it's an output parameter, and it hasn't been // set since ParseCommandLine() was called. REQUIRE(IO::GetPrintableParam("matrix2") ==