Coverage for tests/test_dynamicDetection.py: 35%

60 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-08 01:16 -0800

1import unittest 

2 

3import lsst.utils.tests 

4import numpy as np 

5from lsst.afw.geom import makeCdMatrix, makeSkyWcs 

6from lsst.afw.table import SourceTable 

7from lsst.geom import Box2I, Extent2I, Point2D, Point2I, SpherePoint, degrees 

8from lsst.meas.algorithms import DynamicDetectionTask 

9from lsst.meas.algorithms.testUtils import plantSources 

10 

11 

12class DynamicDetectionTest(lsst.utils.tests.TestCase): 

13 def setUp(self): 

14 xy0 = Point2I(12345, 67890) # xy0 for image 

15 dims = Extent2I(2345, 2345) # Dimensions of image 

16 box = Box2I(xy0, dims) # Bounding box of image 

17 sigma = 3.21 # PSF sigma 

18 buffer = 4.0 # Buffer for star centers around edge 

19 nSigmaForKernel = 5.0 # Number of PSF sigmas for kernel 

20 sky = 12345.6 # Sky level 

21 numStars = 100 # Number of stars 

22 noise = np.sqrt(sky)*np.pi*sigma**2 # Poisson noise per PSF 

23 faint = 1.0*noise # Faintest level for star fluxes 

24 bright = 100.0*noise # Brightest level for star fluxes 

25 starBox = Box2I(box) # Area on image in which we can put star centers 

26 starBox.grow(-int(buffer*sigma)) 

27 scale = 1.0e-5*degrees # Pixel scale 

28 

29 rng = np.random.Generator(np.random.MT19937(12345)) 

30 stars = [(xx, yy, ff, sigma) for xx, yy, ff in 

31 zip(rng.uniform(starBox.getMinX(), starBox.getMaxX(), numStars), 

32 rng.uniform(starBox.getMinY(), starBox.getMaxY(), numStars), 

33 np.linspace(faint, bright, numStars))] 

34 self.exposure = plantSources(box, 2*int(nSigmaForKernel*sigma) + 1, sky, stars, True) 

35 self.exposure.setWcs(makeSkyWcs(crpix=Point2D(0, 0), 

36 crval=SpherePoint(0, 0, degrees), 

37 cdMatrix=makeCdMatrix(scale=scale))) 

38 

39 self.config = DynamicDetectionTask.ConfigClass() 

40 self.rtol = 0.1 

41 

42 def tearDown(self): 

43 del self.exposure 

44 

45 def check(self, expectFactor): 

46 schema = SourceTable.makeMinimalSchema() 

47 task = DynamicDetectionTask(config=self.config, schema=schema) 

48 table = SourceTable.make(schema) 

49 

50 results = task.run(table, self.exposure, expId=12345) 

51 self.assertFloatsAlmostEqual(results.factor, expectFactor, rtol=self.rtol) 

52 

53 def testVanilla(self): 

54 """Dynamic detection used as normal detection.""" 

55 self.check(1.0) 

56 

57 def testDynamic(self): 

58 """Modify the variance plane, and see if the task is able to determine 

59 what we did. 

60 """ 

61 factor = 2.0 

62 self.exposure.maskedImage.variance /= factor 

63 self.check(1.0/np.sqrt(factor)) 

64 

65 def testNoWcs(self): 

66 """Check that dynamic detection runs when the exposure wcs is None.""" 

67 self.exposure.setWcs(None) 

68 self.check(1.0) 

69 

70 def testMinimalSkyObjects(self): 

71 """Check that dynamic detection runs when there are a relatively small 

72 number of sky objects. 

73 """ 

74 self.config.skyObjects.nSources = int(0.1 * self.config.skyObjects.nSources) 

75 self.check(1.0) 

76 

77 def testNoSkyObjects(self): 

78 """Check that dynamic detection runs when there are no sky objects. 

79 

80 Notes 

81 ----- 

82 This test, originally named 'testNoSources', was deactivated on 

83 DM-39809 because the dynamic detection task is not able to function 

84 without any sky objects. This test should be reactivated once DM-39826 

85 has been completed. 

86 """ 

87 pass 

88 

89 

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

91 pass 

92 

93 

94def setup_module(module): 

95 lsst.utils.tests.init() 

96 

97 

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

99 import sys 

100 setup_module(sys.modules['__main__']) 

101 unittest.main()