Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jun 2, 2022
1 parent 290e59e commit 66b0ad2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsc/annotation_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def from_dataframe(self, dataframe: pd.DataFrame, extent: int) -> None:
mode=mode,
)

self.set_soma_object_type_metadata()
self._set_soma_object_type_metadata()

if self._verbose:
print(util.format_elapsed(s, f"{self._indent}FINISH WRITING {self.uri}"))
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsc/annotation_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def from_matrix_and_dim_values(self, matrix, dim_values):
else:
self._numpy_ndarray_or_scipy_sparse_csr_matrix(matrix, dim_values)

self.set_soma_object_type_metadata()
self._set_soma_object_type_metadata()

if self._verbose:
print(util.format_elapsed(s, f"{self._indent}FINISH WRITING {self.uri}"))
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsc/assay_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def from_matrix_and_dim_values(self, matrix, row_names, col_names) -> None:
else:
self._create_empty_array(matrix_dtype=matrix.dtype)

self.set_soma_object_type_metadata()
self._set_soma_object_type_metadata()

self._ingest_data(matrix, row_names, col_names)
if self._verbose:
Expand Down
5 changes: 3 additions & 2 deletions apis/python/src/tiledbsc/tiledb_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(
self,
uri: str,
name: str,
#### XXX parent: Optional[TileDBGroup] = None,
# It's a circular import if we say this, but this is really:
# parent: Optional[TileDBGroup] = None,
parent=None,
):
"""
Expand Down Expand Up @@ -98,7 +99,7 @@ def has_attr_name(self, attr_name: str) -> bool:
"""
return attr_name in self.attr_names()

def set_soma_object_type_metadata(self) -> None:
def _set_soma_object_type_metadata(self) -> None:
"""
This helps nested-structured traversals (especially those that start at the SOMACollection
level) confidently navigate with a minimum of introspection on group contents.
Expand Down
38 changes: 37 additions & 1 deletion apis/python/src/tiledbsc/tiledb_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,45 @@ def _create(self):
if self._verbose:
print(f"{self._indent}Creating TileDB group {self.uri}")
tiledb.group_create(uri=self.uri, ctx=self._ctx)

self._set_soma_object_type_metadata()

def _set_soma_object_type_metadata(self):
"""
This helps nested-structured traversals (especially those that start at the SOMACollection
level) confidently navigate with a minimum of introspection on group contents.
"""
with self._open("w") as G:
G.meta[
tiledbsc.util_tiledb.SOMA_OBJECT_TYPE_METADATA_KEY
] = self.__class__.__name__

def _set_soma_object_type_metadata_recursively(self):
"""
SOMAs/SOCOs written very early on in the development of this project may not have these set.
Using this method we can after-populate these, without needig to re-ingest entire datasets.
Any SOMAs/SOCOs ingested from June 2022 onward won't need this -- this metadata will be
written at ingestion time.
"""
self._set_soma_object_type_metadata()
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._set_soma_object_type_metadata_recursively()
elif object_type == "array":
array = TileDBArray(uri=O.uri, name=O.name, parent=self)
array._set_soma_object_type_metadata()
else:
raise Exception(
f"Unexpected object_type found: {object_type} at {O.uri}"
)

def _open(self, mode="r"):
"""
This is just a convenience wrapper around tiledb group-open.
Expand Down Expand Up @@ -157,4 +191,6 @@ def show_metadata(self, recursively=True, indent=""):
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}")
raise Exception(
f"Unexpected object_type found: {object_type} at {O.uri}"
)
36 changes: 15 additions & 21 deletions apis/python/src/tiledbsc/uns_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ def to_dict_of_matrices(self) -> Dict:
"""
Reads the recursive group/array uns data from TileDB storage and returns them as a recursive dict of matrices.
"""
grp = None
try: # Not all groups have uns
grp = tiledb.Group(self.uri, mode="r")
except:
pass
if grp == None:
if not self.exists():
if self._verbose:
print(f"{self._indent}{self.uri} not found")
return {}
Expand All @@ -128,24 +123,23 @@ def to_dict_of_matrices(self) -> Dict:
s = util.get_start_stamp()
print(f"{self._indent}START read {self.uri}")

retval = {}
for element in grp:
name = os.path.basename(element.uri) # TODO: update for tiledb cloud
with self._open() as G:
retval = {}
for element in G:
name = os.path.basename(element.uri) # TODO: update for tiledb cloud

if element.type == tiledb.tiledb.Group:
child_group = UnsGroup(uri=element.uri, name=name, parent=self)
retval[name] = child_group.to_dict_of_matrices()
if element.type == tiledb.tiledb.Group:
child_group = UnsGroup(uri=element.uri, name=name, parent=self)
retval[name] = child_group.to_dict_of_matrices()

elif element.type == tiledb.libtiledb.Array:
child_array = UnsArray(uri=element.uri, name=name, parent=self)
retval[name] = child_array.to_matrix()
elif element.type == tiledb.libtiledb.Array:
child_array = UnsArray(uri=element.uri, name=name, parent=self)
retval[name] = child_array.to_matrix()

else:
raise Exception(
f"Internal error: found uns group element neither group nor array: type is {str(element.type)}"
)

grp.close()
else:
raise Exception(
f"Internal error: found uns group element neither group nor array: type is {str(element.type)}"
)

if self._verbose:
print(util.format_elapsed(s, f"{self._indent}FINISH read {self.uri}"))
Expand Down

0 comments on commit 66b0ad2

Please sign in to comment.