Skip to content
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

Allow assigning to rownames, colnames and dimnames for sparse matrices #1087

Open
Wainberg opened this issue Jan 28, 2024 · 3 comments
Open

Comments

@Wainberg
Copy link

Currently you need to do r.dimnames(rmatrix)[0] = ... to set the rownames and r.dimnames(rmatrix)[1] = ... to set the colnames.

Would be great to support rmatrix.rownames = ..., rmatrix.colnames = ..., and rmatrix.dimnames = ... for consistency with dense matrices.

@lgautier
Copy link
Member

lgautier commented Feb 11, 2024

The implementation of sparse matrices in R is only available through packages. What R package do you have in mind?

I did play with an extension for rpy2 wrapping the R package Matrix (https://github.com/rpy2/rpy2-Matrix
), but between the complexity requires and the lack of users it stayed as an experiment and it might no longer work with recent version of Matrix.

@Wainberg
Copy link
Author

Yeah it's the sparse matrices from the Matrix package (e.g. dgCMatrix). They're crucial for single-cell analysis, e.g. in the widely used Seurat package.

@lgautier
Copy link
Member

lgautier commented Feb 24, 2024

Class definition in the R package Matrix use the "S4" OOP system (one of the two many ways to do OOP in R).

The base type in R is that this is an S4 object. This is mapped to the base type rpy2.robjects.RS4 in rpy2. In the R package Matrix there is a base S4 class called Matrix, and methods such as dimnames(), colnames() and rownames() work through the notion of generic functions and a multiple dispatch system.

Making the discovery of generics and all signatures associated under the multiple dispatch system fully automagic on the Python side, and map it through dynamic class and method generation on the Python side, is relatively complex. rpy2 has something that can do it (see https://rpy2.github.io/doc/v3.5.x/html/robjects_oop.html#automated-r-in-python-class-definitions) if you want to try, but that not the default behavior. The Python package rpy2-Matrix (https://github.com/rpy2/rpy2-Matrix) is using that dynamic method generation.

However, here it does not seem that colnames and rownames are defined as generic S4 functions by base R or Matrix. You could write child classes in for the rpy2 mapped classes and implement the methods you are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants