Coverage for tests/test_ticket2026.py: 18%

92 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-23 02:38 -0700

1import unittest 

2 

3import lsst.utils.tests 

4import lsst.afw.table as afwTable 

5 

6# Subtract 1 so that ids == indices 

7 

8 

9def getids(c): 

10 return [s.getId()-1 for s in c] 

11 

12 

13def printids(c): 

14 print(getids(c)) 

15 

16 

17class IndexingCatalogTestCase(unittest.TestCase): 

18 

19 def testSimpleCatalogType(self): 

20 schema = afwTable.SourceTable.makeMinimalSchema() 

21 table = afwTable.SourceTable.make(schema) 

22 catalog = afwTable.SourceCatalog(table) 

23 catalog.addNew() 

24 catcopy = catalog.copy() 

25 catsub = catalog[:] 

26 catsub2 = catalog.subset(0, 1, 1) 

27 print('catalog', catalog) 

28 print('catcopy', catcopy) 

29 print('catsub', catsub) 

30 print('catsub2', catsub2) 

31 self.assertEqual(type(catalog), type(catcopy)) 

32 self.assertEqual(type(catalog), type(catsub)) 

33 self.assertEqual(type(catalog), type(catsub2)) 

34 

35 def testMinusOne(self): 

36 schema = afwTable.SourceTable.makeMinimalSchema() 

37 table = afwTable.SourceTable.make(schema) 

38 catalog = afwTable.SourceCatalog(table) 

39 catalog.addNew() 

40 self.assertEqual(len(catalog), 1) 

41 catalog[-1] 

42 catalog.addNew() 

43 catalog.addNew() 

44 catalog[1] = catalog[2] 

45 del catalog[2] 

46 print(catalog) 

47 for src in catalog: 

48 print(src.getId()) 

49 self.assertEqual(len(catalog), 2) 

50 self.assertEqual(catalog[0].getId(), 1) 

51 self.assertEqual(catalog[1].getId(), 3) 

52 self.assertEqual(catalog[-1].getId(), 3) 

53 self.assertEqual(catalog[-2].getId(), 1) 

54 

55 def assertSlice(self, cat, start, stop, step=None): 

56 if step is None: 

57 c = cat[start:stop] 

58 tru = list(range(10))[start:stop] 

59 else: 

60 c = cat[start:stop:step] 

61 tru = list(range(10))[start:stop:step] 

62 printids(c) 

63 self.assertEqual(getids(c), tru) 

64 

65 def testSlice(self): 

66 schema = afwTable.SourceTable.makeMinimalSchema() 

67 table = afwTable.SourceTable.make(schema) 

68 catalog = afwTable.SourceCatalog(table) 

69 for i in range(10): 

70 catalog.addNew() 

71 print('Catalog:', printids(catalog)) 

72 print('Empty range (4,4)') 

73 self.assertSlice(catalog, 4, 4) 

74 print('Count by 2 (1,7,2)') 

75 self.assertSlice(catalog, 1, 7, 2) 

76 print('Normal range (4,7)') 

77 self.assertSlice(catalog, 4, 7) 

78 print('Normal range 2 (4,10)') 

79 self.assertSlice(catalog, 4, 10) 

80 print('Normal range 3 (4,15)') 

81 self.assertSlice(catalog, 4, 15) 

82 print('Negative indexed range (-20,-1)') 

83 self.assertSlice(catalog, -20, -1) 

84 print('Negative end (1,-3)') 

85 self.assertSlice(catalog, 1, -3) 

86 print('Negative step (6:1:-2)') 

87 self.assertSlice(catalog, 6, 1, -2) 

88 print('Negative step (6:0:-2)') 

89 self.assertSlice(catalog, 6, 0, -2) 

90 print('Negative step (-1:-12:-2)') 

91 self.assertSlice(catalog, -1, -12, -2) 

92 print('Negative step (6:0:-1)') 

93 self.assertSlice(catalog, 6, 0, -1) 

94 print('Negative step (6:-20:-1)') 

95 self.assertSlice(catalog, 6, -20, -1) 

96 print('Negative step (6:-20:-2)') 

97 self.assertSlice(catalog, 6, -20, -2) 

98 print('Negative step (5:-20:-2)') 

99 self.assertSlice(catalog, 5, -20, -2) 

100 

101 

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

103 pass 

104 

105 

106def setup_module(module): 

107 lsst.utils.tests.init() 

108 

109 

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

111 lsst.utils.tests.init() 

112 unittest.main()