Ticket #37833: diff-fix-issue109-libsvm-bug.patch

File diff-fix-issue109-libsvm-bug.patch, 1.4 KB (added by andre.dos.anjos@…, 12 years ago)
  • src/machine/cxx/SVM.cc

    diff --git src/machine/cxx/SVM.cc src/machine/cxx/SVM.cc
    index 142b4e7..70fcff7 100644
    blitz::Array<uint8_t,1> mach::svm_pickle 
    178178  return buffer;
    179179}
    180180
     181static boost::shared_ptr<svm_model> make_model(const char* filename) {
     182  boost::shared_ptr<svm_model> retval(svm_load_model(filename),
     183      std::ptr_fun(svm_model_free));
     184#if LIBSVM_VERSION > 315
     185  if (retval) retval->sv_indices = 0; ///< force initialization: see ticket #109
     186#endif
     187  return retval;
     188}
     189
    181190/**
    182191 * Reverts the pickling process, returns the model
    183192 */
    boost::shared_ptr<svm_model> mach::svm_unpickle 
    193202  binfile.close();
    194203
    195204  //reload the file using the appropriate libsvm loading method
    196   boost::shared_ptr<svm_model> retval(svm_load_model(tmp_filename),
    197       std::ptr_fun(svm_model_free));
     205  boost::shared_ptr<svm_model> retval = make_model(tmp_filename);
     206
     207  if (!retval) {
     208    boost::format s("cannot open model file '%s'");
     209    s % tmp_filename;
     210    throw std::runtime_error(s.str());
     211  }
    198212
    199213  //unlinks the temporary file
    200214  boost::filesystem::remove(tmp_filename);
    void mach::SupportVector::reset() { 
    224238}
    225239
    226240mach::SupportVector::SupportVector(const std::string& model_file):
    227   m_model(svm_load_model(model_file.c_str()), std::ptr_fun(svm_model_free))
     241  m_model(make_model(model_file.c_str()))
    228242{
    229243  if (!m_model) {
    230244    boost::format s("cannot open model file '%s'");