Medical Imaging Interaction Toolkit  2018.4.99-6a3ea89d
Medical Imaging Interaction Toolkit
mitkRandomForestIO.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 #ifndef __mitkDecisionForestIO__cpp
14 #define __mitkDecisionForestIO__cpp
15 
16 #include "mitkRandomForestIO.h"
17 #include "itksys/SystemTools.hxx"
18 //#include "mitkHDF5IOMimeTypes.h"
19 
20 #include "vigra/random_forest_hdf5_impex.hxx"
21 
22 #include <iostream>
23 #include <fstream>
24 
26 #include "mitkIOMimeTypes.h"
27 
28 #define GetAttribute(name,type)\
29  type name;\
30  hdf5_file.readAttribute(".",name,name);
31 
33 {
34  std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetLocalFileName().c_str());
35  bool is_loaded = vigra::rf_import_HDF5(m_rf, this->GetInputLocation());
36  return ext == ".forest" && is_loaded == true? IFileReader::Supported : IFileReader::Unsupported;
37 }
38 
40 {
42  if (input.IsNull())
43  {
45  }else{
47  }
48 }
49 
51  : AbstractFileIO(mitk::VigraRandomForestClassifier::GetStaticNameOfClass())
52 {
53  CustomMimeType customReaderMimeType(mitk::IOMimeTypes::DEFAULT_BASE_NAME() + ".forest");
54  std::string category = "Vigra Random Forest File";
55  customReaderMimeType.SetComment("Vigra Random Forest");
56  customReaderMimeType.SetCategory(category);
57  customReaderMimeType.AddExtension("forest");
58 
59  // this->AbstractFileIOWriter::SetRanking(100);
61  this->AbstractFileWriter::SetMimeType(customReaderMimeType);
62  this->SetWriterDescription("Vigra Random Forest");
64  this->AbstractFileReader::SetMimeType(customReaderMimeType);
65  this->SetReaderDescription("Vigra Random Forest");
66 
67  // this->SetReaderDescription(mitk::DecisionForestIOMimeTypes::DECISIONFOREST_MIMETYPE_DESCRIPTION());
68  // this->SetWriterDescription(mitk::DecisionForestIOMimeTypes::DECISIONFOREST_MIMETYPE_DESCRIPTION());
69  this->RegisterService();
70 }
71 
73  : AbstractFileIO(other)
74 {
75 }
76 
78 {}
79 
80 std::vector<itk::SmartPointer<mitk::BaseData> >
83 {
85  std::vector<itk::SmartPointer<mitk::BaseData> > result;
86 
87  if ( this->GetInputLocation().empty())
88  {
89  MITK_ERROR << "Sorry, filename has not been set!";
90  return result;
91  }
92  else
93  {
94  const std::string& locale = "C";
95  const std::string& currLocale = setlocale( LC_ALL, nullptr );
96 
97  if ( locale.compare(currLocale)!=0 )
98  {
99  try
100  {
101  setlocale(LC_ALL, locale.c_str());
102  }
103  catch(...)
104  {
105  MITK_INFO << "Could not set locale " << locale;
106  }
107  }
108 
109  output->SetRandomForest(m_rf);
110  result.push_back(output.GetPointer());
111  vigra::HDF5File hdf5_file(this->GetInputLocation() , vigra::HDF5File::Open);
112 
113 
114  hdf5_file.cd_mk("/_mitkOptions");
115 
116  // ---------------------------------------------------------
117  // Read tree weights
118  if(hdf5_file.existsDataset("treeWeights"))
119  {
120  auto treeWeight = output->GetTreeWeights();
121  treeWeight.resize(m_rf.tree_count(),1);
122  vigra::MultiArrayView<2, double> W(vigra::Shape2(treeWeight.rows(),treeWeight.cols()),treeWeight.data());
123  hdf5_file.read("treeWeights",W);
124  output->SetTreeWeights(treeWeight);
125  }
126  // ---------------------------------------------------------
127 
128  // ---------------------------------------------------------
129  // Read itemList
130  if(hdf5_file.existsDataset("itemList")){
131  std::string items_string;
132  hdf5_file.read("itemList",items_string);
133  auto itemlist = output->GetItemList();
134 
135  std::string current_item = "";
136  for(auto character : items_string)
137  {
138  if(character == ';'){
139  // skip seperator and push back item
140  itemlist.push_back(current_item);
141  current_item.clear();
142  }else{
143  current_item = current_item + character;
144  }
145  }
146  output->SetItemList(itemlist);
147  }
148  // ---------------------------------------------------------
149 
150  hdf5_file.close();
151 
152  return result;
153  }
154 }
155 
157 {
158  mitk::BaseData::ConstPointer input = this->GetInput();
159  if (input.IsNull())
160  {
161  MITK_ERROR <<"Sorry, input to NrrdDiffusionImageWriter is nullptr!";
162  return;
163  }
164  if ( this->GetOutputLocation().empty() )
165  {
166  MITK_ERROR << "Sorry, filename has not been set!";
167  return ;
168  }else{
169  const std::string& locale = "C";
170  const std::string& currLocale = setlocale( LC_ALL, nullptr );
171 
172  if ( locale.compare(currLocale)!=0 )
173  {
174  try
175  {
176  setlocale(LC_ALL, locale.c_str());
177  }
178  catch(...)
179  {
180  MITK_INFO << "Could not set locale " << locale;
181  }
182  }
183 
184  mitk::VigraRandomForestClassifier::ConstPointer mitkDC = dynamic_cast<const mitk::VigraRandomForestClassifier *>(input.GetPointer());
185  //mitkDC->GetRandomForest()
186  vigra::rf_export_HDF5(mitkDC->GetRandomForest(), this->GetOutputLocation());
187 
188  vigra::HDF5File hdf5_file(this->GetOutputLocation() , vigra::HDF5File::Open);
189 
190  hdf5_file.cd_mk("/_mitkOptions");
191 
192  // Write tree weights
193  // ---------------------------------------------------------
194  auto treeWeight = mitkDC->GetTreeWeights();
195  vigra::MultiArrayView<2, double> W(vigra::Shape2(treeWeight.rows(),treeWeight.cols()),treeWeight.data());
196  hdf5_file.write("treeWeights",W);
197  // ---------------------------------------------------------
198 
199  // Write itemList
200  // ---------------------------------------------------------
201  auto items = mitkDC->GetItemList();
202  std::string item_stringlist;
203  for(auto entry : items)
204  item_stringlist = item_stringlist + entry + ";";
205 
206  hdf5_file.write("itemList",item_stringlist);
207  // ---------------------------------------------------------
208 
209  hdf5_file.close();
210  }
211 }
212 
213 mitk::AbstractFileIO* mitk::RandomForestFileIO::IOClone() const
214 {
215  return new RandomForestFileIO(*this);
216 }
217 
218 #endif
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
ConfidenceLevel GetWriterConfidenceLevel() const override
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< itk::SmartPointer< BaseData > > DoRead() override
void SetComment(const std::string &comment)
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
std::string GetLocalFileName() const
Get a local file name for reading.
void SetMimeTypePrefix(const std::string &prefix)
static std::string DEFAULT_BASE_NAME()
vigra::RandomForest< int > m_rf
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
const BaseData * GetInput() const override
Get the input data set via SetInput().
void SetReaderDescription(const std::string &description)
void AddExtension(const std::string &extension)
void Write() override
Write the base data to the specified location or output stream.
void SetWriterDescription(const std::string &description)
void SetCategory(const std::string &category)
void SetMimeType(const CustomMimeType &mimeType)
std::string GetOutputLocation() const override
Get the current output location.
std::string GetInputLocation() const override
Get the current input location.
void SetMimeTypePrefix(const std::string &prefix)
Abstract class for implementing a reader and writer.
ConfidenceLevel GetReaderConfidenceLevel() const override