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

23 statements  

« prev     ^ index     » next       coverage.py v7.2.4, created at 2023-04-29 02:58 -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 _datasetType = np.ndarray 

40 

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

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

43 

44 Parameters 

45 ---------- 

46 composite : `~numpy.ndarray` 

47 Numpy table to access component. 

48 componentName : `str` 

49 Name of component to retrieve. 

50 

51 Returns 

52 ------- 

53 component : `object` 

54 The component. 

55 

56 Raises 

57 ------ 

58 AttributeError 

59 The component can not be found. 

60 """ 

61 match componentName: 

62 case "columns": 

63 return list(composite.dtype.names) 

64 case "schema": 

65 return ArrowNumpySchema(composite.dtype) 

66 case "rowcount": 

67 return len(composite) 

68 

69 raise AttributeError( 

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

71 ) 

72 

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

74 return inMemoryDataset.dtype.names 

75 

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

77 return inMemoryDataset[columns]