aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2017-02-07 22:06:11 +0800
committerAaron LI <aaronly.me@outlook.com>2017-02-07 22:06:11 +0800
commit9fc1477f957fafc9f5b5734d0fe662a30fb30c91 (patch)
treee627b850438924a72a5761249d9e97472072f78c
parentc866739c9464ec6638eac839e7a72ff2b1447377 (diff)
downloadopt-utilities-9fc1477f957fafc9f5b5734d0fe662a30fb30c91.tar.bz2
misc/data_loaders.hpp: Ignore unrecognized/unmatched lines
Also various style fixes
-rw-r--r--misc/data_loaders.hpp165
1 files changed, 92 insertions, 73 deletions
diff --git a/misc/data_loaders.hpp b/misc/data_loaders.hpp
index 2b4f869..26af780 100644
--- a/misc/data_loaders.hpp
+++ b/misc/data_loaders.hpp
@@ -13,33 +13,34 @@
#include <data_sets/default_data_set.hpp>
#include <iostream>
#include <fstream>
+#include <sstream>
#include <cmath>
namespace opt_utilities
{
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_y_ye;
- template <typename Ty,typename Tx>
- std::istream& operator>>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl);
+ template <typename Ty, typename Tx>
+ std::istream& operator>>(std::istream& ifs, dl_x_y_ye<Ty,Tx>& dl);
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_xe_y_ye;
- template <typename Ty,typename Tx>
- std::istream& operator>>(std::istream& ifs,dl_x_xe_y_ye<Ty,Tx>& dl);
+ template <typename Ty, typename Tx>
+ std::istream& operator>>(std::istream& ifs, dl_x_xe_y_ye<Ty,Tx>& dl);
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_xu_xl_y_yu_yl;
- template <typename Ty,typename Tx>
- std::istream& operator>> (std::istream& ifs,dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl);
+ template <typename Ty, typename Tx>
+ std::istream& operator>> (std::istream& ifs, dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl);
/**
loading data from file in format [x] [y] [y error]
*/
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_y_ye
{
private:
@@ -52,23 +53,29 @@ namespace opt_utilities
void load_from(std::istream& ifs)
{
- for(;;)
- {
- Tx x;
- Tx x_err;
- Ty y;
- Ty y_err(0);
-
- ifs>>x>>y>>y_err;
-
- if(!ifs.good())
- {
- break;
- }
- data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err);
- ds.add_data(d);
- }
- //return ifs;
+ Tx x;
+ Tx x_err(0);
+ Ty y;
+ Ty y_err(0);
+ std::string line;
+ while (std::getline(ifs, line))
+ {
+ if (line.empty())
+ continue;
+
+ /* 3-column data: [x] [y] [y_error] */
+ std::istringstream iss(line);
+ if ((iss >> x >> y >> y_err))
+ {
+ data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err);
+ ds.add_data(d);
+ }
+ else
+ {
+ /* skip unmatched line */
+ std::cerr << "SKIP LINE: " << line << std::endl;
+ }
+ }
}
void load_from(const char* name)
@@ -76,15 +83,14 @@ namespace opt_utilities
std::ifstream ifs(name);
load_from(ifs);
}
- //friend std::istream& operator>> <>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl);
};
/**
stream operator
*/
- template <typename Ty,typename Tx>
- std::istream& operator>>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl)
+ template <typename Ty, typename Tx>
+ std::istream& operator>>(std::istream& ifs, dl_x_y_ye<Ty,Tx>& dl)
{
dl.load_from(ifs);
return ifs;
@@ -96,7 +102,7 @@ namespace opt_utilities
[x] [x error] [y] [y error]
*/
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_xe_y_ye
{
private:
@@ -109,22 +115,29 @@ namespace opt_utilities
void load_from(std::istream& ifs)
{
- for(;;)
- {
- Tx x;
- Tx x_err;
- Ty y;
- Ty y_err(0);
-
- ifs>>x>>x_err>>y>>y_err;
-
- if(!ifs.good())
- {
- break;
- }
- data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err);
- ds.add_data(d);
- }
+ Tx x;
+ Tx x_err(0);
+ Ty y;
+ Ty y_err(0);
+ std::string line;
+ while (std::getline(ifs, line))
+ {
+ if (line.empty())
+ continue;
+
+ /* 4-column data: [x] [x_error] [y] [y_error] */
+ std::istringstream iss(line);
+ if ((iss >> x >> x_err >> y >> y_err))
+ {
+ data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err);
+ ds.add_data(d);
+ }
+ else
+ {
+ /* skip unmatched line */
+ std::cerr << "SKIP LINE: " << line << std::endl;
+ }
+ }
}
void load_from(const char* name)
@@ -138,8 +151,8 @@ namespace opt_utilities
/**
stream operator
*/
- template <typename Ty,typename Tx>
- std::istream& operator>>(std::istream& ifs,dl_x_xe_y_ye<Ty,Tx>& dl)
+ template <typename Ty, typename Tx>
+ std::istream& operator>>(std::istream& ifs, dl_x_xe_y_ye<Ty,Tx>& dl)
{
dl.load_from(ifs);
return ifs;
@@ -151,7 +164,7 @@ namespace opt_utilities
[x] [x lower error] [x upper error] [y] [y lower error] [y upper error]
*/
- template <typename Ty,typename Tx>
+ template <typename Ty, typename Tx>
class dl_x_xu_xl_y_yu_yl
{
private:
@@ -163,27 +176,33 @@ namespace opt_utilities
}
void load_from(std::istream& ifs)
{
- for(;;)
- {
- Tx x;
- Tx xl,xu;
- Ty y;
- Ty yl(0),yu(0);
-
- ifs>>x>>xu>>xl>>y>>yu>>yl;
-
- xu=std::abs(xu);
- xl=std::abs(xl);
- yu=std::abs(yu);
- yl=std::abs(yl);
-
- if(!ifs.good())
- {
- break;
- }
- data<Ty,Tx> d(x,y,yl,yu,xl,xu);
- ds.add_data(d);
- }
+ Tx x;
+ Tx xl(0), xu(0);
+ Ty y;
+ Ty yl(0), yu(0);
+ std::string line;
+ while (std::getline(ifs, line))
+ {
+ if (line.empty())
+ continue;
+
+ /* 4-column data: [x] [x_error] [y] [y_error] */
+ std::istringstream iss(line);
+ if ((iss >> x >> xu >> xl >> y >> yu >> yl))
+ {
+ xu = std::abs(xu);
+ xl = std::abs(xl);
+ yu = std::abs(yu);
+ yl = std::abs(yl);
+ data<Ty,Tx> d(x,y,yl,yu,xl,xu);
+ ds.add_data(d);
+ }
+ else
+ {
+ /* skip unmatched line */
+ std::cerr << "SKIP LINE: " << line << std::endl;
+ }
+ }
}
void load_from(const char* name)
@@ -197,8 +216,8 @@ namespace opt_utilities
/**
stream operator
*/
- template <typename Ty,typename Tx>
- std::istream& operator>> (std::istream& ifs,dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl)
+ template <typename Ty, typename Tx>
+ std::istream& operator>> (std::istream& ifs, dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl)
{
dl.load_from(ifs);
return ifs;