anndict.AdataDict.set_hierarchy#
- AdataDict.set_hierarchy(nesting_list)[source]#
Rearrange the hierarchy of
AdataDict
based on the provided nesting structure.- Parameters:
- nesting_list
TypeAliasType
|TypeAliasType
Nested list indicating the new hierarchy structure.
- nesting_list
Notes
Supports input as a nested list or nested tuple. Recommended to supply as a nested list. Nested tupple support is supplied for ease of use when caching, see Case 4: Caching the hierarchy below.
Examples
Case 1: Flat hierarchy
adata_dict.set_hierarchy(["Donor", "Tissue"]) print(adata_dict) > { > ("Donor1", "Tissue1"): adata1, > ("Donor1", "Tissue2"): adata2, > ("Donor2", "Tissue1"): adata3, > }
Case 2: Nested hierarchy
adata_dict.set_hierarchy(["Donor", ["Tissue"]]) # Note the nested list here print(adata_dict) > { > ("Donor1",): { > ("Tissue1",): adata1, > ("Tissue2",): adata2, > }, > ("Donor2",): { > ("Tissue1",): adata3, > }, > }
Case 3: Complex nested hierarchy
adata_dict.set_hierarchy(["Donor", ["Tissue", "Cell Type"]]) # Note the nested list here print(adata_dict) > { > ("Donor1",): { > ("Tissue1", "CellType1"): adata1, > ("Tissue1", "CellType2"): adata2, > ("Tissue2", "CellType3"): adata3, > }, > ("Donor2",): { > ("Tissue1", "CellType1"): adata4, > }, > }
Case 4: Caching the hierarchy
# Initial Structure print(adata_dict) # Is nested > { > ("Donor1",): { > ("Tissue1",): adata1, > ("Tissue2",): adata2, > }, > ("Donor2",): { > ("Tissue1",): adata3, > }, > } # Cache the hierarchy cached_hierarchy = adata_dict.hierarchy # Change the hierarchy (flatten for this example) adata_dict.flatten() print(adata_dict) # Is now flat > { > ("Donor1", "Tissue1"): adata1, > ("Donor1", "Tissue2"): adata2, > ("Donor2", "Tissue1"): adata3, > } #Restore the hierarchy adata_dict.set_hierarchy(cached_hierarchy) print(adata_dict) # Is nested again > { > ("Donor1",): { > ("Tissue1",): adata1, > ("Tissue2",): adata2, > }, > ("Donor2",): { > ("Tissue1",): adata3, > }, > }