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