Coverage for tests/test_readTextCatalog.py: 25%
72 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-23 08:56 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-23 08:56 +0000
1#
2# LSST Data Management System
3#
4# Copyright 2008-2016 AURA/LSST.
5#
6# This product includes software developed by the
7# LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <https://www.lsstcorp.org/LegalNotices/>.
22#
24import os
25import unittest
27import numpy as np
29from lsst.meas.algorithms.readTextCatalogTask import ReadTextCatalogTask
30import lsst.utils.tests
32# If you want to update the FITS table used for this test:
33# - modify makeFitsTable to create the table as you want it
34# - set SaveTextCatalog = True
35# - sun the test once to create the new file
36# - set SaveTextCatalog = False again
37SaveTextCatalog = False # construct and save a new text table file?
38TestDir = os.path.dirname(__file__)
39TextPath = os.path.join(TestDir, "data", "testReadTextCatalog.csv")
42def makeCatalog():
43 """Create an object catalog as a numpy structured array
45 dtypes are chosen to match how the data is read back in, for ease in testing
46 Including a '£' in demonstates that we can round-trip UTF-8 rather than
47 being limited to ASCII.
48 """
49 dtype = [("name", "U8"), ("ra", "float64"), ("dec", "float64"),
50 ("counts", "int64"), ("flux", "float64"), ("resolved", "int64")]
51 data = [
52 ("£object1", -5, 10, 1000, 1.1, True),
53 ("£object2", 45, 5, 2000, 1.2, False),
54 ]
55 return np.array(data, dtype=dtype)
58if SaveTextCatalog: 58 ↛ 59line 58 didn't jump to line 59, because the condition on line 58 was never true
59 print("Warning: writing a new text catalog file; to stop this set SaveTextCatalog = False")
60 arr = makeCatalog()
61 with open(TextPath, "w") as f:
62 f.write(", ".join(arr.dtype.names))
63 f.write("\n")
64 for row in arr:
65 f.write(", ".join(str(val) for val in row))
66 f.write("\n")
69class ReadTextCatalogTaskTestCase(lsst.utils.tests.TestCase):
70 """Test ReadTextCatalogTask"""
72 def setUp(self):
73 self.arr = makeCatalog()
75 def testDefaultNames(self):
76 """Test reading without renaming
77 """
78 task = ReadTextCatalogTask()
79 arr = task.run(TextPath)
80 self.assertTrue(np.array_equal(arr, self.arr))
81 self.assertEqual(len(arr), 2)
83 def testGivenNames(self):
84 """Test reading with column names in the config
85 """
86 colnames = ("id", "ra_deg", "dec_deg", "total_counts", "total_flux", "is_resolved")
87 config = ReadTextCatalogTask.ConfigClass()
88 config.colnames = colnames
89 config.header_lines = 1
90 task = ReadTextCatalogTask(config=config)
91 arr = task.run(TextPath)
92 self.assertEqual(arr.dtype.names, colnames)
93 self.assertEqual(len(arr), 2)
94 for inname, outname in zip(self.arr.dtype.names, colnames):
95 self.assertTrue(np.array_equal(self.arr[inname], arr[outname]))
97 def testBadPath(self):
98 """Test that an invalid path causes an error"""
99 task = ReadTextCatalogTask()
100 badPath = "does/not/exists.garbage"
101 with self.assertRaises(IOError):
102 task.run(badPath)
104 def testTooFewColumnNames(self):
105 """Test that too few names in config.colnames causes an error"""
106 config = ReadTextCatalogTask.ConfigClass()
107 for badColNames in (
108 ["name", "ra", "dec", "counts", "flux"],
109 ["name"],
110 ):
111 config.colnames = badColNames
112 config.header_lines = 1
113 task = ReadTextCatalogTask(config=config)
114 with self.assertRaises(ValueError):
115 task.run(TextPath)
117 def testNullValues(self):
118 """Test that missing values designated by a string are converted to
119 zeros and that their column has the appropriate data type."""
120 config = ReadTextCatalogTask.ConfigClass()
121 config.fill_values = ['null', '0']
122 config.replace_missing_floats_with_nan = True
123 task = ReadTextCatalogTask(config=config)
124 nullValuesFile = os.path.join(TestDir, "data", "testReadTextCatalog_nullValues.csv")
125 arr = task.run(nullValuesFile)
126 dtype = [("name", "U7"), ("ra", "float64"), ("dec", "float64"),
127 ("counts", "int64"), ("flux", "float64"), ("resolved", "int64")]
129 for (name, dt) in dtype:
130 self.assertEqual(dt, arr.dtype[name])
132 self.assertTrue(np.isnan(arr['flux'][1]))
133 self.assertEqual(arr['resolved'][1], 0)
136class TestMemory(lsst.utils.tests.MemoryTestCase):
137 pass
140def setup_module(module):
141 lsst.utils.tests.init()
144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true
145 lsst.utils.tests.init()
146 unittest.main()