Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
CLRandomSampling.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 
13 #include "mitkCommandLineParser.h"
14 #include "mitkIOUtil.h"
15 #include <mitkRandomImageSampler.h>
16 
17 static std::vector<double> splitDouble(std::string str, char delimiter) {
18  std::vector<double> internal;
19  std::stringstream ss(str); // Turn the string into a stream.
20  std::string tok;
21  double val;
22  while (getline(ss, tok, delimiter)) {
23  std::stringstream s2(tok);
24  s2 >> val;
25  internal.push_back(val);
26  }
27 
28  return internal;
29 }
30 
31 static std::vector<unsigned int> splitUInt(std::string str, char delimiter) {
32  std::vector<unsigned int> internal;
33  std::stringstream ss(str); // Turn the string into a stream.
34  std::string tok;
35  unsigned int val;
36  while (getline(ss, tok, delimiter)) {
37  std::stringstream s2(tok);
38  s2 >> val;
39  internal.push_back(val);
40  }
41 
42  return internal;
43 }
44 
45 int main(int argc, char* argv[])
46 {
47  mitkCommandLineParser parser;
48 
49  parser.setTitle("Random Sampling");
50  parser.setCategory("Classification Command Tools");
51  parser.setDescription("");
52  parser.setContributor("German Cancer Research Center (DKFZ)");
53 
54  parser.setArgumentPrefix("--", "-");
55  // Add command line argument names
56  parser.addArgument("help", "h", mitkCommandLineParser::Bool, "Help:", "Show this help text");
57  parser.addArgument("input", "i", mitkCommandLineParser::Directory, "Input file:", "Input file", us::Any(), false, false, false, mitkCommandLineParser::Input);
58  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
59 
60  parser.addArgument("single-rate", "sr", mitkCommandLineParser::File, "Single Acceptance rate for all voxel", "Output file", us::Any(), true, false, false, mitkCommandLineParser::Output);
61  parser.addArgument("class-rate", "cr", mitkCommandLineParser::File, "Class-dependend acceptance rate", "Output file", us::Any(), true, false, false, mitkCommandLineParser::Output);
62  parser.addArgument("single-number", "sn", mitkCommandLineParser::File, "Single Number of Voxel for each class", "Output file", us::Any(), true, false, false, mitkCommandLineParser::Output);
63  parser.addArgument("class-number", "cn", mitkCommandLineParser::File, "Class-dependedn number of voxels ", "Output file", us::Any(), true, false, false, mitkCommandLineParser::Output);
64 
65  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
66 
67  if (parsedArgs.size() == 0)
68  return EXIT_FAILURE;
69 
70  // Show a help message
71  if (parsedArgs.count("help") || parsedArgs.count("h"))
72  {
73  std::cout << parser.helpText();
74  return EXIT_SUCCESS;
75  }
76 
77  if (parsedArgs.count("single-rate") + parsedArgs.count("class-rate") + parsedArgs.count("single-number") + parsedArgs.count("class-number") < 1)
78  {
79  std::cout << "Please specify the sampling rate or number of voxels to be labeled" << std::endl << std::endl;
80  std::cout << parser.helpText();
81  return EXIT_SUCCESS;
82  }
83 
84  if (parsedArgs.count("single-rate") + parsedArgs.count("class-rate") + parsedArgs.count("single-number") + parsedArgs.count("class-number") > 2)
85  {
86  std::cout << "Please specify only one way for the sampling rate or number of voxels to be labeled" << std::endl << std::endl;
87  std::cout << parser.helpText();
88  return EXIT_SUCCESS;
89  }
90 
91 
92  std::string inputName = us::any_cast<std::string>(parsedArgs["input"]);
93  std::string outputName = us::any_cast<std::string>(parsedArgs["output"]);
94  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(inputName);
95 
97  filter->SetInput(image);
98 
99  if (parsedArgs.count("single-rate"))
100  {
102  auto rate = splitDouble(parsedArgs["single-rate"].ToString(), ';');
103  if (rate.size() != 1)
104  {
105  std::cout << "Please specify a single double value for single-rate, for example 0.3." << std::endl << std::endl;
106  std::cout << parser.helpText();
107  return EXIT_SUCCESS;
108  }
109  filter->SetAcceptanceRate(rate[0]);
110  }
111 
112  else if (parsedArgs.count("class-rate"))
113  {
115  auto rate = splitDouble(parsedArgs["class-rate"].ToString(), ';');
116  if (rate.size() < 2)
117  {
118  std::cout << "Please specify at least two, semicolon separated values for class-rate, for example '0.3;0.2' ." << std::endl << std::endl;
119  std::cout << parser.helpText();
120  return EXIT_SUCCESS;
121  }
122  filter->SetAcceptanceRateVector(rate);
123  }
124 
125  else if (parsedArgs.count("single-number"))
126  {
128  auto rate = splitUInt(parsedArgs["single-number"].ToString(), ';');
129  if (rate.size() != 1)
130  {
131  std::cout << "Please specify a single double value for single-number, for example 100." << std::endl << std::endl;
132  std::cout << parser.helpText();
133  return EXIT_SUCCESS;
134  }
135  filter->SetNumberOfSamples(rate[0]);
136  }
137 
138  else if (parsedArgs.count("class-number"))
139  {
141  auto rate = splitUInt(parsedArgs["class-number"].ToString(), ';');
142  if (rate.size() < 2)
143  {
144  std::cout << "Please specify at least two, semicolon separated values for class-number, for example '100;200' ." << std::endl << std::endl;
145  std::cout << parser.helpText();
146  return EXIT_SUCCESS;
147  }
148  filter->SetNumberOfSamplesVector(rate);
149  }
150  filter->Update();
151  mitk::IOUtil::Save(filter->GetOutput(), outputName);
152 
153  return EXIT_SUCCESS;
154 }
static std::vector< double > splitDouble(std::string str, char delimiter)
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)
static Pointer New()
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
int main(int argc, char *argv[])
void setTitle(std::string title)
static std::vector< unsigned int > splitUInt(std::string str, char delimiter)
void setDescription(std::string description)