Skip to content

Commit

Permalink
Convert tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
mplough-kobold committed Aug 20, 2024
1 parent b0b3018 commit be9af7f
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Run Tests
run: |
pytest --cov-config=.coveragerc --cov-report=xml --cov=pymatsolver -s -v
make coverage
- name: Test Documentation
if: ${{ matrix.os == 'ubuntu-latest' }} and {{ matrix.python-version == '3.8' }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.coverage
coverage.xml
*.pyc
*.so
build/
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ build:
python setup.py build_ext --inplace

coverage:
nosetests --logging-level=INFO --with-coverage --cover-package=pymatsolver --cover-html
open cover/index.html
pytest --cov-config=.coveragerc --cov-report=xml --cov=pymatsolver -s -v

lint:
pylint --output-format=html pymatsolver > pylint.html
Expand All @@ -14,7 +13,7 @@ graphs:
pyreverse -my -A -o pdf -p pymatsolver pymatsolver/**.py pymatsolver/**/**.py

tests:
nosetests --logging-level=INFO
pytest

docs:
cd docs;make html
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ From a clean install on Ubuntu:
./miniconda.sh -b
export PATH=/root/anaconda/bin:/root/miniconda/bin:$PATH
conda update --yes conda
conda install --yes numpy scipy matplotlib cython ipython nose
conda install --yes numpy scipy matplotlib cython ipython pytest coverage
git clone https://github.com/rowanc1/pymatsolver.git
cd pymatsolver
Expand Down
2 changes: 1 addition & 1 deletion pymatsolver/mumps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ clean:
rm -rf *.dSYM

test:
cd ..;nosetests
cd ../..;pytest
32 changes: 13 additions & 19 deletions tests/test_Basic.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import unittest
import numpy as np
import pytest
import scipy.sparse as sp
from pymatsolver import Diagonal

TOL = 1e-12


class TestBasic(unittest.TestCase):
def test_DiagonalSolver():

def test_DiagonalSolver(self):
A = sp.identity(5)*2.0
rhs = np.c_[np.arange(1, 6), np.arange(2, 11, 2)]
X = Diagonal(A) * rhs
x = Diagonal(A) * rhs[:, 0]

A = sp.identity(5)*2.0
rhs = np.c_[np.arange(1, 6), np.arange(2, 11, 2)]
X = Diagonal(A) * rhs
x = Diagonal(A) * rhs[:, 0]
sol = rhs/2.0

sol = rhs/2.0
with pytest.raises(TypeError):
Diagonal(A, check_accuracy=np.array([1, 2, 3]))
with pytest.raises(TypeError):
Diagonal(A, accuracy_tol=0)

with self.assertRaises(TypeError):
Diagonal(A, check_accuracy=np.array([1, 2, 3]))
with self.assertRaises(TypeError):
Diagonal(A, accuracy_tol=0)

self.assertLess(np.linalg.norm(sol-X, np.inf), TOL)
self.assertLess(np.linalg.norm(sol[:, 0]-x, np.inf), TOL)


if __name__ == '__main__':
unittest.main()
assert np.linalg.norm(sol-X, np.inf) < TOL
assert np.linalg.norm(sol[:, 0]-x, np.inf) < TOL
35 changes: 16 additions & 19 deletions tests/test_BicgJacobi.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import unittest
from pymatsolver import BicgJacobi
import numpy as np
import scipy.sparse as sp

TOL = 1e-6


class TestBicgJacobi(unittest.TestCase):
class TestBicgJacobi:

def setUp(self):
@classmethod
def setup_class(cls):

nSize = 100
A = sp.rand(nSize, nSize, 0.05, format='csr', random_state=100)
Expand All @@ -19,9 +19,9 @@ def setUp(self):
sol = np.random.rand(nSize, 4)
rhs = A.dot(sol)

self.A = A
self.rhs = rhs
self.sol = sol
cls.A = A
cls.rhs = rhs
cls.sol = sol

def test(self):
rhs = self.rhs
Expand All @@ -30,7 +30,7 @@ def test(self):
for i in range(3):
err = np.linalg.norm(
self.A*solb[:, i] - rhs[:, i]) / np.linalg.norm(rhs[:, i])
self.assertLess(err, TOL)
assert err < TOL
Ainv.clean()

def test_T(self):
Expand All @@ -42,13 +42,14 @@ def test_T(self):
for i in range(3):
err = np.linalg.norm(
self.A.T*solb[:, i] - rhs[:, i]) / np.linalg.norm(rhs[:, i])
self.assertLess(err, TOL)
assert err < TOL
Ainv.clean()


class TestPardisoComplex(unittest.TestCase):
class TestBicgJacobiComplex:

def setUp(self):
@classmethod
def setup_class(cls):
nSize = 100
A = sp.rand(nSize, nSize, 0.05, format='csr', random_state=100)
A.data = A.data + 1j*np.random.rand(A.nnz)
Expand All @@ -59,9 +60,9 @@ def setUp(self):
sol = np.random.rand(nSize, 5) + 1j*np.random.rand(nSize, 5)
rhs = A.dot(sol)

self.A = A
self.rhs = rhs
self.sol = sol
cls.A = A
cls.rhs = rhs
cls.sol = sol

def test(self):
rhs = self.rhs
Expand All @@ -70,7 +71,7 @@ def test(self):
for i in range(3):
err = np.linalg.norm(
self.A*solb[:, i] - rhs[:, i]) / np.linalg.norm(rhs[:, i])
self.assertLess(err, TOL)
assert err < TOL
Ainv.clean()

def test_T(self):
Expand All @@ -82,9 +83,5 @@ def test_T(self):
for i in range(3):
err = np.linalg.norm(
self.A.T*solb[:, i] - rhs[:, i]) / np.linalg.norm(rhs[:, i])
self.assertLess(err, TOL)
assert err < TOL
Ainv.clean()


if __name__ == '__main__':
unittest.main()
43 changes: 16 additions & 27 deletions tests/test_Mumps.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import unittest
import numpy as np
import scipy.sparse as sp
import pymatsolver

TOL = 1e-11

"""
class TestMumps(unittest.TestCase):
class TestMumps:
if pymatsolver.AvailableSolvers['Mumps']:
def setUp(self):
@classmethod
def setup_class(cls):
n = 5
irn = np.r_[0, 1, 3, 4, 1, 0, 4, 2, 1, 2, 0, 2]
jcn = np.r_[1, 2, 2, 4, 0, 0, 1, 3, 4, 1, 2, 2]
Expand All @@ -23,46 +23,41 @@ def setUp(self):
sol = np.r_[1., 2., 3., 4., 5.]
sol = np.c_[sol, 10*sol, 100*sol]
A = sp.coo_matrix((a, (irn, jcn)), shape=(n, n))
self.A = A
self.rhs = rhs
self.sol = sol
cls.A = A
cls.rhs = rhs
cls.sol = sol
def test_1to5(self):
rhs = self.rhs
sol = self.sol
Ainv = pymatsolver.Mumps(self.A)
for i in range(3):
self.assertLess(
np.linalg.norm(Ainv * rhs[:, i] - sol[:, i]), TOL
)
self.assertLess(np.linalg.norm(Ainv * rhs - sol, np.inf), TOL)
assert np.linalg.norm(Ainv * rhs[:, i] - sol[:, i]) < TOL
assert np.linalg.norm(Ainv * rhs - sol, np.inf) < TOL
def test_1to5_cmplx(self):
rhs = self.rhs.astype(complex)
sol = self.sol.astype(complex)
self.A = self.A.astype(complex)
Ainv = pymatsolver.Mumps(self.A)
for i in range(3):
self.assertLess(
np.linalg.norm(Ainv * rhs[:, i] - sol[:, i]), TOL
)
self.assertLess(np.linalg.norm(Ainv * rhs - sol, np.inf), TOL)
assert np.linalg.norm(Ainv * rhs[:, i] - sol[:, i]) < TOL
assert np.linalg.norm(Ainv * rhs - sol, np.inf) < TOL
def test_1to5_T(self):
rhs = self.rhs
sol = self.sol
Ainv = pymatsolver.Mumps(self.A)
AinvT = Ainv.T
for i in range(3):
self.assertLess(
np.linalg.norm(AinvT.T * rhs[:, i] - sol[:, i]), TOL
)
self.assertLess(np.linalg.norm(AinvT.T * rhs - sol, np.inf), TOL)
assert np.linalg.norm(AinvT.T * rhs[:, i] - sol[:, i]) < TOL
assert np.linalg.norm(AinvT.T * rhs - sol, np.inf) < TOL
# def test_singular(self):
# A = sp.identity(5).tocsr()
# A[-1, -1] = 0
# self.assertRaises(Exception, pymatsolver.Mumps, A)
# with pytest.raises(Exception):
# pymatsolver.Mumps(A)
def test_multiFactorsInMem(self):
n = 100
Expand All @@ -72,15 +67,9 @@ def test_multiFactorsInMem(self):
solvers = [pymatsolver.Mumps(A) for _ in range(20)]
for Ainv in solvers:
self.assertLess(
np.linalg.norm(Ainv * rhs - x)/np.linalg.norm(rhs), TOL)
assert np.linalg.norm(Ainv * rhs - x)/np.linalg.norm(rhs) < TOL
Ainv.clean()
for Ainv in solvers:
self.assertLess(
np.linalg.norm(Ainv * rhs - x)/np.linalg.norm(rhs), TOL
)
if __name__ == '__main__':
unittest.main()
assert np.linalg.norm(Ainv * rhs - x)/np.linalg.norm(rhs) < TOL
"""
Loading

0 comments on commit be9af7f

Please sign in to comment.