Coverage for tests / test_metadetection_shear.py: 34%

37 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-25 08:50 +0000

1# This file is part of drp_tasks. 

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 

22 

23import unittest 

24 

25import numpy as np 

26import pyarrow as pa 

27from felis.datamodel import Schema 

28 

29import lsst.utils.tests 

30from lsst.drp.tasks.metadetection_shear import MetadetectionShearTask 

31from lsst.resources import ResourcePath 

32 

33 

34class ShearObjectSchemaConsistencyTestCase(unittest.TestCase): 

35 """Verify that the ShearObject table in lsstcam.yaml is a subset of 

36 the schema produced by MetadetectionShearTask.make_metadetect_schema. 

37 """ 

38 

39 def assertFieldsEqual(self, field1, field2): 

40 """Assert that the two fields are identical. 

41 

42 Specifically, it checks that the following are identical: 

43 - name 

44 - type width (if not string) 

45 """ 

46 self.assertEqual(field1.name, field2.name) 

47 if field1.type != "string": 

48 self.assertEqual(field1.type.byte_width, field2.type.byte_width) 

49 

50 def _read_shearobject_columns_from_yaml(self) -> pa.Schema: 

51 dtypes = { 

52 "char": "U2", 

53 "int": np.int32, 

54 "uint": np.uint32, 

55 "float": np.float32, 

56 "double": np.float64, 

57 } 

58 # Load the lsstcam.yaml schema resource from sdm_schemas 

59 

60 resource = ResourcePath("resource://lsst.sdm.schemas/lsstcam.yaml") 

61 schema = Schema.from_uri(resource, context={"id_generation": True}) 

62 # Find ShearObject table and collect column names 

63 for table in schema.tables: 

64 if table.name == "ShearObject": 

65 cols = pa.schema( 

66 [ 

67 pa.field( 

68 col.name, 

69 pa.from_numpy_dtype(dtypes.get(col.datatype, col.datatype)), 

70 ) 

71 for col in table.columns 

72 ] 

73 ) 

74 return cols 

75 

76 def test_yaml_is_subset_of_generated_schema(self) -> None: 

77 sdm_schema = self._read_shearobject_columns_from_yaml() 

78 # Generate schema from task 

79 config = MetadetectionShearTask.ConfigClass() 

80 # Ensure defaults are applied (e.g., shear_bands) 

81 config.setDefaults() 

82 task_schema = MetadetectionShearTask.make_metadetect_schema(config) 

83 

84 missing_field_names = set(sdm_schema.names).difference(task_schema.names) 

85 self.assertFalse(missing_field_names, f"Missing fields in generated schema: {missing_field_names}") 

86 

87 for field in sdm_schema: 

88 with self.subTest(field_name=field.name): 

89 self.assertFieldsEqual(field, task_schema.field(field.name)) 

90 

91 

92def setup_module(module): 

93 lsst.utils.tests.init() 

94 

95 

96class MatchMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

97 pass 

98 

99 

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

101 lsst.utils.tests.init() 

102 unittest.main()