|
| 1 | +/********************************************************************* |
| 2 | +* Author : Himangshu Saikia |
| 3 | + |
| 4 | +* Project : Merge Tree Library |
| 5 | +* |
| 6 | +********************************************************************* |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "FileIO.h" |
| 10 | + |
| 11 | +namespace mtlib { |
| 12 | + |
| 13 | + void FileIO::readRegionsFromCSV(const std::string & filename, std::map<dagkey, Region>& dagNodes) |
| 14 | + { |
| 15 | + dagNodes.clear(); |
| 16 | + std::ifstream f; |
| 17 | + f.open(filename); |
| 18 | + |
| 19 | + std::string line; |
| 20 | + |
| 21 | + bool first = true; |
| 22 | + |
| 23 | + int count = 0; |
| 24 | + |
| 25 | + while (std::getline(f, line)) |
| 26 | + { |
| 27 | + //std::cout << "Reading Line " << count << "\n"; |
| 28 | + |
| 29 | + std::istringstream iss(line); |
| 30 | + |
| 31 | + if (first) { |
| 32 | + first = false; |
| 33 | + |
| 34 | + std::string token; |
| 35 | + |
| 36 | + while (std::getline(iss, token, ',')) { |
| 37 | + count++; |
| 38 | + } |
| 39 | + |
| 40 | + std::cout << "Histogram Size is " << count - 11 << "\n"; |
| 41 | + |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + Region R; |
| 46 | + |
| 47 | + R.hist.init(count - 11); |
| 48 | + |
| 49 | + int sttype, par_id; |
| 50 | + dagkey key; |
| 51 | + |
| 52 | + char c; // comma |
| 53 | + |
| 54 | + iss >> R.timestep >> c >> R.index >> c >> R.vindex >> c >> sttype >> c >> par_id >> c >> key >> c >> R.hist.hdf |
| 55 | + >> c >> R.hist.vol >> c >> R.hist.cm.x >> c >> R.hist.cm.y >> c >> R.hist.cm.z; |
| 56 | + |
| 57 | + for (auto& bin : R.hist.bins) { |
| 58 | + iss >> c >> bin; |
| 59 | + } |
| 60 | + |
| 61 | + dagNodes[key] = R; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + f.close(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + void FileIO::readMergeTreesFromCSV(const std::string & filename, std::vector<std::map<int, int>>& unAugTrees) |
| 70 | + { |
| 71 | + unAugTrees.clear(); |
| 72 | + |
| 73 | + std::ifstream f; |
| 74 | + f.open(filename); |
| 75 | + |
| 76 | + std::string line; |
| 77 | + |
| 78 | + bool first = true; |
| 79 | + |
| 80 | + int count = 0; |
| 81 | + |
| 82 | + while (std::getline(f, line)) |
| 83 | + { |
| 84 | + if (first) { |
| 85 | + first = false; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + count++; |
| 90 | + |
| 91 | + std::istringstream iss(line); |
| 92 | + int t, id, vid, sttype, par_id, seq_id, st_vol; |
| 93 | + float st_hdf; |
| 94 | + Vertex3Df cm; |
| 95 | + char c; // comma |
| 96 | + iss >> t >> c >> id >> c >> vid >> c >> sttype >> c >> par_id >> c >> seq_id >> c >> st_hdf >> c >> st_vol >> c >> cm.x >> c >> cm.y >> c >> cm.z; |
| 97 | + |
| 98 | + if (t >= unAugTrees.size()) { |
| 99 | + unAugTrees.resize(t + 1); |
| 100 | + } |
| 101 | + unAugTrees[t].insert(std::pair<int, int>(id, par_id)); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + std::cout << "[Tracking::readMergeTreesFromCSV] Number of Merge Trees " << unAugTrees.size() << ". Num lines read " << count << "\n"; |
| 106 | + |
| 107 | + f.close(); |
| 108 | + } |
| 109 | + |
| 110 | + void FileIO::writeCostMatrixToDisk(const std::string & filename, const std::vector<std::vector<double>>& costMatrix) |
| 111 | + { |
| 112 | + std::ofstream f; |
| 113 | + f.open(filename); |
| 114 | + |
| 115 | + f << costMatrix.size() << "\n"; |
| 116 | + |
| 117 | + for (size_t i = 0; i < costMatrix.size(); i++) { |
| 118 | + for (size_t j = 0; j < costMatrix[i].size(); j++) { |
| 119 | + f << costMatrix[i][j] << "\t"; |
| 120 | + } |
| 121 | + f << "\n"; |
| 122 | + } |
| 123 | + f.close(); |
| 124 | + } |
| 125 | + |
| 126 | + void FileIO::readCostMatrixFromDisk(const std::string & filename, std::vector<std::vector<double>>& costMatrix) |
| 127 | + { |
| 128 | + std::ifstream f; |
| 129 | + f.open(filename); |
| 130 | + |
| 131 | + size_t n; |
| 132 | + f >> n; |
| 133 | + |
| 134 | + costMatrix.clear(); |
| 135 | + costMatrix.resize(n, std::vector<double>(n)); |
| 136 | + |
| 137 | + for (size_t i = 0; i < n; i++) { |
| 138 | + for (size_t j = 0; j < n; j++) { |
| 139 | + f >> costMatrix[i][j]; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + f.close(); |
| 144 | + } |
| 145 | + |
| 146 | + void FileIO::drawMatches(const std::map<size_t, size_t>& matches, const std::string& prefix1, const std::string& prefix2, std::ofstream & file) |
| 147 | + { |
| 148 | + for (const auto& match : matches) { |
| 149 | + file << prefix1 << match.first << "->" << prefix2 << match.second << "[color=\"#00b300\",style=\"setlinewidth(2)\"];\n"; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + void FileIO::addHeaderToStatisticsFile(std::ofstream & fs, const int hist_buckets) |
| 154 | + { |
| 155 | + fs << "Timestep,Merge_Tree_Id,Volume_Vertex,Subtree_Repr_Type,Merge_Tree_Parent_Id,DAG_Key,Subtree_Height_Difference,Subtree_Volume,Subtree_Center_of_Mass_x,Subtree_Center_of_Mass_y,Subtree_Center_of_Mass_z"; |
| 156 | + for (auto i = 0; i < hist_buckets; i++) { |
| 157 | + fs << ",Hist_" << i; |
| 158 | + } |
| 159 | + fs << "\n"; |
| 160 | + } |
| 161 | + |
| 162 | + void FileIO::addToStatistics(std::ofstream & fs, const int & timestep, const ConciseSMT & smt, const std::vector<Hist>& hists, const double globalRange) |
| 163 | + { |
| 164 | + for (auto i = 0; i < smt.cps.size() - 1; i++) { |
| 165 | + |
| 166 | + int mtParentId = i; |
| 167 | + if (smt.unAugIdxTree.find(i) != smt.unAugIdxTree.end()) { |
| 168 | + mtParentId = smt.unAugIdxTree.find(i)->second; |
| 169 | + } |
| 170 | + else { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + if (mtParentId == smt.unAugIdxTree.size()) { |
| 175 | + mtParentId = -1; // global minimum |
| 176 | + } |
| 177 | + |
| 178 | + auto key = DagNode::makeKey(timestep, i); |
| 179 | + |
| 180 | + fs << timestep << "," |
| 181 | + << i << "," |
| 182 | + << smt.cps[i].idx << "," |
| 183 | + << smt.cps[i].typ << "," |
| 184 | + << mtParentId << "," |
| 185 | + << key << "," |
| 186 | + << (hists[i].maxVal - hists[i].minVal) * globalRange << "," |
| 187 | + << hists[i].vol << "," |
| 188 | + << hists[i].cm.x << "," |
| 189 | + << hists[i].cm.y << "," |
| 190 | + << hists[i].cm.z; |
| 191 | + |
| 192 | + for (const auto& bin : hists[i].bins) { |
| 193 | + fs << "," << bin; |
| 194 | + } |
| 195 | + |
| 196 | + fs << "\n"; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments