# TypeError when trying to set up scvi.model.TOTALVI()

**URL:** <https://discourse.scverse.org/t/typeerror-when-trying-to-set-up-scvi-model-totalvi/1709>\
**Category:** scvi-tools\
**Tags:** totalvi\
**Created:** [August 23, 2023, 7:25pm UTC](https://discourse.scverse.org/t/typeerror-when-trying-to-set-up-scvi-model-totalvi/1709 "2023-08-23T19:25:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmnzo5gy](https://avatars.discourse-cdn.com/v4/letter/h/9e8a1a/32.png) [@hmnzo5gy](https://discourse.scverse.org/u/hmnzo5gy)\
**Post date:** [August 23, 2023, 7:25pm UTC](https://discourse.scverse.org/t/typeerror-when-trying-to-set-up-scvi-model-totalvi/1709/1 "2023-08-23T19:25:22Z")

</div>

When I try to set up MuData model for TotalVI using scvi.model.TOTALVI(mdata), I keep encountering “Type Error”: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

```auto
model = scvi.model.TOTALVI(mdata)
INFO Computing empirical prior initialization for protein background.                                          
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[17], line 1
----> 1 model = scvi.model.TOTALVI(mdata)

File ~/miniconda3/envs/scanpy/lib/python3.11/site-packages/scvi/model/_totalvi.py:142, in TOTALVI. __init__ (self, adata, n_latent, gene_dispersion, protein_dispersion, gene_likelihood, latent_distribution, empirical_protein_background_prior, override_missing_proteins, **model_kwargs)
    136 emp_prior = (
    137 empirical_protein_background_prior
    138 if empirical_protein_background_prior is not None
    139 else (self.summary_stats.n_proteins > 10)
    140 )
    141 if emp_prior:
--> 142 prior_mean, prior_scale = self._get_totalvi_protein_priors(adata)
    143 else:
    144 prior_mean, prior_scale = None, None

File ~/miniconda3/envs/scanpy/lib/python3.11/site-packages/scvi/model/_totalvi.py:1163, in TOTALVI._get_totalvi_protein_priors(self, adata, n_cells)
   1161 for c in batch_pro_exp:
   1162 try:
-> 1163 gmm.fit(np.log1p(c.reshape(-1, 1)))
   1164 # when cell is all 0
   1165 except ConvergenceWarning:

File ~/miniconda3/envs/scanpy/lib/python3.11/site-packages/sklearn/mixture/_base.py:181, in BaseMixture.fit(self, X, y)
    155 """Estimate model parameters with the EM algorithm.
...
    538 )
    539 elif isinstance(accept_sparse, (list, tuple)):
    540 if len(accept_sparse) == 0:

TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

```

I import the cellranger multi results using muon.read\_10x\_h5 and the imported mdata looks like the following

```auto
MuData object with n_obs × n_vars = 21812 × 19403
  var:	'gene_ids', 'feature_types', 'genome'
  3 modalities
    rna:	21812 x 15390
      obs:	'most_likely_hypothesis', 'Classification', 'n_genes', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'total_counts_ribo', 'pct_counts_ribo', 'batch'
      var:	'gene_ids', 'feature_types', 'genome', 'n_cells', 'mt', 'ribo', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'highly_variable', 'highly_variable_rank', 'means', 'variances', 'variances_norm', 'highly_variable_nbatches'
      uns:	'log1p', 'hvg'
      layers:	'counts'
    protein:	21812 x 13
      obs:	'most_likely_hypothesis', 'Classification', 'batch'
      var:	'gene_ids', 'feature_types', 'genome'
      layers:	'counts'
    rna_subset:	21812 x 4000
      obs:	'most_likely_hypothesis', 'Classification', 'n_genes', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'total_counts_ribo', 'pct_counts_ribo', 'batch'
      var:	'gene_ids', 'feature_types', 'genome', 'n_cells', 'mt', 'ribo', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'highly_variable', 'highly_variable_rank', 'means', 'variances', 'variances_norm', 'highly_variable_nbatches'
      uns:	'log1p', 'hvg'
      layers:	'counts'

```

How can I resolve this problem with totalVI? Thank you

---

<div class="post-metadata">

**Author:** ![martinkim0](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/martinkim0/32/881_2.png) [@martinkim0](https://discourse.scverse.org/u/martinkim0)\
**Post date:** [August 28, 2023, 9:04pm UTC](https://discourse.scverse.org/t/typeerror-when-trying-to-set-up-scvi-model-totalvi/1709/2 "2023-08-28T21:04:51Z")

</div>

Hi, looks like you have to convert your protein data to dense format prior to using it with the model.

---

<div class="post-metadata">

**Author:** ![hmnzo5gy](https://avatars.discourse-cdn.com/v4/letter/h/9e8a1a/32.png) [@hmnzo5gy](https://discourse.scverse.org/u/hmnzo5gy)\
**Post date:** [August 29, 2023, 3:57am UTC](https://discourse.scverse.org/t/typeerror-when-trying-to-set-up-scvi-model-totalvi/1709/3 "2023-08-29T03:57:54Z")

</div>

I was able to get it work with the following.

```auto
adata = mdata["rna"].copy()
adata.obsm["protein_expression"] = mdata["prot"].layers["counts"].A.copy()
protein_adata = ad.AnnData(adata.obsm["protein_expression"])
protein_adata.obs_names = adata.obs_names
del adata.obsm["protein_expression"]
mdata = md.MuData({"rna": adata, "protein": protein_adata})

```

Are there any easier ways to prepare the protein layer of mdata for totalVI straight out of reading in cellranger multi output .h5 file?
