Skip to content
Snippets Groups Projects
Commit 040147b9 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Fixed the issue #1

It seems that just checking if the file is good to read is not enough (std::ifstream::good).
This function reads one extra line.

I did a hard check with the number of samples and solved the problem.
parent fa90f1e1
No related branches found
No related tags found
No related merge requests found
...@@ -488,8 +488,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all ...@@ -488,8 +488,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all
auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values); auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values);
blitz::Range all = blitz::Range::all(); blitz::Range all = blitz::Range::all();
int k = 0; int k = 0;
while (self->cxx->good()) {
while ((self->cxx->good()) && ((size_t)k < self->cxx->samples())) {
blitz::Array<double,1> v_ = (*bzval)(k, all); blitz::Array<double,1> v_ = (*bzval)(k, all);
int label = 0; int label = 0;
bool ok = self->cxx->read_(label, v_); bool ok = self->cxx->read_(label, v_);
if (ok) (*bzlab)(k) = label; if (ok) (*bzlab)(k) = label;
......
...@@ -131,6 +131,7 @@ def test_data_loading(): ...@@ -131,6 +131,7 @@ def test_data_loading():
nose.tools.eq_(data.fail(), False) nose.tools.eq_(data.fail(), False)
nose.tools.eq_(data.eof(), False) nose.tools.eq_(data.eof(), False)
#tries loading the data, one by one #tries loading the data, one by one
all_data = [] all_data = []
all_labels = [] all_labels = []
...@@ -153,13 +154,18 @@ def test_data_loading(): ...@@ -153,13 +154,18 @@ def test_data_loading():
counter += 1 counter += 1
entry = data.read() entry = data.read()
#tries loading the file all in a single shot #tries loading the file all in a single shot
data.reset() data.reset()
labels, data = data.read_all() labels, data = data.read_all()
assert numpy.array_equal(labels, all_labels) assert numpy.array_equal(labels, all_labels)
for k, l in zip(data, all_data): for k, l in zip(data, all_data):
assert numpy.array_equal(k, l) assert numpy.array_equal(k, l)
#makes sure the first 3 examples are correctly read #makes sure the first 3 examples are correctly read
ex = [] ex = []
ex.append(numpy.array([0.708333 , 1, 1, -0.320755 , -0.105023 , -1, 1, ex.append(numpy.array([0.708333 , 1, 1, -0.320755 , -0.105023 , -1, 1,
...@@ -169,10 +175,12 @@ def test_data_loading(): ...@@ -169,10 +175,12 @@ def test_data_loading():
ex.append(numpy.array([0.166667, 1, -0.333333, -0.433962, -0.383562, -1, ex.append(numpy.array([0.166667, 1, -0.333333, -0.433962, -0.383562, -1,
-1, 0.0687023, -1, -0.903226, -1, -1, 1], 'float64')) -1, 0.0687023, -1, -0.903226, -1, -1, 1], 'float64'))
ls = [+1, -1, +1] ls = [+1, -1, +1]
for k, (l, e) in enumerate(zip(ls, ex)): for k, (l, e) in enumerate(zip(ls, ex)):
nose.tools.eq_( l, labels[k] ) nose.tools.eq_( l, labels[k] )
assert numpy.array_equal(e, data[k]) assert numpy.array_equal(e, data[k])
@nose.tools.raises(RuntimeError) @nose.tools.raises(RuntimeError)
def test_raises(): def test_raises():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment