Skip to content

Commit

Permalink
Some repr fixes (#479)
Browse files Browse the repository at this point in the history
* Some repr fixes

* Changelog

* Attempt to fix the tileddataset plot

* Add a test for numpy repr
  • Loading branch information
Cadair authored Jan 6, 2025
1 parent 9c17054 commit f650849
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/479.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix some small issues with `Dataset.__repr__`.
8 changes: 8 additions & 0 deletions dkist/dataset/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def test_repr(dataset, dataset_3d):
r = repr(dataset_3d)
assert str(dataset_3d.data) in r

def test_repr_numpy(dataset):
# Do it the old way to support old ndcube
dataset._data = dataset.data.compute()
r = repr(dataset)
assert "numpy.ndarray" in r
assert f"{dataset.data.shape}" in r
assert f"{dataset.data.dtype}" in r


@pytest.mark.accept_cli_dataset
def test_flat_repr(large_tiled_dataset):
Expand Down
2 changes: 1 addition & 1 deletion dkist/dataset/tests/test_tiled_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_tileddataset_plot(share_zscale):
newtiles = []
for tile in ds.flat:
newtiles.append(tile.rebin((1, 8, 8), operation=np.sum))
ds = TiledDataset(np.array(newtiles).reshape(ds.shape), inventory=ds.inventory)
ds = TiledDataset(np.array(newtiles).reshape(ds.shape), inventory=newtiles[0].inventory)
fig = plt.figure(figsize=(600, 800))
ds.plot(0, share_zscale=share_zscale)
return plt.gcf()
Expand Down
15 changes: 13 additions & 2 deletions dkist/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
__all__ = ["dataset_info_str"]


def get_array_repr(array):
"""
Return a "repr-like" string for an array, without any values.
The objective of this function is primarily to provide a dask array like repr for numpy arrays.
"""
if isinstance(array, np.ndarray):
return f"numpy.ndarray<shape={array.shape}, dtype={array.dtype}>"
return repr(array)


def dataset_info_str(ds_in):
# Import here to remove circular import
from dkist.dataset import TiledDataset
Expand Down Expand Up @@ -48,7 +59,7 @@ def dataset_info_str(ds_in):
s += "\nThis "
s += f"Dataset has {wcs.pixel_n_dim} pixel and {wcs.world_n_dim} world dimensions.\n\n"

s += f"The data are represented by a {type(ds.data)} object:\n{ds.data}\n\n"
s += f"The data are represented by a {type(ds.data)} object:\n{get_array_repr(ds.data)}\n\n"

array_shape = wcs.array_shape or (0,)
pixel_shape = wcs.pixel_shape or (None,) * wcs.pixel_n_dim
Expand Down Expand Up @@ -139,7 +150,7 @@ def _get_pp_matrix(wcs):
world.insert(0, "")
mstr = np.insert(mstr, 0, world, axis=1)
widths = [np.max([len(a) for a in col]) for col in mstr.T]
mstr = np.insert(mstr, 2, ["-"*wid for wid in widths], axis=0)
mstr = np.insert(mstr, header.shape[0], ["-"*wid for wid in widths], axis=0)
for i, col in enumerate(mstr.T):
if i == 0:
mstr[:, i] = np.char.rjust(col, widths[i])
Expand Down

0 comments on commit f650849

Please sign in to comment.