Skip to content

Commit ad0166a

Browse files
authored
Fix a bug accessing matrix's dimnames (#55)
Update tests
1 parent c78706a commit ad0166a

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.6.1
4+
5+
- Fix name of the attribute that contains names of dimensions in matrices.
6+
- Update relevant tests and generate new rds files to test matrix behavior.
7+
38
## Version 0.6.0
49

510
- chore: Remove Python 3.8 (EOL).

src/rds2py/read_matrix.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ def _as_sparse_matrix(robject: dict, **kwargs) -> spmatrix:
9393
)
9494

9595
names = None
96-
if "dimnames" in robject["attributes"]:
97-
names = _dispatcher(robject["attributes"]["dimnames"], **kwargs)
96+
if "Dimnames" in robject["attributes"]:
97+
names = _dispatcher(robject["attributes"]["Dimnames"], **kwargs)
9898
if names is not None and len(names) > 0:
99-
return MatrixWrapper(mat, names)
99+
# Use the wrapper class onyly if names are available
100+
# for atleast one dimension
101+
if not all(x is None for x in names):
102+
return MatrixWrapper(mat, names)
100103

101104
return mat
102105

tests/data/generate_files.R

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ saveRDS(df, file="lists_df_rownames.rds")
9797
y <- Matrix::rsparsematrix(100, 10, 0.05)
9898
saveRDS(y, file="s4_matrix.rds")
9999

100+
rownames(y) <- paste("row", 1:nrow(y), sep="_")
101+
saveRDS(y, file="matrix_with_row_names.rds")
102+
103+
colnames(y) <- paste("col", 1:ncol(y), sep="_")
104+
saveRDS(y, file="matrix_with_dim_names.rds")
105+
100106
setClass("FOO", slots=c(bar="integer"))
101107
y <- new("FOO", bar=2L)
102108
saveRDS(y, file="s4_class.rds")

tests/data/matrix_with_dim_names.rds

812 Bytes
Binary file not shown.

tests/data/matrix_with_row_names.rds

781 Bytes
Binary file not shown.

tests/test_matrices.py

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ def test_read_s4_matrix_dgc():
1717
assert array is not None
1818
assert isinstance(array, sp.spmatrix)
1919

20+
def test_read_s4_matrix_dgc_with_rownames():
21+
array = read_rds("tests/data/matrix_with_row_names.rds")
22+
23+
assert array is not None
24+
assert isinstance(array, MatrixWrapper)
25+
assert len(array.dimnames[0]) == 100
26+
assert array.dimnames[1] is None
27+
28+
29+
def test_read_s4_matrix_dgc_with_bothnames():
30+
array = read_rds("tests/data/matrix_with_dim_names.rds")
31+
32+
assert array is not None
33+
assert isinstance(array, MatrixWrapper)
34+
assert len(array.dimnames[0]) == 100
35+
assert len(array.dimnames[1]) == 10
2036

2137
def test_read_s4_matrix_dgt():
2238
array = read_rds("tests/data/s4_matrix_dgt.rds")

0 commit comments

Comments
 (0)