Skip to content

Commit

Permalink
Merge pull request #25 from saudzahirr/Fix-unittest
Browse files Browse the repository at this point in the history
Update unittest
  • Loading branch information
saudzahirr authored Aug 31, 2024
2 parents 2138136 + ce6b478 commit d736116
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 36 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- master
- Fix-build
- Fix-ci/cd-pipeline
- Fix-unittest
pull_request:
branches:
- master
Expand Down Expand Up @@ -35,7 +36,7 @@ jobs:
run: |
source $(conda info --base)/etc/profile.d/conda.sh
conda activate vm
pytest -s test/unit-test
pytest -s . > test.log 2>&1
- name: Code Lint
run: |
Expand All @@ -53,5 +54,4 @@ jobs:
path: |
build.log
vonMises.log
test/unit-test.log
test/*.log
test.log
6 changes: 3 additions & 3 deletions .github/workflows/win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- master
- Fix-build
- Fix-ci/cd-pipeline
- Fix-unittest
pull_request:
branches:
- master
Expand All @@ -32,7 +33,7 @@ jobs:
- name: Unittest
run: |
conda activate vm
pytest -s test\unit-test
pytest -s . > test.log 2>&1
- name: Code Lint
run: |
Expand All @@ -49,5 +50,4 @@ jobs:
path: |
build.log
vonMises.log
test\unit-test.log
test\*.log
test.log
2 changes: 2 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def run_command(command, log_file=log_file):
if args.mode == "wheel":
print("Starting wheel build")
run_command(build_cmd)
print("Wheel build completed")

elif args.mode == "install":
print("Starting install")
run_command(install_cmd)
print("Finished installation")

elif args.mode == "develop":
print("Starting development build")
Expand Down
6 changes: 2 additions & 4 deletions src/vonmises/eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import ctypes
import numpy as np
from vonmises.lib import vonmises_lib


Expand All @@ -32,24 +33,21 @@ def solve(self, A):

size = len(A)

# Allocate and flatten the matrix
A_ptr = (ctypes.POINTER(ctypes.c_double) * size)()
for i, row in enumerate(A):
A_ptr[i] = (ctypes.c_double * len(row))(*row)

# Allocate memory for eigenvalues and eigenvectors
eigenvalues = (ctypes.c_double * size)()
eigenvectors = (ctypes.POINTER(ctypes.c_double) * size)()
for i in range(size):
eigenvectors[i] = (ctypes.c_double * size)()

# Call the C++ function
solveEigenValueProblem(A_ptr, size, eigenvalues, eigenvectors)

eigenvalues = list(eigenvalues)
eigenvectors = [list(eigenvectors[i][0:size]) for i in range(size)]

return eigenvalues, eigenvectors
return np.array(eigenvalues), np.array(eigenvectors).T


def eigen(A):
Expand Down
2 changes: 1 addition & 1 deletion src/vonmiseslib/vonMises_API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

void solveEigenValueProblem(double** A, size_t size, double* eigenvalues, double** eigenvectors) {
INFO_OUT("----------------------------------------------");
INFO_OUT("\n----------------------------------------------");
INFO_OUT("CMake Version :: {}", CMake_VERSION);
INFO_OUT("GCC Version :: {}", gcc_VERSION);
INFO_OUT("OMP max threads :: {}", omp_get_max_threads());
Expand Down
25 changes: 13 additions & 12 deletions test/unit-test/test_array_to_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
from scipy.io import loadmat
from vonmises.utils import array_to_mat

test_mat_key = "A"
test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)


class TestUtils(unittest.TestCase):
def setUp(self):
self.test_mat_key = "A"
self.test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)

def test_array_to_mat(self):
test_matfile_path = "MATRIX.mat"
array_to_mat(test_array, test_matfile_path, test_mat_key)
array_to_mat(self.test_array, test_matfile_path, self.test_mat_key)
loaded_data = loadmat(test_matfile_path)
os.remove(test_matfile_path)
self.assertTrue(np.array_equal(loaded_data[test_mat_key], test_array))
self.assertTrue(np.array_equal(loaded_data[self.test_mat_key], self.test_array))


if __name__ == "__main__":
Expand Down
23 changes: 23 additions & 0 deletions test/unit-test/test_compare_eigenvalues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
import numpy as np
from vonmises import eigen

test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)


class TestCompareEigenvalues(unittest.TestCase):
def test_compare_eigenvalues(self):
eigenvalues = eigen.eigenvalues(test_array)
ref_eigenvalues = np.linalg.eigvals(test_array)
self.assertTrue(np.allclose(eigenvalues, ref_eigenvalues, atol=1e-7))


if __name__ == "__main__":
unittest.main()
35 changes: 35 additions & 0 deletions test/unit-test/test_eigen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
import numpy as np
import numpy.testing as npt
from vonmises import eigen

test_matrix = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)


class TestEigenvalues(unittest.TestCase):
def test_eigenvalues_and_eigenvectors(self):
eigenvalues, eigenvectors = eigen.eigen(test_matrix)
self.verify_eigen(test_matrix, eigenvalues, eigenvectors)

def verify_eigen(self, matrix, eigenvalues, eigenvectors):
for i in range(len(eigenvalues)):
v = eigenvectors[:, i]
c = eigenvalues[i]

Av = np.dot(matrix, v)
cv = c * v
residual = np.linalg.norm(Av - cv)

npt.assert_allclose(Av, cv, atol=1e-6)
self.assertAlmostEqual(0, residual, places=8)


if __name__ == "__main__":
unittest.main()
25 changes: 25 additions & 0 deletions test/unit-test/test_eigenvalues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest
import numpy as np
from vonmises import eigen

test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)
test_eigenvalues = np.array(
[14.073477752817423, 10.819060924370755, 8.143435247151785, 5.964026075660039]
)


class TestEigenvalues(unittest.TestCase):
def test_eigenvalues(self):
eigenvalues = eigen.eigenvalues(test_array)
self.assertTrue(np.allclose(eigenvalues, test_eigenvalues, atol=1e-7))


if __name__ == "__main__":
unittest.main()
53 changes: 53 additions & 0 deletions test/unit-test/test_eigenvectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import unittest
import numpy as np
from vonmises import eigen

test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)
test_eigenvectors = np.array(
[
[
-0.3934745132662989,
0.6233611125319993,
-0.6404299749594337,
-0.21551839356810434,
],
[
0.680207701966034,
0.4912946824964888,
0.2267043001245381,
-0.4945221708293822,
],
[
-0.4613009870624494,
0.5009327057718649,
0.7078520091508445,
0.1876522229135619,
],
[
0.4119425796529732,
0.3451331375304617,
-0.19339115962115574,
0.8208448622167394,
],
]
)


class TestEigenvectors(unittest.TestCase):
def test_eigenvectors(self):
eigenvectors = eigen.eigenvectors(test_array)
eigenvectors_abs = np.abs(eigenvectors)
test_eigenvectors_abs = np.abs(test_eigenvectors)

self.assertTrue(np.allclose(eigenvectors_abs, test_eigenvectors_abs, atol=1e-7))


if __name__ == "__main__":
unittest.main()
28 changes: 15 additions & 13 deletions test/unit-test/test_mat_to_array.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import unittest
import numpy as np
from vonmises.utils import mat_to_array

test_matfile_path = "test/test-data/MATRIX.mat"
test_mat_key = "A"
test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)
from pathlib import Path


class TestUtils(unittest.TestCase):
def setUp(self):
self.test_matfile_path = Path("test/test-data/MATRIX.mat")
self.key = "A"
self.test_array = np.array(
[
[10.0, -1.0, 2.0, 0.0],
[-1.0, 11.0, -1.0, 3.0],
[2.0, -1.0, 10.0, -1.0],
[0.0, 3.0, -1.0, 8.0],
]
)

def test_mat_to_array(self):
loaded_array = mat_to_array(test_matfile_path, test_mat_key)
self.assertTrue(np.array_equal(loaded_array, test_array))
loaded_array = mat_to_array(self.test_matfile_path, self.key)
self.assertTrue(np.array_equal(loaded_array, self.test_array))


if __name__ == "__main__":
Expand Down

0 comments on commit d736116

Please sign in to comment.