Skip to content

Commit 3670a6c

Browse files
updated Matrix unit test
1 parent 137de06 commit 3670a6c

File tree

9 files changed

+316
-64
lines changed

9 files changed

+316
-64
lines changed

GRT/DataStructures/ClassificationData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class ClassificationData : public GRTBase{
450450
451451
@return an unsigned int representing the number of classes
452452
*/
453-
UINT inline getNumClasses() const{ return (UINT)classTracker.size(); }
453+
UINT inline getNumClasses() const{ return classTracker.getSize(); }
454454

455455
/**
456456
Gets the minimum class label in the dataset. If there are no values in the dataset then the value 99999 will be returned.

GRT/DataStructures/Matrix.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
@file
33
@author Nicholas Gillian <[email protected]>
4-
@version 1.0
54
65
@brief The Matrix class is a basic class for storing any type of data. This class is a template and can therefore be used with any generic data type.
76
*/
@@ -115,7 +114,7 @@ template <class T> class Matrix{
115114
if( resize(tempRows,tempCols) ){
116115
for(unsigned int i=0; i<tempRows; i++){
117116
for(unsigned int j=0; j<tempCols; j++){
118-
dataPtr[i][j] = data[i][j];
117+
dataPtr[(i*cols)+j] = data[i][j];
119118
}
120119
}
121120
}
@@ -326,20 +325,33 @@ template <class T> class Matrix{
326325
}
327326

328327
/**
328+
@deprecated
329+
This function is now depreciated! You should use setAll(const T &value) instead.
330+
329331
Sets all the values in the Matrix to the input value
330332
331333
@param value: the value you want to set all the Matrix values to
332334
@return returns true or false, indicating if the set was successful
333335
*/
334-
bool setAllValues(const T &value){
335-
if(dataPtr!=NULL){
336+
bool setAllValues(const T &value){
337+
return setAll( value );
338+
}
339+
340+
/**
341+
Sets all the values in the Matrix to the input value
342+
343+
@param value: the value you want to set all the Matrix values to
344+
@return returns true or false, indicating if the set was successful
345+
*/
346+
bool setAll(const T &value){
347+
if(dataPtr!=NULL){
336348
unsigned int i =0;
337-
for(i=0; i<size; i++)
349+
for(i=0; i<size; i++)
338350
dataPtr[i] = value;
339351
return true;
340-
}
352+
}
341353
return false;
342-
}
354+
}
343355

344356
/**
345357
Sets all the values in the row at rowIndex with the values in the vector called row.

GRT/Util/GRTTypedefs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class grt_numeric_limits{
6868
static T max() { return std::numeric_limits< T >::max(); }
6969
};
7070

71-
inline Float grt_sqr( const float &x ){ return x*x; }
71+
inline Float grt_sqr( const Float &x ){ return x*x; }
7272

73-
inline Float grt_sqrt( const float &x ){ return sqrt(x); }
73+
inline Float grt_sqrt( const Float &x ){ return sqrt(x); }
7474

7575
inline Float grt_antilog( const Float &x ){ return exp( x ); }
7676

@@ -219,4 +219,4 @@ typedef MatrixFloat MatrixDouble;
219219

220220
GRT_END_NAMESPACE
221221

222-
#endif //GRT_TYPEDEFS_HEADER
222+
#endif //GRT_TYPEDEFS_HEADER

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Key things to know about the GRT:
1414
* The toolkit consists of two parts: a comprehensive **C++ API** and a front-end **graphical user interface (GUI)**. You can access the source code for both the C++ API and GUI in this repository, a precompiled version of the GUI can be downloaded [here](http://www.nickgillian.com/wiki/pmwiki.php/GRT/Download)
1515
* Both the C++ API and GUI are designed to work with real-time sensor data, but they can also be used for more conventional offline machine-learning tasks
1616
* The input to the GRT can be any *N*-dimensional floating-point vector - this means you can use the GRT with Cameras, Kinect, Leap Motion, accelerometers, or any other custom sensor you might have built
17-
* The toolkit defines a generic **float_t** type, this defaults to double precision float, but can easily be changed to single precision via the main GRT Typedefs header
18-
* The precision of the GRT VectorFloat and MatrixFloat classes is automatically updated based on the main float_t precision
17+
* The toolkit defines a generic **Float** type, this defaults to double precision float, but can easily be changed to single precision via the main GRT Typedefs header
18+
* The precision of the GRT VectorFloat and MatrixFloat classes is automatically updated based on the main Float precision
1919
* The toolkit reserves the class label value of zero as a special **null gesture** class label for automatic gesture spotting, so if you want to use gesture spotting avoid labelling any of your gestures with the class label of zero
2020
* Training data and models are saved as custom **.grt** files. These consist of a simple header followed by the main dataset. In addition to the grt files, you can also import/export data via CSV files by using the *.csv* file extension when saving/loading files
2121
* Almost all the GRT classes support the following functions:
@@ -78,8 +78,8 @@ int main (int argc, const char * argv[])
7878
{
7979
//Parse the training data filename from the command line
8080
if( argc != 2 ){
81-
cout << "Error: failed to parse data filename from command line. You should run this example with one argument pointing to a data file\n";
82-
return EXIT_FAILURE;
81+
cout << "Error: failed to parse data filename from command line. You should run this example with one argument pointing to a data file\n";
82+
return EXIT_FAILURE;
8383
}
8484
const string filename = argv[1];
8585

@@ -88,8 +88,8 @@ int main (int argc, const char * argv[])
8888

8989
cout << "Loading dataset..." << endl;
9090
if( !trainingData.load( filename ) ){
91-
cout << "ERROR: Failed to load training data from file\n";
92-
return EXIT_FAILURE;
91+
cout << "ERROR: Failed to load training data from file\n";
92+
return EXIT_FAILURE;
9393
}
9494

9595
cout << "Data Loaded" << endl;

jni/grt.i

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ using namespace GRT;
1313
%include "std_string.i"
1414

1515
// ———— Vector ————————————
16-
%template(VectorDoubleBase) std::vector<double>;
16+
%template(VectorFloatBase) GRT::VectorFloat;
1717
%include "../GRT/Util/GRTTypedefs.h"
18+
// instantiate Vector, just to give SWIG something to do with it
19+
// %template(VectorFloatBase) GRT::VectorFloat;
20+
// %include "../GRT/DataStructures/VectorFloat.h"
1821

1922
// ———— Matrix ———————————–
20-
%include "../GRT/Util/Matrix.h"
23+
%include "../GRT/DataStructures/Matrix.h"
2124
// instantiate Matrix, just to give SWIG something to do with it
22-
// %template(MatrixDoubleBase) GRT::Matrix<double>;
23-
// %include "../GRT/Util/MatrixDouble.h"
24-
25+
// %template(MatrixFloatBase) GRT::MatrixFloat;
26+
// %include "../GRT/DataStructures/MatrixFloat.h"
2527

2628
// ——————————————————-
2729
/* For the rest, let’s just grab the original header files here and

tests/DataStructures/MatrixTest.cpp

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include <GRT.h>
2+
#include "gtest/gtest.h"
3+
using namespace GRT;
4+
5+
//Unit tests for the GRT Matrix class
6+
7+
// Tests the default c'tor.
8+
TEST(Matrix, DefaultConstructor) {
9+
Matrix< int > mat;
10+
11+
EXPECT_EQ(0, mat.getSize());
12+
EXPECT_EQ(0, mat.getNumRows());
13+
EXPECT_EQ(0, mat.getNumCols());
14+
}
15+
16+
// Tests the resize c'tor.
17+
TEST(Matrix, ResizeConstructor) {
18+
const UINT numRows = 100;
19+
const UINT numCols = 50;
20+
Matrix< int > mat( numRows, numCols );
21+
EXPECT_EQ(numRows*numCols, mat.getSize());
22+
EXPECT_EQ(numRows, mat.getNumRows());
23+
EXPECT_EQ(numCols, mat.getNumCols());
24+
}
25+
26+
// Tests the copy c'tor.
27+
TEST(Matrix, CopyConstructor) {
28+
const UINT numRows = 100;
29+
const UINT numCols = 50;
30+
Matrix< int > mat1( numRows, numCols );
31+
EXPECT_EQ(numRows, mat1.getNumRows());
32+
EXPECT_EQ(numCols, mat1.getNumCols());
33+
Matrix< int > mat2( mat1 );
34+
EXPECT_EQ(numRows, mat2.getNumRows());
35+
EXPECT_EQ(numCols, mat2.getNumCols());
36+
}
37+
38+
// Tests the equals operator.
39+
TEST(Matrix, EqualsConstructor) {
40+
const UINT numRows = 100;
41+
const UINT numCols = 50;
42+
Matrix< int > mat1( numRows, numCols );
43+
EXPECT_EQ(numRows, mat1.getNumRows());
44+
EXPECT_EQ(numCols, mat1.getNumCols());
45+
Matrix< int > mat2;
46+
mat2 = mat1;
47+
EXPECT_EQ(numRows, mat2.getNumRows());
48+
EXPECT_EQ(numCols, mat2.getNumCols());
49+
}
50+
51+
// Tests the Vector c'tor.
52+
TEST(Matrix, VectorConstructor) {
53+
const UINT numRows = 100;
54+
const UINT numCols = 50;
55+
Vector< Vector< int > > vec( numRows, Vector< int >( numCols ) );
56+
for(UINT i=0; i<numRows; i++){
57+
for(UINT j=0; j<numCols; j++){
58+
vec[i][j] = 1;
59+
}
60+
}
61+
62+
Matrix< int > mat( vec );
63+
EXPECT_EQ(numRows, mat.getNumRows());
64+
EXPECT_EQ(numCols, mat.getNumCols());
65+
for(UINT i=0; i<numRows; i++){
66+
for(UINT j=0; j<numCols; j++){
67+
EXPECT_EQ(mat[i][j],vec[i][j]);
68+
}
69+
}
70+
}
71+
72+
// Tests the [] operator
73+
TEST(Matrix, AccessOperator) {
74+
const UINT numRows = 100;
75+
const UINT numCols = 50;
76+
Matrix< int > mat( numRows, numCols );
77+
EXPECT_EQ(numRows, mat.getNumRows());
78+
EXPECT_EQ(numCols, mat.getNumCols());
79+
for(UINT i=0; i<numRows; i++){
80+
for(UINT j=0; j<numCols; j++){
81+
mat[i][j] = i*j;
82+
}
83+
}
84+
for(UINT i=0; i<numRows; i++){
85+
for(UINT j=0; j<numCols; j++){
86+
EXPECT_EQ(mat[i][j],i*j);
87+
}
88+
}
89+
}
90+
91+
// Tests the getRowVector
92+
TEST(Matrix, GetRowVector) {
93+
const UINT numRows = 100;
94+
const UINT numCols = 50;
95+
Matrix< int > mat( numRows, numCols );
96+
EXPECT_EQ(numRows, mat.getNumRows());
97+
EXPECT_EQ(numCols, mat.getNumCols());
98+
for(UINT i=0; i<numRows; i++){
99+
for(UINT j=0; j<numCols; j++){
100+
mat[i][j] = i*j;
101+
}
102+
}
103+
for(UINT i=0; i<numRows; i++){
104+
Vector< int > rowVector = mat.getRowVector( i );
105+
for(UINT j=0; j<numCols; j++){
106+
EXPECT_EQ(rowVector[j],i*j);
107+
}
108+
}
109+
}
110+
111+
// Tests the getColVector
112+
TEST(Matrix, GetColVector) {
113+
const UINT numRows = 100;
114+
const UINT numCols = 50;
115+
Matrix< int > mat( numRows, numCols );
116+
EXPECT_EQ(numRows, mat.getNumRows());
117+
EXPECT_EQ(numCols, mat.getNumCols());
118+
for(UINT i=0; i<numRows; i++){
119+
for(UINT j=0; j<numCols; j++){
120+
mat[i][j] = i*j;
121+
}
122+
}
123+
for(UINT j=0; j<numCols; j++){
124+
Vector< int > colVector = mat.getColVector( j );
125+
for(UINT i=0; i<numRows; i++){
126+
EXPECT_EQ(colVector[i],i*j);
127+
}
128+
}
129+
}
130+
131+
// Tests the resize
132+
TEST(Matrix, Resize) {
133+
const UINT numRows = 100;
134+
const UINT numCols = 50;
135+
const UINT newRows = 200;
136+
const UINT newCols = 100;
137+
Matrix< int > mat( numRows, numCols );
138+
EXPECT_EQ(numRows, mat.getNumRows());
139+
EXPECT_EQ(numCols, mat.getNumCols());
140+
EXPECT_TRUE( mat.resize( newRows, newCols ) );
141+
EXPECT_EQ(newRows, mat.getNumRows());
142+
EXPECT_EQ(newCols, mat.getNumCols());
143+
}
144+
145+
// Tests the copy
146+
TEST(Matrix, Copy) {
147+
const UINT numRows = 100;
148+
const UINT numCols = 50;
149+
Matrix< int > mat1( numRows, numCols );
150+
EXPECT_EQ(numRows, mat1.getNumRows());
151+
EXPECT_EQ(numCols, mat1.getNumCols());
152+
for(UINT i=0; i<numRows; i++){
153+
for(UINT j=0; j<numCols; j++){
154+
mat1[i][j] = i*j;
155+
}
156+
}
157+
Matrix< int > mat2;
158+
EXPECT_TRUE( mat2.copy( mat1 ) );
159+
EXPECT_EQ(numRows, mat2.getNumRows());
160+
EXPECT_EQ(numCols, mat2.getNumCols());
161+
for(UINT i=0; i<numRows; i++){
162+
for(UINT j=0; j<numCols; j++){
163+
EXPECT_EQ( mat1[i][j], mat2[i][j] );
164+
}
165+
}
166+
}
167+
168+
// Tests the setAllValues
169+
TEST(Matrix, SetAll) {
170+
const UINT numRows = 100;
171+
const UINT numCols = 50;
172+
Matrix< int > mat( numRows, numCols );
173+
EXPECT_EQ(numRows, mat.getNumRows());
174+
EXPECT_EQ(numCols, mat.getNumCols());
175+
for(UINT i=0; i<numRows; i++){
176+
for(UINT j=0; j<numCols; j++){
177+
mat[i][j] = i*j;
178+
}
179+
}
180+
EXPECT_TRUE( mat.setAll( 0 ) );
181+
for(UINT i=0; i<numRows; i++){
182+
for(UINT j=0; j<numCols; j++){
183+
EXPECT_EQ( mat[i][j], 0 );
184+
}
185+
}
186+
}
187+
188+
// Tests the setRowVector
189+
TEST(Matrix, SetRowVector) {
190+
const UINT numRows = 100;
191+
const UINT numCols = 50;
192+
Matrix< int > mat( numRows, numCols );
193+
EXPECT_EQ(numRows, mat.getNumRows());
194+
EXPECT_EQ(numCols, mat.getNumCols());
195+
for(UINT i=0; i<numRows; i++){
196+
for(UINT j=0; j<numCols; j++){
197+
mat[i][j] = 0;
198+
}
199+
}
200+
Vector< int > vec( numCols );
201+
EXPECT_TRUE( vec.setAll( 1000 ) );
202+
EXPECT_TRUE( mat.setRowVector( vec, 0 ) );
203+
for(UINT i=0; i<numRows; i++){
204+
for(UINT j=0; j<numCols; j++){
205+
EXPECT_EQ( mat[i][j], i == 0 ? 1000 : 0 );
206+
}
207+
}
208+
}
209+
210+
int main(int argc, char **argv) {
211+
::testing::InitGoogleTest( &argc, argv );
212+
return RUN_ALL_TESTS();
213+
}

tests/DataStructures/VectorFloatTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include <GRT.h>
32
#include "gtest/gtest.h"
43
using namespace GRT;

0 commit comments

Comments
 (0)