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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

from builtins import zip 

import matplotlib 

matplotlib.use("Agg") 

import numpy as np 

import healpy as hp 

import unittest 

import lsst.sims.maf.metrics as metrics 

import lsst.utils.tests 

 

 

class TestSummaryMetrics(unittest.TestCase): 

 

def testTableFractionMetric(self): 

"""Test the table summary metric """ 

metricdata1 = np.arange(0, 1.5, .02) 

metricdata = np.array(list(zip(metricdata1)), dtype=[('testdata', 'float')]) 

for nbins in [10, 20, 5]: 

metric = metrics.TableFractionMetric('testdata', nbins=nbins) 

table = metric.run(metricdata) 

self.assertEqual(len(table), nbins+3) 

self.assertEqual(table['value'][0], np.size(np.where(metricdata1 == 0)[0])) 

self.assertEqual(table['value'][-1], np.size(np.where(metricdata1 > 1)[0])) 

self.assertEqual(table['value'][-2], np.size(np.where(metricdata1 == 1)[0])) 

self.assertEqual(table['value'].sum(), metricdata1.size) 

 

def testIdentityMetric(self): 

"""Test identity metric.""" 

dv = np.arange(0, 10, .5) 

dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')]) 

testmetric = metrics.IdentityMetric('testdata') 

np.testing.assert_equal(testmetric.run(dv), dv['testdata']) 

 

def testfONv(self): 

""" 

Test the fONv metric. 

""" 

nside = 128 

npix = hp.nside2npix(nside) 

names = ['metricdata'] 

types = [int] 

data = np.zeros(npix, dtype=list(zip(names, types))) 

data['metricdata'] += 826 

metric = metrics.fONv(col='ack', nside=nside, Nvisit=825, Asky=18000.) 

slicePoint = {'sid': 0} 

result = metric.run(data, slicePoint) 

# result is recarray with 'min' and 'median' number of visits 

# over the Asky area. 

# All pixels had 826 visits, so that is min and median here. 

min_nvis = result['value'][np.where(result['name'] == 'MinNvis')] 

median_nvis = result['value'][np.where(result['name'] == 'MedianNvis')] 

self.assertEqual(min_nvis, 826) 

self.assertEqual(median_nvis, 826) 

# Now update so that 13k of sky is 826, rest 0. 

deginsph = 41253 

npix_nk = np.int(npix * (13000. / deginsph)) 

data['metricdata'] = 0 

data['metricdata'][:npix_nk] = 826 

result = metric.run(data, slicePoint) 

min_nvis = result['value'][np.where(result['name'] == 'MinNvis')] 

median_nvis = result['value'][np.where(result['name'] == 'MedianNvis')] 

self.assertEqual(min_nvis, 0) 

self.assertEqual(median_nvis, 826) 

 

def testfOArea(self): 

"""Test fOArea metric.""" 

nside = 128 

npix = hp.nside2npix(nside) 

names = ['metricdata'] 

types = [int] 

data = np.zeros(npix, dtype=list(zip(names, types))) 

data['metricdata'] += 826 

metric = metrics.fOArea(col='ack', nside=nside, Nvisit=825, Asky=18000.) 

slicePoint = {'sid': 0} 

result = metric.run(data, slicePoint) 

# fOArea returns the area with at least Nvisits. 

deginsph = 129600. / np.pi 

np.testing.assert_almost_equal(result, deginsph) 

data['metricdata'][:data.size // 2] = 0 

result = metric.run(data, slicePoint) 

np.testing.assert_almost_equal(result, deginsph / 2.) 

 

def testNormalizeMetric(self): 

"""Test normalize metric.""" 

data = np.ones(10, dtype=list(zip(['testcol'], ['float']))) 

metric = metrics.NormalizeMetric(col='testcol', normVal=5.5) 

result = metric.run(data) 

np.testing.assert_equal(result, np.ones(10, float)/5.5) 

 

def testZeropointMetric(self): 

"""Test zeropoint metric.""" 

data = np.ones(10, dtype=list(zip(['testcol'], ['float']))) 

metric = metrics.ZeropointMetric(col='testcol', zp=5.5) 

result = metric.run(data) 

np.testing.assert_equal(result, np.ones(10, float)+5.5) 

 

def testTotalPowerMetric(self): 

nside = 128 

data = np.ones(12*nside**2, dtype=list(zip(['testcol'], ['float']))) 

metric = metrics.TotalPowerMetric(col='testcol') 

result = metric.run(data) 

np.testing.assert_equal(result, 0.0) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()