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# LSST Data Management System 

2# Copyright 2017 AURA/LSST. 

3# 

4# This product includes software developed by the 

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

6# 

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

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

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

10# (at your option) any later version. 

11# 

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

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

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

15# GNU General Public License for more details. 

16# 

17# You should have received a copy of the LSST License Statement and 

18# the GNU General Public License along with this program. If not, 

19# see <https://www.lsstcorp.org/LegalNotices/>. 

20 

21 

22import numpy as np 

23import os 

24import unittest 

25 

26import lsst.utils.tests 

27from lsst.validate.drp.photerrmodel import photErrModel, fitPhotErrModel 

28 

29 

30class PhotErrCase(lsst.utils.tests.TestCase): 

31 """Testing photometric error model fitting performance and failure modes.""" 

32 def setUp(self): 

33 self.sigmaSys, self.gamma, self.m5 = 0.01, 0.039, 24.35 # mag, '', mag 

34 

35 # Set seed for repeatibility 

36 np.random.seed(96701237) 

37 m = np.random.randn(1000)*2 + 25 

38 self.mag = m[m < 25] 

39 self.mag_err = photErrModel( 

40 self.mag, self.sigmaSys, self.gamma, self.m5) 

41 

42 # Resample mag 

43 # Set seed for repeatibility. 

44 # We explicityly reset it here to a known value 

45 # in case `photErrModel` called np.random 

46 np.random.seed(23987) 

47 self.noisy_mag = self.mag + np.random.randn(len(self.mag_err))*self.mag_err 

48 

49 def test_perfect_fit_phot_error_model(self): 

50 """Does a simple fit to a small, perfect, data set work?""" 

51 fit_results = fitPhotErrModel(self.mag, self.mag_err) 

52 

53 self.assertFloatsAlmostEqual( 

54 fit_results['sigmaSys'].value, self.sigmaSys, atol=1e-7) 

55 self.assertFloatsAlmostEqual( 

56 fit_results['gamma'].value, self.gamma, atol=1e-7) 

57 self.assertFloatsAlmostEqual( 

58 fit_results['m5'].value, self.m5, atol=1e-7) 

59 

60 def test_noisy_fit_phot_error_model(self): 

61 """Does a simple fit to a small, perfect, data set work?""" 

62 fit_results = fitPhotErrModel(self.noisy_mag, self.mag_err) 

63 

64 # Different absolute tolerances because we expect some variation in the 

65 # fit results to the hnoisy data and that variation is different 

66 # for different parameters 

67 self.assertFloatsAlmostEqual( 

68 fit_results['sigmaSys'].value, self.sigmaSys, atol=1e-2) 

69 self.assertFloatsAlmostEqual( 

70 fit_results['gamma'].value, self.gamma, atol=2e-2) 

71 self.assertFloatsAlmostEqual( 

72 fit_results['m5'].value, self.m5, atol=0.2) 

73 

74 def test_failed_fit_phot_error_model(self): 

75 """Does a failed fit recover and return NaN?""" 

76 testDir = os.path.dirname(__file__) 

77 failing_datafile = os.path.join(testDir, 'mag_magerr_bright.dat') 

78 failing_mag, failing_mag_err = np.loadtxt(failing_datafile) 

79 

80 fit_results = fitPhotErrModel(failing_mag, failing_mag_err) 

81 

82 self.assertTrue(np.isnan(fit_results['sigmaSys'].value)) 

83 self.assertTrue(np.isnan(fit_results['gamma'].value)) 

84 self.assertTrue(np.isnan(fit_results['m5'].value)) 

85 

86 

87class MemoryTester(lsst.utils.tests.MemoryTestCase): 

88 pass 

89 

90 

91def setup_module(module): 

92 lsst.utils.tests.init() 

93 

94 

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

96 lsst.utils.tests.init() 

97 unittest.main()