diff --git a/changelog/479.bugfix.rst b/changelog/479.bugfix.rst new file mode 100644 index 00000000..19d1eb1e --- /dev/null +++ b/changelog/479.bugfix.rst @@ -0,0 +1 @@ +Fix some small issues with `Dataset.__repr__`. diff --git a/dkist/dataset/tests/test_dataset.py b/dkist/dataset/tests/test_dataset.py index 90b8c577..e227f4a3 100644 --- a/dkist/dataset/tests/test_dataset.py +++ b/dkist/dataset/tests/test_dataset.py @@ -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_wcs_roundtrip(dataset): diff --git a/dkist/dataset/tests/test_tiled_dataset.py b/dkist/dataset/tests/test_tiled_dataset.py index e72b8117..c01abd36 100644 --- a/dkist/dataset/tests/test_tiled_dataset.py +++ b/dkist/dataset/tests/test_tiled_dataset.py @@ -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() diff --git a/dkist/dataset/utils.py b/dkist/dataset/utils.py index bab3e338..d29af480 100644 --- a/dkist/dataset/utils.py +++ b/dkist/dataset/utils.py @@ -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" + return repr(array) + + def dataset_info_str(ds_in): # Check for an attribute that only appears on TiledDataset # Not using isinstance to avoid circular import @@ -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 @@ -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])