# Buggy code
def __init__(self):
    # registers
    self.dataset_versions = set()
    self.gene_attribute_names = set()
    self.cell_attribute_names = set()
    self.cell_categorical_attribute_names = set()
    self.attribute_mappings = defaultdict(list)
    self.cell_measurements_col_mappings = dict()

    # initialize attributes
    self._X = None
    self._batch_indices = None
    self._labels = None
    self.n_batches = None
    self.n_labels = None
    self.gene_names = None
    self.cell_types = None
    self.local_means = None
    self.local_vars = None
    self._norm_X = None
    self._corrupted_X = None

    # attributes that should not be set by initialization methods
    self.protected_attributes = ["X"]


# Traceback (IndexError)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-89-02337f1f2d24> in <module>
1 adata2.obs = adata2.obs[['n_genes']]
2 adata2.obs.columns = ['_X']
----> 3 scvi_data = AnnDatasetFromAnnData(adata2)

~/miniconda3/envs/solo-sc/lib/python3.7/site-packages/scvi/dataset/anndataset.py in __init__(self, ad, batch_label, ctype_label, class_label, use_raw, cell_measurements_col_mappings)
91             cell_types=cell_types,
92             cell_attributes_dict=obs,
---> 93             gene_attributes_dict=var,
94         )
95         self.filter_cells_by_count()

~/miniconda3/envs/solo-sc/lib/python3.7/site-packages/scvi/dataset/dataset.py in populate_from_data(self, X, Ys, batch_indices, labels, gene_names, cell_types, cell_attributes_dict, gene_attributes_dict, remap_attributes)
200         if gene_attributes_dict:
201             for attribute_name, attribute_value in gene_attributes_dict.items():
--> 202                 self.initialize_gene_attribute(attribute_name, attribute_value)
203
204         if remap_attributes:

~/miniconda3/envs/solo-sc/lib/python3.7/site-packages/scvi/dataset/dataset.py in initialize_gene_attribute(self, attribute_name, attribute)
779             )
780             attribute_name = valid_attribute_name
--> 781         if not self.nb_genes == len(attribute):
782             raise ValueError(
783                 "Number of genes ({n_genes}) and length of gene attribute ({n_attr}) mismatch".format(

~/miniconda3/envs/solo-sc/lib/python3.7/site-packages/scvi/dataset/dataset.py in nb_genes(self)
641     @property
642     def nb_genes(self) -> int:
--> 643         return self.X.shape[1]
644
645     @property

IndexError: tuple index out of range

# Fixed code
def __init__(self):
    # registers
    self.dataset_versions = set()
    self.gene_attribute_names = set()
    self.cell_attribute_names = set()
    self.cell_categorical_attribute_names = set()
    self.attribute_mappings = defaultdict(list)
    self.cell_measurements_col_mappings = dict()

    # initialize attributes
    self._X = None
    self._batch_indices = None
    self._labels = None
    self.n_batches = None
    self.n_labels = None
    self.gene_names = None
    self.cell_types = None
    self.local_means = None
    self.local_vars = None
    self._norm_X = None
    self._corrupted_X = None

    # attributes that should not be set by initialization methods
    self.protected_attributes = ["X", "_X"]