Coverage for python/lsst/daf/butler/delegates/arrownumpy.py: 45%

23 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-12 10:56 -0700

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 numpy tables (structured 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 

31from lsst.utils.introspection import get_full_type_name 

32 

33from .arrowtable import ArrowTableDelegate 

34 

35__all__ = ["ArrowNumpyDelegate"] 

36 

37 

38class ArrowNumpyDelegate(ArrowTableDelegate): 

39 """Delegate that understands the ``ArrowNumpy`` storage class.""" 

40 

41 _datasetType = np.ndarray 

42 

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

44 """Get a component from a numpy table stored via ArrowNumpy. 

45 

46 Parameters 

47 ---------- 

48 composite : `~numpy.ndarray` 

49 Numpy table to access component. 

50 componentName : `str` 

51 Name of component to retrieve. 

52 

53 Returns 

54 ------- 

55 component : `object` 

56 The component. 

57 

58 Raises 

59 ------ 

60 AttributeError 

61 The component can not be found. 

62 """ 

63 match componentName: 

64 case "columns": 

65 return list(composite.dtype.names) 

66 case "schema": 

67 return ArrowNumpySchema(composite.dtype) 

68 case "rowcount": 

69 return len(composite) 

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: np.ndarray) -> list[str]: 

76 return inMemoryDataset.dtype.names 

77 

78 def _selectColumns(self, inMemoryDataset: np.ndarray, columns: list[str]) -> np.ndarray: 

79 return inMemoryDataset[columns]