Coverage for tests/test_source.py: 13%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 03:40 -0700

1# This file is part of lsst.scarlet.lite. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

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

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

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

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

21 

22import numpy as np 

23from lsst.scarlet.lite import Box, Image, Source 

24from lsst.scarlet.lite.component import FactorizedComponent 

25from lsst.scarlet.lite.utils import integrated_circular_gaussian 

26from utils import ScarletTestCase 

27 

28 

29class TestSource(ScarletTestCase): 

30 def test_constructor(self): 

31 # Test empty source 

32 source = Source([]) 

33 

34 self.assertEqual(source.n_components, 0) 

35 self.assertIsNone(source.center) 

36 self.assertIsNone(source.source_center) 

37 self.assertTrue(source.is_null) 

38 self.assertBoxEqual(source.bbox, Box((0, 0))) 

39 self.assertTupleEqual(source.bands, ()) 

40 

41 # Test a source with a single component 

42 bands = tuple("grizy") 

43 center = (27, 32) 

44 morph1 = integrated_circular_gaussian(sigma=0.8).astype(np.float32) 

45 spectrum1 = np.arange(5).astype(np.float32) 

46 component_box1 = Box((15, 15), (20, 25)) 

47 components = [ 

48 FactorizedComponent( 

49 bands, 

50 spectrum1, 

51 morph1, 

52 component_box1, 

53 center, 

54 ), 

55 ] 

56 source = Source(components) 

57 self.assertEqual(source.n_components, 1) 

58 self.assertTupleEqual(source.center, center) 

59 self.assertTupleEqual(source.source_center, (7, 7)) 

60 self.assertFalse(source.is_null) 

61 self.assertBoxEqual(source.bbox, component_box1) 

62 self.assertTupleEqual(source.bands, bands) 

63 self.assertImageEqual( 

64 source.get_model(), 

65 Image( 

66 spectrum1[:, None, None] * morph1[None, :, :], 

67 yx0=component_box1.origin, 

68 bands=bands, 

69 ), 

70 ) 

71 self.assertIsNone(source.get_model(True)) 

72 self.assertEqual(source.get_model().dtype, np.float32) 

73 

74 # Test a source with multiple components 

75 morph2 = integrated_circular_gaussian(sigma=2.1).astype(np.float32) 

76 spectrum2 = np.arange(5)[::-1].astype(np.float32) 

77 component_box2 = Box((15, 15), (10, 35)) 

78 

79 components = [ 

80 FactorizedComponent( 

81 bands, 

82 spectrum1, 

83 morph1, 

84 component_box1, 

85 center, 

86 ), 

87 FactorizedComponent( 

88 bands, 

89 spectrum2, 

90 morph2, 

91 component_box2, 

92 center, 

93 ), 

94 ] 

95 source = Source(components) 

96 self.assertEqual(source.n_components, 2) 

97 self.assertTupleEqual(source.center, center) 

98 self.assertTupleEqual(source.source_center, (17, 7)) 

99 self.assertFalse(source.is_null) 

100 self.assertBoxEqual(source.bbox, Box((25, 25), (10, 25))) 

101 self.assertTupleEqual(source.bands, bands) 

102 self.assertEqual(str(source), "Source<2>") 

103 self.assertEqual(source.get_model().dtype, np.float32) 

104 

105 model = np.zeros((5, 25, 25), dtype=np.float32) 

106 model[:, 10:25, :15] = spectrum1[:, None, None] * morph1[None, :, :] 

107 model[:, :15, 10:25] += spectrum2[:, None, None] * morph2[None, :, :] 

108 model = Image(model, yx0=(10, 25), bands=tuple("grizy")) 

109 

110 self.assertImageEqual( 

111 source.get_model(), 

112 model, 

113 ) 

114 self.assertIsNone(source.get_model(True)) 

115 

116 source = Source([]) 

117 result = source.get_model() 

118 self.assertEqual(result, 0)