From c6b85be84416e6fc1e3b5a11feb22b85e7b8c30f Mon Sep 17 00:00:00 2001 From: kstiegli Date: Mon, 19 Jun 2017 18:39:39 +0200 Subject: [PATCH 1/2] Load adjacency matrix from file. Fix issue #10. --- graphtool/Graph.cpp | 41 ++++++++++++++++++++++++++++-- graphtool/Graph.h | 10 ++++++-- test/main.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/graphtool/Graph.cpp b/graphtool/Graph.cpp index d5275dc..2482c39 100644 --- a/graphtool/Graph.cpp +++ b/graphtool/Graph.cpp @@ -35,7 +35,7 @@ Graph::Graph(vector> adjacencyMatrix, vector node_names) { if (node_names.size() == getNumberOfNodes()) { this->nodes = vector(); - for(string node : node_names) { + for (string node : node_names) { nodes.push_back(node); } } else { @@ -818,4 +818,41 @@ const string Graph::getAdjacencyMatrixString() const { } return ss.str(); -} \ No newline at end of file +} + +Graph *Graph::loadAdjacencyFile(string file) { + string line; + ifstream myfile(file); + + vector> result; + if (myfile.is_open()) { + while (getline(myfile, line)) { + + vector cols; + boost::split(cols, line, boost::is_any_of(",")); + result.push_back(cols); + } + myfile.close(); + + + // check if the first column is a name + bool isFirstColName = result.at(0).size() != result.size(); + + //Load into vector + vector> adjacencyMatrix = getZeroizedMatrix((int) result.size(), (int) result.size()); + for (unsigned long rIndex = 0; rIndex < result.size(); rIndex++) { + // add values to matrix + for (unsigned long cIndex = 0; cIndex < result.size(); cIndex++) { + unsigned long tmpCIndex = isFirstColName ? cIndex + 1 : cIndex; + try { + adjacencyMatrix.at(rIndex).at(cIndex) = stoi(result.at(rIndex)[tmpCIndex]); + } catch (const invalid_argument &e) { + throw invalid_argument("matrix entries must be and contain at least one integer."); + } + } + } + return new Graph(adjacencyMatrix); + } else { + return nullptr; + } +} diff --git a/graphtool/Graph.h b/graphtool/Graph.h index 3d41a21..7717d3e 100644 --- a/graphtool/Graph.h +++ b/graphtool/Graph.h @@ -148,7 +148,7 @@ class Graph { * * @return a zeroized matrix */ - vector> getZeroizedMatrix(int rows, int cols); + static vector> getZeroizedMatrix(int rows, int cols); /** * Checks whether matrix A (m x n) and B (m x n) are identical. @@ -171,9 +171,9 @@ class Graph { const vector> &getAdjacencyMatrix() const; - public: Graph(vector> adjacencyMatrix); + Graph(vector> adjacencyMatrix, vector node_names); /** @@ -342,4 +342,10 @@ class Graph { */ string exportDot(vector path); + /** + * Load the Graph from a file within an adjacency matrix. + * @param file Path to the file + * @return New Graph object + */ + static Graph *loadAdjacencyFile(string file); }; diff --git a/test/main.cpp b/test/main.cpp index 5a4535b..de9f9e8 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,6 +1,7 @@ #include "gtest/gtest.h" #include #include +#include #include "Graph.h" TEST(graph_check, test_directed) { @@ -427,6 +428,66 @@ TEST(graph_check, test_adjazenzmatrix_string) { std::vector strs; boost::split(strs, mat, boost::is_any_of("\n")); + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); +} +TEST(graph_check, test_adjazenzmatrix_load_file) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "1,0,1\n"; + myfile << "1,1,1\n"; + myfile << "0,0,1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); +} +TEST(graph_check, test_adjazenzmatrix_load_file_space) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "1 ,0,1\n"; + myfile << "1,1 ,1\n"; + myfile << "0,0, 1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); +} +TEST(graph_check, test_adjazenzmatrix_load_file_graph_name) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "Demo,1,0,1\n"; + myfile << "Test,1,1,1\n"; + myfile << "42,0,0,1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + ASSERT_EQ(strs[0], "1,0,1"); ASSERT_EQ(strs[1], "1,1,1"); ASSERT_EQ(strs[2], "0,0,1"); From 55e19e2ca5de8d6f9b1e21fae47d88a37fe7ce69 Mon Sep 17 00:00:00 2001 From: Kevin Stieglitz Date: Tue, 27 Jun 2017 12:04:20 +0200 Subject: [PATCH 2/2] Add "load csv" to gui. --- gui/main.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gui/main.cpp b/gui/main.cpp index 9aaed0d..134500d 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -105,7 +105,13 @@ int main(int argc, char **argv) { */ // Get input from csv file - // TODO @ForrestFalcon issue #10, vgl. Implementierung inputMatlab + vector inputCSV = getValues("-i", allArgs); + for (int i = 0; i < inputCSV.size(); i++) { + Graph *g = Graph::loadAdjacencyFile(inputCSV[i]); + printv("Graph %d (CSV-Datei) \"%s\" hinzugefügt.", graphs.size(), inputCSV[i].c_str()); + graphs.push_back(g); + } + printv("%d CSV-Dateien gefunden.", inputCSV.size()); // Get input from matlab string vector inputMatlab = getValues("-iml", allArgs);