FileIO.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include <algorithm>
  5. #include "FileIO.h"
  6. #include "Logger.h"
  7. #include "dirent.h"
  8. namespace util
  9. {
  10. void FileIO::ReadCSV(Vector3d& values,
  11. const std::string& file_name,
  12. char delimiter)
  13. {
  14. Logger::LogInfo("Reading CSV file");
  15. std::ifstream in(file_name, std::ifstream::in);
  16. std::string line;
  17. // Read lines while the reader is in good condition
  18. while (in.good() && !in.eof())
  19. {
  20. getline(in, line);
  21. // Ignore empty lines
  22. if (line.size() == 0) continue;
  23. // Get frame index
  24. size_t dIndex = line.find(delimiter);
  25. std::string part = line.substr(0, dIndex);
  26. size_t frameIndex = std::stoul(part.c_str());
  27. // Extract point values
  28. std::vector<double> pointValues;
  29. while (dIndex != std::string::npos)
  30. {
  31. line = line.substr(dIndex + 1);
  32. dIndex = line.find(delimiter);
  33. part = line.substr(0, dIndex);
  34. if (part.size() > 0)
  35. {
  36. try
  37. {
  38. pointValues.push_back(std::stof(part.c_str()));
  39. }
  40. catch (const std::exception& e)
  41. {
  42. /* EMPTY */
  43. // Possible cause: "\n"
  44. }
  45. }
  46. }
  47. // Add point data to detection data
  48. while (frameIndex >= values.size())
  49. {
  50. values.push_back(std::vector<std::vector<double>>());
  51. }
  52. values[frameIndex].push_back(pointValues);
  53. }
  54. in.close();
  55. Logger::LogDebug("frame count " + std::to_string(values.size()));
  56. }
  57. void FileIO::ReadCSV(Vector2d& values,
  58. const std::string& file_name,
  59. char delimiter)
  60. {
  61. Logger::LogInfo("Reading CSV file");
  62. std::ifstream in(file_name, std::ifstream::in);
  63. std::string line;
  64. // Read lines while the reader is in good condition and the
  65. // end of file is not reached
  66. while (in.good() && !in.eof())
  67. {
  68. getline(in, line);
  69. // Ignore empty lines
  70. if (line.size() == 0) continue;
  71. // Extract point values
  72. size_t dIndex;
  73. std::string part;
  74. std::vector<double> pointValues;
  75. do
  76. {
  77. dIndex = line.find(delimiter);
  78. part = line.substr(0, dIndex);
  79. if (part.size() > 0)
  80. {
  81. pointValues.push_back(std::stof(part.c_str()));
  82. }
  83. line = line.substr(dIndex + 1);
  84. }
  85. while (dIndex != std::string::npos);
  86. // Add point data to detection data
  87. values.push_back(pointValues);
  88. }
  89. in.close();
  90. Logger::LogDebug("line count " + std::to_string(values.size()));
  91. }
  92. void FileIO::ListFiles(const std::string& folder,
  93. std::vector<std::string>& file_names,
  94. bool sort)
  95. {
  96. Logger::LogInfo("Listing files in folder");
  97. DIR* dir;
  98. struct dirent *ent;
  99. if ((dir = opendir(folder.c_str())) != NULL)
  100. {
  101. int offset = 2;
  102. while ((ent = readdir(dir)) != NULL)
  103. {
  104. if (offset <= 0)
  105. {
  106. file_names.push_back(ent->d_name);
  107. }
  108. offset--;
  109. }
  110. closedir(dir);
  111. if (sort)
  112. {
  113. std::sort(file_names.begin(), file_names.end());
  114. }
  115. Logger::LogDebug("file count " + std::to_string(file_names.size()));
  116. }
  117. else
  118. {
  119. Logger::LogError("Could not open folder");
  120. }
  121. }
  122. void FileIO::WriteCSVMatlab(DirectedGraph& graph,
  123. const std::string& file_name)
  124. {
  125. char delimiter = ',';
  126. std::ofstream out(file_name, std::ofstream::out);
  127. // Iterate all outgoing edges of every vertex
  128. EdgeWeightMap weights = boost::get(boost::edge_weight, graph);
  129. VertexIndexMap indices = boost::get(boost::vertex_index, graph);
  130. boost::graph_traits<DirectedGraph>::vertex_iterator vi, vi_end;
  131. boost::graph_traits<DirectedGraph>::out_edge_iterator oei, oei_end;
  132. for (boost::tie(vi, vi_end) = boost::vertices(graph); vi != vi_end; ++vi)
  133. {
  134. for (boost::tie(oei, oei_end) = boost::out_edges(*vi, graph);
  135. oei != oei_end; ++oei)
  136. {
  137. unsigned long si = indices[boost::source(*oei, graph)];
  138. unsigned long ti = indices[boost::target(*oei, graph)];
  139. // Write the edge to file
  140. out << (si + 1) << delimiter
  141. << (ti + 1) << delimiter
  142. << weights[*oei] << std::endl;
  143. }
  144. }
  145. out.close();
  146. }
  147. void FileIO::WriteCSVMatlab(MultiPredecessorMap& map,
  148. Vertex& origin,
  149. const std::string& file_name)
  150. {
  151. char delimiter = ',';
  152. std::ofstream out("/home/wrede/Dokumente/paths.csv", std::ofstream::out);
  153. for (Vertex first : map[origin])
  154. {
  155. out << (origin + 1) << delimiter << (first + 1);
  156. for (Vertex u = first, v = (*map[u].begin());
  157. u != v; u = v, v = (*map[v].begin()))
  158. {
  159. out << delimiter << (v + 1);
  160. }
  161. out << std::endl;
  162. }
  163. out.close();
  164. }
  165. void FileIO::ReadCSV(ValueMapVector& values,
  166. const std::string& file_name,
  167. char delimiter)
  168. {
  169. // Read the file
  170. std::ifstream in(file_name, std::ifstream::in);
  171. std::string line;
  172. // Get the first line that is not empty
  173. while (in.good() && !in.eof())
  174. {
  175. getline(in, line);
  176. if (line.size() > 0) break;
  177. }
  178. in.close();
  179. ReadCSV(values, line, file_name, delimiter);
  180. }
  181. void FileIO::ReadCSV(ValueMapVector& values, const std::string& header,
  182. const std::string& file_name, char delimiter)
  183. {
  184. Logger::LogInfo("Reading CSV file");
  185. // Read the file
  186. std::ifstream in(file_name, std::ifstream::in);
  187. std::string line, part;
  188. size_t d_index;
  189. // Split the first line into separate keys
  190. std::vector<std::string> key_vector;
  191. line = header;
  192. do
  193. {
  194. d_index = line.find(delimiter);
  195. part = line.substr(0, d_index);
  196. key_vector.push_back(part);
  197. line = line.substr(d_index + 1);
  198. }
  199. while (d_index != std::string::npos);
  200. util::Logger::LogDebug("parsed keys:");
  201. for (std::string str : key_vector)
  202. {
  203. util::Logger::LogDebug(str);
  204. }
  205. // Read lines while the reader is in good condition and the
  206. // end of file is not reached
  207. while (in.good() && !in.eof())
  208. {
  209. getline(in, line);
  210. // Ignore empty lines
  211. if (line.size() == 0) continue;
  212. // Extract detection values
  213. size_t key_index = 0;
  214. ValueMap detection_values;
  215. do
  216. {
  217. d_index = line.find(delimiter);
  218. part = line.substr(0, d_index);
  219. // Try to parse the value
  220. double value;
  221. try
  222. {
  223. value = std::stof(part.c_str());
  224. }
  225. catch (std::exception& e)
  226. {
  227. util::Logger::LogError(e.what());
  228. value = 0.0;
  229. }
  230. // Store the value
  231. detection_values[key_vector[key_index]] = value;
  232. ++key_index;
  233. line = line.substr(d_index + 1);
  234. }
  235. while (d_index != std::string::npos &&
  236. key_index < key_vector.size());
  237. // Add point data to detection data
  238. values.push_back(detection_values);
  239. }
  240. util::Logger::LogDebug("parsed values in line 1:");
  241. for (std::string str : key_vector)
  242. {
  243. util::Logger::LogDebug(str + "=" + std::to_string(values[0][str]));
  244. }
  245. in.close();
  246. }
  247. }