A (hacky) method for binning groups of genes correlated along the
        SAM manifold.

        Parameters
        ----------
        number_of_features - int, optional, default None
            The number of genes to bin. Capped at 5000 due to memory
            considerations.

        input_gene - str, optional, default None
            If not None, use this gene as the first seed when growing the
            correlation bins.

def corr_bin_genes(self, number_of_features=None, input_gene=None):
        """A (hacky) method for binning groups of genes correlated along the
        SAM manifold.

        Parameters
        ----------
        number_of_features - int, optional, default None
            The number of genes to bin. Capped at 5000 due to memory
            considerations.

        input_gene - str, optional, default None
            If not None, use this gene as the first seed when growing the
            correlation bins.

        """

        weights = self.adata.var['spatial_dispersions'].values
        all_gene_names = np.array(list(self.adata.var_names))
        D_avg = self.adata.layers['X_knn_avg']

        idx2 = np.argsort(-weights)[:weights[weights > 0].size]

        if(number_of_features is None or number_of_features > idx2.size):
            number_of_features = idx2.size

        if number_of_features > 1000:
            number_of_features = 1000

        if(input_gene is not None):
            input_gene = np.where(all_gene_names == input_gene)[0]
            if(input_gene.size == 0):
                print(
                    "Gene note found in the filtered dataset. Note "
                    "that genes are case sensitive.")
                return
            seeds = [np.array([input_gene])]
            pw_corr = np.corrcoef(
                D_avg[:, idx2[:number_of_features]].T.toarray())
            for i in range(1, number_of_features):
                flag = False
                maxd = np.mean(pw_corr[i, :][pw_corr[i, :] > 0])
                maxi = 0
                for j in range(len(seeds)):
                    if(pw_corr[np.where(idx2 == seeds[j][0])[0], i]
                       > maxd):
                        maxd = pw_corr[np.where(idx2 == seeds[j][0])[0], i]
                        maxi = j
                        flag = True
                if(not flag):
                    seeds.append(np.array([idx2[i]]))
                else:
                    seeds[maxi] = np.append(seeds[maxi], idx2[i])

            geneID_groups = []
            for i in range(len(seeds)):
                geneID_groups.append(all_gene_names[seeds[i]])

            return geneID_groups[0]
        else:
            seeds = [np.array([idx2[0]])]
            pw_corr = np.corrcoef(
                D_avg[:, idx2[:number_of_features]].T.toarray())
            for i in range(1, number_of_features):
                flag = False
                maxd = np.mean(pw_corr[i, :][pw_corr[i, :] > 0])
                maxi = 0
                for j in range(len(seeds)):
                    if(pw_corr[np.where(idx2 == seeds[j][0])[0], i]
                       > maxd):
                        maxd = pw_corr[np.where(idx2 == seeds[j][0])[0], i]
                        maxi = j
                        flag = True
                if(not flag):
                    seeds.append(np.array([idx2[i]]))
                else:
                    seeds[maxi] = np.append(seeds[maxi], idx2[i])

            geneID_groups = []
            for i in range(len(seeds)):
                geneID_groups.append(
                    all_gene_names[seeds[i]])

            self.adata.uns['gene_groups'] = geneID_groups
            return geneID_groups