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

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

# This file is part of meas_algorithms. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import os 

import unittest 

import numpy as np 

from scipy import signal 

import astropy.units as u 

 

import lsst.meas.algorithms as algorithms 

import lsst.utils.tests 

from lsst.geom import Point2I, Point2D, Box2I, Extent2I 

import lsst.afw.cameraGeom.utils as cgUtils 

 

TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

 

 

class MockAmp: 

def __init__(self, name, bbox): 

self.name = name 

self.box = bbox 

 

def getName(self): 

return self.name 

 

def getBBox(self): 

return self.box 

 

 

class CurveTestCase(lsst.utils.tests.TestCase): 

"""Tests for the Curve class""" 

 

def setUp(self): 

self.wavelength = np.linspace(3000, 5000, 150)*u.angstrom 

self.efficiency = signal.gaussian(len(self.wavelength), std=100)*u.percent 

self.metadata = dict([('MODE', 'AMP'), ('TYPE', 'QE'), ('CALIBDATE', '1970-01-01T00:00:00'), 

('INSTRUME', 'ts8'), ('OBSTYPE', 'qe_curve'), ('DETECTOR', 99), 

('DATE', '2019-09-27T22:15:13.518320'), ('CALIB_CREATION_DATE', '2019-09-27'), 

('CALIB_CREATION_TIME', '22:15:13')]) 

 

def tearDown(self): 

del self.wavelength 

del self.efficiency 

del self.metadata 

 

def curve_tester(self, curve_class, args): 

curve = curve_class(*args) 

 

# Serialization round trip 

table = curve.toTable() 

curve2 = curve_class.fromTable(table) 

self.assertEqual(curve, curve2) 

 

# via FITS 

with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

curve.writeFits(tmpFile) 

curve2 = algorithms.Curve.readFits(tmpFile) 

 

self.assertEqual(curve2, curve) 

 

# via text file 

with lsst.utils.tests.getTempFilePath(".ecsv") as tmpFile: 

curve.writeText(tmpFile) 

curve2 = algorithms.Curve.readText(tmpFile) 

 

self.assertEqual(curve2, curve) 

 

# Check bad values 

with self.assertRaises(ValueError): 

# test that raised when non quantities are passed 

nargs = [] 

for arg in args: 

if hasattr(arg, 'unit'): 

nargs.append(arg.value) 

else: 

nargs.append(arg) 

_ = curve_class(*nargs) 

 

# Check bad values 

with self.assertRaises(ValueError): 

# test that raised when non-length quantities are passed 

nargs = [] 

for arg in args: 

if hasattr(arg, 'unit'): 

nargs.append(arg.value*u.amp) 

else: 

nargs.append(arg) 

_ = curve_class(*nargs) 

 

def interp_tester(self, curve_class, args, detector): 

curve = curve_class(*args) 

w = 3500*u.angstrom 

xs = np.linspace(0, 1023, 33) 

ys = np.linspace(0, 1023, 33) 

val_map = {'A': 0.9329662, 'B': 0.7463730} 

# Does interpolation work 

for x, y in zip(xs, ys): 

point = Point2D(x, y) 

if detector: 

amp = cgUtils.findAmp(detector, Point2I(point)) 

value = val_map[amp.getName()] 

else: 

value = 0.9329662 

interp_val = curve.evaluate(detector, point, w) 

self.assertAlmostEqual(interp_val.value, value, places=5) 

self.assertEqual(interp_val.unit, u.percent) 

# Does interpolation work with arrays 

w_arr = np.linspace(320, 430, 70)*u.nm 

out_arr = curve.evaluate(detector, point, w_arr) 

self.assertEqual(len(w_arr), len(out_arr)) 

# Does interpolation with different units work as expected 

point = Point2D(500., 500.) 

val1 = curve.evaluate(detector, point, w) 

new_w = w.to(u.mm) 

val2 = curve.evaluate(detector, point, new_w) 

self.assertEqual(val1.value, val2.value) 

# interpolation with non-quantity should raise 

with self.assertRaises(ValueError): 

interp_val = curve.evaluate(detector, point, w.value) 

# Does out of band interpolation do something reasonable 

with self.assertRaises(ValueError): 

w = 0.*u.angstrom 

interp_val = curve.evaluate(detector, point, w) 

# Does interpolation fail with non-length unit 

with self.assertRaises(ValueError): 

w = 0.*u.Kelvin 

interp_val = curve.evaluate(detector, point, w) 

 

def test_detector_curve(self): 

args = (self.wavelength, self.efficiency, self.metadata) 

self.curve_tester(algorithms.DetectorCurve, args) 

self.interp_tester(algorithms.DetectorCurve, args, None) 

 

def test_amp_curve(self): 

# Future versions of astropy will pass unit through concatenation 

amp_wavelength = np.concatenate([self.wavelength.value, self.wavelength.value])*u.angstrom # Two amps 

amp_efficiency = np.concatenate([self.efficiency.value, 

self.efficiency.value*0.8])*u.percent # Two amps 

amp_name = np.concatenate([['A' for el in self.wavelength], ['B' for el in self.wavelength]]) 

amplist = [MockAmp('A', Box2I(Point2I(0, 0), Extent2I(512, 1025))), 

MockAmp('B', Box2I(Point2I(512, 10), Extent2I(512, 1024)))] 

args = (amp_name, amp_wavelength, amp_efficiency, self.metadata) 

self.curve_tester(algorithms.AmpCurve, args) 

self.interp_tester(algorithms.AmpCurve, args, amplist) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()