FileIO.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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& source, Vertex& sink,
  149. const std::string& file_name)
  150. {
  151. char delimiter = ',';
  152. std::ofstream out(file_name, std::ofstream::out);
  153. for (Vertex first : map[sink])
  154. {
  155. out << (sink + 1) << delimiter << (first + 1);
  156. for (Vertex u = first, v = (*map[u].begin()); u != v; u = v, v = (*map[v].begin()))
  157. {
  158. out << delimiter << (v + 1);
  159. if (v == source)
  160. {
  161. break;
  162. }
  163. }
  164. out << std::endl;
  165. }
  166. out.close();
  167. }
  168. void FileIO::ReadCSV(ValueMapVector& values,
  169. const std::string& file_name,
  170. char delimiter)
  171. {
  172. // Read the file
  173. std::ifstream in(file_name, std::ifstream::in);
  174. // Only proceed if the file could be opened
  175. if (!in.is_open())
  176. {
  177. throw "Unable to open file: " + file_name;
  178. }
  179. std::string line;
  180. // Get the first line that is not empty
  181. while (in.good() && !in.eof())
  182. {
  183. getline(in, line);
  184. if (line.size() > 0) break;
  185. }
  186. in.close();
  187. ReadCSV(values, line, file_name, delimiter);
  188. }
  189. void FileIO::ReadCSV(ValueMapVector& values, const std::string& header,
  190. const std::string& file_name, char delimiter)
  191. {
  192. Logger::LogInfo("Reading CSV file");
  193. // Read the file
  194. std::ifstream in(file_name, std::ifstream::in);
  195. // Only proceed if the file could be opened
  196. if (!in.is_open())
  197. {
  198. throw "Unable to open file: " + file_name;
  199. }
  200. std::string line, part;
  201. size_t d_index;
  202. // Split the header into separate keys
  203. std::vector<std::string> key_vector;
  204. line = header;
  205. do
  206. {
  207. d_index = line.find(delimiter);
  208. part = line.substr(0, d_index);
  209. key_vector.push_back(part);
  210. line = line.substr(d_index + 1);
  211. }
  212. while (d_index != std::string::npos);
  213. util::Logger::LogDebug("parsed keys:");
  214. for (std::string str : key_vector)
  215. {
  216. util::Logger::LogDebug(str);
  217. }
  218. // Read lines while the reader is in good condition and the
  219. // end of file is not reached
  220. while (in.good() && !in.eof())
  221. {
  222. getline(in, line);
  223. // Ignore empty lines
  224. if (line.size() == 0) continue;
  225. // Extract detection values
  226. size_t key_index = 0;
  227. ValueMap detection_values;
  228. do
  229. {
  230. d_index = line.find(delimiter);
  231. part = line.substr(0, d_index);
  232. // Try to parse the value
  233. double value;
  234. try
  235. {
  236. value = std::stof(part.c_str());
  237. }
  238. catch (std::exception& e)
  239. {
  240. util::Logger::LogError(e.what());
  241. value = 0.0;
  242. }
  243. // Store the value
  244. detection_values[key_vector[key_index]] = value;
  245. ++key_index;
  246. line = line.substr(d_index + 1);
  247. }
  248. while (d_index != std::string::npos &&
  249. key_index < key_vector.size());
  250. // Add point data to detection data
  251. values.push_back(detection_values);
  252. }
  253. util::Logger::LogDebug("parsed values in line 2:");
  254. for (std::string str : key_vector)
  255. {
  256. util::Logger::LogDebug(str + "=" + std::to_string(values[1][str]));
  257. }
  258. in.close();
  259. }
  260. void FileIO::WriteCSV(std::vector<core::TrackletPtr>& tracks, const std::string& file_name,
  261. char delimiter)
  262. {
  263. // Get the frame range
  264. size_t first_frame = tracks[0]->GetFirstFrameIndex();
  265. size_t last_frame = tracks[0]->GetLastFrameIndex();
  266. for (auto track : tracks)
  267. {
  268. if (track->GetFirstFrameIndex() < first_frame)
  269. first_frame = track->GetFirstFrameIndex();
  270. if (track->GetLastFrameIndex() > last_frame)
  271. last_frame = track->GetLastFrameIndex();
  272. }
  273. std::ofstream out(file_name, std::ios::out);
  274. if (!out.is_open())
  275. {
  276. util::Logger::LogError("Unable to open the the file: " + file_name);
  277. return;
  278. }
  279. //TODO
  280. out.close();
  281. }
  282. }