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

Shortcuts 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

33 statements  

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 

22from astropy.table import Table as AstropyTable 

23import numpy as np 

24 

25from .. 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 def __init__(self, dataIds): 

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

40 self.dataIds = dict.fromkeys(dataIds) 

41 

42 def getAstropyTable(self, order): 

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

44 

45 Returns 

46 ------- 

47 table : `astropy.table.Table` 

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

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

50 order : `bool` 

51 If True then order rows based on DataIds. 

52 """ 

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

54 # causes a _Table to be created. 

55 if not self.dataIds: 

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

57 

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

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

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

61 

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

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

64 # will not work properly without. 

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

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

67 

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

69 

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

71 if order: 

72 table = sortAstropyTable(table, dimensions) 

73 return table 

74 

75 

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

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

78 

79 butler = Butler(repo) 

80 results = butler.registry.queryDataIds(dimensions, 

81 datasets=datasets, 

82 where=where, 

83 collections=collections) 

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