Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some repr fixes #479

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 59 in dkist/dataset/tests/test_dataset.py

View check run for this annotation

Codecov / codecov/patch

dkist/dataset/tests/test_dataset.py#L55-L59

Added lines #L55 - L59 were not covered by tests


@pytest.mark.accept_cli_dataset
def test_wcs_roundtrip(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)

Check warning on line 22 in dkist/dataset/utils.py

View check run for this annotation

Codecov / codecov/patch

dkist/dataset/utils.py#L20-L22

Added lines #L20 - L22 were not covered by tests


def dataset_info_str(ds_in):
# Check for an attribute that only appears on TiledDataset
# Not using isinstance to avoid circular import
Expand Down Expand Up @@ -48,7 +59,7 @@
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"

Check warning on line 62 in dkist/dataset/utils.py

View check run for this annotation

Codecov / codecov/patch

dkist/dataset/utils.py#L62

Added line #L62 was not covered by tests

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 @@
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)

Check warning on line 153 in dkist/dataset/utils.py

View check run for this annotation

Codecov / codecov/patch

dkist/dataset/utils.py#L153

Added line #L153 was not covered by tests
for i, col in enumerate(mstr.T):
if i == 0:
mstr[:, i] = np.char.rjust(col, widths[i])
Expand Down
Loading