Coverage for tests/test_io_persistence.py: 92%

38 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-14 08:07 +0000

1# This file is part of meas_extensions_scarlet. 

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"""Butler persistence tests for ``LsstScarletModelData``.""" 

23 

24 

25import os 

26import tempfile 

27import unittest 

28 

29import lsst.meas.extensions.scarlet as mes 

30import lsst.utils.tests 

31from lsst.daf.butler import ( 

32 Config, 

33 DatasetRef, 

34 DatasetType, 

35 FileDataset, 

36 StorageClass, 

37) 

38from lsst.daf.butler.tests import makeTestCollection, makeTestRepo 

39 

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

41 

42 

43class TestIoPersistence(lsst.utils.tests.TestCase): 

44 """Butler put/get and legacy-model tests for 

45 ``LsstScarletModelData`` storage in 

46 ``lsst.meas.extensions.scarlet.io``. 

47 """ 

48 

49 def test_lsst_scarlet_model_conversion(self): 

50 """Test converting an LsstScarletModelData to a ScarletModelData 

51 (lossily) via the Butler. 

52 """ 

53 repo = self._setup_butler() 

54 newDatasetType = DatasetType( 

55 "new_scarlet_model_data", 

56 dimensions=(), 

57 storageClass="LsstScarletModelData", 

58 universe=repo.dimensions, 

59 ) 

60 ref = DatasetRef( 

61 newDatasetType, 

62 run="test_ingestion", 

63 dataId={}, 

64 ) 

65 dataset = FileDataset( 

66 path=os.path.join(TESTDIR, "data", "v29_models.json"), 

67 formatter="lsst.daf.butler.formatters.json.JsonFormatter", 

68 refs=[ref], 

69 ) 

70 butler = makeTestCollection(repo, uniqueId="ingestion") 

71 repo.registry.registerDatasetType(newDatasetType) 

72 butler.ingest(dataset) 

73 model = butler.get("new_scarlet_model_data", dataId={}, storageClass="ScarletModelData") 

74 self.assertIsInstance(model, lsst.scarlet.lite.io.ScarletModelData) 

75 self.assertEqual(len(model.blends), 2) 

76 

77 def _setup_butler(self): 

78 # Initialize a Butler to test persistence 

79 repo_dir = tempfile.TemporaryDirectory(ignore_cleanup_errors=True) 

80 self.repo_dir = repo_dir.name 

81 self.addCleanup(tempfile.TemporaryDirectory.cleanup, repo_dir) 

82 config = Config() 

83 config["datastore", "cls"] = "lsst.daf.butler.datastores.fileDatastore.FileDatastore" 

84 repo = makeTestRepo(repo_dir.name, config=config) 

85 storageClass = StorageClass( 

86 "LsstScarletModelData", 

87 pytype=mes.io.LsstScarletModelData, 

88 parameters=('blend_id',), 

89 delegate="lsst.meas.extensions.scarlet.io.ScarletModelDelegate", 

90 ) 

91 datasetType = DatasetType( 

92 "scarlet_model_data", 

93 dimensions=(), 

94 storageClass=storageClass, 

95 universe=repo.dimensions, 

96 ) 

97 repo.registry.registerDatasetType(datasetType) 

98 return repo 

99 

100 

101def setup_module(module): 

102 lsst.utils.tests.init() 

103 

104 

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

106 pass 

107 

108 

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

110 lsst.utils.tests.init() 

111 unittest.main()