-
Notifications
You must be signed in to change notification settings - Fork 12
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
DM-22073: Add support for writing matplotlib figures #205
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ bin/* | |
.coverage | ||
.pytest_cache/ | ||
.mypy_cache/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Support for writing matplotlib figures.""" | ||
|
||
__all__ = ("MatplotlibFormatter",) | ||
|
||
from lsst.daf.butler.formatters.fileFormatter import FileFormatter | ||
|
||
|
||
class MatplotlibFormatter(FileFormatter): | ||
"""Interface for writing matplotlib figures. | ||
""" | ||
|
||
extension = ".png" | ||
"""Matplotlib figures are always written in PNG format.""" | ||
|
||
def _readFile(self, path, pytype=None): | ||
# docstring inherited from FileFormatter._readFile | ||
raise NotImplementedError( | ||
f"matplotlib figures cannot be read by the butler; path is {path}") | ||
|
||
def _writeFile(self, inMemoryDataset): | ||
# docstring inherited from FileFormatter._writeFile | ||
inMemoryDataset.savefig(self.fileDescriptor.location.path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Tests for MatplotlibFormatter. | ||
""" | ||
|
||
import unittest | ||
import tempfile | ||
import os | ||
import shutil | ||
|
||
try: | ||
import matplotlib | ||
matplotlib.use("Agg") # noqa:E402 | ||
from matplotlib import pyplot | ||
except ImportError: | ||
pyplot = None | ||
|
||
import numpy as np | ||
from lsst.daf.butler import Butler, DatasetType | ||
import filecmp | ||
import urllib | ||
|
||
TESTDIR = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
|
||
@unittest.skipIf(pyplot is None, | ||
"skipping test because matplotlib import failed") | ||
class MatplotlibFormatterTestCase(unittest.TestCase): | ||
"""Test for MatplotlibFormatter. | ||
""" | ||
|
||
def setUp(self): | ||
self.root = tempfile.mkdtemp(dir=TESTDIR) | ||
Butler.makeRepo(self.root) | ||
|
||
def tearDown(self): | ||
if os.path.exists(self.root): | ||
shutil.rmtree(self.root, ignore_errors=True) | ||
|
||
def testMatplotlibFormatter(self): | ||
butler = Butler(self.root, run="testrun") | ||
datasetType = DatasetType("test_plot", [], "Plot", | ||
universe=butler.registry.dimensions) | ||
butler.registry.registerDatasetType(datasetType) | ||
pyplot.imshow(np.random.randn(3, 4)) | ||
ref = butler.put(pyplot.gcf(), datasetType) | ||
parsed = urllib.parse.urlparse(butler.getUri(ref)) | ||
with tempfile.NamedTemporaryFile(suffix=".png") as file: | ||
pyplot.gcf().savefig(file.name) | ||
self.assertTrue( | ||
filecmp.cmp( | ||
parsed.path, | ||
file.name, | ||
shallow=True | ||
) | ||
) | ||
self.assertTrue(butler.datasetExists(ref)) | ||
with self.assertRaises(ValueError): | ||
butler.get(ref) | ||
butler.remove(ref) | ||
with self.assertRaises(LookupError): | ||
butler.datasetExists(ref) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add calls to
butler.datasetExists
andbutler.remove
so that we can check that these PNG images are being treated like a normal dataset. Also try to do abutler.get
and check that it fails withwith self.assertRaises
.