Coverage for tests/testResultsDb.py : 24%

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
1from builtins import str
2from builtins import range
3import matplotlib
4matplotlib.use("Agg")
5import os
6import warnings
7import unittest
8import numpy as np
9import lsst.sims.maf.db as db
10import shutil
11import tempfile
12import lsst.utils.tests
15class TestResultsDb(unittest.TestCase):
17 def setUp(self):
18 self.outDir = 'Out'
19 self.metricName = 'Count ExpMJD'
20 self.slicerName = 'OneDSlicer'
21 self.runName = 'fakeopsim'
22 self.constraint = ''
23 self.metadata = 'Dithered'
24 self.metricDataFile = 'testmetricdatafile.npz'
25 self.plotType = 'BinnedData'
26 self.plotName = 'testmetricplot_BinnedData.png'
27 self.summaryStatName1 = 'Mean'
28 self.summaryStatValue1 = 20
29 self.summaryStatName2 = 'Median'
30 self.summaryStatValue2 = 18
31 self.summaryStatName3 = 'TableFrac'
32 self.summaryStatValue3 = np.empty(10, dtype=[('name', '|S12'), ('value', float)])
33 for i in range(10):
34 self.summaryStatValue3['name'] = 'test%d' % (i)
35 self.summaryStatValue3['value'] = i
36 self.displayDict = {'group': 'seeing', 'subgroup': 'all', 'order': 1, 'caption': 'lalalalal'}
38 def testDbCreation(self):
39 # Test default sqlite file created.
40 tempdir = tempfile.mkdtemp(prefix='resDb')
41 resultsdb = db.ResultsDb(outDir=tempdir)
42 self.assertTrue(os.path.isfile(os.path.join(tempdir, 'resultsDb_sqlite.db')))
43 resultsdb.close()
44 # Test that get appropriate exception if directory doesn't exist.
45 sqlitefilename = os.path.join(self.outDir + 'test', 'testDb_sqlite.db')
46 self.assertRaises(ValueError, db.ResultsDb, database=sqlitefilename)
47 shutil.rmtree(tempdir)
49 def testAddData(self):
50 tempdir = tempfile.mkdtemp(prefix='resDb')
51 resultsDb = db.ResultsDb(outDir=tempdir)
52 # Add metric.
53 metricId = resultsDb.updateMetric(self.metricName, self.slicerName,
54 self.runName, self.constraint,
55 self.metadata, self.metricDataFile)
56 # Try to re-add metric (should get back same metric id as previous, with no add).
57 metricId2 = resultsDb.updateMetric(self.metricName, self.slicerName,
58 self.runName, self.constraint,
59 self.metadata, self.metricDataFile)
60 self.assertEqual(metricId, metricId2)
61 run1 = resultsDb.session.query(db.MetricRow).filter_by(metricId=metricId).all()
62 self.assertEqual(len(run1), 1)
63 # Add plot.
64 resultsDb.updatePlot(metricId, self.plotType, self.plotName)
65 # Add normal summary statistics.
66 resultsDb.updateSummaryStat(metricId, self.summaryStatName1, self.summaryStatValue1)
67 resultsDb.updateSummaryStat(metricId, self.summaryStatName2, self.summaryStatValue2)
68 # Add something like tableFrac summary statistic.
69 resultsDb.updateSummaryStat(metricId, self.summaryStatName3, self.summaryStatValue3)
70 # Test get warning when try to add a non-conforming summary stat (not 'name' & 'value' cols).
71 teststat = np.empty(10, dtype=[('col', '|S12'), ('value', float)])
72 with warnings.catch_warnings(record=True) as w:
73 warnings.simplefilter("always")
74 resultsDb.updateSummaryStat(metricId, 'testfail', teststat)
75 self.assertIn("not save", str(w[-1].message))
76 # Test get warning when try to add a string (non-conforming) summary stat.
77 teststat = 'teststring'
78 with warnings.catch_warnings(record=True) as w:
79 warnings.simplefilter("always")
80 resultsDb.updateSummaryStat(metricId, 'testfail', teststat)
81 self.assertIn("not save", str(w[-1].message))
82 shutil.rmtree(tempdir)
85class TestUseResultsDb(unittest.TestCase):
87 def setUp(self):
88 self.outDir = 'Out'
89 self.metricName = 'Count ExpMJD'
90 self.slicerName = 'OneDSlicer'
91 self.runName = 'fakeopsim'
92 self.constraint = ''
93 self.metadata = 'Dithered'
94 self.metricDataFile = 'testmetricdatafile.npz'
95 self.plotType = 'BinnedData'
96 self.plotName = 'testmetricplot_BinnedData.png'
97 self.summaryStatName1 = 'Mean'
98 self.summaryStatValue1 = 20
99 self.summaryStatName2 = 'Median'
100 self.summaryStatValue2 = 18
101 self.tempdir = tempfile.mkdtemp(prefix='resDb')
102 self.resultsDb = db.ResultsDb(self.tempdir)
103 self.metricId = self.resultsDb.updateMetric(self.metricName, self.slicerName,
104 self.runName, self.constraint,
105 self.metadata, self.metricDataFile)
106 self.resultsDb.updatePlot(self.metricId, self.plotType, self.plotName)
107 self.resultsDb.updateSummaryStat(self.metricId, self.summaryStatName1, self.summaryStatValue1)
108 self.resultsDb.updateSummaryStat(self.metricId, self.summaryStatName2, self.summaryStatValue2)
110 def testgetIds(self):
111 mids = self.resultsDb.getAllMetricIds()
112 self.assertEqual(mids[0], self.metricId)
113 mid = self.resultsDb.getMetricId(self.metricName)
114 self.assertEqual(mid[0], self.metricId)
115 mid = self.resultsDb.getMetricId('notreal')
116 self.assertEqual(len(mid), 0)
118 def testshowSummary(self):
119 self.resultsDb.getSummaryStats()
121 def tearDown(self):
122 self.resultsDb.close()
123 shutil.rmtree(self.tempdir)
126class TestMemory(lsst.utils.tests.MemoryTestCase):
127 pass
130def setup_module(module):
131 lsst.utils.tests.init()
134if __name__ == "__main__": 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true
135 lsst.utils.tests.init()
136 unittest.main()