Skip to content

Commit

Permalink
v0.9.4 Fix spmatrix.A
Browse files Browse the repository at this point in the history
  • Loading branch information
asistradition committed Jul 2, 2024
1 parent e8f87e0 commit 2cb7753
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 0.9.4

* Replace deprecated scipy `spmatrix.A` calls with `spmatrix.todense()`

### Version 0.9.3

* Directly expose service functions `mkl_get_max_threads`, `mkl_set_num_threads`,
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ If A is sparse, it will return a sparse matrix unless `dense=True` is set.
It will also convert a CSC matrix to a CSR matrix if necessary.

#### Service Functions
Several service functions are available and can be imported from the base `sparse_dot_mkl` package.

`mkl_get_max_threads()` returns the maximum number of threads used by MKL as an integer

`mkl_set_num_threads(n_threads)` will set a maximum number of threads hint for MKL
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages

DISTNAME = 'sparse_dot_mkl'
VERSION = '0.9.3'
VERSION = '0.9.4'
DESCRIPTION = "Intel MKL wrapper for sparse matrix multiplication"
MAINTAINER = 'Chris Jackson'
MAINTAINER_EMAIL = '[email protected]'
Expand Down
2 changes: 1 addition & 1 deletion sparse_dot_mkl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.9.3'
__version__ = '0.9.4'


from sparse_dot_mkl.sparse_dot import (
Expand Down
10 changes: 8 additions & 2 deletions sparse_dot_mkl/_mkl_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ def _validate_dtype():
test_array = _spsparse.random(
5, 5, density=0.5, format="csc", dtype=_np.float32, random_state=50
)
test_comparison = test_array.A
test_comparison = test_array.todense()

# Make sure this is an array and not np.matrix
try:
test_comparison = test_comparison.A
except AttributeError:
pass

csc_ref, precision_flag, _ = _create_mkl_sparse(test_array)

Expand All @@ -84,7 +90,7 @@ def _validate_dtype():
output_type='csr_matrix'
)

if not _np.allclose(test_comparison, final_array.A):
if not _np.allclose(test_comparison, final_array.todense()):
raise ValueError("Match failed after matrix conversion")
_destroy_mkl_handle(csr_ref)
finally:
Expand Down
6 changes: 3 additions & 3 deletions sparse_dot_mkl/tests/test_dense_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestDenseDenseMultiplication(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.MATRIX_1, cls.MATRIX_2 = MATRIX_1.copy().A, MATRIX_2.copy().A
cls.MATRIX_1, cls.MATRIX_2 = MATRIX_1.copy().todense(), MATRIX_2.copy().todense()

def setUp(self):
self.mat1 = self.MATRIX_1.copy()
Expand Down Expand Up @@ -123,8 +123,8 @@ class _ComplexMixin:
@classmethod
def setUpClass(cls):
cls.MATRIX_1, cls.MATRIX_2 = make_matrixes(200, 100, 300, 0.05, dtype=np.cdouble)
cls.MATRIX_1 = cls.MATRIX_1.A
cls.MATRIX_2 = cls.MATRIX_2.A
cls.MATRIX_1 = cls.MATRIX_1.todense()
cls.MATRIX_2 = cls.MATRIX_2.todense()

def test_inherits(self):

Expand Down
16 changes: 8 additions & 8 deletions sparse_dot_mkl/tests/test_mkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def np_almost_equal(a, b, **kwargs):
a = a.toarray()

if isinstance(a, np.matrix):
a = a.A
a = a.todense()

if _spsparse.issparse(b):
b = b.toarray()

if isinstance(b, np.matrix):
b = b.A
b = b.todense()

return npt.assert_array_almost_equal(a, b, **kwargs)

Expand All @@ -73,16 +73,16 @@ def setUp(self):
self.mat1 = MATRIX_1_EMPTY.copy()
self.mat2 = MATRIX_2.copy()

self.mat1_d = np.asarray(MATRIX_1_EMPTY.A, order="C")
self.mat2_d = np.asarray(MATRIX_2.A, order="C")
self.mat1_d = np.asarray(MATRIX_1_EMPTY.todense(), order="C")
self.mat2_d = np.asarray(MATRIX_2.todense(), order="C")

self.mat1_zero = np.zeros((0, 300))

def test_sparse_sparse(self):
mat3 = dot_product_mkl(self.mat1, self.mat2)
mat3_np = np.dot(self.mat1_d, self.mat2_d)

npt.assert_array_almost_equal(mat3_np, mat3.A)
npt.assert_array_almost_equal(mat3_np, mat3.todense())

def test_sparse_dense(self):
mat3 = dot_product_mkl(self.mat1, self.mat2_d)
Expand Down Expand Up @@ -141,8 +141,8 @@ def test_empty_handle(self):
_destroy_mkl_handle(mkl_handle_empty)

def test_3d_matrixes(self):
d1 = self.mat1.A.reshape(200, 300, 1)
d2 = self.mat2.A.reshape(300, 100, 1)
d1 = self.mat1.todense().A.reshape(200, 300, 1)
d2 = self.mat2.todense().A.reshape(300, 100, 1)

with self.assertRaises(ValueError):
dot_product_mkl(d1, d2)
Expand Down Expand Up @@ -199,7 +199,7 @@ def is_sparse_identical_internal(sparse_1, sparse_2):
npt.assert_array_equal(sparse_1.indices, sparse_2.indices)

def is_sparse_identical_A(self, sparse_1, sparse_2):
npt.assert_array_almost_equal(sparse_1.A, sparse_2.A)
npt.assert_array_almost_equal(sparse_1.todense(), sparse_2.todense())

def test_create_export(self):
mat1 = self.csc_constructor(self.mat1).copy()
Expand Down
19 changes: 15 additions & 4 deletions sparse_dot_mkl/tests/test_qr_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestSparseSolver(unittest.TestCase):
def setUpClass(cls):
cls.A = _spsparse.diags((MATRIX_1.data[0:100].copy()), format="csr")
cls.B = MATRIX_1.data[0:100].copy().reshape(-1, 1)
cls.X = np.linalg.lstsq(cls.A.A, cls.B, rcond=None)[0]
cls.X = np.linalg.lstsq(cls.A.todense(), cls.B, rcond=None)[0]

def setUp(self):
self.mat1 = self.A.copy()
Expand All @@ -25,15 +25,26 @@ def test_sparse_solver(self):
npt.assert_array_almost_equal(self.mat3, mat3)

def test_sparse_solver_single(self):
mat3 = sparse_qr_solve_mkl(self.mat1.astype(np.float32), self.mat2.astype(np.float32))
mat3 = sparse_qr_solve_mkl(
self.mat1.astype(np.float32),
self.mat2.astype(np.float32)
)
npt.assert_array_almost_equal(self.mat3, mat3)

def test_sparse_solver_cast_B(self):
mat3 = sparse_qr_solve_mkl(self.mat1, self.mat2.astype(np.float32), cast=True)
mat3 = sparse_qr_solve_mkl(
self.mat1,
self.mat2.astype(np.float32),
cast=True
)
npt.assert_array_almost_equal(self.mat3, mat3)

def test_sparse_solver_cast_A(self):
mat3 = sparse_qr_solve_mkl(self.mat1.astype(np.float32), self.mat2, cast=True)
mat3 = sparse_qr_solve_mkl(
self.mat1.astype(np.float32),
self.mat2,
cast=True
)
npt.assert_array_almost_equal(self.mat3, mat3)

def test_sparse_solver_cast_CSC(self):
Expand Down

0 comments on commit 2cb7753

Please sign in to comment.