File tree Expand file tree Collapse file tree 4 files changed +34
-3
lines changed Expand file tree Collapse file tree 4 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ ignore_missing_imports = True
18
18
[mypy-numpy.*]
19
19
ignore_missing_imports = True
20
20
21
+ [mypy-matplotlib.*]
22
+ ignore_missing_imports = True
23
+
21
24
[mypy-pyarrow.*]
22
25
ignore_missing_imports = True
23
26
Original file line number Diff line number Diff line change @@ -342,10 +342,14 @@ storageClasses:
342
342
dict : lsst.utils.packages.Packages
343
343
NumpyArray :
344
344
pytype : numpy.ndarray
345
+ converters :
346
+ matplotlib.figure.Figure : lsst.daf.butler.formatters.matplotlib.MatplotlibFormatter.dummyConverter
345
347
Thumbnail :
346
348
pytype : numpy.ndarray
347
349
Plot :
348
350
pytype : matplotlib.figure.Figure
351
+ converters :
352
+ numpy.ndarray : lsst.daf.butler.formatters.matplotlib.MatplotlibFormatter.fromArray
349
353
MetricValue :
350
354
pytype : lsst.verify.Measurement
351
355
StampsBase :
Original file line number Diff line number Diff line change 27
27
28
28
from typing import Any
29
29
30
+ import matplotlib .pyplot as plt
31
+ import numpy as np
32
+
30
33
from .file import FileFormatter
31
34
32
35
@@ -38,8 +41,22 @@ class MatplotlibFormatter(FileFormatter):
38
41
39
42
def _readFile (self , path : str , pytype : type [Any ] | None = None ) -> Any :
40
43
# docstring inherited from FileFormatter._readFile
41
- raise NotImplementedError ( f"matplotlib figures cannot be read by the butler; path is { path } " )
44
+ return plt . imread ( path )
42
45
43
46
def _writeFile (self , inMemoryDataset : Any ) -> None :
44
47
# docstring inherited from FileFormatter._writeFile
45
48
inMemoryDataset .savefig (self .fileDescriptor .location .path )
49
+
50
+ @staticmethod
51
+ def fromArray (cls : np .ndarray ) -> plt .Figure :
52
+ """Convert an array into a Figure."""
53
+ fig = plt .figure ()
54
+ plt .imshow (cls )
55
+ return fig
56
+
57
+ @staticmethod
58
+ def dummyCovnerter (cls : np .ndarray ) -> np .ndarray :
59
+ """This converter exists to trick the Butler into allowing
60
+ a numpy array on read with ``storageClass='NumpyArray'``.
61
+ """
62
+ return cls
Original file line number Diff line number Diff line change 27
27
import unittest
28
28
from random import Random
29
29
30
+ import numpy as np
31
+
30
32
try :
31
33
import matplotlib
32
34
@@ -78,8 +80,13 @@ def testMatplotlibFormatter(self):
78
80
pyplot .gcf ().savefig (file .name )
79
81
self .assertTrue (filecmp .cmp (local .ospath , file .name , shallow = True ))
80
82
self .assertTrue (butler .exists (ref ))
81
- with self .assertRaises (ValueError ):
82
- butler .get (ref )
83
+
84
+ fig = butler .get (ref )
85
+ # Ensure that the result is a figure
86
+ self .assertTrue (isinstance (fig , pyplot .Figure ))
87
+ image = butler .get (ref , storageClass = "NumpyArray" )
88
+ self .assertTrue (isinstance (image , np .ndarray ))
89
+
83
90
butler .pruneDatasets ([ref ], unstore = True , purge = True )
84
91
self .assertFalse (butler .exists (ref ))
85
92
You can’t perform that action at this time.
0 commit comments