Skip to content

Commit

Permalink
show_metadata() method
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jun 2, 2022
1 parent 84fa41a commit 290e59e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
12 changes: 10 additions & 2 deletions apis/python/src/tiledbsc/tiledb_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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}")
26 changes: 26 additions & 0 deletions apis/python/src/tiledbsc/tiledb_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}")
53 changes: 24 additions & 29 deletions apis/python/src/tiledbsc/util_tiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


# ================================================================
Expand All @@ -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)
4 changes: 4 additions & 0 deletions apis/python/tools/desc-soma
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 290e59e

Please sign in to comment.