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
......@@ -488,8 +488,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all
auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values);
blitz::Range all = blitz::Range::all();
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);
int label = 0;
bool ok = self->cxx->read_(label, v_);
if (ok) (*bzlab)(k) = label;
......
......@@ -131,6 +131,7 @@ def test_data_loading():
nose.tools.eq_(data.fail(), False)
nose.tools.eq_(data.eof(), False)
#tries loading the data, one by one
all_data = []
all_labels = []
......@@ -153,13 +154,18 @@ def test_data_loading():
counter += 1
entry = data.read()
#tries loading the file all in a single shot
data.reset()
labels, data = data.read_all()
assert numpy.array_equal(labels, all_labels)
for k, l in zip(data, all_data):
assert numpy.array_equal(k, l)
#makes sure the first 3 examples are correctly read
ex = []
ex.append(numpy.array([0.708333 , 1, 1, -0.320755 , -0.105023 , -1, 1,
......@@ -169,10 +175,12 @@ def test_data_loading():
ex.append(numpy.array([0.166667, 1, -0.333333, -0.433962, -0.383562, -1,
-1, 0.0687023, -1, -0.903226, -1, -1, 1], 'float64'))
ls = [+1, -1, +1]
for k, (l, e) in enumerate(zip(ls, ex)):
nose.tools.eq_( l, labels[k] )
assert numpy.array_equal(e, data[k])
@nose.tools.raises(RuntimeError)
def test_raises():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment