Coverage for python/lsst/daf/butler/delegates/arrownumpydict.py: 94%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-17 09:32 +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 

22"""Support for reading dictionaries of numpy arrays with the Arrow 

23formatter. 

24""" 

25from __future__ import annotations 

26 

27from typing import Any 

28 

29import numpy as np 

30from lsst.daf.butler.formatters.parquet import ArrowNumpySchema, _numpy_dict_to_dtype 

31from lsst.utils.introspection import get_full_type_name 

32 

33from .arrowtable import ArrowTableDelegate 

34 

35__all__ = ["ArrowNumpyDictDelegate"] 

36 

37 

38class ArrowNumpyDictDelegate(ArrowTableDelegate): 

39 _datasetType = dict 

40 

41 def getComponent(self, composite: dict[str, np.ndarray], componentName: str) -> Any: 

42 """Get a component from a dict of numpy arrays stored via 

43 ArrowNumpyDict. 

44 

45 Parameters 

46 ---------- 

47 composite : `~numpy.ndarray` 

48 Numpy table to access component. 

49 componentName : `str` 

50 Name of component to retrieve. 

51 

52 Returns 

53 ------- 

54 component : `object` 

55 The component. 

56 

57 Raises 

58 ------ 

59 AttributeError 

60 The component can not be found. 

61 """ 

62 match componentName: 

63 case "columns": 

64 return list(composite.keys()) 

65 case "schema": 

66 dtype, _ = _numpy_dict_to_dtype(composite) 

67 return ArrowNumpySchema(dtype) 

68 case "rowcount": 68 ↛ 71line 68 didn't jump to line 71, because the pattern on line 68 never matched

69 return len(composite[list(composite.keys())[0]]) 

70 

71 raise AttributeError( 

72 f"Do not know how to retrieve component {componentName} from {get_full_type_name(composite)}" 

73 ) 

74 

75 def _getColumns(self, inMemoryDataset: dict[str, np.ndarray]) -> list[str]: 

76 return list(inMemoryDataset.keys()) 

77 

78 def _selectColumns( 

79 self, inMemoryDataset: dict[str, np.ndarray], columns: list[str] 

80 ) -> dict[str, np.ndarray]: 

81 return {column: inMemoryDataset[column] for column in columns}