ProteoWizard
DemuxSolverTest.cpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Austin Keller <atkeller .@. uw.edu>
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 
23 
24 using namespace pwiz::util;
25 using namespace pwiz::analysis;
26 using namespace pwiz::msdata;
27 
29 public:
30  void Run()
31  {
32  SetUp();
33  NNLSSolverTest();
34  TearDown();
35  }
36 
37 protected:
38 
39  virtual void SetUp()
40  {
41  }
42 
43  void TearDown()
44  {
45  }
46 
48  {
49  // Assume an expected solution
50  vector<double> expectedSolution = {
51  0.0,
52  0.0,
53  0.0,
54  11.0,
55  13.0,
56  0.0,
57  0.0
58  };
59 
60  // Assume that the trailing precursor window that is only half represented has no spectral contribution from the unseen portion
61  double trailingWindowIntensity = 0.0;
62 
63  TestNNLSGivenSolution(expectedSolution, trailingWindowIntensity);
64 
65  // Try a more dense solution
66  expectedSolution = {
67  5.0,
68  3.0,
69  2.0,
70  11.0,
71  13.0,
72  9.0,
73  3.0
74  };
75 
76  TestNNLSGivenSolution(expectedSolution, trailingWindowIntensity);
77  }
78 
79  void TestNNLSGivenSolution(const vector<double>& expectedSolution, double trailingWindowIntensity)
80  {
81  NNLSSolver solver;
82  MatrixPtr signal;
83  MatrixPtr masks;
84  MatrixPtr solution;
85  int numSpectra = 7;
86  int numDemuxWindows = 7;
87  int numTransitions = 1;
88  signal.reset(new MatrixType(numSpectra, numTransitions));
89  masks.reset(new MatrixType(numSpectra, numDemuxWindows));
90  solution.reset(new MatrixType(numDemuxWindows, numTransitions));
91 
92  /*
93  * Create mask matrix of the form
94  * 1 1 0 0 0 0 0 \ \00000
95  * 0 1 1 0 0 0 0 0\ \0000
96  * 0 0 1 1 0 0 0 00\ \000
97  * 0 0 0 1 1 0 0 000\ \00
98  * 0 0 0 0 1 1 0 0000\ \0
99  * 0 0 0 0 0 1 1 00000\ j = i + 1
100  * 0 0 0 0 0 0 1 000000j = i
101  *
102  * This mask matrix is used in overlap demultiplexing
103  */
104  for (int i = 0; i < numSpectra; ++i)
105  {
106  for (int j = 0; j < numDemuxWindows; ++j)
107  {
108  if (j == i || j == i + 1)
109  {
110  masks->row(i)[j] = 1.0;
111  }
112  else
113  {
114  masks->row(i)[j] = 0.0;
115  }
116  }
117  }
118 
119  // Create a multiplexed signal from the expected solution
120  vector<double> signalVec;
121  for (int i = 0; i < numSpectra; ++i)
122  {
123  double signalSum = expectedSolution[i];
124  if (i + 1 < numSpectra)
125  signalSum += expectedSolution[i + 1];
126  else
127  signalSum += trailingWindowIntensity;
128  signalVec.push_back(signalSum);
129  }
130  for (size_t i = 0; i < signalVec.size(); ++i)
131  {
132  signal->row(i)[0] = signalVec[i];
133  }
134 
135  solver.Solve(masks, signal, solution);
136 
137  // Verify result
138  for (size_t i = 0; i < expectedSolution.size(); ++i)
139  {
140  unit_assert_equal(expectedSolution[i], solution->row(i)[0], 0.0001);
141  }
142  }
143 };
144 
145 int main(int argc, char* argv[])
146 {
147  TEST_PROLOG(argc, argv)
148 
149  try
150  {
151  DemuxSolverTest tester;
152  tester.Run();
153  }
154  catch (exception& e)
155  {
156  TEST_FAILED(e.what())
157  }
158  catch (...)
159  {
160  TEST_FAILED("Caught unknown exception.")
161  }
162 
164 }
main
int main(int argc, char *argv[])
Definition: DemuxSolverTest.cpp:145
pwiz::analysis::NNLSSolver
Implementation of the DemuxSolver interface as a non-negative least squares (NNLS) problem.
Definition: DemuxSolver.hpp:56
unit_assert_equal
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
pwiz::analysis::NNLSSolver::Solve
void Solve(const MatrixPtr &masks, const MatrixPtr &signal, MatrixPtr &solution) override
Implementation of DemuxSolver interface.
DemuxSolverTest::NNLSSolverTest
void NNLSSolverTest()
Definition: DemuxSolverTest.cpp:47
DemuxTypes::MatrixPtr
boost::shared_ptr< MatrixType > MatrixPtr
Definition: DemuxTypes.hpp:39
DemuxSolverTest::TestNNLSGivenSolution
void TestNNLSGivenSolution(const vector< double > &expectedSolution, double trailingWindowIntensity)
Definition: DemuxSolverTest.cpp:79
DemuxSolverTest::TearDown
void TearDown()
Definition: DemuxSolverTest.cpp:43
pwiz::analysis
Definition: ChromatogramList_Filter.hpp:37
pwiz::msdata
Definition: DemuxTypes.hpp:27
DemuxTypes::MatrixType
Matrix< DemuxScalar, Dynamic, Dynamic > MatrixType
Definition: DemuxTypes.hpp:38
pwiz::util
Definition: almost_equal.hpp:33
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
Std.hpp
DemuxSolverTest::Run
void Run()
Definition: DemuxSolverTest.cpp:30
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
DemuxSolverTest::SetUp
virtual void SetUp()
Definition: DemuxSolverTest.cpp:39
unit.hpp
DemuxSolverTest
Definition: DemuxSolverTest.cpp:28
DemuxSolver.hpp