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

Dimension names lost in dgCMatrix %*% IterableMatrix multiply. #43

Closed
brgew opened this issue Sep 1, 2023 · 2 comments
Closed

Dimension names lost in dgCMatrix %*% IterableMatrix multiply. #43

brgew opened this issue Sep 1, 2023 · 2 comments

Comments

@brgew
Copy link
Contributor

brgew commented Sep 1, 2023

Hi,

I suspect that the matrix multiply for a dgCMatrix %*% IterableMatrix does not include the names for at least one of the dimensions. Does that make sense? Here is what I see

The R code looks like

  mapping <- my.dMcast(groupings2, form)
  colnames(mapping) <- substring(colnames(mapping), 2)
  result <- Matrix::t(mapping) %*% x
  # result has no colnames
  browser()

and the mapping, x, and result objects look like

Browse[1]> str(mapping)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:42035] 6 8 19 20 23 24 27 28 30 41 ...
  ..@ p       : int [1:32] 0 13963 14356 14710 14989 15234 15476 15716 15892 15998 ...
  ..@ Dim     : int [1:2] 42035 31
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  ..@ x       : num [1:42035] 1 1 1 1 1 1 1 1 1 1 ...
  ..@ factors : list()
  ..$ assign   : int [1:31] 1 1 1 1 1 1 1 1 1 1 ...
  ..$ contrasts:List of 1
  .. ..$ A:Formal class 'ddiMatrix' [package "Matrix"] with 4 slots
  .. .. .. ..@ diag    : chr "U"
  .. .. .. ..@ Dim     : int [1:2] 31 31
  .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. .. .. .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. .. .. ..@ x       : num(0)

Browse[1]> str(x)
Formal class 'ConvertMatrixType' [package "BPCells"] with 5 slots
  ..@ matrix   :Formal class 'TransformBinarize' [package "BPCells"] with 7 slots
  .. .. ..@ matrix       :Formal class 'MatrixSubset' [package "BPCells"] with 7 slots
  .. .. .. .. ..@ matrix       :Formal class 'MatrixDir' [package "BPCells"] with 7 slots
  .. .. .. .. .. .. ..@ dir        : chr "/home/brent/git/monocle3/monocle.bpcells.20230901.cfd034512a5b.tmp"
  .. .. .. .. .. .. ..@ compressed : logi FALSE
  .. .. .. .. .. .. ..@ buffer_size: int 8192
  .. .. .. .. .. .. ..@ type       : chr "double"
  .. .. .. .. .. .. ..@ dim        : int [1:2] 42035 20271
  .. .. .. .. .. .. ..@ transpose  : logi TRUE
  .. .. .. .. .. .. ..@ dimnames   :List of 2
  .. .. .. .. .. .. .. ..$ : chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. .. .. .. ..$ : chr [1:20271] "WBGene00000001" "WBGene00000002" "WBGene00000003" "WBGene00000004" ...
  .. .. .. .. ..@ row_selection: int(0)
  .. .. .. .. ..@ col_selection: Named int [1:42035] 1 2 3 4 5 6 7 8 9 10 ...
  .. .. .. .. .. ..- attr(*, "names")= chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. ..@ zero_dims    : logi [1:2] FALSE FALSE
  .. .. .. .. ..@ dim          : int [1:2] 42035 20271
  .. .. .. .. ..@ transpose    : logi TRUE
  .. .. .. .. ..@ dimnames     :List of 2
  .. .. .. .. .. ..$ : chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. .. ..$ : chr [1:20271] "WBGene00000001" "WBGene00000002" "WBGene00000003" "WBGene00000004" ...
  .. .. ..@ row_params   : num[0 , 0 ]
  .. .. ..@ col_params   : num[0 , 0 ]
  .. .. ..@ global_params: num [1:2] 0 1
  .. .. ..@ dim          : int [1:2] 42035 20271
  .. .. ..@ transpose    : logi TRUE
  .. .. ..@ dimnames     :List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : NULL
  ..@ type     : chr "uint32_t"
  ..@ dim      : int [1:2] 42035 20271
  ..@ transpose: logi TRUE
  ..@ dimnames :List of 2
  .. ..$ : chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. ..$ : chr [1:20271] "WBGene00000001" "WBGene00000002" "WBGene00000003" "WBGene00000004" ...

Browse[1]> str(result)
Formal class 'MatrixMultiply' [package "BPCells"] with 5 slots
  ..@ left     :Formal class 'TransformBinarize' [package "BPCells"] with 7 slots
  .. .. ..@ matrix       :Formal class 'MatrixSubset' [package "BPCells"] with 7 slots
  .. .. .. .. ..@ matrix       :Formal class 'MatrixDir' [package "BPCells"] with 7 slots
  .. .. .. .. .. .. ..@ dir        : chr "/home/brent/git/monocle3/monocle.bpcells.20230901.cfd034512a5b.tmp"
  .. .. .. .. .. .. ..@ compressed : logi FALSE
  .. .. .. .. .. .. ..@ buffer_size: int 8192
  .. .. .. .. .. .. ..@ type       : chr "double"
  .. .. .. .. .. .. ..@ dim        : int [1:2] 42035 20271
  .. .. .. .. .. .. ..@ transpose  : logi TRUE
  .. .. .. .. .. .. ..@ dimnames   :List of 2
  .. .. .. .. .. .. .. ..$ : chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. .. .. .. ..$ : chr [1:20271] "WBGene00000001" "WBGene00000002" "WBGene00000003" "WBGene00000004" ...
  .. .. .. .. ..@ row_selection: int(0)
  .. .. .. .. ..@ col_selection: Named int [1:42035] 1 2 3 4 5 6 7 8 9 10 ...
  .. .. .. .. .. ..- attr(*, "names")= chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. ..@ zero_dims    : logi [1:2] FALSE FALSE
  .. .. .. .. ..@ dim          : int [1:2] 42035 20271
  .. .. .. .. ..@ transpose    : logi TRUE
  .. .. .. .. ..@ dimnames     :List of 2
  .. .. .. .. .. ..$ : chr [1:42035] "cele-001-001.CATGACTCAA" "cele-001-001.AAGACGGCCA" "cele-001-001.GCCAACGCCA" "cele-001-001.ATAGGAGTAC" ...
  .. .. .. .. .. ..$ : chr [1:20271] "WBGene00000001" "WBGene00000002" "WBGene00000003" "WBGene00000004" ...
  .. .. ..@ row_params   : num[0 , 0 ]
  .. .. ..@ col_params   : num[0 , 0 ]
  .. .. ..@ global_params: num [1:2] 0 1
  .. .. ..@ dim          : int [1:2] 42035 20271
  .. .. ..@ transpose    : logi TRUE
  .. .. ..@ dimnames     :List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : NULL
  ..@ right    :Formal class 'Iterable_dgCMatrix_wrapper' [package "BPCells"] with 4 slots
  .. .. ..@ mat      :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. .. .. .. ..@ i       : int [1:42035] 6 8 19 20 23 24 27 28 30 41 ...
  .. .. .. .. ..@ p       : int [1:32] 0 13963 14356 14710 14989 15234 15476 15716 15892 15998 ...
  .. .. .. .. ..@ Dim     : int [1:2] 42035 31
  .. .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. .. .. .. ..@ x       : num [1:42035] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. ..@ factors : list()
  .. .. ..@ dim      : int [1:2] 31 42035
  .. .. ..@ transpose: logi TRUE
  .. .. ..@ dimnames :List of 2
  .. .. .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. .. .. ..$ : NULL
  ..@ dim      : int [1:2] 31 20271
  ..@ transpose: logi TRUE
  ..@ dimnames :List of 2
  .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. ..$ : NULL


Browse[2]> str(as(result, 'dgCMatrix'))
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:213536] 0 1 2 4 5 6 8 9 11 21 ...
  ..@ p       : int [1:20272] 0 17 31 48 68 73 93 100 105 108 ...
  ..@ Dim     : int [1:2] 31 20271
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:31] "1" "10" "11" "12" ...
  .. ..$ : NULL
  ..@ x       : num [1:213536] 157 7 3 3 1 1 14 2 581 1 ...
  ..@ factors : list()

I appreciate your consideration and guidance.

Thank you.

Ever grateful,
Brent

@bnprks
Copy link
Owner

bnprks commented Sep 2, 2023

Hi Brent,

Thanks for the detailed example code, which helped a lot in diagnosing the issue.

The core problem was due to some weird corner case bug I had in convert_matrix_type() -- basically when two convert_matrix_type() calls went from double -> uint32_t -> double, then the dimnames were lost on the second call. The binarize followed by matrix multiplication was triggering this pattern of conversions internally

I've fixed this in the main branch now (though please comment if my fix didn't actually solve your issue)

-Ben

@bnprks bnprks closed this as completed Sep 2, 2023
@brgew
Copy link
Contributor Author

brgew commented Sep 5, 2023

Hi Ben,

Thank you for the fix. The dimension names survive now the matrix multiplication. And people in Cole's lab are beginning to use BPCells in Monocle3.

Ever grateful,
Brent

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