-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for saving matrices to files in npz format #154
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e9c735d
Added save_npz and load_npz methods
28c2fed
Added unittest
9a6c38e
flake8 fixes, made test python2.7 compatible
2c8d94a
Added docstrings
c394c0b
exposed methods in __init__, cosmetics in test
7b8d9da
Added docs to /docs/generated
770e433
fixed PR comments
nimroha 34aec2c
added .rst files
nimroha d5c19a7
fixed more PR comments
nimroha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load\_npz | ||
========= | ||
|
||
.. currentmodule:: sparse | ||
|
||
.. autofunction:: load_npz |
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,6 @@ | ||
save\_npz | ||
========= | ||
|
||
.. currentmodule:: sparse | ||
|
||
.. autofunction:: save_npz |
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,104 @@ | ||
import numpy as np | ||
|
||
from .coo.core import COO | ||
|
||
|
||
def save_npz(filename, matrix, compressed=True): | ||
""" Save a sparse matrix to disk in numpy's ``.npz`` format. | ||
Note: This is not binary compatible with scipy's ``save_npz()``. | ||
Will save a file that can only be opend with this package's ``load_npz()``. | ||
|
||
Parameters | ||
---------- | ||
filename : string or file | ||
Either the file name (string) or an open file (file-like object) | ||
where the data will be saved. If file is a string or a Path, the | ||
``.npz`` extension will be appended to the file name if it is not | ||
already there | ||
matrix : COO | ||
The matrix to save to disk | ||
compressed : bool | ||
Whether to save in compressed or uncompressed mode | ||
|
||
Example | ||
-------- | ||
Store sparse matrix to disk, and load it again: | ||
|
||
>>> import os | ||
>>> import sparse | ||
>>> import numpy as np | ||
>>> dense_mat = np.array([[[0., 0.], [0., 0.70677779]], [[0., 0.], [0., 0.86522495]]]) | ||
>>> mat = sparse.COO(dense_mat) | ||
>>> mat | ||
<COO: shape=(2, 2, 2), dtype=float64, nnz=2> | ||
>>> sparse.save_npz('mat.npz', mat) | ||
>>> loaded_mat = sparse.load_npz('mat.npz') | ||
>>> loaded_mat | ||
<COO: shape=(2, 2, 2), dtype=float64, nnz=2> | ||
>>> loaded_mat.todense() | ||
array([[[0. , 0. ], | ||
[0. , 0.70677779]], | ||
<BLANKLINE> | ||
[[0. , 0. ], | ||
[0. , 0.86522495]]]) | ||
>>> os.remove('mat.npz') | ||
|
||
See Also | ||
-------- | ||
load_npz | ||
scipy.sparse.save_npz | ||
scipy.sparse.load_npz | ||
numpy.savez | ||
numpy.load | ||
|
||
""" | ||
|
||
nodes = {'data': matrix.data, | ||
'coords': matrix.coords, | ||
'shape': matrix.shape} | ||
|
||
if compressed: | ||
np.savez_compressed(filename, **nodes) | ||
else: | ||
np.savez(filename, **nodes) | ||
|
||
|
||
def load_npz(filename): | ||
""" Load a sparse matrix in numpy's ``.npz`` format from disk. | ||
Note: This is not binary compatible with scipy's ``save_npz()`` | ||
output. Will only load files saved by this package. | ||
|
||
Parameters | ||
---------- | ||
filename : file-like object, string, or pathlib.Path | ||
The file to read. File-like objects must support the | ||
``seek()`` and ``read()`` methods. | ||
|
||
Returns | ||
------- | ||
COO | ||
The sparse matrix at path ``filename`` | ||
|
||
Example | ||
-------- | ||
See :obj:`save_npz` for usage examples. | ||
|
||
See Also | ||
-------- | ||
save_npz | ||
scipy.sparse.save_npz | ||
scipy.sparse.load_npz | ||
numpy.savez | ||
numpy.load | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: Optional, but would be really nice to have: A see also section with |
||
""" | ||
|
||
with np.load(filename) as fp: | ||
try: | ||
coords = fp['coords'] | ||
data = fp['data'] | ||
shape = tuple(fp['shape']) | ||
except KeyError: | ||
raise RuntimeError('The file {} does not contain a valid sparse matrix'.format(filename)) | ||
|
||
return COO(coords=coords, data=data, shape=shape, sorted=True, has_duplicates=False) |
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,39 @@ | ||
import os | ||
import tempfile | ||
import shutil | ||
import pytest | ||
import numpy as np | ||
|
||
import sparse | ||
|
||
from sparse import save_npz, load_npz | ||
from sparse.utils import assert_eq | ||
|
||
|
||
@pytest.mark.parametrize('compression', [True, False]) | ||
def test_save_load_npz_file(compression): | ||
x = sparse.random((2, 3, 4, 5), density=.25) | ||
y = x.todense() | ||
|
||
dir_name = tempfile.mkdtemp() | ||
filename = os.path.join(dir_name, 'mat.npz') | ||
|
||
save_npz(filename, x, compressed=compression) | ||
z = load_npz(filename) | ||
assert_eq(x, z) | ||
assert_eq(y, z.todense()) | ||
|
||
shutil.rmtree(dir_name) | ||
|
||
|
||
def test_load_wrong_format_exception(): | ||
x = np.array([1, 2, 3]) | ||
|
||
dir_name = tempfile.mkdtemp() | ||
filename = os.path.join(dir_name, 'mat.npz') | ||
|
||
np.savez(filename, x) | ||
with pytest.raises(RuntimeError): | ||
load_npz(filename) | ||
|
||
shutil.rmtree(dir_name) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional, but would be really nice to have: A see also section with
load_npz
,np.savez
andscipy.sparse.save_npz
. Also a note saying that it's binary incompatible. See examples in our code and here: https://numpydoc.readthedocs.io/en/latest/format.html