Skip to content

Commit

Permalink
make soma.uns display by default in notebooks (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl authored Jun 27, 2022
1 parent a82b360 commit 2bd4ac1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
8 changes: 4 additions & 4 deletions apis/python/doc/uns_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def __iter__() -> List

Implements `for element in soma.uns: ...`

<a id="tiledbsc.uns_group.UnsGroup.show"></a>
<a id="tiledbsc.uns_group.UnsGroup.__repr__"></a>

#### show
#### \_\_repr\_\_

```python
def show(display_name="uns") -> None
def __repr__() -> str
```

Recursively displays the uns data.
Default display for uns groups.

<a id="tiledbsc.uns_group.UnsGroup.from_anndata_uns"></a>

Expand Down
47 changes: 22 additions & 25 deletions apis/python/src/tiledbsc/uns_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,41 +102,38 @@ def __iter__(self) -> List: # List[Union[UnsGroup, UnsArray]]
return iter(retval)

# ----------------------------------------------------------------
def show(self, display_name="uns") -> None:
def __repr__(self) -> str:
"""
Default display for uns groups.
"""
return self._repr_aux()

# ----------------------------------------------------------------
def _repr_aux(self, display_name="uns", indent="") -> str:
"""
Recursively displays the uns data.
"""
print()
print(display_name + ":")
strings = []
strings.append(indent + display_name + ":")
# Scalars are stored as metadata, not 1D arrays
for k, v in self.metadata().items():
if not k.startswith("__"):
print(k + ":", v)
strings.append(indent + k + ": " + repr(v))
# Now do subgroups, and non-scalar values
for e in self:
element_display_name = display_name + "/" + e.name
if isinstance(e, UnsGroup):
e.show(display_name=element_display_name)
strings.append(
indent
+ e._repr_aux(
display_name=element_display_name, indent=indent + " "
)
)
else:
print(element_display_name + "/")
strings.append(indent + element_display_name + "/")
with e._open() as A:
print(A[:])

# uns:
# scalar_float: 3.25
# scalar_string: a string
# [1]
# [ 0. 1.25 2.5 3.75 5. 6.25 7.5 8.75 10. 11.25]
# [ 0 10 20 30 40 50 60 70 80 90]
# [1 2 3]
# [[1. 2. 3.]
# [4. 5. 6.]]
#
# uns/simple_dict:
# B: one
# [0]
# ['a' 'b' 'c']
# OrderedDict([('A', array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)), ('__tiledb_rows', array([b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9'],
# dtype=object))])
# ['0' '100' '200' '300' '400' '500' '600' '700' '800' '900']
strings.append(indent + str(A[:]))
return "\n".join(strings)

# ----------------------------------------------------------------
def from_anndata_uns(self, uns: ad.compat.OverloadedDict) -> None:
Expand Down
2 changes: 1 addition & 1 deletion apis/python/tests/test_anndata_uns.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_from_anndata_uns(tmp_path):

# Example of what we're verifying:

# >>> soma.uns.show()
# >>> soma.uns
#
# uns:
# scalar_float: 3.25
Expand Down

0 comments on commit 2bd4ac1

Please sign in to comment.