38 #define GET_VECTOR(fname, sg_type, datatype) \ 39 void CBinaryFile::fname(sg_type*& vec, int32_t& len) \ 42 SG_ERROR("File invalid.\n") \ 43 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 44 if (dtype!=datatype) \ 45 SG_ERROR("Datatype mismatch\n") \ 47 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 48 SG_ERROR("Failed to read vector length\n") \ 49 vec=SG_MALLOC(sg_type, len); \ 50 if (fread(vec, sizeof(sg_type), len, file)!=(size_t) len) \ 51 SG_ERROR("Failed to read Matrix\n") \ 68 #define GET_MATRIX(fname, sg_type, datatype) \ 69 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ 72 SG_ERROR("File invalid.\n") \ 73 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 74 if (dtype!=datatype) \ 75 SG_ERROR("Datatype mismatch\n") \ 77 if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 || \ 78 fread(&num_vec, sizeof(int32_t), 1, file)!=1) \ 79 SG_ERROR("Failed to read Matrix dimensions\n") \ 80 matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec); \ 81 if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \ 82 SG_ERROR("Failed to read Matrix\n") \ 99 #define GET_NDARRAY(fname,sg_type,datatype) \ 100 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\ 105 SG_ERROR("File invalid.\n") \ 107 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \ 108 read_header(&dtype); \ 110 if (dtype!=datatype) \ 111 SG_ERROR("Datatype mismatch\n") \ 113 if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \ 114 SG_ERROR("Failed to read number of dimensions") \ 116 dims = SG_MALLOC(int32_t, num_dims); \ 117 if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \ 118 SG_ERROR("Failed to read sizes of dimensions!") \ 120 for (int32_t i = 0;i < num_dims;i++) \ 123 array = SG_MALLOC(sg_type, total); \ 124 if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \ 125 SG_ERROR("Failed to read array data!") \ 137 #define GET_SPARSEMATRIX(fname, sg_type, datatype) \ 138 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \ 141 SG_ERROR("File invalid.\n") \ 143 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 144 if (dtype!=datatype) \ 145 SG_ERROR("Datatype mismatch\n") \ 147 if (fread(&num_vec, sizeof(int32_t), 1, file)!=1) \ 148 SG_ERROR("Failed to read number of vectors\n") \ 150 matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec); \ 152 for (int32_t i=0; i<num_vec; i++) \ 154 new (&matrix[i]) SGSparseVector<sg_type>(); \ 156 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 157 SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i) \ 158 matrix[i].num_feat_entries=len; \ 159 SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \ 160 if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \ 161 SG_ERROR("Failed to read sparse vector %d\n", i) \ 162 matrix[i].features=vec; \ 163 num_feat = CMath::max(num_feat, matrix[i].get_num_dimensions()); \ 179 #undef GET_SPARSEMATRIX 182 #define GET_STRING_LIST(fname, sg_type, datatype) \ 183 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \ 190 SG_ERROR("File invalid.\n") \ 192 TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \ 193 if (dtype!=datatype) \ 194 SG_ERROR("Datatype mismatch\n") \ 196 if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \ 197 SG_ERROR("Failed to read number of strings\n") \ 199 strings=SG_MALLOC(SGString<sg_type>, num_str); \ 201 for (int32_t i=0; i<num_str; i++) \ 204 if (fread(&len, sizeof(int32_t), 1, file)!=1) \ 205 SG_ERROR("Failed to read string length of string with idx=%d\n", i) \ 206 strings[i].slen=len; \ 207 sg_type* str = SG_MALLOC(sg_type, len); \ 208 if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \ 209 SG_ERROR("Failed to read string %d\n", i) \ 210 strings[i].string=str; \ 226 #undef GET_STRING_LIST 230 #define SET_VECTOR(fname, sg_type, dtype) \ 231 void CBinaryFile::fname(const sg_type* vec, int32_t len) \ 233 if (!(file && vec)) \ 234 SG_ERROR("File or vector invalid.\n") \ 236 TSGDataType t dtype; write_header(&t); \ 238 if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \ 239 fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \ 240 SG_ERROR("Failed to write vector\n") \ 256 #define SET_MATRIX(fname, sg_type, dtype) \ 257 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ 259 if (!(file && matrix)) \ 260 SG_ERROR("File or matrix invalid.\n") \ 262 TSGDataType t dtype; write_header(&t); \ 264 if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \ 265 fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \ 266 fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \ 267 SG_ERROR("Failed to write Matrix\n") \ 283 #define SET_NDARRAY(fname,sg_type,datatype) \ 284 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \ 289 SG_ERROR("File invalid.\n") \ 292 SG_ERROR("Invalid array!\n") \ 294 TSGDataType t datatype; \ 297 if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \ 298 SG_ERROR("Failed to write number of dimensions!\n") \ 300 if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \ 301 SG_ERROR("Failed to write sizes of dimensions!\n") \ 303 for (int32_t i = 0;i < num_dims;i++) \ 306 if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \ 307 SG_ERROR("Failed to write array data!\n") \ 319 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \ 320 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \ 321 int32_t num_feat, int32_t num_vec) \ 323 if (!(file && matrix)) \ 324 SG_ERROR("File or matrix invalid.\n") \ 326 TSGDataType t dtype; write_header(&t); \ 328 if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \ 329 SG_ERROR("Failed to write Sparse Matrix\n") \ 331 for (int32_t i=0; i<num_vec; i++) \ 333 SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \ 334 int32_t len=matrix[i].num_feat_entries; \ 335 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \ 336 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \ 337 SG_ERROR("Failed to write Sparse Matrix\n") \ 353 #undef SET_SPARSEMATRIX 355 #define SET_STRING_LIST(fname, sg_type, dtype) \ 356 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \ 358 if (!(file && strings)) \ 359 SG_ERROR("File or strings invalid.\n") \ 361 TSGDataType t dtype; write_header(&t); \ 362 for (int32_t i=0; i<num_str; i++) \ 364 int32_t len = strings[i].slen; \ 365 if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \ 366 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \ 367 SG_ERROR("Failed to write Sparse Matrix\n") \ 382 #undef SET_STRING_LIST 401 if (fseek(
file, 0L, SEEK_SET)!=0)
407 if (fread(&fourcc,
sizeof(
char), 4,
file)!=4)
410 if (fread(&endian,
sizeof(uint16_t), 1,
file)!=1)
417 if (strncmp(fourcc,
"SG01", 4))
426 const char* fourcc=
"SG01";
427 uint16_t endian=0x1234;
429 if (!((fwrite(fourcc,
sizeof(
char), 4,
file)==4) &&
430 (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)