Skip to content

Visualise/compare Anndata object structure #671

@chris-rands

Description

@chris-rands

With large Anndata objects, I sometimes want check the attributes present. The print/repr call does this okay, but becomes hard to read with highly populated objects and not possible to compared two objects attributes progamatically.

Currently my workaround is to use a function to output a dict, which can then be parsed to JSON or printed etc., but I don't know if there is a better choice? Or indeed if such a function could be builtin to Anndata? e.g.

import scanpy as sc
import pprint


def repr_dict(adata):
    d = {}
    for attr in (
        "n_obs",
        "n_vars",
        "obs",
        "var",
        "uns",
        "obsm",
        "varm",
        "layers",
        "obsp",
        "varp",
    ):
        got_attr = getattr(adata, attr)
        if isinstance(got_attr, int):
            d[attr] = got_attr
        else:
            keys = list(got_attr.keys())
            if keys:
                d[attr] = keys
    return d


adata = sc.datasets.pbmc68k_reduced()

print(adata)
pprint.pprint(repr_dict(adata))

Outputs:

AnnData object with n_obs × n_vars = 700 × 765
    obs: 'bulk_labels', 'n_genes', 'percent_mito', 'n_counts', 'S_score', 'G2M_score', 'phase', 'louvain'
    var: 'n_counts', 'means', 'dispersions', 'dispersions_norm', 'highly_variable'
    uns: 'bulk_labels_colors', 'louvain', 'louvain_colors', 'neighbors', 'pca', 'rank_genes_groups'
    obsm: 'X_pca', 'X_umap'
    varm: 'PCs'
    obsp: 'distances', 'connectivities'
{'n_obs': 700,
 'n_vars': 765,
 'obs': ['bulk_labels',
         'n_genes',
         'percent_mito',
         'n_counts',
         'S_score',
         'G2M_score',
         'phase',
         'louvain'],
 'obsm': ['X_pca', 'X_umap'],
 'obsp': ['distances', 'connectivities'],
 'uns': ['bulk_labels_colors',
         'louvain',
         'louvain_colors',
         'neighbors',
         'pca',
         'rank_genes_groups'],
 'var': ['n_counts',
         'means',
         'dispersions',
         'dispersions_norm',
         'highly_variable'],
 'varm': ['PCs']}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions