SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HDF5File.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2010 Soeren Sonnenburg
00008  * Copyright (C) 2010 Berlin Institute of Technology
00009  */
00010 
00011 #include <shogun/lib/config.h>
00012 
00013 #ifdef HAVE_HDF5
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <hdf5.h>
00018 
00019 #include <shogun/lib/memory.h>
00020 #include <shogun/io/HDF5File.h>
00021 
00022 #include <shogun/features/StringFeatures.h>
00023 #include <shogun/features/SparseFeatures.h>
00024 
00025 using namespace shogun;
00026 
00027 CHDF5File::CHDF5File()
00028 {
00029     SG_UNSTABLE("CHDF5File::CHDF5File()", "\n")
00030 
00031     get_boolean_type();
00032     h5file = -1;
00033 }
00034 
00035 CHDF5File::CHDF5File(char* fname, char rw, const char* name) : CFile()
00036 {
00037     get_boolean_type();
00038     H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
00039 
00040     if (name)
00041         set_variable_name(name);
00042 
00043     switch (rw)
00044     {
00045         case 'r':
00046             h5file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
00047             break;
00048         case 'w':
00049             h5file = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
00050             break;
00051         case 'a':
00052             h5file = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
00053             if (h5file <0)
00054                 h5file = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
00055             break;
00056         default:
00057             SG_ERROR("unknown mode '%c'\n", rw)
00058     };
00059 
00060     if (h5file<0)
00061         SG_ERROR("Could not open file '%s'\n", fname)
00062 }
00063 
00064 CHDF5File::~CHDF5File()
00065 {
00066     H5Fclose(h5file);
00067 }
00068 
00069 #define GET_VECTOR(fname, sg_type, datatype)                                        \
00070 void CHDF5File::fname(sg_type*& vec, int32_t& len)                                  \
00071 {                                                                                   \
00072     if (!h5file)                                                                    \
00073         SG_ERROR("File invalid.\n")                                             \
00074                                                                                     \
00075     int32_t* dims;                                                                  \
00076     int32_t ndims;                                                                  \
00077     int64_t nelements;                                                              \
00078     hid_t dataset = H5Dopen2(h5file, variable_name, H5P_DEFAULT);                   \
00079     if (dataset<0)                                                                  \
00080         SG_ERROR("Error opening data set\n")                                        \
00081     hid_t dtype = H5Dget_type(dataset);                                             \
00082     H5T_class_t t_class=H5Tget_class(dtype);                                        \
00083     TSGDataType t datatype; hid_t h5_type=get_compatible_type(t_class, &t);         \
00084     if (h5_type==-1)                                                                \
00085     {                                                                               \
00086         H5Dclose(dataset);                                                          \
00087         SG_INFO("No compatible datatype found\n")                                   \
00088     }                                                                               \
00089     get_dims(dataset, dims, ndims, nelements);                                      \
00090     if (!((ndims==2 && dims[0]==nelements && dims[1]==1) ||                         \
00091             (ndims==2 && dims[0]==1 && dims[1]==nelements) ||                       \
00092             (ndims==1 && dims[0]==nelements)))                                      \
00093         SG_ERROR("Error not a 1-dimensional vector (ndims=%d, dims[0]=%d)\n", ndims, dims[0])   \
00094     vec=SG_MALLOC(sg_type, nelements);                                                      \
00095     len=nelements;                                                                  \
00096     herr_t status = H5Dread(dataset, h5_type, H5S_ALL,                              \
00097             H5S_ALL, H5P_DEFAULT, vec);                                             \
00098     H5Dclose(dataset);                                                              \
00099     H5Tclose(dtype);                                                                \
00100     SG_FREE(dims);                                                                  \
00101     if (status<0)                                                                   \
00102     {                                                                               \
00103         SG_FREE(vec);                                                               \
00104         SG_ERROR("Error reading dataset\n")                                     \
00105     }                                                                               \
00106 }
00107 
00108 GET_VECTOR(get_vector, bool, (CT_VECTOR, ST_NONE, PT_BOOL))
00109 GET_VECTOR(get_vector, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
00110 GET_VECTOR(get_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
00111 GET_VECTOR(get_vector, char, (CT_VECTOR, ST_NONE, PT_CHAR))
00112 GET_VECTOR(get_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00113 GET_VECTOR(get_vector, uint32_t, (CT_VECTOR, ST_NONE, PT_UINT32))
00114 GET_VECTOR(get_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
00115 GET_VECTOR(get_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
00116 GET_VECTOR(get_vector, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
00117 GET_VECTOR(get_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00118 GET_VECTOR(get_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00119 GET_VECTOR(get_vector, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
00120 GET_VECTOR(get_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64))
00121 #undef GET_VECTOR
00122 
00123 #define GET_MATRIX(fname, sg_type, datatype)                                        \
00124 void CHDF5File::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec)        \
00125 {                                                                                   \
00126     if (!h5file)                                                                    \
00127         SG_ERROR("File invalid.\n")                                             \
00128                                                                                     \
00129     int32_t* dims;                                                                  \
00130     int32_t ndims;                                                                  \
00131     int64_t nelements;                                                              \
00132     hid_t dataset = H5Dopen2(h5file, variable_name, H5P_DEFAULT);                   \
00133     if (dataset<0)                                                                  \
00134         SG_ERROR("Error opening data set\n")                                        \
00135     hid_t dtype = H5Dget_type(dataset);                                             \
00136     H5T_class_t t_class=H5Tget_class(dtype);                                        \
00137     TSGDataType t datatype; hid_t h5_type=get_compatible_type(t_class, &t);         \
00138     if (h5_type==-1)                                                                \
00139     {                                                                               \
00140         H5Dclose(dataset);                                                          \
00141         SG_INFO("No compatible datatype found\n")                                   \
00142     }                                                                               \
00143     get_dims(dataset, dims, ndims, nelements);                                      \
00144     if (ndims!=2)                                                                   \
00145         SG_ERROR("Error not a 2-dimensional matrix\n")                              \
00146     matrix=SG_MALLOC(sg_type, nelements);                                                   \
00147     num_feat=dims[0];                                                               \
00148     num_vec=dims[1];                                                                \
00149     herr_t status = H5Dread(dataset, h5_type, H5S_ALL,                              \
00150             H5S_ALL, H5P_DEFAULT, matrix);                                          \
00151     H5Dclose(dataset);                                                              \
00152     H5Tclose(dtype);                                                                \
00153     SG_FREE(dims);                                                                  \
00154     if (status<0)                                                                   \
00155     {                                                                               \
00156         SG_FREE(matrix);                                                            \
00157         SG_ERROR("Error reading dataset\n")                                     \
00158     }                                                                               \
00159 }
00160 
00161 GET_MATRIX(get_matrix, bool, (CT_MATRIX, ST_NONE, PT_BOOL))
00162 GET_MATRIX(get_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
00163 GET_MATRIX(get_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
00164 GET_MATRIX(get_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00165 GET_MATRIX(get_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00166 GET_MATRIX(get_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00167 GET_MATRIX(get_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00168 GET_MATRIX(get_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00169 GET_MATRIX(get_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00170 GET_MATRIX(get_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
00171 GET_MATRIX(get_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
00172 GET_MATRIX(get_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
00173 #undef GET_MATRIX
00174 
00175 void CHDF5File::get_ndarray(uint8_t*& array, int32_t*& dims, int32_t& num_dims)
00176 {
00177 }
00178 
00179 void CHDF5File::get_ndarray(char*& array, int32_t*& dims, int32_t& num_dims)
00180 {
00181 }
00182 
00183 void CHDF5File::get_ndarray(int32_t*& array, int32_t*& dims, int32_t& num_dims)
00184 {
00185 }
00186 
00187 void CHDF5File::get_ndarray(float32_t*& array, int32_t*& dims, int32_t& num_dims)
00188 {
00189 }
00190 
00191 void CHDF5File::get_ndarray(float64_t*& array, int32_t*& dims, int32_t& num_dims)
00192 {
00193 }
00194 
00195 void CHDF5File::get_ndarray(int16_t*& array, int32_t*& dims, int32_t& num_dims)
00196 {
00197 }
00198 
00199 void CHDF5File::get_ndarray(uint16_t*& array, int32_t*& dims, int32_t& num_dims)
00200 {
00201 }
00202 
00203 #define GET_SPARSEMATRIX(fname, sg_type, datatype)                                      \
00204 void CHDF5File::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec)    \
00205 {                                                                                       \
00206     if (!(file))                                                                        \
00207         SG_ERROR("File invalid.\n")                                                 \
00208 }
00209 GET_SPARSEMATRIX(get_sparse_matrix, bool, DT_SPARSE_BOOL)
00210 GET_SPARSEMATRIX(get_sparse_matrix, char, DT_SPARSE_CHAR)
00211 GET_SPARSEMATRIX(get_sparse_matrix, int8_t, DT_SPARSE_INT8)
00212 GET_SPARSEMATRIX(get_sparse_matrix, uint8_t, DT_SPARSE_BYTE)
00213 GET_SPARSEMATRIX(get_sparse_matrix, int32_t, DT_SPARSE_INT)
00214 GET_SPARSEMATRIX(get_sparse_matrix, uint32_t, DT_SPARSE_UINT)
00215 GET_SPARSEMATRIX(get_sparse_matrix, int64_t, DT_SPARSE_LONG)
00216 GET_SPARSEMATRIX(get_sparse_matrix, uint64_t, DT_SPARSE_ULONG)
00217 GET_SPARSEMATRIX(get_sparse_matrix, int16_t, DT_SPARSE_SHORT)
00218 GET_SPARSEMATRIX(get_sparse_matrix, uint16_t, DT_SPARSE_WORD)
00219 GET_SPARSEMATRIX(get_sparse_matrix, float32_t, DT_SPARSE_SHORTREAL)
00220 GET_SPARSEMATRIX(get_sparse_matrix, float64_t, DT_SPARSE_REAL)
00221 GET_SPARSEMATRIX(get_sparse_matrix, floatmax_t, DT_SPARSE_LONGREAL)
00222 #undef GET_SPARSEMATRIX
00223 
00224 
00225 #define GET_STRING_LIST(fname, sg_type, datatype)                                               \
00226 void CHDF5File::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
00227 {                                                                                               \
00228 }
00229 
00230 GET_STRING_LIST(get_string_list, bool, DT_STRING_BOOL)
00231 GET_STRING_LIST(get_string_list, char, DT_STRING_CHAR)
00232 GET_STRING_LIST(get_string_list, int8_t, DT_STRING_INT8)
00233 GET_STRING_LIST(get_string_list, uint8_t, DT_STRING_BYTE)
00234 GET_STRING_LIST(get_string_list, int32_t, DT_STRING_INT)
00235 GET_STRING_LIST(get_string_list, uint32_t, DT_STRING_UINT)
00236 GET_STRING_LIST(get_string_list, int64_t, DT_STRING_LONG)
00237 GET_STRING_LIST(get_string_list, uint64_t, DT_STRING_ULONG)
00238 GET_STRING_LIST(get_string_list, int16_t, DT_STRING_SHORT)
00239 GET_STRING_LIST(get_string_list, uint16_t, DT_STRING_WORD)
00240 GET_STRING_LIST(get_string_list, float32_t, DT_STRING_SHORTREAL)
00241 GET_STRING_LIST(get_string_list, float64_t, DT_STRING_REAL)
00242 GET_STRING_LIST(get_string_list, floatmax_t, DT_STRING_LONGREAL)
00243 #undef GET_STRING_LIST
00244 
00247 #define SET_VECTOR(fname, sg_type, dtype, h5type)                           \
00248 void CHDF5File::fname(const sg_type* vec, int32_t len)                      \
00249 {                                                                           \
00250     if (h5file<0 || !vec)                                                   \
00251         SG_ERROR("File or vector invalid.\n")                               \
00252                                                                             \
00253     create_group_hierarchy();                                               \
00254                                                                             \
00255     hsize_t dims=(hsize_t) len;                                             \
00256     hid_t dataspace, dataset, status;                                       \
00257     dataspace=H5Screate_simple(1, &dims, NULL);                         \
00258     if (dataspace<0)                                                        \
00259         SG_ERROR("Could not create hdf5 dataspace\n")                       \
00260     dataset=H5Dcreate2(h5file, variable_name, h5type, dataspace, H5P_DEFAULT,\
00261             H5P_DEFAULT, H5P_DEFAULT);                                      \
00262     if (dataset<0)                                                          \
00263     {                                                                       \
00264         SG_ERROR("Could not create hdf5 dataset - does"                     \
00265                 " dataset '%s' already exist?\n", variable_name);           \
00266     }                                                                       \
00267     status=H5Dwrite(dataset, h5type, H5S_ALL, H5S_ALL, H5P_DEFAULT, vec);   \
00268     if (status<0)                                                           \
00269         SG_ERROR("Failed to write hdf5 dataset\n")                          \
00270     H5Dclose(dataset);                                                      \
00271     H5Sclose(dataspace);                                                    \
00272 }
00273 SET_VECTOR(set_vector, bool, DT_VECTOR_BOOL, boolean_type)
00274 SET_VECTOR(set_vector, int8_t, DT_VECTOR_BYTE, H5T_NATIVE_INT8)
00275 SET_VECTOR(set_vector, uint8_t, DT_VECTOR_BYTE, H5T_NATIVE_UINT8)
00276 SET_VECTOR(set_vector, char, DT_VECTOR_CHAR, H5T_NATIVE_CHAR)
00277 SET_VECTOR(set_vector, int32_t, DT_VECTOR_INT, H5T_NATIVE_INT32)
00278 SET_VECTOR(set_vector, uint32_t, DT_VECTOR_UINT, H5T_NATIVE_UINT32)
00279 SET_VECTOR(set_vector, float32_t, DT_VECTOR_SHORTREAL, H5T_NATIVE_FLOAT)
00280 SET_VECTOR(set_vector, float64_t, DT_VECTOR_REAL, H5T_NATIVE_DOUBLE)
00281 SET_VECTOR(set_vector, floatmax_t, DT_VECTOR_LONGREAL, H5T_NATIVE_LDOUBLE)
00282 SET_VECTOR(set_vector, int16_t, DT_VECTOR_SHORT, H5T_NATIVE_INT16)
00283 SET_VECTOR(set_vector, uint16_t, DT_VECTOR_WORD, H5T_NATIVE_UINT16)
00284 SET_VECTOR(set_vector, int64_t, DT_VECTOR_LONG, H5T_NATIVE_LLONG)
00285 SET_VECTOR(set_vector, uint64_t, DT_VECTOR_ULONG, H5T_NATIVE_ULLONG)
00286 #undef SET_VECTOR
00287 
00288 #define SET_MATRIX(fname, sg_type, dtype, h5type)                               \
00289 void CHDF5File::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
00290 {                                                                               \
00291     if (h5file<0 || !matrix)                                                    \
00292         SG_ERROR("File or matrix invalid.\n")                                   \
00293                                                                                 \
00294     create_group_hierarchy();                                                   \
00295                                                                                 \
00296     hsize_t dims[2]={(hsize_t) num_feat, (hsize_t) num_vec};                    \
00297     hid_t dataspace, dataset, status;                                           \
00298     dataspace=H5Screate_simple(2, dims, NULL);                                  \
00299     if (dataspace<0)                                                            \
00300         SG_ERROR("Could not create hdf5 dataspace\n")                           \
00301     dataset=H5Dcreate2(h5file, variable_name, h5type, dataspace, H5P_DEFAULT,   \
00302             H5P_DEFAULT, H5P_DEFAULT);                                          \
00303     if (dataset<0)                                                              \
00304     {                                                                           \
00305         SG_ERROR("Could not create hdf5 dataset - does"                         \
00306                 " dataset '%s' already exist?\n", variable_name);               \
00307     }                                                                           \
00308     status=H5Dwrite(dataset, h5type, H5S_ALL, H5S_ALL, H5P_DEFAULT, matrix);    \
00309     if (status<0)                                                               \
00310         SG_ERROR("Failed to write hdf5 dataset\n")                              \
00311     H5Dclose(dataset);                                                          \
00312     H5Sclose(dataspace);                                                        \
00313 }
00314 SET_MATRIX(set_matrix, bool, DT_DENSE_BOOL, boolean_type)
00315 SET_MATRIX(set_matrix, char, DT_DENSE_CHAR, H5T_NATIVE_CHAR)
00316 SET_MATRIX(set_matrix, int8_t, DT_DENSE_BYTE, H5T_NATIVE_INT8)
00317 SET_MATRIX(set_matrix, uint8_t, DT_DENSE_BYTE, H5T_NATIVE_UINT8)
00318 SET_MATRIX(set_matrix, int32_t, DT_DENSE_INT, H5T_NATIVE_INT32)
00319 SET_MATRIX(set_matrix, uint32_t, DT_DENSE_UINT, H5T_NATIVE_UINT32)
00320 SET_MATRIX(set_matrix, int64_t, DT_DENSE_LONG, H5T_NATIVE_INT64)
00321 SET_MATRIX(set_matrix, uint64_t, DT_DENSE_ULONG, H5T_NATIVE_UINT64)
00322 SET_MATRIX(set_matrix, int16_t, DT_DENSE_SHORT, H5T_NATIVE_INT16)
00323 SET_MATRIX(set_matrix, uint16_t, DT_DENSE_WORD, H5T_NATIVE_UINT16)
00324 SET_MATRIX(set_matrix, float32_t, DT_DENSE_SHORTREAL, H5T_NATIVE_FLOAT)
00325 SET_MATRIX(set_matrix, float64_t, DT_DENSE_REAL, H5T_NATIVE_DOUBLE)
00326 SET_MATRIX(set_matrix, floatmax_t, DT_DENSE_LONGREAL, H5T_NATIVE_LDOUBLE)
00327 #undef SET_MATRIX
00328 
00329 #define SET_SPARSEMATRIX(fname, sg_type, dtype)         \
00330 void CHDF5File::fname(const SGSparseVector<sg_type>* matrix,    \
00331         int32_t num_feat, int32_t num_vec)                  \
00332 {                                                           \
00333     if (!(file && matrix))                                  \
00334         SG_ERROR("File or matrix invalid.\n")               \
00335                                                             \
00336 }
00337 SET_SPARSEMATRIX(set_sparse_matrix, bool, DT_SPARSE_BOOL)
00338 SET_SPARSEMATRIX(set_sparse_matrix, char, DT_SPARSE_CHAR)
00339 SET_SPARSEMATRIX(set_sparse_matrix, int8_t, DT_SPARSE_INT8)
00340 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, DT_SPARSE_BYTE)
00341 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, DT_SPARSE_INT)
00342 SET_SPARSEMATRIX(set_sparse_matrix, uint32_t, DT_SPARSE_UINT)
00343 SET_SPARSEMATRIX(set_sparse_matrix, int64_t, DT_SPARSE_LONG)
00344 SET_SPARSEMATRIX(set_sparse_matrix, uint64_t, DT_SPARSE_ULONG)
00345 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, DT_SPARSE_SHORT)
00346 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, DT_SPARSE_WORD)
00347 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, DT_SPARSE_SHORTREAL)
00348 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, DT_SPARSE_REAL)
00349 SET_SPARSEMATRIX(set_sparse_matrix, floatmax_t, DT_SPARSE_LONGREAL)
00350 #undef SET_SPARSEMATRIX
00351 
00352 #define SET_STRING_LIST(fname, sg_type, dtype) \
00353 void CHDF5File::fname(const SGString<sg_type>* strings, int32_t num_str)    \
00354 {                                                                                       \
00355     if (!(file && strings))                                                             \
00356         SG_ERROR("File or strings invalid.\n")                                          \
00357                                                                                         \
00358 }
00359 SET_STRING_LIST(set_string_list, bool, DT_STRING_BOOL)
00360 SET_STRING_LIST(set_string_list, char, DT_STRING_CHAR)
00361 SET_STRING_LIST(set_string_list, int8_t, DT_STRING_INT8)
00362 SET_STRING_LIST(set_string_list, uint8_t, DT_STRING_BYTE)
00363 SET_STRING_LIST(set_string_list, int32_t, DT_STRING_INT)
00364 SET_STRING_LIST(set_string_list, uint32_t, DT_STRING_UINT)
00365 SET_STRING_LIST(set_string_list, int64_t, DT_STRING_LONG)
00366 SET_STRING_LIST(set_string_list, uint64_t, DT_STRING_ULONG)
00367 SET_STRING_LIST(set_string_list, int16_t, DT_STRING_SHORT)
00368 SET_STRING_LIST(set_string_list, uint16_t, DT_STRING_WORD)
00369 SET_STRING_LIST(set_string_list, float32_t, DT_STRING_SHORTREAL)
00370 SET_STRING_LIST(set_string_list, float64_t, DT_STRING_REAL)
00371 SET_STRING_LIST(set_string_list, floatmax_t, DT_STRING_LONGREAL)
00372 #undef SET_STRING_LIST
00373 
00374 void CHDF5File::get_boolean_type()
00375 {
00376     boolean_type=H5T_NATIVE_UCHAR;
00377     switch (sizeof(bool))
00378     {
00379         case 1:
00380             boolean_type = H5T_NATIVE_UCHAR;
00381             break;
00382         case 2:
00383             boolean_type = H5T_NATIVE_UINT16;
00384             break;
00385         case 4:
00386             boolean_type = H5T_NATIVE_UINT32;
00387             break;
00388         case 8:
00389             boolean_type = H5T_NATIVE_UINT64;
00390             break;
00391         default:
00392             SG_ERROR("Boolean type not supported on this platform\n")
00393     }
00394 }
00395 
00396 hid_t CHDF5File::get_compatible_type(H5T_class_t t_class,
00397                                      const TSGDataType* datatype)
00398 {
00399     switch (t_class)
00400     {
00401         case H5T_FLOAT:
00402         case H5T_INTEGER:
00403             switch (datatype->m_ptype)
00404             {
00405             case PT_BOOL: return boolean_type;
00406             case PT_CHAR: return H5T_NATIVE_CHAR;
00407             case PT_INT8: return H5T_NATIVE_INT8;
00408             case PT_UINT8: return H5T_NATIVE_UINT8;
00409             case PT_INT16: return H5T_NATIVE_INT16;
00410             case PT_UINT16: return H5T_NATIVE_UINT16;
00411             case PT_INT32: return H5T_NATIVE_INT32;
00412             case PT_UINT32: return H5T_NATIVE_UINT32;
00413             case PT_INT64: return H5T_NATIVE_INT64;
00414             case PT_UINT64: return H5T_NATIVE_UINT64;
00415             case PT_FLOAT32: return H5T_NATIVE_FLOAT;
00416             case PT_FLOAT64: return H5T_NATIVE_DOUBLE;
00417             case PT_FLOATMAX: return H5T_NATIVE_LDOUBLE;
00418             case PT_COMPLEX128:
00419                 SG_ERROR("complex128_t not compatible with HDF5File!");
00420                 return -1;
00421             case PT_SGOBJECT:
00422             case PT_UNDEFINED:
00423                 SG_ERROR("Implementation error during writing "
00424                          "HDF5File!");
00425                 return -1;
00426             }
00427         case H5T_STRING:
00428             SG_ERROR("Strings not supported")
00429             return -1;
00430         case H5T_VLEN:
00431             SG_ERROR("Variable length containers currently not supported")
00432             return -1;
00433         case H5T_ARRAY:
00434             SG_ERROR("Array containers currently not supported")
00435             return -1;
00436         default:
00437             SG_ERROR("Datatype mismatchn")
00438             return -1;
00439     }
00440 }
00441 
00442 void CHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& total_elements)
00443 {
00444     hid_t dataspace = H5Dget_space(dataset);
00445     if (dataspace<0)
00446         SG_ERROR("Error obtaining hdf5 dataspace\n")
00447 
00448     ndims = H5Sget_simple_extent_ndims(dataspace);
00449     total_elements=H5Sget_simple_extent_npoints(dataspace);
00450     hsize_t* dims_out=SG_MALLOC(hsize_t, ndims);
00451     dims=SG_MALLOC(int32_t, ndims);
00452     H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
00453     for (int32_t i=0; i<ndims; i++)
00454         dims[i]=dims_out[i];
00455     SG_FREE(dims_out);
00456     H5Sclose(dataspace);
00457 }
00458 
00459 void CHDF5File::create_group_hierarchy()
00460 {
00461     char* vname=get_strdup(variable_name);
00462     int32_t vlen=strlen(vname);
00463     for (int32_t i=0; i<vlen; i++)
00464     {
00465         if (i!=0 && vname[i]=='/')
00466         {
00467             vname[i]='\0';
00468             hid_t g = H5Gopen2(h5file, vname, H5P_DEFAULT);
00469             if (g<0)
00470             {
00471                 g=H5Gcreate2(h5file, vname, H5P_DEFAULT, H5P_DEFAULT,
00472                         H5P_DEFAULT);
00473                 if (g<0)
00474                     SG_ERROR("Error creating group '%s'\n", vname)
00475                 vname[i]='/';
00476             }
00477             H5Gclose(g);
00478         }
00479     }
00480     SG_FREE(vname);
00481 }
00482 #endif //  HDF5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation