Browse Source

minor fix to Matlab unit test for data conversion

Johannes Ruehle 11 năm trước cách đây
mục cha
commit
3b8313ecf9

+ 0 - 69
features/simplefeatures/matlab/HelperDataConversionMex.h

@@ -1,69 +0,0 @@
-#ifndef HELPERDATACONVERSIONMEX_H
-#define HELPERDATACONVERSIONMEX_H
-
-// 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/ConverterMatlabToNICE.h"
-#include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
-
-#include "vislearning/cbaselib/Example.h"
-
-namespace NICE {
-
-namespace MatlabConversion {
-
-/**
- * @brief Create an Examples class from a given full matrix of features and a matrix/vector of labels.
- *
- * The Examples object consists of individual Example objects containing the label and a pointer to the provided raw feature data.
- * Note: No feature data is copied - an Example only contains a pointer to the raw double data.
- * An NICE::Vector is created as an wrapper around this raw double pointer using it, but not copying it.
- * You need to take care to delete these wrapper vectors once you're finished working with the Examples object, otherwise you generate a memory leak.
- *
- * @param p_pArrTrainData double MATLAB matrix containing the features (dimension M) of N samples ( M x N matrix )
- * @param p_pArrTrainLabels double MATLAB matrix containing the labels for N samples (1xN)
- *
- * @param p_ExamplesTrain created Examples class (vector of N Example object with each Example containing a valid vec-ptr to the feature data [uncopied] )
- *
- * @return true for successful Examples creation
- * @author Johannes Ruehle
- */
-bool convertDoubleRawPointersToExamples( const mxArray *p_pArrTrainData, const mxArray *p_pArrTrainLabels, OBJREC::Examples &p_ExamplesTrain )
-{
-
-    int iNumFeatureDimension = mxGetM( p_pArrTrainData ); // feature dimensions
-
-    NICE::Vector yValuesTrain = convertDoubleVectorToNice( p_pArrTrainLabels );
-    NICE::Matrix matDataTrain = convertDoubleMatrixToNice( p_pArrTrainData   );
-    assert( yValuesTrain.size() == matDataTrain.cols() );
-    assert( iNumFeatureDimension == matDataTrain.rows() );
-
-    p_ExamplesTrain.reserve( matDataTrain.cols() );
-
-    const double *pDataPtr = matDataTrain.getDataPointer();
-
-    for (int i = 0; i < (int)matDataTrain.cols(); i++, pDataPtr+= iNumFeatureDimension )
-    {
-        NICE::Vector *t_pVecTrainData = new NICE::Vector( pDataPtr , (size_t)iNumFeatureDimension);
-        OBJREC::Example t_Example;
-        t_Example.vec = t_pVecTrainData;
-
-        p_ExamplesTrain.push_back( std::pair<int, OBJREC::Example>( (int)yValuesTrain[i], t_Example ) );
-    }
-
-    return true;
-}
-
-}
-
-}
-
-#endif //HELPERDATACONVERSIONMEX_H

+ 7 - 15
features/simplefeatures/matlab/testHelperDataConversionMex.cpp

@@ -14,14 +14,13 @@
 #include <core/vector/MatrixT.h>
 #include <core/vector/VectorT.h>
 
+#include "vislearning/cbaselib/Example.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"
 
-#include "HelperDataConversionMex.h"
-
 using namespace std; //C basics
 using namespace NICE;  // nice-core
 
@@ -65,9 +64,13 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         const mxArray *t_pArrTrainData   = prhs[1];
         const mxArray *t_pArrTrainLabels = prhs[2];
 
+        NICE::Vector t_vecLabelsTrain = MatlabConversion::convertDoubleVectorToNice( t_pArrTrainLabels );
+        NICE::Matrix t_matDataTrain   = MatlabConversion::convertDoubleMatrixToNice( t_pArrTrainData   );
+
         OBJREC::Examples t_ExamplesTrain;
 
-            bool bConversionSuccess = MatlabConversion::convertDoubleRawPointersToExamples( t_pArrTrainData, t_pArrTrainLabels, t_ExamplesTrain );
+        bool bConversionSuccess = OBJREC::Examples::wrapExamplesAroundFeatureMatrix( t_matDataTrain, t_vecLabelsTrain, t_ExamplesTrain );
+        //wrapExamplesAroundFeatureMatrix
 
         std::cerr << "Examples size: " << t_ExamplesTrain.size() << std::endl;
         for(int i=0; i< t_ExamplesTrain.size(); i++)
@@ -80,18 +83,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         }
 
         // clean up
-        for(int i=0; i< t_ExamplesTrain.size(); i++)
-        {
-            OBJREC::Example &t_Example = t_ExamplesTrain[i].second;
-
-            if (t_Example.vec != NULL )
-            {
-                delete t_Example.vec;
-                t_Example.vec = NULL;
-            }
-
-        }
-
+        t_ExamplesTrain.clean();
 
         // output
         plhs[0] = mxCreateLogicalScalar( bConversionSuccess );