Skip to content

Commit

Permalink
Merge pull request #1074 from PetholzA/feature/02062023_python_tests_…
Browse files Browse the repository at this point in the history
…algebraic

Unit testing: adds test_algebraic.py and more tests for MatricesGtest.cpp
  • Loading branch information
fabratu committed Jul 10, 2023
2 parents 00c2a14 + 4ccc5d3 commit b7511ad
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
20 changes: 19 additions & 1 deletion networkit/cpp/algebraic/test/MatricesGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,24 @@ class MatricesGTest : public testing::Test {
template <class Matrix>
void testParallelForNonZeroElementsInRowOrder();

template <class Matrix>
void testMatrixToGraph();

// TODO: Test other matrix classes

// TODO: Test mmT multiplication, etc.!
};

template <class Matrix>
void MatricesGTest::testMatrixToGraph() {

Matrix mat = Matrix::adjacencyMatrix(METISGraphReader{}.read("input/power.graph"));
Graph G = MatrixTools::matrixToGraph(mat);

EXPECT_EQ(G.numberOfNodes(), 4941);
EXPECT_EQ(G.numberOfEdges(), 6594);
}

template <class Matrix>
void MatricesGTest::testDimension() {
Matrix mat(10);
Expand Down Expand Up @@ -1200,6 +1213,11 @@ TEST_F(MatricesGTest, testDimension) {
testDimension<DenseMatrix>();
}

TEST_F(MatricesGTest, testMatrixToGraph) {
testMatrixToGraph<DynamicMatrix>();
testMatrixToGraph<CSRMatrix>();
}

TEST_F(MatricesGTest, testNNZInRow) {
testNNZInRow<DynamicMatrix>();
testNNZInRow<CSRMatrix>();
Expand Down Expand Up @@ -1355,4 +1373,4 @@ TEST_F(MatricesGTest, testCSRMatrixSort) {
CSRGeneralMatrix<double> csr(4, 4, triplets);
csr.sort();
}
} /* namespace NetworKit */
} /* namespace NetworKit */
89 changes: 89 additions & 0 deletions networkit/test/test_algebraic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
import unittest
import os
import networkit as nk
import numpy as np
import scipy
import random

class TestAlgebraic(unittest.TestCase):

def testAdjacencyMatrixDenseDirected(self):
G = nk.Graph(2, directed=True)
G.addEdge(0,1)
A = nk.algebraic.adjacencyMatrix(G, "dense")
self.assertEqual(A[0][0], 0.0)
self.assertEqual(A[0][1], 1.0)
self.assertEqual(A[1][0], 0.0)
self.assertEqual(A[1][1], 0.0)
self.assertIsInstance(A, np.ndarray)

G2 = nk.Graph(2, directed=True, weighted=False)
G2.addEdge(0,1)
A2 = nk.algebraic.adjacencyMatrix(G2, "dense")
self.assertEqual(A[0][0], 0)
self.assertEqual(A[0][1], 1)
self.assertEqual(A[1][0], 0)
self.assertEqual(A[1][1], 0)

def testAdjacencyMatrixDenseUndirected(self):
G = nk.Graph(2, directed=False)
G.addEdge(0,1)
A = nk.algebraic.adjacencyMatrix(G, "dense")
self.assertEqual(A[0][0], 0.0)
self.assertEqual(A[0][1], 1.0)
self.assertEqual(A[1][0], 1.0)
self.assertEqual(A[1][1], 0.0)
self.assertIsInstance(A, np.ndarray)

G2 = nk.Graph(2, directed=False, weighted=False)
G2.addEdge(0,1)
A2 = nk.algebraic.adjacencyMatrix(G2, "dense")
self.assertEqual(A[0][0], 0)
self.assertEqual(A[0][1], 1)
self.assertEqual(A[1][0], 1)
self.assertEqual(A[1][1], 0)

def testAdjacencyMatrixSparse(self):
G = nk.Graph(3)
G.addEdge(0,1)
G.addEdge(1,2)
A2 = nk.algebraic.adjacencyMatrix(G, "sparse")
self.assertIsInstance(A2, scipy.sparse.csr_matrix)

def testAdjacencyEigenvector(self):
G = nk.readGraph("input/jazz2_directed.gml",nk.Format.GML)
eigen1 = nk.algebraic.adjacencyEigenvector(G, 1)
self.assertAlmostEqual(eigen1[0], 1.0000000000000004+7.45058059692383e-09j, delta=0.5)

def testLaplacianMatrix(self):
G = nk.readGraph("input/jazz2_directed.gml",nk.Format.GML)
B = nk.algebraic.laplacianMatrix(G)
self.assertIsInstance(B, scipy.sparse.coo_matrix)

def testLaplacianEigenvectorsDirected(self):
G = nk.readGraph("input/jazz2_directed.gml",nk.Format.GML)
eigen = nk.algebraic.laplacianEigenvectors(G)
self.assertAlmostEqual(eigen[0][0], 3.4994785119452635e-34+0j, delta=0.5)
eigenIndex1 = nk.algebraic.laplacianEigenvector(G, 1)
self.assertAlmostEqual(eigenIndex1[0], 8.15657495e-01+0.j, delta=0.5)

def testLaplacianEigenvectorsUndirected(self):
G2 = nk.readGraph("input/jazz2_undirected.gml",nk.Format.GML)
eigen = nk.algebraic.laplacianEigenvectors(G2)
self.assertAlmostEqual(eigen[0][0], 8.071939453165987e-18, delta=0.5)
eigenIndex1 = nk.algebraic.laplacianEigenvector(G2, 1)
self.assertAlmostEqual(eigenIndex1[0], 3.0, delta=0.5)

def testEigenvectors(self):
A = scipy.sparse.csr_matrix(np.array([[-1.3, 2.7, 0.2], [0.8, 4.1, 2.2], [2.1, 4.4, -1.9]]))
eigen = nk.algebraic.eigenvectors(A)
self.assertAlmostEqual(eigen[0][0], 5.89137873002843+0j, delta=0.5)

def testEigenvectorsReverse(self):
A = scipy.sparse.csr_matrix(np.array([[-1.3, 2.7, 0.2], [0.8, 4.1, 2.2], [2.1, 4.4, -1.9]]))
eigen = nk.algebraic.eigenvectors(A, reverse=True)
self.assertAlmostEqual(eigen[0][0], -2.495689365014214+0.517336521966834j, delta=0.5)

if __name__ == "__main__":
unittest.main()

0 comments on commit b7511ad

Please sign in to comment.