[mlpack-svn] r15567 - in mlpack/branches/mlpack-bindings: . src/mlpack/bindings src/mlpack/methods/neighbor_search
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Jul 26 17:30:34 EDT 2013
Author: pyrotekniq
Date: Fri Jul 26 17:30:34 2013
New Revision: 15567
Log:
Changes to files:
- /CMakeLists.txt : Added command line switches SWIGPYTHON, SWIGR, SWIGOCTAVE
- /src/mlpack/bindings/CMakeLists.txt : Now recurses into ./python
- /src/mlpack/methods/neighbor_search/allknn_main.cpp : Refactored (see comments)
New files:
- /src/mlpack/bindings/python/allknn.i : SWIG interface file for allknn
- /src/mlpack/bindings/python/numpy.i : SWIG interface file for Python NumPy library
Modified:
mlpack/branches/mlpack-bindings/CMakeLists.txt
mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt
mlpack/branches/mlpack-bindings/src/mlpack/methods/neighbor_search/allknn_main.cpp
Modified: mlpack/branches/mlpack-bindings/CMakeLists.txt
==============================================================================
--- mlpack/branches/mlpack-bindings/CMakeLists.txt (original)
+++ mlpack/branches/mlpack-bindings/CMakeLists.txt Fri Jul 26 17:30:34 2013
@@ -11,6 +11,13 @@
# This is as of yet unused.
#option(PGO "Use profile-guided optimization if not a debug build" ON)
+# SWIG Options
+# - Additionally compile bindings (if ON).
+# - Python , Octave , R
+option(SWIGPYTHON "Generate and compile the Python bindings." OFF)
+option(SWIGOCTAVE "Generate and compile the Octave bindings." OFF)
+option(SWIGR "Generate and compile the R bindings." OFF)
+
# Set the CFLAGS and CXXFLAGS depending on the options the user specified.
# Only GCC-like compilers support -Wextra, and other compilers give tons of
# output for -Wall, so only -Wall and -Wextra on GCC.
Modified: mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt
==============================================================================
--- mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt (original)
+++ mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt Fri Jul 26 17:30:34 2013
@@ -1,4 +1,21 @@
# Recurse into individual binding subdirectories, if we are supposed to.
+
+
+#MATLAB ( Old / not SWIG )
if(MATLAB_BINDINGS)
add_subdirectory(matlab)
endif(MATLAB_BINDINGS)
+
+
+#SWIG
+if(SWIGPYTHON)
+ add_subdirectory(python)
+endif(SWIGPYTHON)
+
+if(SWIGR)
+ add_subdirectory(r)
+endif(SWIGR)
+
+if(SWIGOCTAVE)
+ add_subdirectory(octave)
+endif(SWIGOCTAVE)
Modified: mlpack/branches/mlpack-bindings/src/mlpack/methods/neighbor_search/allknn_main.cpp
==============================================================================
--- mlpack/branches/mlpack-bindings/src/mlpack/methods/neighbor_search/allknn_main.cpp (original)
+++ mlpack/branches/mlpack-bindings/src/mlpack/methods/neighbor_search/allknn_main.cpp Fri Jul 26 17:30:34 2013
@@ -47,20 +47,29 @@
PARAM_STRING_REQ("distances_file", "File to output distances into.", "d");
PARAM_STRING_REQ("neighbors_file", "File to output neighbors into.", "n");
-PARAM_INT_REQ("k", "Number of nearest neighbors to find.", "k");
+PARAM_INT_REQ("k", "Number of furthest neighbors to find.", "k");
PARAM_STRING("query_file", "File containing query points (optional).", "q", "");
PARAM_INT("leaf_size", "Leaf size for tree building.", "l", 20);
PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N");
PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to "
- "dual-tree search).", "S");
+ "dual-tree search).", "s");
PARAM_FLAG("cover_tree", "If true, use cover trees to perform the search "
"(experimental, may be slow).", "c");
PARAM_FLAG("random_basis", "Before tree-building, project the data onto a "
"random orthogonal basis.", "R");
PARAM_INT("seed", "Random seed (if 0, std::time(NULL) is used).", "s", 0);
+
+
+//Function prototypes for allKnn and main (consider renaming allKnn)
+void allKnn(arma::mat &referenceData, arma::mat &queryData, arma::mat &distances, arma::Mat<size_t> &neighbors, int lsInt, size_t k, bool naive, bool singleMode, bool randomBasis, bool coverTree);
+void saveOutput(string distancesFile, string neighborsFile, arma::mat &distances, arma::Mat<size_t> &neighbors);
+//End prototypes
+
+
+
int main(int argc, char *argv[])
{
// Give CLI the command line parameters the user passed in.
@@ -84,7 +93,8 @@
bool naive = CLI::HasParam("naive");
bool singleMode = CLI::HasParam("single_mode");
- const bool randomBasis = CLI::HasParam("random_basis");
+ bool randomBasis = CLI::HasParam("random_basis"); //CHANGED FROM CONST BOOL -> BOOL
+ bool coverTree = CLI::HasParam("cover_tree"); //ADDED
arma::mat referenceData;
arma::mat queryData; // So it doesn't go out of scope.
@@ -100,6 +110,29 @@
<< queryData.n_rows << " x " << queryData.n_cols << ")." << endl;
}
+
+ //Initialize some containers to hold results
+
+ arma::mat distances;
+
+ arma::Mat< size_t > neighbors;
+
+
+
+ //Call the allknn function, passing all containers by reference
+
+ allKnn(referenceData, queryData, distances, neighbors, lsInt, k, naive, singleMode, randomBasis, coverTree);
+
+
+
+ //Call the saveData function with the result from allknn function
+
+ saveOutput(distancesFile, neighborsFile, distances, neighbors);
+}
+
+
+//allknn function
+void allKnn(arma::mat &referenceData, arma::mat &queryData, arma::mat &distances, arma::Mat<size_t> &neighbors, int lsInt, size_t k, bool naive, bool singleMode, bool randomBasis, bool coverTree){
// Sanity check on k value: must be greater than 0, must be less than the
// number of reference points.
if (k > referenceData.n_cols)
@@ -155,7 +188,7 @@
if (arma::det(q) >= 0)
{
referenceData = q * referenceData;
- if (queryFile != "")
+ if (queryData.n_elem > 0) //CHECK IF QUERYDATA IS INITIALIZED BY LOOKING AT WHETHER IT HAS MORE THAN 0 ELEMENTS... REPLACEMENT FOR EMPTY QUERYFILE STRING CHECK
queryData = q * queryData;
break;
}
@@ -163,10 +196,7 @@
}
}
- arma::Mat<size_t> neighbors;
- arma::mat distances;
-
- if (!CLI::HasParam("cover_tree"))
+ if (!coverTree) //CHECK IF COVERTREE FALSE... REPLACEMENT FOR CLI PARAM CHECK
{
// Because we may construct it differently, we need a pointer.
AllkNN* allknn = NULL;
@@ -188,14 +218,14 @@
std::vector<size_t> oldFromNewQueries;
- if (CLI::GetParam<string>("query_file") != "")
+ if (queryData.n_elem > 0) //REPLACE CLI PARAM CHECK WITH CHECK FOR n_elem > 0 in QUERYDATA
{
if (naive && leafSize < queryData.n_cols)
leafSize = queryData.n_cols;
- Log::Info << "Loaded query data from '" << queryFile << "' ("
- << queryData.n_rows << " x " << queryData.n_cols << ")." << endl;
-
+ //Log::Info << "Loaded query data from '" << queryFile << "' (" //THIS SEEMS OUT OF PLACE... COMMENTED OUT FOR NOW
+ // << queryData.n_rows << " x " << queryData.n_cols << ")." << endl; // -- -- -- -- --
+
Log::Info << "Building query tree..." << endl;
// Build trees by hand, so we can save memory: if we pass a tree to
@@ -236,10 +266,10 @@
Log::Info << "Re-mapping indices..." << endl;
// Map the results back to the correct places.
- if ((CLI::GetParam<string>("query_file") != "") && !singleMode)
+ if ((queryData.n_elem > 0) && !singleMode) //CLI CHECK REPLACED, SEE ABOVE
Unmap(neighborsOut, distancesOut, oldFromNewRefs, oldFromNewQueries,
neighbors, distances);
- else if ((CLI::GetParam<string>("query_file") != "") && singleMode)
+ else if ((queryData.n_elem > 0) && singleMode) // --
Unmap(neighborsOut, distancesOut, oldFromNewRefs, neighbors, distances);
else
Unmap(neighborsOut, distancesOut, oldFromNewRefs, oldFromNewRefs,
@@ -270,7 +300,7 @@
QueryStat<NearestNeighborSort> > >* allknn = NULL;
// See if we have query data.
- if (CLI::HasParam("query_file"))
+ if (queryData.n_elem > 0) //CLI CHECK REPLACED
{
// Build query tree.
if (!singleMode)
@@ -307,7 +337,13 @@
delete queryTree;
}
- // Save output.
+}
+
+
+
+// Save output.
+void saveOutput(string distancesFile, string neighborsFile, arma::mat &distances, arma::Mat<size_t> &neighbors){
+
data::Save(distancesFile, distances);
data::Save(neighborsFile, neighbors);
}
More information about the mlpack-svn
mailing list