[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

random_forest_hdf5_impex.hxx
1/************************************************************************/
2/* */
3/* Copyright 2009 by Rahul Nair and Ullrich Koethe */
4/* */
5/* This file is part of the VIGRA computer vision library. */
6/* The VIGRA Website is */
7/* http://hci.iwr.uni-heidelberg.de/vigra/ */
8/* Please direct questions, bug reports, and contributions to */
9/* ullrich.koethe@iwr.uni-heidelberg.de or */
10/* vigra@informatik.uni-hamburg.de */
11/* */
12/* Permission is hereby granted, free of charge, to any person */
13/* obtaining a copy of this software and associated documentation */
14/* files (the "Software"), to deal in the Software without */
15/* restriction, including without limitation the rights to use, */
16/* copy, modify, merge, publish, distribute, sublicense, and/or */
17/* sell copies of the Software, and to permit persons to whom the */
18/* Software is furnished to do so, subject to the following */
19/* conditions: */
20/* */
21/* The above copyright notice and this permission notice shall be */
22/* included in all copies or substantial portions of the */
23/* Software. */
24/* */
25/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32/* OTHER DEALINGS IN THE SOFTWARE. */
33/* */
34/************************************************************************/
35
36#ifndef VIGRA_RANDOM_FOREST_IMPEX_HDF5_HXX
37#define VIGRA_RANDOM_FOREST_IMPEX_HDF5_HXX
38
39#include "config.hxx"
40#include "random_forest.hxx"
41#include "hdf5impex.hxx"
42#include <algorithm>
43#include <string>
44
45 namespace vigra
46{
47
48 static const char *const rf_hdf5_options = "_options";
49 static const char *const rf_hdf5_ext_param = "_ext_param";
50 static const char *const rf_hdf5_labels = "labels";
51 static const char *const rf_hdf5_topology = "topology";
52 static const char *const rf_hdf5_parameters = "parameters";
53 static const char *const rf_hdf5_tree = "Tree_";
54 static const char *const rf_hdf5_version_group = ".";
55 static const char *const rf_hdf5_version_tag = "vigra_random_forest_version";
56 static const double rf_hdf5_version = 0.1;
57
58 namespace detail
59{
60
61VIGRA_EXPORT void options_import_HDF5(HDF5File &, RandomForestOptions &,
62 const std::string &);
63
64VIGRA_EXPORT void options_export_HDF5(HDF5File &, const RandomForestOptions &,
65 const std::string &);
66
67VIGRA_EXPORT void dt_import_HDF5(HDF5File &, detail::DecisionTree &,
68 const std::string &);
69
70VIGRA_EXPORT void dt_export_HDF5(HDF5File &, const detail::DecisionTree &,
71 const std::string &);
72
73 template<class X>
74 void rf_import_HDF5_to_map(HDF5File & h5context, X & param,
75 const char *const ignored_label = 0)
76{
77 // read a map containing all the double fields
78 typedef typename X::map_type map_type;
79 typedef std::pair<typename map_type::iterator, bool> inserter_type;
80 typedef typename map_type::value_type value_type;
81 typedef typename map_type::mapped_type mapped_type;
82
83 map_type serialized_param;
84 bool ignored_seen = ignored_label == 0;
85
86 std::vector<std::string> names = h5context.ls();
87 std::vector<std::string>::const_iterator j;
88 for (j = names.begin(); j != names.end(); ++j)
89 {
90 if (ignored_label && *j == ignored_label)
91 {
92 ignored_seen = true;
93 continue;
94 }
95 // get sort of an iterator to a new empty array vector in the map ...
96 inserter_type new_array
97 = serialized_param.insert(value_type(*j, mapped_type()));
98 // ... and read the data into that place.
99 h5context.readAndResize(*j, (*(new_array.first)).second);
100 }
101 vigra_precondition(ignored_seen, "rf_import_HDF5_to_map(): "
102 "labels are missing.");
103 param.make_from_map(serialized_param);
104}
105
106 template<class T>
107 void problemspec_import_HDF5(HDF5File & h5context, ProblemSpec<T> & param,
108 const std::string & name)
109{
110 h5context.cd(name);
111 rf_import_HDF5_to_map(h5context, param, rf_hdf5_labels);
112 // load_class_labels
113 ArrayVector<T> labels;
114 h5context.readAndResize(rf_hdf5_labels, labels);
115 param.classes_(labels.begin(), labels.end());
116 h5context.cd_up();
117}
118
119 template<class X>
120 void rf_export_map_to_HDF5(HDF5File & h5context, const X & param)
121{
122 typedef typename X::map_type map_type;
123 map_type serialized_param;
124 // get a map containing all the double fields
125 param.make_map(serialized_param);
126 typename map_type::const_iterator j;
127 for (j = serialized_param.begin(); j != serialized_param.end(); ++j)
128 h5context.write(j->first, j->second);
129}
130
131 template<class T>
132 void problemspec_export_HDF5(HDF5File & h5context, ProblemSpec<T> const & param,
133 const std::string & name)
134{
135 h5context.cd_mk(name);
136 rf_export_map_to_HDF5(h5context, param);
137 h5context.write(rf_hdf5_labels, param.classes);
138 h5context.cd_up();
139}
140
141 struct padded_number_string_data;
142 class VIGRA_EXPORT padded_number_string
143{
144 private:
145 padded_number_string_data* padded_number;
146 protected:
147 padded_number_string(const padded_number_string &);
148 void operator=(const padded_number_string &);
149 public:
150 padded_number_string(int n);
151 std::string operator()(int k) const;
152 ~padded_number_string();
153};
154
155 inline std::string get_cwd(HDF5File & h5context)
156{
157 return h5context.get_absolute_path(h5context.pwd());
158}
159
160} // namespace detail
161
162/** \brief Save a random forest to an HDF5File object into a specified HDF5
163 group.
164
165 The random forest is saved as a set of HDF5 datasets, groups, and
166 attributes below a certain HDF5 group (default: current group of the
167 HDF5File object). No additional data should be stored in that group.
168
169 \param rf Random forest object to be exported
170 \param h5context HDF5File object to use
171 \param pathname If empty or not supplied, save the random forest to the
172 current group of the HDF5File object. Otherwise, save to a
173 new-created group specified by the path name, which may
174 be either relative or absolute.
175*/
176 template<class T, class Tag>
177 void rf_export_HDF5(const RandomForest<T, Tag> & rf,
178 HDF5File & h5context,
179 const std::string & pathname = "")
180{
181 std::string cwd;
182 if (pathname.size()) {
183 cwd = detail::get_cwd(h5context);
184 h5context.cd_mk(pathname);
185 }
186 // version attribute
187 h5context.writeAttribute(rf_hdf5_version_group, rf_hdf5_version_tag,
188 rf_hdf5_version);
189 // save serialized options
190 detail::options_export_HDF5(h5context, rf.options(), rf_hdf5_options);
191 // save external parameters
192 detail::problemspec_export_HDF5(h5context, rf.ext_param(),
193 rf_hdf5_ext_param);
194 // save trees
195 int tree_count = rf.options_.tree_count_;
196 detail::padded_number_string tree_number(tree_count);
197 for (int i = 0; i < tree_count; ++i)
198 detail::dt_export_HDF5(h5context, rf.tree(i),
199 rf_hdf5_tree + tree_number(i));
200
201 if (pathname.size())
202 h5context.cd(cwd);
203}
204
205/** \brief Save a random forest to a named HDF5 file into a specified HDF5
206 group.
207
208 The random forest is saved as a set of HDF5 datasets, groups, and
209 attributes below a certain HDF5 group (default: root). No additional data
210 should be stored in that group.
211
212 \param rf Random forest object to be exported
213 \param filename Name of an HDF5 file to open
214 \param pathname If empty or not supplied, save the random forest to the
215 root group of the HDF5 file. Otherwise, save to a
216 new-created group specified by the path name (relative
217 to the root group).
218*/
219 template<class T, class Tag>
220 void rf_export_HDF5(const RandomForest<T, Tag> & rf,
221 const std::string & filename,
222 const std::string & pathname = "")
223{
224 HDF5File h5context(filename , HDF5File::Open);
225 rf_export_HDF5(rf, h5context, pathname);
226}
227
228/** \brief Save a random forest to an HDF5 file specified by its id
229
230 The random forest is saved as a set of HDF5 datasets, groups, and
231 attributes below a certain HDF5 group (default: root). No additional data
232 should be stored in that group.
233
234 \warning In case the underlying HDF5 library used by Vigra is not
235 exactly the same library used to open the file with the given id, this
236 method will lead to crashes.
237
238 \param rf Random forest object to be exported
239 \param outf_id HDF5 file id
240 \param pathname If empty or not supplied, save the random forest to the
241 root group of the HDF5 file. Otherwise, save to a
242 new-created group specified by the path name (relative
243 to the root group).
244*/
245 template<class T, class Tag>
246 void rf_export_HDF5(const RandomForest<T, Tag> & rf,
247 hid_t outf_id,
248 const std::string & pathname = "")
249{
250 HDF5HandleShared fileHandle(outf_id, NULL, "");
251 HDF5File h5context(fileHandle, pathname);
252 rf_export_HDF5(rf, h5context);
253}
254
255/** \brief Read a random forest from an HDF5File object's specified group.
256
257 The random forest is read from a certain HDF5 group (default: current group
258 of the HDF5File object) as a set of HDF5 datasets, groups, and
259 attributes. No additional data should be present in that group.
260
261 \param rf Random forest object to be imported
262 \param h5context HDF5File object to use
263 \param pathname If empty or not supplied, read from the random forest
264 from the current group of the HDF5File object. Otherwise,
265 use the group specified by the path name, which may
266 be either relative or absolute.
267*/
268 template<class T, class Tag>
269 bool rf_import_HDF5(RandomForest<T, Tag> & rf,
270 HDF5File & h5context,
271 const std::string & pathname = "")
272{
273 std::string cwd;
274 if (pathname.size()) {
275 cwd = detail::get_cwd(h5context);
276 h5context.cd(pathname);
277 }
278 // version attribute
279 if (h5context.existsAttribute(rf_hdf5_version_group, rf_hdf5_version_tag))
280 {
281 double read_version;
282 h5context.readAttribute(rf_hdf5_version_group, rf_hdf5_version_tag,
283 read_version);
284 vigra_precondition(read_version <= rf_hdf5_version,
285 "rf_import_HDF5(): unexpected file format version.");
286 }
287 // get serialized options
288 detail::options_import_HDF5(h5context, rf.options_, rf_hdf5_options);
289 // get external parameters
290 detail::problemspec_import_HDF5(h5context, rf.ext_param_,
291 rf_hdf5_ext_param);
292 rf.trees_.clear();
293 // get all groups in base path
294 // no check for the rf_hdf5_tree prefix...
295 std::vector<std::string> names = h5context.ls();
296 // HDF5File::ls() uses H5_ITER_NATIVE, which does not guarantee alphabetical
297 // order. Sort so that trees are imported in the same order they were exported.
298 std::sort(names.begin(), names.end());
299 std::vector<std::string>::const_iterator j;
300 for (j = names.begin(); j != names.end(); ++j)
301 {
302 if ((*j->rbegin() == '/') && (*j->begin() != '_')) // skip the above
303 {
304 rf.trees_.push_back(detail::DecisionTree(rf.ext_param_));
305 detail::dt_import_HDF5(h5context, rf.trees_.back(), *j);
306 }
307 }
308 if (pathname.size())
309 h5context.cd(cwd);
310 return true;
311}
312
313/** \brief Read a random forest from a named HDF5 file's specified group.
314
315 The random forest is read from a certain HDF5 group (default: root group
316 of the HDF5 file) as a set of HDF5 datasets, groups, and attributes.
317 No additional data should be present in that group.
318
319 \param rf Random forest object to be imported
320 \param filename Name of an HDF5 file to open
321 \param pathname If empty or not supplied, read from the random forest
322 from the current group of the HDF5 file. Otherwise,
323 use the group specified by the path name, which may
324 be either relative or absolute.
325*/
326 template<class T, class Tag>
327 bool rf_import_HDF5(RandomForest<T, Tag> & rf,
328 const std::string & filename,
329 const std::string & pathname = "")
330{
331 HDF5File h5context(filename, HDF5File::OpenReadOnly);
332 return rf_import_HDF5(rf, h5context, pathname);
333}
334
335/** \brief Read a random forest from an HDF5 file specified by its id.
336
337 The random forest is read from a certain HDF5 group (default: root group
338 of the HDF5 file) as a set of HDF5 datasets, groups, and attributes.
339 No additional data should be present in that group.
340
341 \warning In case the underlying HDF5 library used by Vigra is not
342 exactly the same library used to open the file with the given id, this
343 method will lead to crashes.
344
345 \param rf Random forest object to be imported
346 \param inf_id HDF5 file id
347 \param pathname If empty or not supplied, read from the random forest
348 from the current group of the HDF5 file. Otherwise,
349 use the group specified by the path name, which may
350 be either relative or absolute.
351*/
352 template<class T, class Tag>
353 bool rf_import_HDF5(RandomForest<T, Tag> & rf,
354 hid_t inf_id,
355 const std::string & pathname = "")
356{
357 HDF5HandleShared fileHandle(inf_id, NULL, "");
358 HDF5File h5context(fileHandle, pathname, true);
359 return rf_import_HDF5(rf, h5context);
360}
361
362} // namespace vigra
363
364#endif // VIGRA_RANDOM_FOREST_HDF5_IMPEX_HXX
vigra::HDF5File
Access to HDF5 files.
Definition hdf5impex.hxx:975
vigra::HDF5File::existsAttribute
bool existsAttribute(std::string object_name, std::string attribute_name)
Test if attribute exists.
Definition hdf5impex.hxx:1681
vigra::HDF5File::readAttribute
void readAttribute(std::string object_name, std::string attribute_name, MultiArrayView< N, T, Stride > array)
Read MultiArray Attributes. In contrast to datasets, subarray access is not available.
Definition hdf5impex.hxx:1698
vigra::HDF5File::writeAttribute
void writeAttribute(std::string object_name, std::string attribute_name, const MultiArrayView< N, T, Stride > &array)
Write MultiArray Attributes. In contrast to datasets, subarray access, chunks and compression are not...
Definition hdf5impex.hxx:1611
vigra::HDF5File::cd
void cd(std::string groupName)
Change the current group. Both absolute and relative group names are allowed.
Definition hdf5impex.hxx:1216
vigra::HDF5File::cd_mk
void cd_mk(std::string groupName)
Change the current group; create it if necessary. If the first character is a "/",...
Definition hdf5impex.hxx:1285
vigra::HDF5File::ls
std::vector< std::string > ls() const
List the contents of the current group. The function returns a vector of strings holding the entries ...
Definition hdf5impex.hxx:1310
vigra::HDF5HandleShared
Wrapper for shared hid_t objects.
Definition hdf5impex.hxx:432
vigra::RandomForest
Random forest version 2 (see also RandomForest for version 3).
Definition random_forest.hxx:148
vigra
vigra::rf_export_HDF5
void rf_export_HDF5(const RandomForest< T, Tag > &rf, HDF5File &h5context, const std::string &pathname="")
Save a random forest to an HDF5File object into a specified HDF5 group.
Definition random_forest_hdf5_impex.hxx:177
vigra::rf_import_HDF5
bool rf_import_HDF5(RandomForest< T, Tag > &rf, HDF5File &h5context, const std::string &pathname="")
Read a random forest from an HDF5File object's specified group.
Definition random_forest_hdf5_impex.hxx:269

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.12.4