From 040147b9077702954c203dc025904f885579b24d Mon Sep 17 00:00:00 2001 From: Tiago Freitas Pereira <tiagofrepereira@gmail.com> Date: Sat, 13 Jun 2015 15:31:54 +0200 Subject: [PATCH] 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. --- bob/learn/libsvm/file.cpp | 4 +++- bob/learn/libsvm/test_machine.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/bob/learn/libsvm/file.cpp b/bob/learn/libsvm/file.cpp index 09ad465..a6720d2 100644 --- a/bob/learn/libsvm/file.cpp +++ b/bob/learn/libsvm/file.cpp @@ -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; diff --git a/bob/learn/libsvm/test_machine.py b/bob/learn/libsvm/test_machine.py index 001610e..c05d912 100644 --- a/bob/learn/libsvm/test_machine.py +++ b/bob/learn/libsvm/test_machine.py @@ -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(): -- GitLab