34 #define GET_VECTOR(fname, sg_type, datatype) \ 35 void CBinaryFile::fname(sg_type*& vec, int32_t& len) \ 38 SG_ERROR("File invalid.\n") \ 39 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 40 if (dtype!=datatype) \ 41 SG_ERROR("Datatype mismatch\n") \ 43 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 44 SG_ERROR("Failed to read vector length\n") \ 45 vec=SG_MALLOC(sg_type, len); \ 46 if (fread(vec, sizeof(sg_type), len, file)!=(size_t) len) \ 47 SG_ERROR("Failed to read Matrix\n") \ 64 #define GET_MATRIX(fname, sg_type, datatype) \ 65 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ 68 SG_ERROR("File invalid.\n") \ 69 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 70 if (dtype!=datatype) \ 71 SG_ERROR("Datatype mismatch\n") \ 73 if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 || \ 74 fread(&num_vec, sizeof(int32_t), 1, file)!=1) \ 75 SG_ERROR("Failed to read Matrix dimensions\n") \ 76 matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec); \ 77 if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \ 78 SG_ERROR("Failed to read Matrix\n") \ 95 #define GET_NDARRAY(fname,sg_type,datatype) \ 96 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\ 101 SG_ERROR("File invalid.\n") \ 103 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \ 104 read_header(&dtype); \ 106 if (dtype!=datatype) \ 107 SG_ERROR("Datatype mismatch\n") \ 109 if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \ 110 SG_ERROR("Failed to read number of dimensions") \ 112 dims = SG_MALLOC(int32_t, num_dims); \ 113 if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \ 114 SG_ERROR("Failed to read sizes of dimensions!") \ 116 for (int32_t i = 0;i < num_dims;i++) \ 119 array = SG_MALLOC(sg_type, total); \ 120 if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \ 121 SG_ERROR("Failed to read array data!") \ 133 #define GET_SPARSEMATRIX(fname, sg_type, datatype) \ 134 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \ 137 SG_ERROR("File invalid.\n") \ 139 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 140 if (dtype!=datatype) \ 141 SG_ERROR("Datatype mismatch\n") \ 143 if (fread(&num_vec, sizeof(int32_t), 1, file)!=1) \ 144 SG_ERROR("Failed to read number of vectors\n") \ 146 matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec); \ 148 for (int32_t i=0; i<num_vec; i++) \ 150 new (&matrix[i]) SGSparseVector<sg_type>(); \ 152 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 153 SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i) \ 154 matrix[i].num_feat_entries=len; \ 155 SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \ 156 if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \ 157 SG_ERROR("Failed to read sparse vector %d\n", i) \ 158 matrix[i].features=vec; \ 159 num_feat = CMath::max(num_feat, matrix[i].get_num_dimensions()); \ 175 #undef GET_SPARSEMATRIX 178 #define GET_STRING_LIST(fname, sg_type, datatype) \ 179 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \ 186 SG_ERROR("File invalid.\n") \ 188 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 189 if (dtype!=datatype) \ 190 SG_ERROR("Datatype mismatch\n") \ 192 if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \ 193 SG_ERROR("Failed to read number of strings\n") \ 195 strings=SG_MALLOC(SGString<sg_type>, num_str); \ 197 for (int32_t i=0; i<num_str; i++) \ 200 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 201 SG_ERROR("Failed to read string length of string with idx=%d\n", i) \ 202 strings[i].slen=len; \ 203 sg_type* str = SG_MALLOC(sg_type, len); \ 204 if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \ 205 SG_ERROR("Failed to read string %d\n", i) \ 206 strings[i].string=str; \ 222 #undef GET_STRING_LIST 226 #define SET_VECTOR(fname, sg_type, dtype) \ 227 void CBinaryFile::fname(const sg_type* vec, int32_t len) \ 229 if (!(file && vec)) \ 230 SG_ERROR("File or vector invalid.\n") \ 232 TSGDataType t dtype; write_header(&t); \ 234 if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \ 235 fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \ 236 SG_ERROR("Failed to write vector\n") \ 252 #define SET_MATRIX(fname, sg_type, dtype) \ 253 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ 255 if (!(file && matrix)) \ 256 SG_ERROR("File or matrix invalid.\n") \ 258 TSGDataType t dtype; write_header(&t); \ 260 if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \ 261 fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \ 262 fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \ 263 SG_ERROR("Failed to write Matrix\n") \ 279 #define SET_NDARRAY(fname,sg_type,datatype) \ 280 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \ 285 SG_ERROR("File invalid.\n") \ 288 SG_ERROR("Invalid array!\n") \ 290 TSGDataType t datatype; \ 293 if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \ 294 SG_ERROR("Failed to write number of dimensions!\n") \ 296 if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \ 297 SG_ERROR("Failed to write sizes of dimensions!\n") \ 299 for (int32_t i = 0;i < num_dims;i++) \ 302 if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \ 303 SG_ERROR("Failed to write array data!\n") \ 315 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \ 316 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \ 317 int32_t num_feat, int32_t num_vec) \ 319 if (!(file && matrix)) \ 320 SG_ERROR("File or matrix invalid.\n") \ 322 TSGDataType t dtype; write_header(&t); \ 324 if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \ 325 SG_ERROR("Failed to write Sparse Matrix\n") \ 327 for (int32_t i=0; i<num_vec; i++) \ 329 SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \ 330 int32_t len=matrix[i].num_feat_entries; \ 331 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \ 332 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \ 333 SG_ERROR("Failed to write Sparse Matrix\n") \ 349 #undef SET_SPARSEMATRIX 351 #define SET_STRING_LIST(fname, sg_type, dtype) \ 352 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \ 354 if (!(file && strings)) \ 355 SG_ERROR("File or strings invalid.\n") \ 357 TSGDataType t dtype; write_header(&t); \ 358 for (int32_t i=0; i<num_str; i++) \ 360 int32_t len = strings[i].slen; \ 361 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \ 362 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \ 363 SG_ERROR("Failed to write Sparse Matrix\n") \ 378 #undef SET_STRING_LIST 397 if (fseek(
file, 0L, SEEK_SET)!=0)
403 if (fread(&fourcc,
sizeof(
char), 4,
file)!=4)
406 if (fread(&endian,
sizeof(uint16_t), 1,
file)!=1)
413 if (strncmp(fourcc,
"SG01", 4))
422 const char* fourcc=
"SG01";
423 uint16_t endian=0x1234;
425 if (!((fwrite(fourcc,
sizeof(
char), 4,
file)==4) &&
426 (fwrite(&endian,
sizeof(uint16_t), 1,
file)==1) &&
#define GET_MATRIX(fname, sg_type, datatype)
virtual void set_vector(const int8_t *vector, int32_t len)
virtual void get_sparse_matrix(SGSparseVector< bool > *&matrix, int32_t &num_feat, int32_t &num_vec)
#define GET_STRING_LIST(fname, sg_type, datatype)
#define SET_STRING_LIST(fname, sg_type, dtype)
#define SET_MATRIX(fname, sg_type, dtype)
Datatypes that shogun supports.
int32_t parse_next_header(TSGDataType &type)
A File access base class.
virtual void set_matrix(const uint8_t *matrix, int32_t num_feat, int32_t num_vec)
#define SET_SPARSEMATRIX(fname, sg_type, dtype)
virtual void get_ndarray(uint8_t *&array, int32_t *&dims, int32_t &num_dims)
int32_t parse_first_header(TSGDataType &type)
virtual void get_vector(int8_t *&vector, int32_t &len)
void read_header(TSGDataType *dest)
#define SET_NDARRAY(fname, sg_type, datatype)
virtual void set_ndarray(const uint8_t *array, int32_t *dims, int32_t num_dims)
#define SET_VECTOR(fname, sg_type, dtype)
all of classes and functions are contained in the shogun namespace
#define GET_VECTOR(fname, sg_type, datatype)
virtual void get_string_list(SGString< uint8_t > *&strings, int32_t &num_str, int32_t &max_string_len)
virtual void get_matrix(uint8_t *&matrix, int32_t &num_feat, int32_t &num_vec)
virtual void set_string_list(const SGString< uint8_t > *strings, int32_t num_str)
void write_header(const TSGDataType *datatype)
#define GET_NDARRAY(fname, sg_type, datatype)
#define SG_UNSTABLE(func,...)
#define GET_SPARSEMATRIX(fname, sg_type, datatype)
virtual void set_sparse_matrix(const SGSparseVector< bool > *matrix, int32_t num_feat, int32_t num_vec)