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

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-05 01: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 

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 """Delegate that understands the ``ArrowNumpyDict`` storage class.""" 

40 

41 _datasetType = dict 

42 

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

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

45 ArrowNumpyDict. 

46 

47 Parameters 

48 ---------- 

49 composite : `~numpy.ndarray` 

50 Numpy table to access component. 

51 componentName : `str` 

52 Name of component to retrieve. 

53 

54 Returns 

55 ------- 

56 component : `object` 

57 The component. 

58 

59 Raises 

60 ------ 

61 AttributeError 

62 The component can not be found. 

63 """ 

64 match componentName: 

65 case "columns": 

66 return list(composite.keys()) 

67 case "schema": 

68 dtype, _ = _numpy_dict_to_dtype(composite) 

69 return ArrowNumpySchema(dtype) 

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

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

72 

73 raise AttributeError( 

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

75 ) 

76 

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

78 return list(inMemoryDataset.keys()) 

79 

80 def _selectColumns( 

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

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

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