From 290e59ea960efb62b7f4129ccdbacce96b5a6553 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Thu, 2 Jun 2022 16:12:20 -0400 Subject: [PATCH] show_metadata() method --- apis/python/src/tiledbsc/tiledb_array.py | 12 +++++- apis/python/src/tiledbsc/tiledb_group.py | 26 ++++++++++++ apis/python/src/tiledbsc/util_tiledb.py | 53 +++++++++++------------- apis/python/tools/desc-soma | 4 ++ 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/apis/python/src/tiledbsc/tiledb_array.py b/apis/python/src/tiledbsc/tiledb_array.py index b9dd8ebbf0..85c7485886 100644 --- a/apis/python/src/tiledbsc/tiledb_array.py +++ b/apis/python/src/tiledbsc/tiledb_array.py @@ -2,7 +2,6 @@ import tiledbsc.util_tiledb from .soma_options import SOMAOptions from .tiledb_object import TileDBObject -from .tiledb_group import TileDBGroup from typing import Optional, List, Dict @@ -18,7 +17,8 @@ def __init__( self, uri: str, name: str, - parent: Optional[TileDBGroup] = None, + #### XXX parent: Optional[TileDBGroup] = None, + parent=None, ): """ See the TileDBObject constructor. @@ -107,3 +107,11 @@ def set_soma_object_type_metadata(self) -> None: A.meta[ tiledbsc.util_tiledb.SOMA_OBJECT_TYPE_METADATA_KEY ] = self.__class__.__name__ + + def show_metadata(self, recursively=True, indent=""): + """ + Shows metadata for the array. + """ + print(f"{indent}[{self.name}]") + for key, value in self.metadata().items(): + print(f"{indent}- {key}: {value}") diff --git a/apis/python/src/tiledbsc/tiledb_group.py b/apis/python/src/tiledbsc/tiledb_group.py index 2a3e6ce09c..dcc9e62bd1 100644 --- a/apis/python/src/tiledbsc/tiledb_group.py +++ b/apis/python/src/tiledbsc/tiledb_group.py @@ -2,6 +2,7 @@ import tiledbsc.util_tiledb from .soma_options import SOMAOptions from .tiledb_object import TileDBObject +from .tiledb_array import TileDBArray from contextlib import contextmanager @@ -132,3 +133,28 @@ def _get_member_names_to_uris(self) -> Dict[str, str]: """ with self._open("r") as G: return {O.name: O.uri for O in G} + + def show_metadata(self, recursively=True, indent=""): + """ + Shows metadata for the group, recursively by default. + """ + print(f"{indent}[{self.name}]") + for key, value in self.metadata().items(): + print(f"{indent}- {key}: {value}") + if recursively: + child_indent = indent + " " + with self._open() as G: + for O in G: # This returns a tiledb.object.Object + # It might appear simpler to have all this code within TileDBObject class, + # rather than (with a little duplication) in TileDBGroup and TileDBArray. + # However, getting it to work with a recursive data structure and finding the + # required methods, it was simpler to split the logic this way. + object_type = tiledb.object_type(O.uri) + if object_type == "group": + group = TileDBGroup(uri=O.uri, name=O.name, parent=self) + group.show_metadata(recursively, indent=child_indent) + elif object_type == "array": + array = TileDBArray(uri=O.uri, name=O.name, parent=self) + array.show_metadata(recursively, indent=child_indent) + else: + raise Exception(f"Unexpected object_type found: {object_type}") diff --git a/apis/python/src/tiledbsc/util_tiledb.py b/apis/python/src/tiledbsc/util_tiledb.py index 5e318a6038..56ecc9c33e 100644 --- a/apis/python/src/tiledbsc/util_tiledb.py +++ b/apis/python/src/tiledbsc/util_tiledb.py @@ -49,16 +49,10 @@ def __show_array_schema(uri: str, ctx: Optional[tiledb.Ctx] = None): # ---------------------------------------------------------------- def __show_array_schemas_for_group(group_uri: str, ctx: Optional[tiledb.Ctx] = None): - group = None - try: - group = tiledb.Group(group_uri, mode="r", ctx=ctx) - except: - return - - for element in group: - if element.type == tiledb.libtiledb.Array: - __show_array_schema(element.uri, ctx) - group.close() + with tiledb.Group(group_uri, mode="r", ctx=ctx) as G: + for element in G: + if element.type == tiledb.libtiledb.Array: + __show_array_schema(element.uri, ctx) # ================================================================ @@ -68,22 +62,23 @@ def show_tiledb_group_array_schemas(uri: str, ctx: Optional[tiledb.Ctx] = None): single-cell matrix-API data, and won't necessarily traverse items in a familiar application-specific order. """ - group = tiledb.Group(uri, mode="r", ctx=ctx) - print() - print("================================================================") - print(uri) - - for element in group: - # Note: use `element.type` rather than `isinstance(element, tiledb.group.Group)` - # since type(element) is `tiledb.object.Object` in all cases. - if element.type == tiledb.group.Group: - show_tiledb_group_array_schemas(element.uri) - elif element.type == tiledb.libtiledb.Array: - print() - print("----------------------------------------------------------------") - print(element.uri) - with tiledb.open(element.uri, ctx=ctx) as A: - print(A.schema) - else: - print("Skipping element type", element.type) - group.close() + with tiledb.Group(uri, mode="r", ctx=ctx) as G: + print() + print("================================================================") + print(uri) + + for element in G: + # Note: use `element.type` rather than `isinstance(element, tiledb.group.Group)` + # since type(element) is `tiledb.object.Object` in all cases. + if element.type == tiledb.group.Group: + show_tiledb_group_array_schemas(element.uri) + elif element.type == tiledb.libtiledb.Array: + print() + print( + "----------------------------------------------------------------" + ) + print(element.uri) + with tiledb.open(element.uri, ctx=ctx) as A: + print(A.schema) + else: + print("Skipping element type", element.type) diff --git a/apis/python/tools/desc-soma b/apis/python/tools/desc-soma index 39ddd1f4fb..102f1542e4 100755 --- a/apis/python/tools/desc-soma +++ b/apis/python/tools/desc-soma @@ -21,6 +21,10 @@ def main(): for uri in sys.argv[1:]: tiledbsc.util_tiledb.show_single_cell_group(uri) + print() + print("METADATA:") + soma = tiledbsc.SOMA(uri) + soma.show_metadata(recursively=True) if __name__ == "__main__":