Skip to content

Commit

Permalink
Fix a bug accessing matrix's dimnames (#55)
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
jkanche authored Jan 13, 2025
1 parent c78706a commit ad0166a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 0.6.1

- Fix name of the attribute that contains names of dimensions in matrices.
- Update relevant tests and generate new rds files to test matrix behavior.

## Version 0.6.0

- chore: Remove Python 3.8 (EOL).
Expand Down
9 changes: 6 additions & 3 deletions src/rds2py/read_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ def _as_sparse_matrix(robject: dict, **kwargs) -> spmatrix:
)

names = None
if "dimnames" in robject["attributes"]:
names = _dispatcher(robject["attributes"]["dimnames"], **kwargs)
if "Dimnames" in robject["attributes"]:
names = _dispatcher(robject["attributes"]["Dimnames"], **kwargs)
if names is not None and len(names) > 0:
return MatrixWrapper(mat, names)
# Use the wrapper class onyly if names are available
# for atleast one dimension
if not all(x is None for x in names):
return MatrixWrapper(mat, names)

return mat

Expand Down
6 changes: 6 additions & 0 deletions tests/data/generate_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ saveRDS(df, file="lists_df_rownames.rds")
y <- Matrix::rsparsematrix(100, 10, 0.05)
saveRDS(y, file="s4_matrix.rds")

rownames(y) <- paste("row", 1:nrow(y), sep="_")
saveRDS(y, file="matrix_with_row_names.rds")

colnames(y) <- paste("col", 1:ncol(y), sep="_")
saveRDS(y, file="matrix_with_dim_names.rds")

setClass("FOO", slots=c(bar="integer"))
y <- new("FOO", bar=2L)
saveRDS(y, file="s4_class.rds")
Expand Down
Binary file added tests/data/matrix_with_dim_names.rds
Binary file not shown.
Binary file added tests/data/matrix_with_row_names.rds
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ def test_read_s4_matrix_dgc():
assert array is not None
assert isinstance(array, sp.spmatrix)

def test_read_s4_matrix_dgc_with_rownames():
array = read_rds("tests/data/matrix_with_row_names.rds")

assert array is not None
assert isinstance(array, MatrixWrapper)
assert len(array.dimnames[0]) == 100
assert array.dimnames[1] is None


def test_read_s4_matrix_dgc_with_bothnames():
array = read_rds("tests/data/matrix_with_dim_names.rds")

assert array is not None
assert isinstance(array, MatrixWrapper)
assert len(array.dimnames[0]) == 100
assert len(array.dimnames[1]) == 10

def test_read_s4_matrix_dgt():
array = read_rds("tests/data/s4_matrix_dgt.rds")
Expand Down

0 comments on commit ad0166a

Please sign in to comment.