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"""Tests for sims.utils.samplingFunctions.py 

2 

31. test_raiseWraparoundError: The `samplePatchOnSphere` function does not 

4 wrap around theta values near the pole. Check if the approporiate error is 

5 by such a call 

62. test_checkWithinBounds : Check that the samples are indeed within the bounds 

7prescribed by ObsMetaData 

83. test_samplePatchOnSphere : Check functionality by showing that binning up in 

9 dec results in numbers in dec bins changing with area. 

10 """ 

11from __future__ import division 

12 

13import numpy as np 

14import unittest 

15import lsst.utils.tests 

16 

17 

18from lsst.sims.utils import ObservationMetaData 

19from lsst.sims.utils import samplePatchOnSphere 

20from lsst.sims.utils import spatiallySample_obsmetadata 

21 

22 

23def setup_module(module): 

24 lsst.utils.tests.init() 

25 

26 

27class SamplingTests(unittest.TestCase): 

28 

29 @classmethod 

30 def setUpClass(cls): 

31 """ 

32 """ 

33 cls.obsMetaDataforCat = ObservationMetaData(boundType='circle', 

34 boundLength=np.degrees( 

35 0.25), 

36 pointingRA=np.degrees( 

37 0.13), 

38 pointingDec=np.degrees(-1.2), 

39 bandpassName=['r'], 

40 mjd=49350.) 

41 ObsMetaData = cls.obsMetaDataforCat 

42 cls.samples = spatiallySample_obsmetadata(ObsMetaData, size=1000) 

43 

44 cls.theta_c = -60. 

45 cls.phi_c = 30. 

46 cls.delta = 30. 

47 cls.size = 1000000 

48 

49 cls.dense_samples = samplePatchOnSphere(phi=cls.phi_c, theta=cls.theta_c, 

50 delta=cls.delta, size=cls.size, 

51 seed=42) 

52 

53 def test_raiseWraparoundError(self): 

54 """ 

55 Test that appropriate errors are raised when at the poles 

56 """ 

57 # thetamax to be exceeded 

58 deltamax = np.abs(self.theta_c - 0.05) 

59 # to be lower than thetamin 

60 deltamin = 2.67 

61 with self.assertRaises(ValueError): 

62 samplePatchOnSphere(phi=self.phi_c, theta=self.theta_c, 

63 delta=deltamax, size=self.size, seed=42) 

64 samplePatchOnSphere(phi=self.phi_c, theta=self.theta_c, 

65 delta=deltamin, size=self.size, seed=42) 

66 

67 def test_checkWithinBounds(self): 

68 

69 delta = self.obsMetaDataforCat.boundLength 

70 # delta = np.radians(delta) 

71 minPhi = 0.13 - delta 

72 maxPhi = 0.13 + delta 

73 minTheta = -1.2 - delta 

74 maxTheta = -1.2 + delta 

75 

76 self.assertTrue(all(np.radians(self.samples[0]) <= maxPhi), 

77 msg='samples are not <= maxPhi') 

78 self.assertTrue(all(np.radians(self.samples[0]) >= minPhi), 

79 msg='samples are not >= minPhi') 

80 self.assertTrue(all(np.radians(self.samples[1]) >= minTheta), 

81 msg='samples are not >= minTheta') 

82 self.assertTrue(all(np.radians(self.samples[1]) <= maxTheta), 

83 msg='samples are not <= maxTheta') 

84 

85 def test_samplePatchOnSphere(self): 

86 

87 def A(theta_min, theta_max): 

88 return np.sin(theta_max) - np.sin(theta_min) 

89 

90 theta_c = np.radians(self.theta_c) 

91 delta = np.radians(self.delta) 

92 

93 theta_min = theta_c - delta 

94 theta_max = theta_c + delta 

95 tvals = np.arange(theta_min, theta_max, 0.001) 

96 tvalsShifted = np.zeros(len(tvals)) 

97 tvalsShifted[:-1] = tvals[1:] 

98 

99 area = A(tvals, tvalsShifted) 

100 

101 binsize = np.unique(np.diff(tvals)) 

102 self.assertEqual(binsize.size, 1) 

103 normval = np.sum(area) * binsize[0] 

104 

105 theta_samps = np.radians(self.dense_samples[1]) 

106 binnedvals = np.histogram(theta_samps, bins=tvals[:-1], normed=True)[0] 

107 resids = area[:-2] / normval - binnedvals 

108 

109 fiveSigma = np.sqrt(binnedvals) * 5.0 

110 np.testing.assert_array_less(resids, fiveSigma) 

111 

112 

113class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

114 pass 

115 

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

117 lsst.utils.tests.init() 

118 unittest.main()