Skip to content

Commit

Permalink
Merge pull request #1489 from gsomix/feature/libsvm
Browse files Browse the repository at this point in the history
Probably fix for issues #1401 #1402
  • Loading branch information
Soeren Sonnenburg committed Aug 28, 2013
2 parents 64016bc + fea6d2d commit 4f55402
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src/shogun/io/CSVFile.cpp
Expand Up @@ -180,6 +180,8 @@ void CCSVFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \
skip_lines(m_num_to_skip); \
num_lines=get_stats(num_tokens); \
\
SG_SET_LOCALE_C; \
\
matrix=SG_MALLOC(sg_type, num_lines*num_tokens); \
skip_lines(m_num_to_skip); \
while (m_line_reader->has_next()) \
Expand All @@ -200,6 +202,8 @@ void CCSVFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \
current_line_idx++; \
} \
\
SG_RESET_LOCALE; \
\
if (!is_data_transposed) \
{ \
num_feat=num_tokens; \
Expand Down Expand Up @@ -229,6 +233,8 @@ GET_MATRIX(get_matrix, read_ulong, uint64_t)
#define SET_VECTOR(fname, format, sg_type) \
void CCSVFile::fname(const sg_type* vector, int32_t len) \
{ \
SG_SET_LOCALE_C; \
\
if (!is_data_transposed) \
{ \
for (int32_t i=0; i<len; i++) \
Expand All @@ -240,6 +246,8 @@ void CCSVFile::fname(const sg_type* vector, int32_t len) \
fprintf(file, "%" format "%c", vector[i], m_delimiter); \
fprintf(file, "\n"); \
} \
\
SG_RESET_LOCALE; \
}

SET_VECTOR(set_vector, SCNi8, int8_t)
Expand All @@ -259,6 +267,8 @@ SET_VECTOR(set_vector, SCNu16, uint16_t)
#define SET_MATRIX(fname, format, sg_type) \
void CCSVFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
{ \
SG_SET_LOCALE_C; \
\
if (!is_data_transposed) \
{ \
for (int32_t i=0; i<num_vec; i++) \
Expand All @@ -277,6 +287,8 @@ void CCSVFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
fprintf(file, "\n"); \
} \
} \
\
SG_RESET_LOCALE; \
}

SET_MATRIX(set_matrix, SCNi8, int8_t)
Expand Down
8 changes: 8 additions & 0 deletions src/shogun/io/LibSVMFile.cpp
Expand Up @@ -115,6 +115,8 @@ void CLibSVMFile::get_sparse_matrix(SGSparseVector<sg_type>*& matrix, int32_t& n
if (load_labels) \
labels=SG_MALLOC(float64_t, num_vec); \
\
SG_SET_LOCALE_C; \
\
while (m_line_reader->has_next()) \
{ \
num_entries=0; \
Expand Down Expand Up @@ -158,6 +160,8 @@ void CLibSVMFile::get_sparse_matrix(SGSparseVector<sg_type>*& matrix, int32_t& n
SG_PROGRESS(current_line_ind, 0, num_vec, 1, "LOADING:\t") \
} \
\
SG_RESET_LOCALE; \
\
SG_INFO("file successfully read\n") \
}

Expand Down Expand Up @@ -203,6 +207,8 @@ void CLibSVMFile::set_sparse_matrix( \
const SGSparseVector<sg_type>* matrix, int32_t num_feat, int32_t num_vec, \
const float64_t* labels) \
{ \
SG_SET_LOCALE_C; \
\
for (int32_t i=0; i<num_vec; i++) \
{ \
if (labels!=NULL) \
Expand All @@ -217,6 +223,8 @@ void CLibSVMFile::set_sparse_matrix( \
} \
fprintf(file, "\n"); \
} \
\
SG_RESET_LOCALE; \
}

SET_LABELED_SPARSE_MATRIX(SCNi32, bool)
Expand Down
19 changes: 10 additions & 9 deletions src/shogun/io/Parser.cpp
Expand Up @@ -87,28 +87,22 @@ bool CParser::read_bool()
SGVector<char> token=read_cstring();

if (token.vlen>0)
return (bool) strtol(token.vector, NULL, 10);
return (bool) strtod(token.vector, NULL);
else
return (bool) 0L;
}

#define READ_INT_METHOD(fname, convf, sg_type) \
sg_type CParser::fname(int32_t base) \
sg_type CParser::fname() \
{ \
SGVector<char> token=read_cstring(); \
\
if (token.vlen>0) \
return (sg_type) convf(token.vector, NULL, base); \
return (sg_type) convf(token.vector, NULL, 10); \
else \
return (sg_type) 0L; \
}

READ_INT_METHOD(read_char, strtol, char)
READ_INT_METHOD(read_byte, strtoul, uint8_t)
READ_INT_METHOD(read_short, strtol, int16_t)
READ_INT_METHOD(read_word, strtoul, uint16_t)
READ_INT_METHOD(read_int, strtol, int32_t)
READ_INT_METHOD(read_uint, strtoul, uint32_t)
READ_INT_METHOD(read_long, strtoll, int64_t)
READ_INT_METHOD(read_ulong, strtoull, uint64_t)
#undef READ_INT_METHOD
Expand All @@ -124,6 +118,13 @@ sg_type CParser::fname() \
return (sg_type) 0L; \
}

READ_REAL_METHOD(read_char, strtod, char)
READ_REAL_METHOD(read_byte, strtod, uint8_t)
READ_REAL_METHOD(read_short, strtod, int16_t)
READ_REAL_METHOD(read_word, strtod, uint16_t)
READ_REAL_METHOD(read_int, strtod, int32_t)
READ_REAL_METHOD(read_uint, strtod, uint32_t)

READ_REAL_METHOD(read_short_real, strtod, float32_t)
READ_REAL_METHOD(read_real, strtod, float64_t)
#ifdef HAVE_STRTOLD
Expand Down
16 changes: 8 additions & 8 deletions src/shogun/io/Parser.h
Expand Up @@ -50,14 +50,14 @@ class CParser : public CSGObject
/** read one of the several base data types. */
//@{
virtual bool read_bool();
virtual char read_char(int32_t base=10);
virtual uint8_t read_byte(int32_t base=10);
virtual int16_t read_short(int32_t base=10);
virtual uint16_t read_word(int32_t base=10);
virtual int32_t read_int(int32_t base=10);
virtual uint32_t read_uint(int32_t base=10);
virtual int64_t read_long(int32_t base=10);
virtual uint64_t read_ulong(int32_t base=10);
virtual char read_char();
virtual uint8_t read_byte();
virtual int16_t read_short();
virtual uint16_t read_word();
virtual int32_t read_int();
virtual uint32_t read_uint();
virtual int64_t read_long();
virtual uint64_t read_ulong();
virtual float32_t read_short_real();
virtual float64_t read_real();
virtual floatmax_t read_long_real();
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/io/Parser_unittest.cc
Expand Up @@ -55,6 +55,8 @@ TEST(ParserTest, tokenization_reals)

CParser* reader=new CParser(cv, tokenizer);

SG_SET_LOCALE_C;

float64_t tmp=0;
int32_t num_tokens=0;
while (reader->has_next())
Expand All @@ -65,6 +67,8 @@ TEST(ParserTest, tokenization_reals)
}
EXPECT_EQ(num_tokens, ntokens);

SG_RESET_LOCALE;

SG_UNREF(reader);
SG_UNREF(tokenizer);
}

0 comments on commit 4f55402

Please sign in to comment.