[mlpack-git] master: Fix #478 and add tests. (962a37f)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Fri Nov 20 13:50:59 EST 2015


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/fa8919245e69fb9dbfa98e43f6c060213e6a1937...962a37fe8374913c435054aa50e12d912bdfa01c

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

commit 962a37fe8374913c435054aa50e12d912bdfa01c
Author: Ryan Curtin <ryan at ratml.org>
Date:   Fri Nov 20 13:50:39 2015 -0500

    Fix #478 and add tests.


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

962a37fe8374913c435054aa50e12d912bdfa01c
 .../neighbor_search/neighbor_search_impl.hpp       |  5 +++-
 .../methods/range_search/range_search_impl.hpp     |  3 +++
 src/mlpack/methods/rann/ra_search_impl.hpp         |  3 +++
 src/mlpack/tests/allknn_test.cpp                   | 27 ++++++++++++++++++++++
 src/mlpack/tests/allkrann_search_test.cpp          | 27 ++++++++++++++++++++++
 src/mlpack/tests/range_search_test.cpp             | 26 +++++++++++++++++++++
 6 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp
index 68a0245..6f01c0f 100644
--- a/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp
+++ b/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp
@@ -344,10 +344,13 @@ Search(const MatType& querySet,
   if (tree::TreeTraits<Tree>::RearrangesDataset)
   {
     if (!singleMode && !naive)
+    {
       distancePtr = new arma::mat; // Query indices need to be mapped.
+      neighborPtr = new arma::Mat<size_t>;
+    }
 
     if (treeOwner)
-      neighborPtr = new arma::Mat<size_t>; // All indices need mapping.
+      neighborPtr = new arma::Mat<size_t>; // Reference indices need mapping.
   }
 
   // Set the size of the neighbor and distance matrices.
diff --git a/src/mlpack/methods/range_search/range_search_impl.hpp b/src/mlpack/methods/range_search/range_search_impl.hpp
index a32ad73..3ac24f2 100644
--- a/src/mlpack/methods/range_search/range_search_impl.hpp
+++ b/src/mlpack/methods/range_search/range_search_impl.hpp
@@ -310,7 +310,10 @@ void RangeSearch<MetricType, MatType, TreeType>::Search(
     // Query indices only need to be mapped if we are building the query tree
     // ourselves.
     if (!singleMode && !naive)
+    {
       distancePtr = new std::vector<std::vector<double>>;
+      neighborPtr = new std::vector<std::vector<size_t>>;
+    }
 
     // Reference indices only need to be mapped if we built the reference tree
     // ourselves.
diff --git a/src/mlpack/methods/rann/ra_search_impl.hpp b/src/mlpack/methods/rann/ra_search_impl.hpp
index 58f4217..0c60234 100644
--- a/src/mlpack/methods/rann/ra_search_impl.hpp
+++ b/src/mlpack/methods/rann/ra_search_impl.hpp
@@ -160,7 +160,10 @@ Search(const MatType& querySet,
   if (tree::TreeTraits<Tree>::RearrangesDataset)
   {
     if (!singleMode && !naive)
+    {
       distancePtr = new arma::mat; // Query indices need to be mapped.
+      neighborPtr = new arma::Mat<size_t>;
+    }
 
     if (treeOwner)
       neighborPtr = new arma::Mat<size_t>; // All indices need mapping.
diff --git a/src/mlpack/tests/allknn_test.cpp b/src/mlpack/tests/allknn_test.cpp
index c5aa0ef..f678e4e 100644
--- a/src/mlpack/tests/allknn_test.cpp
+++ b/src/mlpack/tests/allknn_test.cpp
@@ -1115,4 +1115,31 @@ BOOST_AUTO_TEST_CASE(DoubleReferenceSearchTest)
   BOOST_REQUIRE_EQUAL(knn.Scores(), scores);
 }
 
+/**
+ * Make sure that the neighborPtr matrix isn't accidentally deleted.
+ * See issue #478.
+ */
+BOOST_AUTO_TEST_CASE(NeighborPtrDeleteTest)
+{
+  arma::mat dataset = arma::randu<arma::mat>(5, 100);
+
+  // Build the tree ourselves.
+  std::vector<size_t> oldFromNewReferences;
+  AllkNN::Tree tree(dataset);
+  AllkNN allknn(&tree);
+
+  // Now make a query set.
+  arma::mat queryset = arma::randu<arma::mat>(5, 50);
+  arma::mat distances;
+  arma::Mat<size_t> neighbors;
+  allknn.Search(queryset, 3, neighbors, distances);
+
+  // These will (hopefully) fail is either the neighbors or the distances matrix
+  // has been accidentally deleted.
+  BOOST_REQUIRE_EQUAL(neighbors.n_cols, 50);
+  BOOST_REQUIRE_EQUAL(neighbors.n_rows, 3);
+  BOOST_REQUIRE_EQUAL(distances.n_cols, 50);
+  BOOST_REQUIRE_EQUAL(distances.n_rows, 3);
+}
+
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/allkrann_search_test.cpp b/src/mlpack/tests/allkrann_search_test.cpp
index 0a1465f..852ce19 100644
--- a/src/mlpack/tests/allkrann_search_test.cpp
+++ b/src/mlpack/tests/allkrann_search_test.cpp
@@ -526,4 +526,31 @@ BOOST_AUTO_TEST_CASE(DualBallTreeTest)
 }
 */
 
+/**
+ * Make sure that the neighborPtr matrix isn't accidentally deleted.
+ * See issue #478.
+ */
+BOOST_AUTO_TEST_CASE(NeighborPtrDeleteTest)
+{
+  arma::mat dataset = arma::randu<arma::mat>(5, 100);
+
+  // Build the tree ourselves.
+  std::vector<size_t> oldFromNewReferences;
+  RASearch<>::Tree tree(dataset);
+  RASearch<> allkrann(&tree);
+
+  // Now make a query set.
+  arma::mat queryset = arma::randu<arma::mat>(5, 50);
+  arma::mat distances;
+  arma::Mat<size_t> neighbors;
+  allkrann.Search(queryset, 3, neighbors, distances);
+
+  // These will (hopefully) fail is either the neighbors or the distances matrix
+  // has been accidentally deleted.
+  BOOST_REQUIRE_EQUAL(neighbors.n_cols, 50);
+  BOOST_REQUIRE_EQUAL(neighbors.n_rows, 3);
+  BOOST_REQUIRE_EQUAL(distances.n_cols, 50);
+  BOOST_REQUIRE_EQUAL(distances.n_rows, 3);
+}
+
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/range_search_test.cpp b/src/mlpack/tests/range_search_test.cpp
index 7823ff3..f7ed67a 100644
--- a/src/mlpack/tests/range_search_test.cpp
+++ b/src/mlpack/tests/range_search_test.cpp
@@ -1378,4 +1378,30 @@ BOOST_AUTO_TEST_CASE(RSModelMonochromaticTest)
   }
 }
 
+/**
+ * Make sure that the neighborPtr matrix isn't accidentally deleted.
+ * See issue #478.
+ */
+BOOST_AUTO_TEST_CASE(NeighborPtrDeleteTest)
+{
+  arma::mat dataset = arma::randu<arma::mat>(5, 100);
+
+  // Build the tree ourselves.
+  std::vector<size_t> oldFromNewReferences;
+  RangeSearch<>::Tree tree(dataset);
+  RangeSearch<> ra(&tree);
+
+  // Now make a query set.
+  arma::mat queryset = arma::randu<arma::mat>(5, 50);
+  vector<vector<double>> distances;
+  vector<vector<size_t>> neighbors;
+  ra.Search(queryset, math::Range(0.2, 0.5), neighbors, distances);
+
+  // These will (hopefully) fail is either the neighbors or the distances matrix
+  // has been accidentally deleted.
+  BOOST_REQUIRE_EQUAL(neighbors.size(), 50);
+  BOOST_REQUIRE_EQUAL(distances.size(), 50);
+}
+
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list