Skip to content

Commit

Permalink
Plotting helpers for TiledDataset (#408)
Browse files Browse the repository at this point in the history
* Add broken plot method for TiledDataset

* Need projection in plots

* Just plot the first image of each tile

* Clear x and y labels except on the middle edge tiles

Assumes a grid of 3x3 tiles, not sure if this will always be true

* Make plot function plot at a user-supplied index

* Also pass plot kwargs through to ds.plot

* More robust axis labelling

* Swap tile titles for suptitle with timestamp

* Optionally share colour scaling

Just takes the clim values from each tile and uses the highest and lowest

* Add figure test for `TiledDataset.plot()`

* Update dkist/dataset/tiled_dataset.py

Co-authored-by: Stuart Mumford <[email protected]>

* Add filter to tar file extraction

This is to avoid a deprecation warning when it's not specified
explicitly, but is just using the default anyway.

* Use sample data for TiledDataset plot test and update hashes

* Add changelog

* Try downloading data before tests instead of during

* Make sure we're not running figure tests outside the figure test env

* Move sample data import into figure test

* Yeah still need this

* Explicit is better than implicit

* Maybe this will help

* Rebin tileddataset for plot test to reduce memory usage

* Try more memory again and smaller plots

* Looks like we don't actually need this after all?

* Update dkist/dataset/tests/test_tiled_dataset.py

Co-authored-by: Stuart Mumford <[email protected]>

* Update dkist/dataset/tests/test_tiled_dataset.py

Co-authored-by: Stuart Mumford <[email protected]>

* Resinstate codecov upload in figure test

* Update scale arg name to zscale

* Update figure hashes

* Add codecov_upload script for circle CI

Not sure how this wasn't already here

* Update TiledDataset plot title

---------

Co-authored-by: Stuart Mumford <[email protected]>
  • Loading branch information
SolarDrew and Cadair authored Aug 7, 2024
1 parent 9bcf699 commit b56cc10
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .circleci/codecov_upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -e
curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import
curl -Os https://uploader.codecov.io/latest/linux/codecov
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
shasum -a 256 -c codecov.SHA256SUM
chmod +x codecov
./codecov "$@"
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
- run: *apt-install
- run: pip install --user -U tox tox-pypi-filter
- run: tox -v
# - run:
# name: Running codecov
# command: bash -e .circleci/codecov_upload.sh -f ".tmp/${TOXENV}/coverage.xml"
- run:
name: Running codecov
command: bash -e .circleci/codecov_upload.sh -f ".tmp/${TOXENV}/coverage.xml"
- store_artifacts:
path: .tmp/<< parameters.jobname >>/figure_test_images

Expand Down
1 change: 1 addition & 0 deletions changelog/408.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `TiledDataset.plot()` quicklook method.
2 changes: 1 addition & 1 deletion dkist/data/_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _download_and_extract_sample_data(names, overwrite, path):
for i, tarpath in enumerate(results):
output_path = path / file_folder[Path(tarpath).name]
with tarfile.open(tarpath, "r:*") as tar:
tar.extractall(path=output_path, filter="data")
tar.extractall(path=output_path, filter=tarfile.fully_trusted_filter)
results[i] = output_path

return results
Expand Down
18 changes: 17 additions & 1 deletion dkist/dataset/tests/test_tiled_dataset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import copy

import matplotlib.pyplot as plt
import numpy as np
import pytest

from dkist import Dataset, TiledDataset
from dkist import Dataset, TiledDataset, load_dataset
from dkist.tests.helpers import figure_test


def test_tiled_dataset(simple_tiled_dataset, dataset):
Expand Down Expand Up @@ -59,3 +61,17 @@ def test_tiled_dataset_from_components(dataset):
assert ds.files == fm
assert ds.meta["inventory"] is inventory
assert ds.meta["headers"] is headers


@figure_test
@pytest.mark.parametrize("share_zscale", [True, False], ids=["share_zscale", "indpendent_zscale"])
def test_tileddataset_plot(share_zscale):
from dkist.data.sample import VBI_AJQWW
ds = load_dataset(VBI_AJQWW)
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)
fig = plt.figure(figsize=(600, 800))
ds.plot(0, share_zscale=share_zscale)
return plt.gcf()
28 changes: 27 additions & 1 deletion dkist/dataset/tiled_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from collections.abc import Collection

import matplotlib.pyplot as plt
import numpy as np

from astropy.table import vstack
Expand Down Expand Up @@ -124,5 +125,30 @@ def shape(self):
"""
return self._data.shape

# TODO: def plot()
def plot(self, slice_index: int, share_zscale=False, **kwargs):
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)
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()
for coord in ax.coords:
if "b" in coord.axislabels.get_visible_axes():
fig.supxlabel(xlabel, y=0.05)
if "l" in coord.axislabels.get_visible_axes():
fig.supylabel(ylabel, x=0.05)
axmin, axmax = ax.get_images()[0].get_clim()
vmin = axmin if axmin < vmin else vmin
vmax = axmax if axmax > vmax else vmax
ax.set_ylabel(" ")
ax.set_xlabel(" ")
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)
return fig

# TODO: def regrid()
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"dkist.dataset.tests.test_plotting.test_2d_plot[aslice0]": "b31423d5ec45941849564f4ec7e276f2c52a0fa5038ce0b3c8d4af1b7f848a1d",
"dkist.dataset.tests.test_plotting.test_2d_plot[aslice1]": "cbb84fbae51d8238803f8f0d6820c575f024fe54b1656f1b181dc4ec645e9ff9",
"dkist.dataset.tests.test_plotting.test_2d_plot[aslice2]": "132c5615832daff457dacb4cb770498f1fbb4460a5b90b5d4d01d224c70eeb28",
"dkist.dataset.tests.test_plotting.test_2d_plot2": "409b5a10ad8ccf005331261505e63ce8febdc38eb8b5a34f8863e567e3cccb9c"
"dkist.dataset.tests.test_plotting.test_2d_plot2": "409b5a10ad8ccf005331261505e63ce8febdc38eb8b5a34f8863e567e3cccb9c",
"dkist.dataset.tests.test_tiled_dataset.test_tileddataset_plot[share_zscale]": "08d480bf75e3461a3eb09b0fb78ac9a7e88ac6d80194f15a58ef531c7fc2bb53",
"dkist.dataset.tests.test_tiled_dataset.test_tileddataset_plot[indpendent_zscale]": "4d767a558ab1e0bd1bfeac5b8311a236e0ddcee484ae2b49cf32ffa8f0a4a93a"
}
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ addopts =
--doctest-rst
-p no:unraisableexception
-p no:threadexception
-m "not mpl_image_compare"
mpl-results-path = figure_test_images
mpl-deterministic = true
filterwarnings =
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extras =
commands_pre =
oldestdeps: minimum_dependencies dkist --filename requirements-min.txt
oldestdeps: pip install -r requirements-min.txt cryptography<42 jsonschema==4.0.1
figure: python -c "from dkist.data.sample import download_all_sample_data; download_all_sample_data()"
pip freeze --all --no-input
commands =
figure: /bin/sh -c "mkdir -p ./figure_test_images; python -c 'import matplotlib as mpl; print(mpl.ft2font.__file__, mpl.ft2font.__freetype_version__, mpl.ft2font.__freetype_build_type__)' > ./figure_test_images/figure_version_info.txt"
Expand Down

0 comments on commit b56cc10

Please sign in to comment.