Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of pipe_tasks. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22import os 

23import unittest 

24import pandas as pd 

25 

26import lsst.utils.tests 

27 

28import pyarrow as pa 

29import pyarrow.parquet as pq 

30from lsst.pipe.tasks.parquetTable import MultilevelParquetTable 

31from lsst.pipe.tasks.functors import HsmFwhm 

32from lsst.pipe.tasks.postprocess import TransformObjectCatalogTask, TransformObjectCatalogConfig 

33 

34ROOT = os.path.abspath(os.path.dirname(__file__)) 

35 

36 

37def setup_module(module): 

38 lsst.utils.tests.init() 

39 

40 

41class TransformObjectCatalogTestCase(unittest.TestCase): 

42 def setUp(self): 

43 # Note that this test input includes HSC-G, HSC-R, and HSC-I data 

44 df = pd.read_csv(os.path.join(ROOT, 'data', 'test_multilevel_parq.csv.gz'), 

45 header=[0, 1, 2], index_col=0) 

46 with lsst.utils.tests.getTempFilePath('*.parq') as filename: 

47 table = pa.Table.from_pandas(df) 

48 pq.write_table(table, filename, compression='none') 

49 self.parq = MultilevelParquetTable(filename) 

50 

51 self.dataId = {"tract": 9615, "patch": "4,4"} 

52 

53 def testNullFilter(self): 

54 """Test that columns for all filters are created despite they may not 

55 exist in the input data. 

56 """ 

57 config = TransformObjectCatalogConfig() 

58 # Want y band columns despite the input data do not have them 

59 # Exclude g band columns despite the input data have them 

60 filterMap = {"HSC-R": "r", "HSC-I": "i", "HSC-Y": "y"} 

61 config.filterMap = filterMap 

62 task = TransformObjectCatalogTask(config=config) 

63 funcs = {'Fwhm': HsmFwhm(dataset='meas')} 

64 df = task.run(self.parq, funcs=funcs, dataId=self.dataId) 

65 self.assertIsInstance(df, pd.DataFrame) 

66 for column in ('coord_ra', 'coord_dec'): 

67 self.assertIn(column, df.columns) 

68 

69 for filt in filterMap.values(): 

70 self.assertIn(filt + 'Fwhm', df.columns) 

71 

72 self.assertNotIn('gFwhm', df.columns) 

73 self.assertTrue(df['yFwhm'].isnull().all()) 

74 self.assertTrue(df['iFwhm'].notnull().all()) 

75 

76 def testUnderscoreColumnFormat(self): 

77 """Test the per-filter column format with an underscore""" 

78 config = TransformObjectCatalogConfig() 

79 filterMap = {"HSC-G": "g", "HSC-R": "r", "HSC-I": "i"} 

80 config.filterMap = filterMap 

81 config.camelCase = False 

82 task = TransformObjectCatalogTask(config=config) 

83 funcs = {'Fwhm': HsmFwhm(dataset='meas')} 

84 df = task.run(self.parq, funcs=funcs, dataId=self.dataId) 

85 self.assertIsInstance(df, pd.DataFrame) 

86 for filt in filterMap.values(): 

87 self.assertIn(filt + '_Fwhm', df.columns) 

88 

89 def testMultilevelOutput(self): 

90 """Test the non-flattened result dataframe with a multilevel column index""" 

91 config = TransformObjectCatalogConfig() 

92 filterMap = {"HSC-R": "r", "HSC-I": "i"} 

93 config.filterMap = filterMap 

94 config.multilevelOutput = True 

95 task = TransformObjectCatalogTask(config=config) 

96 funcs = {'Fwhm': HsmFwhm(dataset='meas')} 

97 df = task.run(self.parq, funcs=funcs, dataId=self.dataId) 

98 self.assertIsInstance(df, pd.DataFrame) 

99 self.assertNotIn('HSC-G', df) 

100 for filt in filterMap: 

101 self.assertIsInstance(df[filt], pd.DataFrame) 

102 self.assertIn('Fwhm', df[filt].columns)