Skip to content

Commit

Permalink
Fix accessing dimnames on matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Jan 13, 2025
1 parent c78706a commit cfb9eac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rds2py/read_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ 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)

Expand Down
2 changes: 2 additions & 0 deletions tests/data/generate_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ 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="_")

setClass("FOO", slots=c(bar="integer"))
y <- new("FOO", bar=2L)
saveRDS(y, file="s4_class.rds")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ 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


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[0]) = 10

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

0 comments on commit cfb9eac

Please sign in to comment.