Medical Imaging Interaction Toolkit  2018.4.99-e1268d66
Medical Imaging Interaction Toolkit
CLWeighting.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef mitkForest_cpp
13 #define mitkForest_cpp
14 
15 #include "time.h"
16 #include <sstream>
17 #include <fstream>
18 #include "mitkCommandLineParser.h"
20 #include <mitkLog.h>
21 #include <mitkLogMacros.h>
22 
23 
24 //#include <mitkLRDensityEstimation.h>
26 
27 struct TrainingSet {
28  vnl_matrix<double> feature;
29  vnl_vector<double> label;
30  double ratio;
31 };
32 
33 
34 const
35 vnl_matrix<double> ReadMatrix(std::string path)
36 {
37  std::fstream file(path);
38  std::vector<std::vector<double> > listOfRows;
39 
40  std::string line;
41  while (std::getline(file, line))
42  {
43  auto current_row = mitk::cl::splitDouble(line, ',');
44  if (listOfRows.size() < 1)
45  {
46  listOfRows.push_back(current_row);
47  }
48  else if (current_row.size() == listOfRows[0].size())
49  {
50  listOfRows.push_back(current_row);
51  }
52  }
53  file.close();
54 
55  vnl_matrix<double> result(listOfRows.size(), listOfRows[0].size());
56  for (std::size_t i = 0; i < listOfRows.size(); ++i)
57  {
58  for (std::size_t j = 0; j < listOfRows[0].size(); ++j)
59  {
60  result(i, j) = listOfRows[i][j];
61  }
62  }
63  return result;
64 }
65 
66 const
67 TrainingSet ReadBothMatrix(std::string train, std::string test)
68 {
69  std::fstream file(train);
70  std::vector<std::vector<double> > listOfRows;
71  std::vector<double> label;
72 
73  double trSamples = 0;
74  double teSamples = 0;
75 
76  std::string line;
77  while (std::getline(file, line))
78  {
79  auto current_row = mitk::cl::splitDouble(line, ',');
80  if (listOfRows.size() < 1)
81  {
82  listOfRows.push_back(current_row);
83  label.push_back(0);
84  trSamples += 1;
85  }
86  else if (current_row.size() == listOfRows[0].size())
87  {
88  listOfRows.push_back(current_row);
89  label.push_back(0);
90  trSamples += 1;
91  }
92  }
93  file.close();
94 
95  std::fstream file2(test);
96  while (std::getline(file2, line))
97  {
98  auto current_row = mitk::cl::splitDouble(line, ',');
99  if (listOfRows.size() < 1)
100  {
101  listOfRows.push_back(current_row);
102  label.push_back(1);
103  teSamples += 1;
104  }
105  else if (current_row.size() == listOfRows[0].size())
106  {
107  listOfRows.push_back(current_row);
108  label.push_back(1);
109  teSamples += 1;
110  }
111  }
112  file2.close();
113 
114  vnl_matrix<double> resultMatrix(listOfRows.size(), listOfRows[0].size());
115  vnl_vector<double> resultLabel(listOfRows.size());
116  for (std::size_t i = 0; i < listOfRows.size(); ++i)
117  {
118  for (std::size_t j = 0; j < listOfRows[0].size(); ++j)
119  {
120  resultMatrix(i, j) = listOfRows[i][j];
121  }
122  resultLabel(i) = label[i];
123  }
124  TrainingSet set;
125  set.feature = resultMatrix;
126  set.label = resultLabel;
127  set.ratio = trSamples / teSamples;
128  return set;
129 }
130 
131 
132 int main(int argc, char* argv[])
133 {
134  mitkCommandLineParser parser;
135  parser.setArgumentPrefix("--", "-");
136  // required params
137  parser.addArgument("training", "t", mitkCommandLineParser::Image, "Input Image", "desc", us::Any(), false, false, false, mitkCommandLineParser::Input);
138  parser.addArgument("prediction", "p", mitkCommandLineParser::Image, "Input Image", "desc", us::Any(), false, false, false, mitkCommandLineParser::Input);
139  parser.addArgument("output", "o", mitkCommandLineParser::Image, "Normalisation mode", "desc", us::Any(), false, false, false, mitkCommandLineParser::Input);
140  //parser.addArgument("algorithm", "a", mitkCommandLineParser::InputImage, "Input Mask", "desc", us::Any(), false);
141 
142  // Miniapp Infos
143  parser.setCategory("Classification Tools");
144  parser.setTitle("Importance weighting algorithm");
145  parser.setDescription("Calculates the importance weighting of two input matrixes. ");
146  parser.setContributor("German Cancer Research Center (DKFZ)");
147 
148  MITK_INFO << "Extracting Parameters....";
149  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
150 
151  std::string trainingPath = us::any_cast<std::string>(parsedArgs["training"]);
152  std::string predictionPath = us::any_cast<std::string>(parsedArgs["prediction"]);
153  std::string outputPath = us::any_cast<std::string>(parsedArgs["output"]);
154  //std::string algorithm = us::any_cast<std::string>(parsedArgs["algorithm"]);
155 
156  MITK_INFO << "Reading Data...";
157  auto input = ReadBothMatrix(trainingPath, predictionPath);
158 
159  MITK_INFO << "Calculating Weights...";
160  mitk::GeneralizedLinearModel glm(input.feature, input.label);
161  auto weights = glm.ExpMu(input.feature);
162 
163  MITK_INFO << "Writing Weights ...";
164  MITK_INFO << outputPath;
165  std::ofstream file(outputPath);
166  for (unsigned int i = 0; i < input.label.size(); ++i)
167  {
168  if (input.label(i) < 0.5)
169  {
170  file << (input.ratio * weights(i)) << std::endl;
171  }
172  }
173  file.close();
174 
175 
177  // Read Images
179  //mitk::DataCollection::Pointer col = mitk::DataCollection::New();
180 
181  //MITK_INFO << "Arg 2 " << argv[2];
182  //mitk::Image::Pointer sur=mitk::IOUtil::Load<mitk::Image>(argv[2]);
183  //col->AddData(sur.GetPointer(),"sur");
184  //MITK_INFO << "Arg 3 " << argv[3];
185  //mitk::Image::Pointer mask=mitk::IOUtil::Load<mitk::Image>(argv[3]);
186  //col->AddData(mask.GetPointer(),"mask");
187 
188  //std::vector<std::string> modalities;
189  //for (int i = 4; i < argc; ++i)
190  //{
191  // MITK_INFO << "Img " << argv[i];
192  // std::stringstream ss;
193  // ss << i;
194  // modalities.push_back(ss.str());
195  // mitk::Image::Pointer img = mitk::IOUtil::Load<mitk::Image>(argv[i]);
196  // col->AddData(img.GetPointer(),ss.str());
197  //}
198 
199  //mitk::LRDensityEstimation est;
200  //est.SetCollection(col);
201  //est.SetTrainMask("sur");
202  //est.SetTestMask("mask");
203  //est.SetModalities(modalities);
204  //est.SetWeightName("weight");
205  //est.Update();
206 
207  //mitk::Image::Pointer w= col->GetMitkImage("weight");
208  //mitk::IOUtil::SaveImage(w,argv[1]);
209 
210  return 0;
211 }
212 
213 #endif
static char * line
Definition: svm.cpp:2870
#define MITK_INFO
Definition: mitkLogMacros.h:18
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
Generalized Linear Model that allows linear models for non-gaussian data.
Definition: usAny.h:163
void setCategory(std::string category)
const vnl_matrix< double > ReadMatrix(std::string path)
Definition: CLWeighting.cpp:35
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
std::vector< double > MITKCLUTILITIES_EXPORT splitDouble(std::string str, char delimiter)
vnl_vector< double > ExpMu(const vnl_matrix< double > &x)
Estimation of the exponential factor for a given function.
void setTitle(std::string title)
void setDescription(std::string description)
const TrainingSet ReadBothMatrix(std::string train, std::string test)
Definition: CLWeighting.cpp:67
int main(int argc, char *argv[])