Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some tests for sparse module, improve docs
- Loading branch information
Mark Levy
committed
Nov 11, 2013
1 parent
236973d
commit e91c086
Showing
3 changed files
with
144 additions
and
18 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
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,74 @@ | ||
import tempfile | ||
import os | ||
from sklearn.utils.testing import assert_equal | ||
from sklearn.utils.testing import assert_array_equal | ||
|
||
from mrec.testing import get_random_coo_matrix | ||
|
||
from mrec.sparse import loadtxt | ||
from mrec.sparse import savez | ||
from mrec.sparse import loadz | ||
from mrec.sparse import fast_sparse_matrix | ||
|
||
def test_loadtxt(): | ||
X = get_random_coo_matrix() | ||
f,path = tempfile.mkstemp(suffix='.npz') | ||
with open(path,'w') as f: | ||
for i,j,v in zip(X.row,X.col,X.data): | ||
print >>f,'{0}\t{1}\t{2}'.format(i+1,j+1,v) | ||
Y = loadtxt(path) | ||
os.remove(path) | ||
assert_array_equal(X.toarray(),Y.toarray()) | ||
|
||
def test_savez_loadz(): | ||
m = get_random_coo_matrix() | ||
f,path = tempfile.mkstemp(suffix='.npz') | ||
savez(m,path) | ||
n = loadz(path) | ||
os.remove(path) | ||
assert_array_equal(n.toarray(),m.toarray()) | ||
|
||
def test_init_fast_sparse_matrix(): | ||
X = get_random_coo_matrix() | ||
Y = X.tocsr() | ||
Z = X.tocsc() | ||
for M in [X,Y,Z]: | ||
m = fast_sparse_matrix(M) | ||
assert_array_equal(m.X.toarray(),M.toarray()) | ||
assert_equal(m.shape,M.shape) | ||
|
||
def test_fast_get_col(): | ||
X = get_random_coo_matrix().tocsc() | ||
m = fast_sparse_matrix(X) | ||
rows,cols = X.shape | ||
for j in xrange(cols): | ||
assert_array_equal(m.fast_get_col(j).toarray(),X[:,j].toarray()) | ||
|
||
def test_fast_update_col(): | ||
X = get_random_coo_matrix().tocsc() | ||
m = fast_sparse_matrix(X) | ||
rows,cols = X.shape | ||
for j in xrange(cols): | ||
col = m.fast_get_col(j) | ||
new_vals = [] | ||
for i in X[:,j].indices: | ||
new_vals.append(X[i,j]+1) | ||
m.fast_update_col(j,new_vals) | ||
expected = X[:,j].toarray() | ||
for i in xrange(expected.shape[0]): | ||
if expected[i] > 0: | ||
expected[i] += 1 | ||
assert_array_equal(m.fast_get_col(j).toarray(),expected) | ||
|
||
def test_save_load(): | ||
"""Save to file as arrays in numpy binary format.""" | ||
X = get_random_coo_matrix() | ||
m = fast_sparse_matrix(X) | ||
f,path = tempfile.mkstemp(suffix='.npz') | ||
m.save(path) | ||
n = fast_sparse_matrix.load(path) | ||
os.remove(path) | ||
assert_equal(m.shape,n.shape) | ||
assert_array_equal(m.X.toarray(),n.X.toarray()) | ||
assert_array_equal(m.col_view.toarray(),n.col_view.toarray()) | ||
|