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

Update TiledDataset.plot() #441

Merged
merged 19 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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/441.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minor updates to `TiledDataset.plot()` for working with more complex arrangements of tiles.
38 changes: 28 additions & 10 deletions dkist/dataset/tiled_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,26 @@ def tiles_shape(self):
"""
return [[tile.data.shape for tile in row] for row in self]

def plot(self, slice_index: int, share_zscale=False, **kwargs):
def plot(self, slice_index, share_zscale=False, **kwargs):
"""
Plot a slice of each tile in the TiledDataset

Parameters
----------
slice_index : `int`, sequence of `int`s or `numpy.s_`
Object representing a slice which will reduce each component dataset of the TiledDataset to a 2D image. This is passed to ``TiledDataset.slice_tiles``
share_zscale : `bool`
Determines whether the color scale of the plots should be calculated independently (``False``) or shared across all plots (``True``).
Defaults to False
"""
if isinstance(slice_index, int):
slice_index = (slice_index,)
vmin, vmax = np.inf, 0
fig = plt.figure()
for i, tile in enumerate(self.flat):
ax = fig.add_subplot(self.shape[0], self.shape[1], i+1, projection=tile[0].wcs)
tile[slice_index].plot(axes=ax, **kwargs)
tiles = self.slice_tiles[slice_index].flat
for i, tile in enumerate(tiles):
ax = fig.add_subplot(self.shape[0], self.shape[1], i+1, projection=tile.wcs)
tile.plot(axes=ax, **kwargs)
if i == 0:
xlabel = ax.coords[0].get_axislabel() or ax.coords[0]._get_default_axislabel()
ylabel = ax.coords[1].get_axislabel() or ax.coords[1]._get_default_axislabel()
Expand All @@ -174,12 +188,20 @@ def plot(self, slice_index: int, share_zscale=False, **kwargs):
if share_zscale:
for ax in fig.get_axes():
ax.get_images()[0].set_clim(vmin, vmax)
timestamp = self[0, 0].axis_world_coords("time")[-1].iso[slice_index]
fig.suptitle(f"{self.inventory['instrumentName']} Dataset ({self.inventory['datasetId']}) at time {timestamp} (slice={slice_index})", y=0.95)
title = f"{self.inventory['instrumentName']} Dataset ({self.inventory['datasetId']}) at "
for i, (coord, val) in enumerate(tiles[0].global_coords.items()):
if coord == "time":
val = val.iso
title += f"{coord} {val}" + (", " if i != len(slice_index)-1 else " ")
title += f"(slice={(slice_index if len(slice_index) > 1 else slice_index[0])})".replace("slice(None, None, None)", ":")
fig.suptitle(title, y=0.95)
return fig

@property
def slice_tiles(self):
"""
Returns a new TiledDataset with the given slice applied to each of the tiles.
"""
return TiledDatasetSlicer(self._data, self.inventory)

# TODO: def regrid()
Expand All @@ -199,10 +221,6 @@ def files(self):
"""
A `~.FileManager` helper for interacting with the files backing the data in this ``Dataset``.
"""
return self._file_manager

@property
def _file_manager(self):
fileuris = [[tile.files.filenames for tile in row] for row in self]
dtype = self[0, 0].files.fileuri_array.dtype
shape = self[0, 0].files.shape
Expand Down