Ver código fonte

added Matlab unit tests for Nice-Matlab-data Conversion functions

Johannes Ruehle 11 anos atrás
pai
commit
64bae94cdf

+ 1 - 1
matlab/ConverterMatlabToNICE.h

@@ -68,7 +68,7 @@ namespace NICE {
     std::string convertMatlabToString( const mxArray *matlabString );
 
     /**
-     * @brief Convert a Matlab int32 variable into an std::int
+     * @brief Convert a Matlab int32 scalar variable into an std::int
      *
      * @param matlabInt32 a matlab int32 variable
      * @return int

+ 10 - 4
matlab/Makefile

@@ -1,12 +1,18 @@
+MEX=/home/matlab/7.14/bin/mex
+#MEX=/home/matlab/8.2/academic/bin/mex
 NICEFLAGS1=$(shell pkg-config libgp-hik-core --cflags --libs)
 NICEFLAGS=$(subst -fopenmp,,$(NICEFLAGS1))
 
 default:
-	/home/matlab/7.14/bin/mex ${NICEFLAGS} -largeArrayDims GPHIKClassifierMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
-	/home/matlab/7.14/bin/mex ${NICEFLAGS} -largeArrayDims GPHIKRegressionMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims GPHIKClassifierMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims GPHIKRegressionMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims testMatlabConversionFunctionsMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
 
 classification:
-	/home/matlab/7.14/bin/mex ${NICEFLAGS} -largeArrayDims GPHIKClassifierMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims GPHIKClassifierMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
 
 regression:        
-	/home/matlab/7.14/bin/mex ${NICEFLAGS} -largeArrayDims GPHIKRegressionMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims GPHIKRegressionMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+
+unittest:        
+	${MEX} ${NICEFLAGS} -largeArrayDims testMatlabConversionFunctionsMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp

+ 137 - 0
matlab/testMatlabConversionFunctionsMex.cpp

@@ -0,0 +1,137 @@
+/** 
+* @file GPHIKClassifierMex.cpp
+* @author Alexander Freytag
+* @date 07-01-2014 (dd-mm-yyyy)
+* @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and  storing/re-storing.
+*/
+
+// STL includes
+#include <math.h>
+#include <matrix.h>
+#include <mex.h>
+
+// NICE-core includes
+#include <core/vector/MatrixT.h>
+#include <core/vector/VectorT.h>
+
+
+// Interface for conversion between Matlab and C objects
+#include "gp-hik-core/matlab/classHandleMtoC.h"
+#include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
+#include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
+
+using namespace std; //C basics
+using namespace NICE;  // nice-core
+
+// MAIN MATLAB FUNCTION
+void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
+{    
+    // get the command string specifying what to do
+    if (nrhs < 1)
+        mexErrMsgTxt("No commands and options passed... Aborting!");        
+    
+    if( !mxIsChar( prhs[0] ) )
+        mexErrMsgTxt("First argument needs to be the command, ie.e, the unit test method to call... Aborting!");
+    
+    std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
+    
+    // in all other cases, there should be a second input,
+    // which the be the class instance handle
+    if (nrhs < 2)
+    {
+        mexErrMsgTxt("Second input should be some kind of matrix variable");
+        return;
+    }
+
+    if (nlhs < 1)
+    {
+        mexErrMsgTxt("No return value defined, possible loss of data... Aborting!");
+    }
+
+    const mxArray *t_pData = prhs[1];
+    
+    ////////////////////////////////////////
+    //  Check which method to call  //
+    ////////////////////////////////////////
+
+
+    if ( !strcmp("convertInt32", cmd.c_str() ) )
+    {
+
+        int t_iTest = MatlabConversion::convertMatlabToInt32( t_pData );
+        std::cerr << "convertInt32: " << t_iTest << std::endl;
+
+        // output
+        plhs[0] = mxCreateDoubleScalar( t_iTest );
+
+        return;
+    }
+    else if ( !strcmp("convertLogical", cmd.c_str() ) )
+    {
+
+        bool t_iTest = MatlabConversion::convertMatlabToBool( t_pData );
+        std::cerr << "convertLogical: " << t_iTest << std::endl;
+
+        // output
+        plhs[0] = mxCreateLogicalScalar( t_iTest );
+
+        return;
+    }
+    else if ( !strcmp("convertDouble", cmd.c_str() ) )
+    {
+
+        double t_dTest = MatlabConversion::convertMatlabToDouble( t_pData );
+        std::cerr << "convertDouble: " << t_dTest << std::endl;
+
+        // output
+        plhs[0] = mxCreateDoubleScalar( t_dTest );
+
+        return;
+    }
+    /// Matrix/vector functions
+    else if ( !strcmp("convertDoubleVector", cmd.c_str() ) )
+    {
+
+        NICE::Vector t_vecTest = MatlabConversion::convertDoubleVectorToNice( t_pData );
+        std::cerr << "convertDoubleVector: " << t_vecTest << std::endl;
+
+        // output
+        plhs[0] = MatlabConversion::convertVectorFromNice( t_vecTest );
+
+        return;
+    }
+    else if ( !strcmp("convertDoubleMatrix", cmd.c_str() ) )
+    {
+
+        NICE::Matrix t_matTest = MatlabConversion::convertDoubleMatrixToNice( t_pData );
+        std::cerr << "convertDoubleMatrix: " << t_matTest << std::endl;
+
+        // output
+        plhs[0] = MatlabConversion::convertMatrixFromNice( t_matTest );
+
+        return;
+    }
+    else if ( !strcmp("convertDoubleSparseVector", cmd.c_str() ) )
+    {
+
+        NICE::SparseVector t_vecTest = MatlabConversion::convertSparseVectorToNice( t_pData );
+	
+	NICE::Vector t_fullVector;
+	t_vecTest.convertToVectorT( t_fullVector );
+        std::cerr << "convertDoubleSparseVector: full version:" << t_fullVector << std::endl;
+
+        // output
+        plhs[0] = MatlabConversion::convertVectorFromNice( t_fullVector );
+
+        return;
+    }
+      
+    
+    
+    // Got here, so command not recognized
+    
+    std::string errorMsg (cmd.c_str() );
+    errorMsg += " -- command not recognized.";
+    mexErrMsgTxt( errorMsg.c_str() );
+
+}

+ 98 - 0
matlab/unittestMatlabConversionFunctionsMex.m

@@ -0,0 +1,98 @@
+% brief:    Unit testing of the NICE::MatlabConversion functions
+% author:   Johannes Ruehle
+% date:     11-04-2014 (dd-mm-yyyy)
+%% test convertInt32
+t = int32( 23 );
+[r] = testMatlabConversionFunctionsMex( 'convertInt32', t );
+assert( t == r);
+
+t = single( 23 );
+try
+    [r] = testMatlabConversionFunctionsMex( 'convertInt32', t );
+catch ecpn
+    assert( strcmp( ecpn.message,'Expected int32'));
+end
+
+%% test logical
+t = true;
+[r] = testMatlabConversionFunctionsMex( 'convertLogical', t );
+assert( t == r);
+
+t = 1;
+try
+    [r] = testMatlabConversionFunctionsMex( 'convertLogical', t );
+catch ecpn
+    assert( strcmp( ecpn.message,'Expected bool'));
+end
+
+%% test convertDouble
+t = double( 23 );
+[r] = testMatlabConversionFunctionsMex( 'convertDouble', t );
+assert( t == r);
+
+t = single( 23 );
+try
+    [r] = testMatlabConversionFunctionsMex( 'convertDouble', t );
+catch ecpn
+    assert( strcmp( ecpn.message,'Expected double'));
+end
+
+t = double( [42, 23]);
+[r] = testMatlabConversionFunctionsMex( 'convertDouble', t );
+assert( t(1) == r);
+
+%% test convertDoubleVector
+t = double( 23 );
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleVector', t );
+assert( t == r);
+
+t = single( 23 );
+try
+    [r] = testMatlabConversionFunctionsMex( 'convertDoubleVector', t );
+catch ecpn
+    assert( strcmp( ecpn.message,'Expected double in convertDoubleVectorToNice'));
+end
+
+t = double( [42, 23]);
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleVector', t );
+%r is a row vector, not an column vector anymore
+assert( size(t,1) == size(r,2) && size(t,2) == size(r,1) && all( t == r' ) );
+
+t = double( [123; 234]);
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleVector', t );
+%r is a row vector, not an column vector
+assert( size(t,1) == size(r,1) && size(t,1) == size(r,1) && all( t == r ) );
+
+%% test convertDoubleMatrix
+t = double( 23 );
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleMatrix', t );
+assert( t == r);
+
+t = single( 23 );
+try
+    [r] = testMatlabConversionFunctionsMex( 'convertDoubleMatrix', t );
+catch ecpn
+    assert( strcmp( ecpn.message,'Expected double in convertDoubleMatrixToNice'));
+end
+
+t = double( [42, 23]);
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleMatrix', t );
+assert( size(t,1) == size(r,1) && size(t,1) == size(r,1) && all( t == r ) );
+
+t = double( [123; 234]);
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleMatrix', t );
+assert( size(t,1) == size(r,1) && size(t,1) == size(r,1) && all( t == r ) );
+
+t = rand(5,5,'double');
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleMatrix', t );
+d = r-t;
+assert( size(t,1) == size(r,1) && size(t,1) == size(r,1) && sum( d(:)) == 0 );
+
+%% test convertDoubleSparseVector
+
+t = sparse( 5,1);
+t(1) = 23; t(3) = 123; t(4) = 42;
+[r] = testMatlabConversionFunctionsMex( 'convertDoubleSparseVector', t );
+d = r-t;
+assert( size(t,1) == size(r,1) && size(t,1) == size(r,1) && sum( d(:)) == 0 );
+