Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22"""Tests for MatplotlibFormatter. 

23""" 

24 

25import unittest 

26import tempfile 

27import os 

28import shutil 

29from random import Random 

30 

31try: 

32 import matplotlib 

33 matplotlib.use("Agg") # noqa:E402 

34 from matplotlib import pyplot 

35except ImportError: 

36 pyplot = None 

37 

38from lsst.daf.butler import Butler, DatasetType 

39import filecmp 

40 

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

42 

43 

44@unittest.skipIf(pyplot is None, 

45 "skipping test because matplotlib import failed") 

46class MatplotlibFormatterTestCase(unittest.TestCase): 

47 """Test for MatplotlibFormatter. 

48 """ 

49 RANDOM_SEED = 10 

50 

51 def setUp(self): 

52 self.root = tempfile.mkdtemp(dir=TESTDIR) 

53 Butler.makeRepo(self.root) 

54 # Create a random image for testing 

55 self.rng = Random(self.RANDOM_SEED) 

56 

57 def tearDown(self): 

58 if os.path.exists(self.root): 

59 shutil.rmtree(self.root, ignore_errors=True) 

60 

61 def testMatplotlibFormatter(self): 

62 butler = Butler(self.root, run="testrun") 

63 datasetType = DatasetType("test_plot", [], "Plot", 

64 universe=butler.registry.dimensions) 

65 butler.registry.registerDatasetType(datasetType) 

66 # Does not have to be a random image 

67 pyplot.imshow([self.rng.sample(range(50), 10), 

68 self.rng.sample(range(50), 10), 

69 self.rng.sample(range(50), 10), 

70 ]) 

71 ref = butler.put(pyplot.gcf(), datasetType) 

72 uri = butler.getURI(ref) 

73 

74 # Following test needs a local file 

75 with uri.as_local() as local: 

76 with tempfile.NamedTemporaryFile(suffix=".png") as file: 

77 pyplot.gcf().savefig(file.name) 

78 self.assertTrue( 

79 filecmp.cmp( 

80 local.ospath, 

81 file.name, 

82 shallow=True 

83 ) 

84 ) 

85 self.assertTrue(butler.datasetExists(ref)) 

86 with self.assertRaises(ValueError): 

87 butler.get(ref) 

88 butler.pruneDatasets([ref], unstore=True, purge=True) 

89 with self.assertRaises(LookupError): 

90 butler.datasetExists(ref) 

91 

92 

93if __name__ == "__main__": 93 ↛ 94line 93 didn't jump to line 94, because the condition on line 93 was never true

94 unittest.main()