# Buggy code
def __init__(
    self,
    adata_seq: AnnData,
    adata_spatial: AnnData,
    generative_distributions: List = ["zinb", "nb"],
    model_library_size: List = [True, False],
    n_latent: int = 10,
    use_cuda: bool = True,
    **model_kwargs,
):
    super(GIMVI, self).__init__(use_cuda=use_cuda)
    self.use_cuda = use_cuda and torch.cuda.is_available()
    self.adatas = [adata_seq, adata_spatial]

    seq_var_names = _get_var_names_from_setup_anndata(adata_seq)
    spatial_var_names = _get_var_names_from_setup_anndata(adata_spatial)

    if not set(spatial_var_names) <= set(seq_var_names):
        raise ValueError("spatial genes needs to be subset of seq genes")

    spatial_gene_loc = [np.argwhere(seq_var_names == g)[0] for g in spatial_var_names]
    spatial_gene_loc = np.concatenate(spatial_gene_loc)
    gene_mappings = [slice(None), spatial_gene_loc]
    sum_stats = [d.uns["_scvi"]["summary_stats"] for d in self.adatas]
    n_inputs = [s["n_vars"] for s in sum_stats]

    total_genes = adata_seq.uns["_scvi"]["summary_stats"]["n_vars"]

    # since we are combining datasets, we need to increment the batch_idx
    # of one of the datasets
    adata_seq_n_batches = adata_seq.uns["_scvi"]["summary_stats"]["n_batch"]
    adata_spatial.obs["_scvi_batch"] += adata_seq_n_batches

    n_batches = sum([s["n_batch"] for s in sum_stats])

    self.model = JVAE(
        n_inputs,
        total_genes,
        gene_mappings,
        generative_distributions,
        model_library_size,
        n_batch=n_batches,
        n_latent=n_latent,
        **model_kwargs,
    )

    self._model_summary_string = "gimVI model with params"
    self.init_params_ = self._get_init_params(locals())


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-125-eb1e0fcfe1d1> in <module>
1 de = model.differential_expression(
----> 2     groupby="leiden",
3 )
4 de.head()

~/anaconda3/lib/python3.7/site-packages/scvi/core/models/rnamixin.py in differential_expression(self, adata, groupby, group1, group2, idx1, idx2, mode, delta, batch_size, all_stats, batch_correction, batchid1, batchid2, **kwargs)
179         adata = self._validate_anndata(adata)
180
--> 181         col_names = _get_var_names_from_setup_anndata(adata)
182         model_fn = partial(
183             self.get_normalized_expression,

~/anaconda3/lib/python3.7/site-packages/scvi/model/_utils.py in _get_var_names_from_setup_anndata(adata)
119         adata.var_names
120         if adata.uns["_scvi"]["use_raw"] is False
--> 121         else adata.raw.var_names
122     )
123

AttributeError: 'NoneType' object has no attribute 'var_names'

# Fixed code
def __init__(
    self,
    adata_seq: AnnData,
    adata_spatial: AnnData,
    generative_distributions: List = ["zinb", "nb"],
    model_library_size: List = [True, False],
    n_latent: int = 10,
    use_cuda: bool = True,
    **model_kwargs,
):
    super(GIMVI, self).__init__(use_cuda=use_cuda)
    self.use_cuda = use_cuda and torch.cuda.is_available()
    self.adatas = [adata_seq, adata_spatial]
    self.scvi_setup_dicts_ = {
        "seq": adata_seq.uns["_scvi"],
        "spatial": adata_spatial.uns["_scvi"],
    }

    seq_var_names = _get_var_names_from_setup_anndata(adata_seq)
    spatial_var_names = _get_var_names_from_setup_anndata(adata_spatial)

    if not set(spatial_var_names) <= set(seq_var_names):
        raise ValueError("spatial genes needs to be subset of seq genes")

    spatial_gene_loc = [np.argwhere(seq_var_names == g)[0] for g in spatial_var_names]
    spatial_gene_loc = np.concatenate(spatial_gene_loc)
    gene_mappings = [slice(None), spatial_gene_loc]
    sum_stats = [d.uns["_scvi"]["summary_stats"] for d in self.adatas]
    n_inputs = [s["n_vars"] for s in sum_stats]

    total_genes = adata_seq.uns["_scvi"]["summary_stats"]["n_vars"]

    # since we are combining datasets, we need to increment the batch_idx
    # of one of the datasets
    adata_seq_n_batches = adata_seq.uns["_scvi"]["summary_stats"]["n_batch"]
    adata_spatial.obs["_scvi_batch"] += adata_seq_n_batches

    n_batches = sum([s["n_batch"] for s in sum_stats])

    self.model = JVAE(
        n_inputs,
        total_genes,
        gene_mappings,
        generative_distributions,
        model_library_size,
        n_batch=n_batches,
        n_latent=n_latent,
        **model_kwargs,
    )

    self._model_summary_string = "gimVI model with params"
    self.init_params_ = self._get_init_params(locals())