Coverage for tests/test_tableIO.py: 20%
69 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 03:31 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 03:31 -0700
1#
2# LSST Data Management System
3# Copyright 2016 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Soeftware Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23import unittest
25import numpy as np
26import astropy.io.fits
28import lsst.utils.tests
29import lsst.geom
30import lsst.afw.table
31import lsst.afw.image
34class TableIoTestCase(lsst.utils.tests.TestCase):
36 def testAngleUnitWriting(self):
37 """Test that Angle columns have TUNIT set appropriately,
38 as per DM-7221.
39 """
40 schema = lsst.afw.table.Schema()
41 key = schema.addField("a", type="Angle", doc="angle field")
42 outCat = lsst.afw.table.BaseCatalog(schema)
43 outCat.addNew().set(key, 1.0*lsst.geom.degrees)
44 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
45 outCat.writeFits(tmpFile)
46 inFits = astropy.io.fits.open(tmpFile)
47 self.assertEqual(inFits[1].header["TTYPE1"], "a")
48 self.assertEqual(inFits[1].header["TUNIT1"], "rad")
49 inFits.close()
51 def testSchemaReading(self):
52 """Test that a Schema can be read from a FITS file
54 Per DM-8211.
55 """
56 schema = lsst.afw.table.Schema()
57 aa = schema.addField("a", type=np.int64, doc="a")
58 bb = schema.addField("b", type=np.float64, doc="b")
59 schema.getAliasMap().set("c", "a")
60 schema.getAliasMap().set("d", "b")
61 cat = lsst.afw.table.BaseCatalog(schema)
62 row = cat.addNew()
63 row.set(aa, 12345)
64 row.set(bb, 1.2345)
65 with lsst.utils.tests.getTempFilePath(".fits") as temp:
66 cat.writeFits(temp)
67 self.assertEqual(lsst.afw.table.Schema.readFits(temp), schema)
68 # Not testing Schema.fromFitsMetadata because afw.fits.readMetadata (which is the only
69 # python-accessible FITS header reader) returns a PropertySet, but we want a PropertyList
70 # and it doesn't up-convert easily.
72 def testPreppedRows(self):
73 schema = lsst.afw.table.Schema()
74 aa = schema.addField("a", type=np.float64, doc="a")
75 bb = schema.addField("b", type=np.float64, doc="b")
76 cc = schema.addField("c", type="ArrayF", doc="c", size=4)
77 self.assertEqual(schema.getRecordSize(), 32)
78 lsst.afw.table.io.setPreppedRowsFactor(128)
79 # nRowsToPrep is PreppedRowsFactor/RecordSize = 4
80 # Test that we can read catalogs both larger and smaller than this.
81 nRowsToPrep = lsst.afw.table.io.getPreppedRowsFactor() // schema.getRecordSize()
82 smaller = lsst.afw.table.BaseCatalog(schema)
83 smaller.resize(nRowsToPrep - 1)
84 smaller[aa] = np.random.randn(nRowsToPrep - 1)
85 smaller[bb] = np.random.randn(nRowsToPrep - 1)
86 smaller[cc] = np.random.randn(nRowsToPrep - 1, 4)
87 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
88 smaller.writeFits(tmpFile)
89 smaller2 = lsst.afw.table.BaseCatalog.readFits(tmpFile)
90 self.assertFloatsEqual(smaller[aa], smaller2[aa])
91 self.assertFloatsEqual(smaller[bb], smaller2[bb])
92 self.assertFloatsEqual(smaller[cc], smaller2[cc])
93 larger = lsst.afw.table.BaseCatalog(schema)
94 larger.resize(nRowsToPrep + 1)
95 larger[aa] = np.random.randn(nRowsToPrep + 1)
96 larger[bb] = np.random.randn(nRowsToPrep + 1)
97 larger[cc] = np.random.randn(nRowsToPrep + 1, 4)
98 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
99 larger.writeFits(tmpFile)
100 larger2 = lsst.afw.table.BaseCatalog.readFits(tmpFile)
101 self.assertFloatsEqual(larger[aa], larger2[aa])
102 self.assertFloatsEqual(larger[bb], larger2[bb])
103 self.assertFloatsEqual(larger[cc], larger2[cc])
106class MemoryTester(lsst.utils.tests.MemoryTestCase):
107 pass
110def setup_module(module):
111 lsst.utils.tests.init()
114if __name__ == "__main__": 114 ↛ 115line 114 didn't jump to line 115, because the condition on line 114 was never true
115 lsst.utils.tests.init()
116 unittest.main()