Coverage for python/lsst/daf/butler/script/queryDataIds.py: 19%

33 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-28 09:25 +0000

1# This file is part of daf_butler. 

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 numpy as np 

23from astropy.table import Table as AstropyTable 

24 

25from .._butler import Butler 

26from ..cli.utils import sortAstropyTable 

27 

28 

29class _Table: 

30 """Aggregates DataIds and creates an astropy table with one DataId per 

31 row. Eliminates duplicate rows. 

32 

33 Parameters 

34 ---------- 

35 dataIds : `iterable` [ ``DataId`` ] 

36 The DataIds to add to the table. 

37 """ 

38 

39 def __init__(self, dataIds): 

40 # use dict to store dataIds as keys to preserve ordering 

41 self.dataIds = dict.fromkeys(dataIds) 

42 

43 def getAstropyTable(self, order): 

44 """Get the table as an astropy table. 

45 

46 Returns 

47 ------- 

48 table : `astropy.table.Table` 

49 The dataIds, sorted by spatial and temporal columns first, and then 

50 the rest of the columns, with duplicate dataIds removed. 

51 order : `bool` 

52 If True then order rows based on DataIds. 

53 """ 

54 # Should never happen; adding a dataset should be the action that 

55 # causes a _Table to be created. 

56 if not self.dataIds: 

57 raise RuntimeError("No DataIds were provided.") 

58 

59 dataId = next(iter(self.dataIds)) 

60 dimensions = list(dataId.full.keys()) 

61 columnNames = [str(item) for item in dimensions] 

62 

63 # Need to hint the column types for numbers since the per-row 

64 # constructor of Table does not work this out on its own and sorting 

65 # will not work properly without. 

66 typeMap = {float: np.float64, int: np.int64} 

67 columnTypes = [typeMap.get(type(value)) for value in dataId.full.values()] 

68 

69 rows = [[value for value in dataId.full.values()] for dataId in self.dataIds] 

70 

71 table = AstropyTable(np.array(rows), names=columnNames, dtype=columnTypes) 

72 if order: 

73 table = sortAstropyTable(table, dimensions) 

74 return table 

75 

76 

77def queryDataIds(repo, dimensions, datasets, where, collections, order_by, limit, offset): 

78 # Docstring for supported parameters is the same as Registry.queryDataIds 

79 

80 butler = Butler(repo) 

81 results = butler.registry.queryDataIds( 

82 dimensions, datasets=datasets, where=where, collections=collections 

83 ) 

84 

85 if order_by: 

86 results.order_by(*order_by) 

87 if limit > 0: 

88 if offset <= 0: 

89 offset = None 

90 results.limit(limit, offset) 

91 

92 if len(results.graph) > 0: 

93 table = _Table(results) 

94 return table.getAstropyTable(not order_by) 

95 else: 

96 return None