Skip to content

Commit 989663e

Browse files
author
allywarner
committed
Center Matrix Test is working!
1 parent 6e23a67 commit 989663e

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/Core/Algorithms/Math/ComputePCA.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void ComputePCAAlgo::run(MatrixHandle input, DenseMatrixHandle& LeftPrinMat, Den
3939
{
4040
if (matrix_is::dense(input))
4141
{
42+
4243
auto denseInput = matrix_cast::as_dense(input);
4344

4445
auto rows = denseInput->rows();
@@ -61,6 +62,18 @@ void ComputePCAAlgo::run(MatrixHandle input, DenseMatrixHandle& LeftPrinMat, Den
6162
}
6263
}
6364

65+
DenseMatrix ComputePCAAlgo::centerData(DenseMatrixHandle input_matrix)
66+
{
67+
auto denseInput = matrix_cast::as_dense(input_matrix);
68+
69+
auto rows = denseInput->rows();
70+
71+
auto centerMatrix = Eigen::MatrixXd::Identity(rows,rows) - (1.0/rows)*Eigen::MatrixXd::Constant(rows,rows,1);
72+
73+
auto denseInputCentered = centerMatrix * *denseInput;
74+
75+
return denseInputCentered.eval();
76+
}
6477

6578
AlgorithmOutput ComputePCAAlgo::run_generic(const AlgorithmInput& input) const
6679
{

src/Core/Algorithms/Math/ComputePCA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace SCIRun {
4343
static AlgorithmOutputName RightPrincipalMatrix;
4444
void run(Datatypes::MatrixHandle input_matrix, Datatypes::DenseMatrixHandle& LeftPrinMat, Datatypes::DenseMatrixHandle& PrinVals, Datatypes::DenseMatrixHandle& RightPrinMat) const;
4545
virtual AlgorithmOutput run_generic(const AlgorithmInput& input) const;
46+
static Datatypes::DenseMatrix centerData(Datatypes::DenseMatrixHandle input_matrix);
4647
};
4748

4849
}}}}

src/Core/Algorithms/Math/Tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SET(Algorithms_Math_Tests_SRCS
3939
ConvertMatrixTypeTests.cc
4040
SelectSubMatrixTests.cc
4141
GetMatrixSliceAlgoTests.cc
42+
ComputePCAtest.cc
4243
)
4344

4445
#SET(Engine_Network_Tests_HEADERS
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2015 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
License for the specific language governing rights and limitations under
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#include <gtest/gtest.h>
30+
31+
#include <Core/Datatypes/DenseMatrix.h>
32+
#include <Core/Datatypes/MatrixComparison.h>
33+
#include <Testing/Utils/MatrixTestUtilities.h>
34+
#include <Core/Algorithms/Math/ComputePCA.h>
35+
36+
using namespace SCIRun::Core::Datatypes;
37+
using namespace SCIRun::Core::Algorithms::Math;
38+
using namespace SCIRun::TestUtils;
39+
40+
41+
namespace
42+
{
43+
//Matrix for input
44+
DenseMatrixHandle inputMatrix()
45+
{
46+
int column1 [12] = {5,4,7,8,10,10,4,6,8,7,9,6};
47+
int column2 [12] = {7,6,9,8,5,7,9,3,4,5,5,9};
48+
49+
DenseMatrixHandle inputM(boost::make_shared<DenseMatrix>(12,2));
50+
for (int i = 0; i < inputM->rows(); i++){
51+
(*inputM)(i,0) = column1[i];
52+
(*inputM)(i,1) = column2[i];
53+
}
54+
return inputM;
55+
}
56+
57+
//A after it is centered
58+
DenseMatrixHandle centeredInputMatrix()
59+
{
60+
61+
double centeredColumn1 [12] = {-2.0,-3.0,-0.0,1.0,3.0,3.0,-3.0,-1.0,1.0,-0.0,2.0,-1.0};
62+
double centeredColumn2 [12] = {0.583333,-0.416667,2.583333,1.583333,-1.416667,0.583333,2.583333,-3.416667,-2.416667,-1.416667,-1.416667,2.583333};
63+
64+
DenseMatrixHandle centeredM(boost::make_shared<DenseMatrix>(12,2));
65+
for (int i = 0; i < centeredM->rows(); i++){
66+
(*centeredM)(i,0) = centeredColumn1[i];
67+
(*centeredM)(i,1) = centeredColumn2[i];
68+
}
69+
return centeredM;
70+
71+
}
72+
73+
//Outputs: U,S,V
74+
DenseMatrixHandle outputU()
75+
{}
76+
DenseMatrixHandle outputS()
77+
{}
78+
DenseMatrixHandle outputV()
79+
{}
80+
81+
}
82+
83+
84+
85+
TEST(ComputePCAtest, CenterData)
86+
{
87+
ComputePCAAlgo algo;
88+
89+
DenseMatrixHandle m1(inputMatrix());
90+
91+
auto centered = ComputePCAAlgo::centerData(m1);
92+
93+
auto expected = *centeredInputMatrix();
94+
95+
for (int i = 0; i < centered.rows(); ++i) {
96+
for (int j = 0; j < centered.cols(); ++j)
97+
EXPECT_NEAR(expected(i,j), centered(i,j), 1e-5);
98+
}
99+
}
100+
101+
//TEST(ComputePCAtest, output1)
102+
//{
103+
// ComputePCAAlgorithm algo;
104+
//
105+
// DenseMatrixHandle m1(testMatrix());
106+
// ComputePCAAlgorithm::Outputs result = algo.run(ComputePCAAlgorithm::Inputs(m1),ComputePCAAlgorithm);
107+
//
108+
//}
109+
//
110+
//TEST(ComputePCAtest, output2)
111+
//{
112+
// ComputePCAAlgorithm algo;
113+
//
114+
// DenseMatrixHandle m1(testMatrix());
115+
//
116+
//}
117+
//
118+
//TEST(ComputePCAtest, output3)
119+
//{
120+
// ComputePCAAlgorithm algo;
121+
//
122+
// DenseMatrixHandle m1(testMatrix());
123+
//
124+
//}

0 commit comments

Comments
 (0)