Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
CurveDescriptorMiniApp.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 // std includes
14 #include <string>
15 
16 // itk includes
17 #include "itksys/SystemTools.hxx"
18 
19 // CTK includes
20 #include "mitkCommandLineParser.h"
21 
22 // MITK includes
23 #include <mitkIOUtil.h>
25 
27 #include <mitkExtractTimeGrid.h>
29 
34 
35 
36 std::string inFilename;
37 std::string outFileName;
38 std::string maskFileName;
39 bool verbose(false);
40 bool preview(false);
43 
44 
45 void onFitEvent(::itk::Object* caller, const itk::EventObject & event, void* /*data*/)
46 {
47  itk::ProgressEvent progressEvent;
48 
49  if (progressEvent.CheckEvent(&event))
50  {
52  std::cout <<castedReporter->GetProgress()*100 << "% ";
53  }
54 }
55 
56 
58 {
59  // set general information about your MiniApp
60  parser.setCategory("Dynamic Data Analysis Tools");
61  parser.setTitle("Curve Descriptor");
62  parser.setDescription("MiniApp that allows to generate curve descriptor maps for dynamic image.");
63  parser.setContributor("DKFZ MIC");
65 
67  // how should arguments be prefixed
68  parser.setArgumentPrefix("--", "-");
69  // add each argument, unless specified otherwise each argument is optional
70  // see mitkCommandLineParser::addArgument for more information
71  parser.beginGroup("Required I/O parameters");
72  parser.addArgument(
73  "input", "i", mitkCommandLineParser::File, "Input file", "input 3D+t image file", us::Any(), false, false, false, mitkCommandLineParser::Input);
74  parser.addArgument("output",
75  "o",
77  "Output file template",
78  "where to save the output parameter images. The specified path will be used as template to determine the format (via extension) and the name \"root\". For each parameter a suffix will be added to the name.",
79  us::Any(),
80  false, false, false, mitkCommandLineParser::Output);
81  parser.endGroup();
82 
83  parser.beginGroup("Optional parameters");
84  parser.addArgument(
85  "mask", "m", mitkCommandLineParser::File, "Mask file", "Mask that defines the spatial image region that should be fitted. Must have the same geometry as the input image!", us::Any(), true, false, false, mitkCommandLineParser::Input);
86  parser.addArgument(
87  "verbose", "v", mitkCommandLineParser::Bool, "Verbose Output", "Whether to produce verbose output");
88  parser.addArgument(
89  "preview", "p", mitkCommandLineParser::Bool, "Preview outputs", "The application previews the outputs (filename, type) it would produce with the current settings.");
90  parser.addArgument("help", "h", mitkCommandLineParser::Bool, "Help:", "Show this help text");
91  parser.endGroup();
93 }
94 
95 bool configureApplicationSettings(std::map<std::string, us::Any> parsedArgs)
96 {
97  if (parsedArgs.size() == 0)
98  return false;
99 
100  verbose = false;
101  if (parsedArgs.count("verbose"))
102  {
103  verbose = us::any_cast<bool>(parsedArgs["verbose"]);
104  }
105 
106  if (parsedArgs.count("mask"))
107  {
108  maskFileName = us::any_cast<std::string>(parsedArgs["mask"]);
109  }
110 
111  preview = false;
112  if (parsedArgs.count("preview"))
113  {
114  preview = us::any_cast<bool>(parsedArgs["preview"]);
115  }
116 
117  inFilename = us::any_cast<std::string>(parsedArgs["input"]);
118  outFileName = us::any_cast<std::string>(parsedArgs["output"]);
119 
120  return true;
121 }
122 
124 {
126  functor->RegisterDescriptionParameter("AUC", parameterFunction);
127 
128  parameterFunction = mitk::AreaUnderFirstMomentDescriptionParameter::New().GetPointer();
129  functor->RegisterDescriptionParameter("AUMC", parameterFunction);
130 
131  parameterFunction = mitk::MeanResidenceTimeDescriptionParameter::New().GetPointer();
132  functor->RegisterDescriptionParameter("MRT", parameterFunction);
133 
134  parameterFunction = mitk::TimeToPeakCurveDescriptionParameter::New().GetPointer();
135  functor->RegisterDescriptionParameter("TimeToPeak", parameterFunction);
136 };
137 
139 {
140  mitk::PixelBasedDescriptionParameterImageGenerator::Pointer generator =
143  ConfigureFunctor(functor);
144  functor->SetGrid(mitk::ExtractTimeGrid(image));
145 
146  generator->SetFunctor(functor);
147  generator->SetDynamicImage(image);
148  generator->SetMask(mask);
149 
150  ::itk::CStyleCommand::Pointer command = ::itk::CStyleCommand::New();
151  command->SetCallback(onFitEvent);
152 
153  std::cout << "Started curve descriptor computation process..." << std::endl;
154  generator->AddObserver(::itk::AnyEvent(), command);
155  generator->Generate();
156  std::cout << std::endl << "Finished computation process" << std::endl;
157 
158  for (auto imageIterator : generator->GetParameterImages())
159  {
160  mitk::storeParameterResultImage(outFileName, imageIterator.first, imageIterator.second);
161  }
162 }
163 
164 void doPreview()
165 {
167  ConfigureFunctor(functor);
168 
169  auto pNames = functor->GetDescriptionParameterNames();
170 
171  for (auto aName : pNames)
172  {
173  auto fullPath = mitk::generateModelFitResultImagePath(outFileName, aName);
174  std::cout << "Store result parameter: " << aName << " -> " << fullPath << std::endl;
175  }
176 }
177 
178 int main(int argc, char* argv[])
179 {
180  mitkCommandLineParser parser;
181  setupParser(parser);
182 
183  mitk::PreferenceListReaderOptionsFunctor readerFilterFunctor = mitk::PreferenceListReaderOptionsFunctor({ "MITK DICOM Reader v2 (classic config)" }, { "MITK DICOM Reader" });
184 
185  const std::map<std::string, us::Any>& parsedArgs = parser.parseArguments(argc, argv);
186  if (!configureApplicationSettings(parsedArgs))
187  {
188  return EXIT_FAILURE;
189  };
190 
191  // Show a help message
192  if (parsedArgs.count("help") || parsedArgs.count("h"))
193  {
194  std::cout << parser.helpText();
195  return EXIT_SUCCESS;
196  }
197 
199  try
200  {
201  if (preview)
202  {
203  doPreview();
204  }
205  else
206  {
207  image = mitk::IOUtil::Load<mitk::Image>(inFilename, &readerFilterFunctor);
208  std::cout << "Input: " << inFilename << std::endl;
209 
210  if (!maskFileName.empty())
211  {
212  mask = mitk::IOUtil::Load<mitk::Image>(maskFileName, &readerFilterFunctor);
213  std::cout << "Mask: " << maskFileName << std::endl;
214  }
215  else
216  {
217  std::cout << "Mask: none" << std::endl;
218  }
219 
220  doDescription();
221  }
222 
223  std::cout << "Processing finished." << std::endl;
224 
225  return EXIT_SUCCESS;
226  }
227  catch (const itk::ExceptionObject& e)
228  {
229  MITK_ERROR << e.what();
230  return EXIT_FAILURE;
231  }
232  catch (const std::exception& e)
233  {
234  MITK_ERROR << e.what();
235  return EXIT_FAILURE;
236  }
237  catch (...)
238  {
239  MITK_ERROR << "Unexpected error encountered.";
240  return EXIT_FAILURE;
241  }
242 }
Option callback functor with a preference list/ black list option selection strategy.
void doPreview()
void setupParser(mitkCommandLineParser &parser)
bool verbose(false)
MITKMODELFIT_EXPORT std::string generateModelFitResultImagePath(const std::string &outputPathTemplate, const std::string &parameterName)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
bool preview(false)
void RegisterDescriptionParameter(const ParameterNameType &parameterName, CurveDescriptionParameterBase *parameterFunction)
bool configureApplicationSettings(std::map< std::string, us::Any > parsedArgs)
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)
std::string outFileName
std::string maskFileName
int main(int argc, char *argv[])
MITKMODELFIT_EXPORT ModelBase::TimeGridType ExtractTimeGrid(const Image *image)
mitk::Image::Pointer mask
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
std::string inFilename
mitk::Image::Pointer image
MITKMODELFIT_EXPORT void storeParameterResultImage(const std::string &outputPathTemplate, const std::string &parameterName, mitk::Image *image, mitk::modelFit::Parameter::Type parameterType=mitk::modelFit::Parameter::ParameterType)
void doDescription()
void ConfigureFunctor(mitk::CurveParameterFunctor *functor)
void onFitEvent(::itk::Object *caller, const itk::EventObject &event, void *)
void setTitle(std::string title)
void setDescription(std::string description)
void beginGroup(const std::string &description)