36#ifndef VIGRA_RANDOM_FOREST_IMPEX_HDF5_HXX
37#define VIGRA_RANDOM_FOREST_IMPEX_HDF5_HXX
40#include "random_forest.hxx"
41#include "hdf5impex.hxx"
48static const char *
const rf_hdf5_options =
"_options";
49static const char *
const rf_hdf5_ext_param =
"_ext_param";
50static const char *
const rf_hdf5_labels =
"labels";
51static const char *
const rf_hdf5_topology =
"topology";
52static const char *
const rf_hdf5_parameters =
"parameters";
53static const char *
const rf_hdf5_tree =
"Tree_";
54static const char *
const rf_hdf5_version_group =
".";
55static const char *
const rf_hdf5_version_tag =
"vigra_random_forest_version";
56static const double rf_hdf5_version = 0.1;
61VIGRA_EXPORT
void options_import_HDF5(HDF5File &, RandomForestOptions &,
64VIGRA_EXPORT
void options_export_HDF5(HDF5File &,
const RandomForestOptions &,
67VIGRA_EXPORT
void dt_import_HDF5(HDF5File &, detail::DecisionTree &,
70VIGRA_EXPORT
void dt_export_HDF5(HDF5File &,
const detail::DecisionTree &,
74void rf_import_HDF5_to_map(HDF5File & h5context, X & param,
75 const char *
const ignored_label = 0)
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;
83 map_type serialized_param;
84 bool ignored_seen = ignored_label == 0;
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)
90 if (ignored_label && *j == ignored_label)
96 inserter_type new_array
97 = serialized_param.insert(value_type(*j, mapped_type()));
99 h5context.readAndResize(*j, (*(new_array.first)).second);
101 vigra_precondition(ignored_seen,
"rf_import_HDF5_to_map(): "
102 "labels are missing.");
103 param.make_from_map(serialized_param);
107void problemspec_import_HDF5(HDF5File & h5context, ProblemSpec<T> & param,
108 const std::string & name)
111 rf_import_HDF5_to_map(h5context, param, rf_hdf5_labels);
113 ArrayVector<T> labels;
114 h5context.readAndResize(rf_hdf5_labels, labels);
115 param.classes_(labels.begin(), labels.end());
120void rf_export_map_to_HDF5(HDF5File & h5context,
const X & param)
122 typedef typename X::map_type map_type;
123 map_type serialized_param;
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);
132void problemspec_export_HDF5(HDF5File & h5context, ProblemSpec<T>
const & param,
133 const std::string & name)
135 h5context.cd_mk(name);
136 rf_export_map_to_HDF5(h5context, param);
137 h5context.write(rf_hdf5_labels, param.classes);
141struct padded_number_string_data;
142class VIGRA_EXPORT padded_number_string
145 padded_number_string_data* padded_number;
147 padded_number_string(
const padded_number_string &);
148 void operator=(
const padded_number_string &);
150 padded_number_string(
int n);
151 std::string operator()(
int k)
const;
152 ~padded_number_string();
155inline std::string get_cwd(HDF5File & h5context)
157 return h5context.get_absolute_path(h5context.pwd());
176template<
class T,
class Tag>
179 const std::string & pathname =
"")
182 if (pathname.size()) {
183 cwd = detail::get_cwd(h5context);
184 h5context.
cd_mk(pathname);
187 h5context.
writeAttribute(rf_hdf5_version_group, rf_hdf5_version_tag,
190 detail::options_export_HDF5(h5context, rf.options(), rf_hdf5_options);
192 detail::problemspec_export_HDF5(h5context, rf.ext_param(),
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));
219template<
class T,
class Tag>
221 const std::string & filename,
222 const std::string & pathname =
"")
224 HDF5File h5context(filename , HDF5File::Open);
245template<
class T,
class Tag>
248 const std::string & pathname =
"")
251 HDF5File h5context(fileHandle, pathname);
268template<
class T,
class Tag>
271 const std::string & pathname =
"")
274 if (pathname.size()) {
275 cwd = detail::get_cwd(h5context);
276 h5context.
cd(pathname);
279 if (h5context.
existsAttribute(rf_hdf5_version_group, rf_hdf5_version_tag))
282 h5context.
readAttribute(rf_hdf5_version_group, rf_hdf5_version_tag,
284 vigra_precondition(read_version <= rf_hdf5_version,
285 "rf_import_HDF5(): unexpected file format version.");
288 detail::options_import_HDF5(h5context, rf.options_, rf_hdf5_options);
290 detail::problemspec_import_HDF5(h5context, rf.ext_param_,
295 std::vector<std::string> names = h5context.
ls();
298 std::sort(names.begin(), names.end());
299 std::vector<std::string>::const_iterator j;
300 for (j = names.begin(); j != names.end(); ++j)
302 if ((*j->rbegin() ==
'/') && (*j->begin() !=
'_'))
304 rf.trees_.push_back(detail::DecisionTree(rf.ext_param_));
305 detail::dt_import_HDF5(h5context, rf.trees_.back(), *j);
326template<
class T,
class Tag>
328 const std::string & filename,
329 const std::string & pathname =
"")
331 HDF5File h5context(filename, HDF5File::OpenReadOnly);
352template<
class T,
class Tag>
355 const std::string & pathname =
"")
358 HDF5File h5context(fileHandle, pathname,
true);
Access to HDF5 files.
Definition hdf5impex.hxx:975
bool existsAttribute(std::string object_name, std::string attribute_name)
Test if attribute exists.
Definition hdf5impex.hxx:1681
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
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
void cd(std::string groupName)
Change the current group. Both absolute and relative group names are allowed.
Definition hdf5impex.hxx:1216
void cd_mk(std::string groupName)
Change the current group; create it if necessary. If the first character is a "/",...
Definition hdf5impex.hxx:1285
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
Wrapper for shared hid_t objects.
Definition hdf5impex.hxx:432
Random forest version 2 (see also RandomForest for version 3).
Definition random_forest.hxx:148
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
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