-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sparse matrix tests and from disk method
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .aequilibrae_matrix import AequilibraeMatrix, matrix_export_types | ||
from .aequilibrae_data import AequilibraeData, data_export_types | ||
from .sparse_matrix import Sparse, COO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from tempfile import gettempdir | ||
from aequilibrae.matrix import COO | ||
from unittest import TestCase | ||
from uuid import uuid4 | ||
import scipy.sparse | ||
import numpy as np | ||
import pathlib | ||
|
||
|
||
class TestSparseMatrix(TestCase): | ||
def setUp(self) -> None: | ||
self.data = np.full((100, 100), 5.0) | ||
self.dir = pathlib.Path(gettempdir()) / uuid4().hex | ||
self.dir.mkdir() | ||
|
||
def tearDown(self) -> None: | ||
pass | ||
|
||
def test_round_trip(self): | ||
p = self.dir / "test.omx" | ||
|
||
coo = COO.from_matrix( | ||
self.data, | ||
) | ||
coo.to_disk(p, "m1") | ||
coo.to_disk(p, "m2") | ||
|
||
sp = coo.to_scipy() | ||
|
||
coo1 = COO.from_disk(p) | ||
coo2 = COO.from_disk(p, aeq=True) | ||
|
||
for m in ["m1", "m2"]: | ||
self.assertIsInstance(coo1[m], scipy.sparse.csr_matrix) | ||
self.assertIsInstance(coo2[m], COO) | ||
|
||
np.testing.assert_allclose(sp.A, coo1[m].A) | ||
np.testing.assert_allclose(sp.A, coo2[m].to_scipy().A) |