Skip to content

Commit

Permalink
Implement Butler.get for matplotlib generated png files
Browse files Browse the repository at this point in the history
  • Loading branch information
fred3m committed Aug 11, 2023
1 parent 42f2c38 commit 7a59242
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions python/lsst/daf/butler/configs/storageClasses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ storageClasses:
dict: lsst.utils.packages.Packages
NumpyArray:
pytype: numpy.ndarray
converters:
matplotlib.figure.Figure: lsst.daf.butler.formatters.matplotlib.MatplotlibFormatter.dummyConverter
Thumbnail:
pytype: numpy.ndarray
Plot:
pytype: matplotlib.figure.Figure
converters:
numpy.ndarray: lsst.daf.butler.formatters.matplotlib.MatplotlibFormatter.fromArray
MetricValue:
pytype: lsst.verify.Measurement
StampsBase:
Expand Down
20 changes: 19 additions & 1 deletion python/lsst/daf/butler/formatters/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

from .file import FileFormatter

import numpy as np
import matplotlib.pyplot as plt


class MatplotlibFormatter(FileFormatter):
"""Interface for writing matplotlib figures."""
Expand All @@ -38,8 +41,23 @@ class MatplotlibFormatter(FileFormatter):

def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
# docstring inherited from FileFormatter._readFile
raise NotImplementedError(f"matplotlib figures cannot be read by the butler; path is {path}")
return plt.imread(path)

def _writeFile(self, inMemoryDataset: Any) -> None:
# docstring inherited from FileFormatter._writeFile
inMemoryDataset.savefig(self.fileDescriptor.location.path)

@staticmethod
def fromArray(cls: np.ndarray) -> plt.Figure:
"""Convert an array into a Figure.
"""
fig = plt.figure()
plt.imshow(cls)
return fig

@staticmethod
def dummyCovnerter(cls):
"""This converter exists to trick the Butler into allowing
a numpy array on read with ``storageClass='NumpyArray'``.
"""
return cls
11 changes: 9 additions & 2 deletions tests/test_matplotlibFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import tempfile
import unittest
from random import Random
import numpy as np

try:
import matplotlib
Expand All @@ -39,6 +40,7 @@

from lsst.daf.butler import Butler, DatasetType
from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir
from lsst.daf.butler.formatters.matplotlib import MatplotlibFormatter

TESTDIR = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -78,8 +80,13 @@ def testMatplotlibFormatter(self):
pyplot.gcf().savefig(file.name)
self.assertTrue(filecmp.cmp(local.ospath, file.name, shallow=True))
self.assertTrue(butler.exists(ref))
with self.assertRaises(ValueError):
butler.get(ref)

fig = butler.get(ref)
# Ensure that the result is a figure
self.assertTrue(isinstance(fig, pyplot.Figure))
image = butler.get(ref, storageClass="NumpyArray")
self.assertTrue(isinstance(image, np.ndarray))

butler.pruneDatasets([ref], unstore=True, purge=True)
self.assertFalse(butler.exists(ref))

Expand Down

0 comments on commit 7a59242

Please sign in to comment.