[mlpack-git] master, mlpack-1.0.x: Modified StringUtil to take a number on Indent, implemented LSHSearch ToString, updated Test. (909e76e)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:42:37 EST 2015


Repository : https://github.com/mlpack/mlpack

On branches: master,mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 909e76ed157e262eae28ab4c90da5f8328c956e1
Author: birm <birm at gatech.edu>
Date:   Thu Feb 6 23:25:37 2014 +0000

    Modified StringUtil to take a number on Indent, implemented LSHSearch ToString, updated Test.


>---------------------------------------------------------------

909e76ed157e262eae28ab4c90da5f8328c956e1
 .../binary_space_tree/binary_space_tree_impl.hpp   |  6 ++++--
 src/mlpack/core/util/string_util.cpp               | 23 ++++++++++++++--------
 src/mlpack/core/util/string_util.hpp               |  4 ++--
 src/mlpack/methods/lsh/lsh_search.hpp              |  4 +++-
 src/mlpack/methods/lsh/lsh_search_impl.hpp         | 18 +++++++++++++++++
 src/mlpack/tests/to_string_test.cpp                | 11 +++++++++++
 6 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp b/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
index 0fad974..e782765 100644
--- a/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
+++ b/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
@@ -712,8 +712,10 @@ std::string BinarySpaceTree<BoundType, StatisticType, MatType>::ToString() const
   convert << "BinarySpaceTree [" << this << "]" << std::endl;
   convert << "  begin: " << begin << std::endl;
   convert << "  count: " << count << std::endl;
-  convert << "  bound: " << mlpack::util::Indent(bound.ToString());
-  convert << "  statistic: " << stat.ToString();
+  convert << "  bound: " << std::endl;
+  convert << mlpack::util::Indent(bound.ToString(),2);
+  convert << "  statistic: " << std::endl;
+  convert << mlpack::util::Indent(stat.ToString(),2);
   convert << "  leaf size: " << leafSize << std::endl;
   convert << "  splitDimension: " << splitDimension << std::endl;
   if (left != NULL)
diff --git a/src/mlpack/core/util/string_util.cpp b/src/mlpack/core/util/string_util.cpp
index 392ecfa..cdead2c 100644
--- a/src/mlpack/core/util/string_util.cpp
+++ b/src/mlpack/core/util/string_util.cpp
@@ -1,6 +1,6 @@
 /**
  * @file string_util.cpp
- *
+ * @author ??? and Ryan Birmingham
  * Defines methods useful for formatting output.
  */
 #include "string_util.hpp"
@@ -11,14 +11,21 @@ using namespace std;
 
 //! A utility function that replaces all all newlines with a number of spaces
 //! depending on the indentation level.
-string mlpack::util::Indent(string input)
+string mlpack::util::Indent(string input, size_t howManyTabs)
 {
-  // Tab the first line.
-  input.insert(0, 1, ' ');
-  input.insert(0, 1, ' ');
-
-  // Get the character sequence to replace all newline characters.
-  std::string tabbedNewline("\n  ");
+  // For each declared...
+  string standardTab = "  ";
+  string bigTab = "";
+  for (size_t ind=0; ind<howManyTabs; ind++)
+  {
+    // increase amount tabbed on later lines
+    bigTab+=standardTab;
+    //add to first line
+    input.insert(0, 1, ' ');
+    input.insert(0, 1, ' ');
+  }  
+// Get the character sequence to replace all newline characters.
+  std::string tabbedNewline("\n"+ bigTab);
 
   // Replace all newline characters with the precomputed character sequence.
   size_t startPos = 0;
diff --git a/src/mlpack/core/util/string_util.hpp b/src/mlpack/core/util/string_util.hpp
index 995327b..504eeed 100644
--- a/src/mlpack/core/util/string_util.hpp
+++ b/src/mlpack/core/util/string_util.hpp
@@ -1,6 +1,6 @@
 /**
  * @file string_util.hpp
- *
+ * @author ??? and Ryan Birmingham
  * Declares methods that are useful for writing formatting output.
  */
 #ifndef __MLPACK_CORE_STRING_UTIL_HPP
@@ -13,7 +13,7 @@ namespace util {
 
 //! A utility function that replaces all all newlines with a number of spaces
 //! depending on the indentation level.
-std::string Indent(std::string input);
+std::string Indent(std::string input, size_t howManyTabs=1);
 
 }; // namespace util
 }; // namespace mlpack
diff --git a/src/mlpack/methods/lsh/lsh_search.hpp b/src/mlpack/methods/lsh/lsh_search.hpp
index 98960a2..d6c6278 100644
--- a/src/mlpack/methods/lsh/lsh_search.hpp
+++ b/src/mlpack/methods/lsh/lsh_search.hpp
@@ -123,6 +123,9 @@ class LSHSearch
               arma::mat& distances,
               const size_t numTablesToSearch = 0);
 
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   /**
    * This function builds a hash table with two levels of hashing as presented
@@ -178,7 +181,6 @@ class LSHSearch
   void InsertNeighbor(const size_t queryIndex, const size_t pos,
                       const size_t neighbor, const double distance);
 
- private:
   //! Reference dataset.
   const arma::mat& referenceSet;
 
diff --git a/src/mlpack/methods/lsh/lsh_search_impl.hpp b/src/mlpack/methods/lsh/lsh_search_impl.hpp
index a47806e..8e01179 100644
--- a/src/mlpack/methods/lsh/lsh_search_impl.hpp
+++ b/src/mlpack/methods/lsh/lsh_search_impl.hpp
@@ -390,6 +390,24 @@ BuildHash()
   secondHashTable.resize(numRowsInTable, maxBucketSize);
 }
 
+template<typename SortPolicy>
+std::string LSHSearch<SortPolicy>::ToString() const
+{
+  std::ostringstream convert;
+  convert << "LSHSearch [" << this << "]" << std::endl;
+  convert << "  Reference Set: " << referenceSet.n_rows << "x" ;
+  convert <<  referenceSet.n_cols << std::endl;
+  if (&referenceSet != & querySet)
+    convert << "  QueryeSet: " << querySet.n_rows << "x" << querySet.n_cols 
+        << std::endl;
+  convert << "  Number of Projections: " << numProj << std::endl;
+  convert << "  Number of Tables: " << numTables << std::endl;
+  convert << "  Hash Width: " << hashWidth << std::endl;
+  convert << "  Metric: " << std::endl;
+  convert << mlpack::util::Indent(metric.ToString(),2);
+  return convert.str();
+}
+
 }; // namespace neighbor
 }; // namespace mlpack
 
diff --git a/src/mlpack/tests/to_string_test.cpp b/src/mlpack/tests/to_string_test.cpp
index faa414f..410eb26 100644
--- a/src/mlpack/tests/to_string_test.cpp
+++ b/src/mlpack/tests/to_string_test.cpp
@@ -45,6 +45,7 @@
 #include <mlpack/methods/linear_regression/linear_regression.hpp>
 #include <mlpack/methods/local_coordinate_coding/lcc.hpp>
 #include <mlpack/methods/logistic_regression/logistic_regression.hpp>
+#include <mlpack/methods/lsh/lsh_search.hpp>
 
 using namespace mlpack;
 using namespace mlpack::kernel;
@@ -367,5 +368,15 @@ BOOST_AUTO_TEST_CASE(LogRegString)
   std::string s = d.ToString();
   BOOST_REQUIRE_NE(s, "");
 }
+BOOST_AUTO_TEST_CASE(LSHString)
+{
+  arma::mat c(40,40);
+  const size_t b=3;
+  c.randn();
+  mlpack::neighbor::LSHSearch<NearestNeighborSort> d(c, b, b);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
 
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list