Fleet  0.0.9
Inference in the LOT
IO.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <thread>
4 #include <mutex>
5 #include <iostream>
6 #include <fstream>
7 #include <string>
8 
9 #include "OrderedLock.h"
10 #include "Strings.h"
11 
12 // Don't warn shadow on this its a nightmare
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wshadow"
15 #include "Dependencies/CL11.hpp"
16 #pragma GCC diagnostic pop
17 
18 // so sick of typing this horseshit
19 #define TAB <<"\t"<<
20 #define NL <<"\n"<<
21 #define ENDL <<std::endl;
22 #define ENDLL <<"\n"<<std::endl;
23 #define CERR std::cerr<<
24 #define COUT std::cout<<
25 #define COUTT std::cout<<"\t"<<
26 #define COUTTT std::cout<<"\t\t"<<
27 
28 const std::string OUTPUT_SEP = "\t"; // standard output separator
29 
30 // These locks manage the standard output in PRINTN and ERRN below
33 
39 template<typename FIRST, typename... ARGS>
40 void OUTPUTN(std::ostream& o, FIRST f, ARGS... args) {
41  o << f;
42  if constexpr(sizeof...(ARGS) > 0) {
43  ((o << OUTPUT_SEP << args), ...);
44  }
45  o << std::endl;
46 }
47 
52 template<typename FIRST, typename... ARGS>
53 void print(FIRST f, ARGS... args) {
54  std::lock_guard guard(output_lock);
55  OUTPUTN(std::cout, f, args...);
56 }
57 
62 template<typename FIRST, typename... ARGS>
63 void ERRN(FIRST f, ARGS... args) {
64  std::lock_guard guard(err_lock);
65  OUTPUTN(std::cerr, f, args...);
66 }
67 
72 template<typename FIRST, typename... ARGS>
73 void DEBUG(FIRST f, ARGS... args) {
74  std::lock_guard guard(output_lock);
75  std::stringstream ss; ss << std::this_thread::get_id();
76  OUTPUTN(std::cout, "DEBUG [thread "+ss.str()+"]", f, args...);
77 }
78 
84 template<typename T>
85 void save(std::string filename, T& hypotheses) {
86 
87  std::ofstream out(filename);
88 
89  for(auto& v: hypotheses) {
90  out << v.serialize() ENDL;
91  }
92 
93  out.close();
94 }
95 
102 template<typename HYP>
103 std::vector<HYP> load(std::string filename) {
104 
105  std::vector<HYP> out;
106 
107  std::ifstream fs(filename);
108 
109  std::string line;
110  while(std::getline(fs, line)) {
111 // auto [col1, col2] = split<2>(line, '\t');
112  out.push_back(HYP::deserialize(line)); // we've stored hypotheses in position 1
113 // CERR line ENDL;
114  }
115  return out;
116 }
117 
124 std::string gulp(std::string filename) {
125  std::string ret;
126  std::ifstream fs(filename);
127 
128  if(fs.fail()) {
129  assert(false && "*** Error file could not be opened.");
130  }
131 
132  std::string line;
133  while(std::getline(fs, line)) {
134  if(line.length() > 0 and line[0] != '#'){
135  ret += line + "\n";
136  }
137  }
138  if(ret.size() > 0)
139  ret.erase(ret.size()-1); // remove last newline
140 
141  return ret;
142 }
143 
155 template<size_t N>
156 std::vector<std::array<std::string,N>> read_csv(const std::string path, bool skipheader, const char delimiter='\t') {
157 
158  std::ifstream file(path);
159  if(file.fail()){ assert(false && "*** File does not exist"); }
160 
161  std::string s;
162  std::vector<std::array<std::string,N>> out;
163  while( std::getline(file,s) ) {
164  if(s.size() == 0 or s[0] == '#') continue; // skip comments and blanks
165 
166  if(skipheader) { // skipping the header
167  skipheader = false;
168  continue;
169  }
170 
171  out.push_back(split<N>(s,delimiter));
172  }
173  return out;
174 }
175 
176 
177 std::vector<std::vector<std::string>> read_csv(const std::string path, bool skipheader, const char delimiter='\t') {
178 
179  std::ifstream file(path);
180  if(file.fail()){ assert(false && "*** File does not exist"); }
181 
182  std::string s;
183  std::vector<std::vector<std::string>> out;
184  while( std::getline(file,s) ) {
185  if(s.size() == 0 or s[0] == '#') continue; // skip comments and blanks
186 
187  if(skipheader) { // skipping the header
188  skipheader = false;
189  continue;
190  }
191 
192  auto q = split(s,delimiter);
193  std::vector<std::string> v;
194  while(! q.empty()) {
195  v.push_back(q.front()); q.pop_front();
196  }
197  out.push_back(v);
198  }
199  return out;
200 }
201 
Definition: OrderedLock.h:16
std::vector< std::array< std::string, N > > read_csv(const std::string path, bool skipheader, const char delimiter='\t')
Load data – divides in columns at the delimiter. NOTE: we are required to say whether we skip a head...
Definition: IO.h:156
OrderedLock output_lock
Definition: IO.h:31
void ERRN(FIRST f, ARGS... args)
Lock err_lock and print to std:cerr.
Definition: IO.h:63
void save(std::string filename, T &hypotheses)
Simple saving of a vector of hypotheses.
Definition: IO.h:85
std::deque< std::string > split(const std::string &s, const char delimiter)
Split is returns a deque of s split up at the character delimiter. It handles these special cases: sp...
Definition: Strings.h:277
std::vector< HYP > load(std::string filename)
Simple loading for a vector of hypotheses.
Definition: IO.h:103
void OUTPUTN(std::ostream &o, FIRST f, ARGS... args)
This assumes o has been appropriately locked.
Definition: IO.h:40
void print(FIRST f, ARGS... args)
Lock output_lcok and print to std:cout.
Definition: IO.h:53
A FIFO mutex (from stackoverflow) https://stackoverflow.com/questions/14792016/creating-a-lock-that-p...
#define ENDL
Definition: IO.h:21
void DEBUG(FIRST f, ARGS... args)
Print to std:ccout with debugging info.
Definition: IO.h:73
std::string gulp(std::string filename)
Read this entire file as a string Skipping comment # lines.
Definition: IO.h:124
const std::string OUTPUT_SEP
Definition: IO.h:28
OrderedLock err_lock
Definition: IO.h:32