# Buggy code
def __init__(
    self,
    adata: AnnData,
    unlabeled_category: Union[str, int, float],
    pretrained_model: Optional[SCVI] = None,
    n_hidden: int = 128,
    n_latent: int = 10,
    n_layers: int = 1,
    dropout_rate: float = 0.1,
    dispersion: Literal["gene", "gene-batch", "gene-label", "gene-cell"] = "gene",
    gene_likelihood: Literal["zinb", "nb", "poisson"] = "zinb",
    use_cuda: bool = True,
    **model_kwargs,
):
    super(SCANVI, self).__init__(adata, use_cuda=use_cuda)
    self.unlabeled_category = unlabeled_category

    if pretrained_model is not None:
        if pretrained_model.is_trained is False:
            raise ValueError("pretrained model has not been trained")
        self._base_model = pretrained_model.model
        self._is_trained_base = True
    else:
        self._base_model = VAE(
            n_input=self.summary_stats["n_vars"],
            n_batch=self.summary_stats["n_batch"],
            n_hidden=n_hidden,
            n_latent=n_latent,
            n_layers=n_layers,
            dropout_rate=dropout_rate,
            dispersion=dispersion,
            gene_likelihood=gene_likelihood,
            **model_kwargs,
        )
        self._is_trained_base = False
    self.model = SCANVAE(
        n_input=self.summary_stats["n_vars"],
        n_batch=self.summary_stats["n_batch"],
        n_labels=self.summary_stats["n_labels"],
        n_hidden=n_hidden,
        n_latent=n_latent,
        n_layers=n_layers,
        dropout_rate=dropout_rate,
        dispersion=dispersion,
        gene_likelihood=gene_likelihood,
        **model_kwargs,
    )

    # get indices for labeled and unlabeled cells
    key = self.scvi_setup_dict_["data_registry"][_CONSTANTS.LABELS_KEY]["attr_key"]
    self._label_mapping = self.scvi_setup_dict_["categorical_mappings"][key]["mapping"]
    original_key = self.scvi_setup_dict_["categorical_mappings"][key]["original_key"]
    labels = np.asarray(self.adata.obs[original_key]).ravel()
    self._code_to_label = {i: l for i, l in enumerate(self._label_mapping)}
    self._unlabeled_indices = np.argwhere(labels == self.unlabeled_category).ravel()
    self._labeled_indices = np.argwhere(labels != self.unlabeled_category).ravel()

    self._model_summary_string = (
        "ScanVI Model with params: \nunlabeled_category: {}, n_hidden: {}, n_latent: {}"
        ", n_layers: {}, dropout_rate: {}, dispersion: {}, gene_likelihood: {}"
    ).format(
        unlabeled_category,
        n_hidden,
        n_latent,
        n_layers,
        dropout_rate,
        dispersion,
        gene_likelihood,
    )
    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: AnnData,
    unlabeled_category: Union[str, int, float],
    pretrained_model: Optional[SCVI] = None,
    n_hidden: int = 128,
    n_latent: int = 10,
    n_layers: int = 1,
    dropout_rate: float = 0.1,
    dispersion: Literal["gene", "gene-batch", "gene-label", "gene-cell"] = "gene",
    gene_likelihood: Literal["zinb", "nb", "poisson"] = "zinb",
    use_cuda: bool = True,
    **model_kwargs,
):
    super(SCANVI, self).__init__(adata, use_cuda=use_cuda)
    self.unlabeled_category = unlabeled_category

    if pretrained_model is not None:
        if pretrained_model.is_trained is False:
            raise ValueError("pretrained model has not been trained")
        self._base_model = pretrained_model.model
        self._is_trained_base = True
    else:
        self._base_model = VAE(
            n_input=self.summary_stats["n_vars"],
            n_batch=self.summary_stats["n_batch"],
            n_hidden=n_hidden,
            n_latent=n_latent,
            n_layers=n_layers,
            dropout_rate=dropout_rate,
            dispersion=dispersion,
            gene_likelihood=gene_likelihood,
            **model_kwargs,
        )
        self._is_trained_base = False
    self.model = SCANVAE(
        n_input=self.summary_stats["n_vars"],
        n_batch=self.summary_stats["n_batch"],
        n_labels=self.summary_stats["n_labels"],
        n_hidden=n_hidden,
        n_latent=n_latent,
        n_layers=n_layers,
        dropout_rate=dropout_rate,
        dispersion=dispersion,
        gene_likelihood=gene_likelihood,
        **model_kwargs,
    )

    # get indices for labeled and unlabeled cells
    key = self.scvi_setup_dict_["data_registry"][_CONSTANTS.LABELS_KEY]["attr_key"]
    self._label_mapping = self.scvi_setup_dict_["categorical_mappings"][key]["mapping"]
    original_key = self.scvi_setup_dict_["categorical_mappings"][key]["original_key"]
    labels = np.asarray(self.adata.obs[original_key]).ravel()
    self._code_to_label = {i: l for i, l in enumerate(self._label_mapping)}
    self._unlabeled_indices = np.argwhere(labels == self.unlabeled_category).ravel()
    self._labeled_indices = np.argwhere(labels != self.unlabeled_category).ravel()
    self.unsupervised_history_ = None
    self.semisupervised_history_ = None

    self._model_summary_string = (
        "ScanVI Model with params: \nunlabeled_category: {}, n_hidden: {}, n_latent: {}"
        ", n_layers: {}, dropout_rate: {}, dispersion: {}, gene_likelihood: {}"
    ).format(
        unlabeled_category,
        n_hidden,
        n_latent,
        n_layers,
        dropout_rate,
        dispersion,
        gene_likelihood,
    )
    self.init_params_ = self._get_init_params(locals())