Skip to content

Commit 8a5d272

Browse files
Merge pull request #9 from florian-huber/issue_8
add to_dict() method
2 parents 4efbd39 + 4a4fddd commit 8a5d272

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

sparsestack/StackedSparseArray.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,26 @@ def to_array(self, name=None):
387387
return array
388388
array = np.zeros((self.__n_row, self.__n_col),
389389
dtype=self.data.dtype)
390-
array[self.row, self.col] = self.data.reshape(-1)
390+
if len(self.row) > 0 and len(self.col) > 0:
391+
array[self.row, self.col] = self.data.reshape(-1)
391392
return array
392393

393394
def to_coo(self, name):
394395
return coo_matrix((self.data[name], (self.row, self.col)),
395396
shape=(self.__n_row, self.__n_col))
396397

398+
def to_dict(self):
399+
"""Convert StackedSparseArray to dictionary.
400+
"""
401+
return {
402+
"n_row": self.__n_row,
403+
"n_col": self.__n_col,
404+
"row": self.row.tolist(),
405+
"col": self.col.tolist(),
406+
"data": self.data.tolist(),
407+
"dtype": self.data.dtype.descr
408+
}
409+
397410

398411
def update_structed_array_names(input_array: np.ndarray, name: str):
399412
if input_array.dtype.names is None: # no structured array

sparsestack/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.1'
1+
__version__ = '0.3.0'

tests/test_stacked_sparse_array.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ def sparsestack_example(dense_array_sparse):
1919
return matrix
2020

2121

22+
@pytest.fixture
23+
def sparsestack_example_2layers(dense_array_sparse):
24+
matrix = StackedSparseArray(5, 6)
25+
matrix.add_dense_matrix(dense_array_sparse[:5, :6], "scoreA")
26+
matrix.add_dense_matrix(0.1 * dense_array_sparse[:5, :6], "scoreB")
27+
return matrix
28+
29+
2230
def test_sparsestack_empty():
2331
matrix = StackedSparseArray(200, 100)
2432
assert matrix.shape == (200, 100, 0)
2533
assert matrix.score_names == []
34+
assert matrix.to_array() is None
2635

2736

2837
def test_sparsestack_add_dense_array():
@@ -349,3 +358,28 @@ def test_missing_score_name():
349358
with pytest.raises(KeyError) as exception:
350359
_ = matrix.guess_score_name()
351360
assert msg in exception.value.args[0]
361+
362+
363+
def test_to_dict(sparsestack_example_2layers):
364+
sparsestack_dict = sparsestack_example_2layers.to_dict()
365+
expected_keys = {"n_row", "n_col", "row", "col", "data", "dtype"}
366+
assert expected_keys == sparsestack_dict.keys()
367+
368+
expected_row = [0, 1, 1, 2, 3, 3, 4]
369+
assert sparsestack_dict["row"] == expected_row
370+
371+
expected_data_1 = [2, 10, 14, 22, 30, 34, 42]
372+
assert sparsestack_dict["data"][0] == (2, 0.2)
373+
assert [x[0] for x in sparsestack_dict["data"]] == expected_data_1
374+
375+
assert sparsestack_dict["dtype"][0][0] == 'scoreA'
376+
assert sparsestack_dict["dtype"][1][0] == 'scoreB'
377+
378+
379+
def test_change_data_to_dict():
380+
sparsestack = StackedSparseArray(4, 3)
381+
test_data = [(1.0, 3), (0.831479419283098, 1), (0.7963641376124944, 3)]
382+
expected_data = [[1.0, 3], [0.831479419283098, 1], [0.7963641376124944, 3]]
383+
sparsestack.data = np.array(test_data)
384+
sparsestack_dict = sparsestack.to_dict()
385+
assert sparsestack_dict["data"] == expected_data

0 commit comments

Comments
 (0)