[mlpack] 150/207: can specify fatal or not if file cannot open

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:49 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit d548cffea5c2a9a16027f89417eb6d9c44005848
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date:   Sun Jun 5 13:13:25 2016 +0800

    can specify fatal or not if file cannot open
---
 src/mlpack/core/data/load_csv.hpp  | 37 +++++++++++++++++++++++++++++++------
 src/mlpack/core/data/load_impl.hpp |  2 +-
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/src/mlpack/core/data/load_csv.hpp b/src/mlpack/core/data/load_csv.hpp
index 499c616..34438b7 100644
--- a/src/mlpack/core/data/load_csv.hpp
+++ b/src/mlpack/core/data/load_csv.hpp
@@ -29,20 +29,23 @@ namespace data /** Functions to load and save matrices and models. */ {
 class LoadCSV
 {      
 public:
-  explicit LoadCSV(const std::string &file) :
+  explicit LoadCSV(std::string file, bool fatal = false) :
     extension(Extension(file)),
+    fatalIfOpenFail(fatal),
+    fileName(std::move(file)),
     inFile(file)
   {
-    if(!inFile.is_open())
-    {
-      throw std::runtime_error("LoadCSV can not open file");
-    }
-    inFile.unsetf(std::ios::skipws);
+    CanOpen();
   }
 
   template<typename T>
   void Load(arma::Mat<T> &inout, DatasetInfo &infoSet, bool transpose = true)
   {
+    if(!CanOpen())
+    {
+      return;
+    }
+
     //please refer to the comments of ColSize if you do not familiar
     //with boost::spirit yet
     if(transpose)
@@ -129,6 +132,26 @@ private:
     }
   };
 
+  bool CanOpen()
+  {
+    if(!inFile.is_open())
+    {
+      if(fatalIfOpenFail)
+      {
+        Log::Fatal << "Cannot open file '" << fileName << "'. " << std::endl;
+      }
+      else
+      {
+        Log::Warn << "Cannot open file '" << fileName << "'; load failed."
+                  << std::endl;
+      }
+      return false;
+    }
+    inFile.unsetf(std::ios::skipws);
+
+    return true;
+  }
+
   template<typename T>
   void NonTranposeParse(arma::Mat<T> &inout, DatasetInfo &infoSet)
   {
@@ -308,6 +331,8 @@ private:
   }
 
   std::string extension;
+  bool fatalIfOpenFail;
+  std::string fileName;
   std::ifstream inFile;
 };
 
diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp
index 7b407da..5ff8067 100644
--- a/src/mlpack/core/data/load_impl.hpp
+++ b/src/mlpack/core/data/load_impl.hpp
@@ -373,7 +373,7 @@ bool Load(const std::string& filename,
 
   if (extension == "csv" || extension == "tsv" || extension == "txt")
   {
-    LoadCSV loader(filename);
+    LoadCSV loader(filename, fatal);
     loader.Load(matrix, info, transpose);
   }
   else if (extension == "arff")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list