From 5337f0d54fcd80eedd76a5ca557df411a5def0f6 Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Wed, 18 Dec 2024 14:40:26 +0000 Subject: [PATCH 1/4] Some repr fixes --- dkist/dataset/utils.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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]) From 031d4da6fc80e9cdab4042cfe8c572ed057dc7cb Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Wed, 18 Dec 2024 14:48:46 +0000 Subject: [PATCH 2/4] Changelog --- changelog/479.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/479.bugfix.rst 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__`. From d29796bbb5a6f06683345f99ec9ebcfc33c4340d Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Wed, 18 Dec 2024 15:00:35 +0000 Subject: [PATCH 3/4] Attempt to fix the tileddataset plot --- dkist/dataset/tests/test_tiled_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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() From 9499245161e74ccbc120589f86bf5bb79a526b98 Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Mon, 6 Jan 2025 11:03:49 +0000 Subject: [PATCH 4/4] Add a test for numpy repr --- dkist/dataset/tests/test_dataset.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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):