Coverage for tests/testSummaryMetrics.py : 23%

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 zip
2import matplotlib
3matplotlib.use("Agg")
4import numpy as np
5import healpy as hp
6import unittest
7import lsst.sims.maf.metrics as metrics
8import lsst.utils.tests
11class TestSummaryMetrics(unittest.TestCase):
13 def testTableFractionMetric(self):
14 """Test the table summary metric """
15 metricdata1 = np.arange(0, 1.5, .02)
16 metricdata = np.array(list(zip(metricdata1)), dtype=[('testdata', 'float')])
17 for nbins in [10, 20, 5]:
18 metric = metrics.TableFractionMetric('testdata', nbins=nbins)
19 table = metric.run(metricdata)
20 self.assertEqual(len(table), nbins+3)
21 self.assertEqual(table['value'][0], np.size(np.where(metricdata1 == 0)[0]))
22 self.assertEqual(table['value'][-1], np.size(np.where(metricdata1 > 1)[0]))
23 self.assertEqual(table['value'][-2], np.size(np.where(metricdata1 == 1)[0]))
24 self.assertEqual(table['value'].sum(), metricdata1.size)
26 def testIdentityMetric(self):
27 """Test identity metric."""
28 dv = np.arange(0, 10, .5)
29 dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')])
30 testmetric = metrics.IdentityMetric('testdata')
31 np.testing.assert_equal(testmetric.run(dv), dv['testdata'])
33 def testfONv(self):
34 """
35 Test the fONv metric.
36 """
37 nside = 128
38 npix = hp.nside2npix(nside)
39 names = ['metricdata']
40 types = [int]
41 data = np.zeros(npix, dtype=list(zip(names, types)))
42 data['metricdata'] += 826
43 metric = metrics.fONv(col='ack', nside=nside, Nvisit=825, Asky=18000.)
44 slicePoint = {'sid': 0}
45 result = metric.run(data, slicePoint)
46 # result is recarray with 'min' and 'median' number of visits
47 # over the Asky area.
48 # All pixels had 826 visits, so that is min and median here.
49 min_nvis = result['value'][np.where(result['name'] == 'MinNvis')]
50 median_nvis = result['value'][np.where(result['name'] == 'MedianNvis')]
51 self.assertEqual(min_nvis, 826)
52 self.assertEqual(median_nvis, 826)
53 # Now update so that 13k of sky is 826, rest 0.
54 deginsph = 41253
55 npix_nk = int(npix * (13000. / deginsph))
56 data['metricdata'] = 0
57 data['metricdata'][:npix_nk] = 826
58 result = metric.run(data, slicePoint)
59 min_nvis = result['value'][np.where(result['name'] == 'MinNvis')]
60 median_nvis = result['value'][np.where(result['name'] == 'MedianNvis')]
61 self.assertEqual(min_nvis, 0)
62 self.assertEqual(median_nvis, 826)
64 def testfOArea(self):
65 """Test fOArea metric."""
66 nside = 128
67 npix = hp.nside2npix(nside)
68 names = ['metricdata']
69 types = [int]
70 data = np.zeros(npix, dtype=list(zip(names, types)))
71 data['metricdata'] += 826
72 metric = metrics.fOArea(col='ack', nside=nside, Nvisit=825, Asky=18000.)
73 slicePoint = {'sid': 0}
74 result = metric.run(data, slicePoint)
75 # fOArea returns the area with at least Nvisits.
76 deginsph = 129600. / np.pi
77 np.testing.assert_almost_equal(result, deginsph)
78 data['metricdata'][:data.size // 2] = 0
79 result = metric.run(data, slicePoint)
80 np.testing.assert_almost_equal(result, deginsph / 2.)
82 def testNormalizeMetric(self):
83 """Test normalize metric."""
84 data = np.ones(10, dtype=list(zip(['testcol'], ['float'])))
85 metric = metrics.NormalizeMetric(col='testcol', normVal=5.5)
86 result = metric.run(data)
87 np.testing.assert_equal(result, np.ones(10, float)/5.5)
89 def testZeropointMetric(self):
90 """Test zeropoint metric."""
91 data = np.ones(10, dtype=list(zip(['testcol'], ['float'])))
92 metric = metrics.ZeropointMetric(col='testcol', zp=5.5)
93 result = metric.run(data)
94 np.testing.assert_equal(result, np.ones(10, float)+5.5)
96 def testTotalPowerMetric(self):
97 nside = 128
98 data = np.ones(12*nside**2, dtype=list(zip(['testcol'], ['float'])))
99 metric = metrics.TotalPowerMetric(col='testcol')
100 result = metric.run(data)
101 np.testing.assert_equal(result, 0.0)
104class TestMemory(lsst.utils.tests.MemoryTestCase):
105 pass
108def setup_module(module):
109 lsst.utils.tests.init()
112if __name__ == "__main__": 112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true
113 lsst.utils.tests.init()
114 unittest.main()