Skip to content
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

Adds inplace transpose method and some tests. #408

Merged
merged 2 commits into from Feb 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/mlpack/core/data/load_impl.hpp
Expand Up @@ -16,6 +16,21 @@
namespace mlpack {
namespace data {

template<typename eT>
bool inline inplace_transpose(arma::Mat<eT>& X)
{
try
{
X = arma::trans(X);
return false;
}
catch (std::bad_alloc& exception)
{
arma::inplace_trans(X, "lowmem");
return true;
}
}

template<typename eT>
bool Load(const std::string& filename,
arma::Mat<eT>& matrix,
Expand Down Expand Up @@ -220,7 +235,9 @@ bool Load(const std::string& filename,

// Now transpose the matrix, if necessary.
if (transpose)
matrix = trans(matrix);
{
inplace_transpose(matrix);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where else we might use inplace_transpose(). Do you think we should just inline it here instead of adding new files in arma_extend/? I don't think this contribution will go back upstream, which is usually what arma_extend is for (backporting new functionality, or preparing new stuff to submit upstream).


Timer::Stop("loading_data");

Expand Down
26 changes: 26 additions & 0 deletions src/mlpack/tests/load_save_test.cpp
Expand Up @@ -100,6 +100,32 @@ BOOST_AUTO_TEST_CASE(SaveCSVTest)
remove("test_file.csv");
}

/**
* Make sure CSVs can be loaded in transposed form.
*/
BOOST_AUTO_TEST_CASE(LoadTransposedCSVTest)
{
std::fstream f;
f.open("test_file.csv", std::fstream::out);

f << "1, 2, 3, 4" << std::endl;
f << "5, 6, 7, 8" << std::endl;

f.close();

arma::mat test;
BOOST_REQUIRE(data::Load("test_file.csv", test, false, true) == true);

BOOST_REQUIRE_EQUAL(test.n_cols, 2);
BOOST_REQUIRE_EQUAL(test.n_rows, 4);

for (size_t i = 0; i < 8; ++i)
BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);

// Remove the file.
remove("test_file.csv");
}

/**
* Make sure CSVs can be loaded in non-transposed form.
*/
Expand Down