Coverage for install/scipp/data/__init__.py: 27%

41 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-17 01:51 +0000

1# SPDX-License-Identifier: BSD-3-Clause 

2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 

3# @author Simon Heybrock 

4from functools import lru_cache 

5from typing import Any 

6 

7from ..core import DataArray, DataGroup, Variable, array, linspace, ones 

8from ..io import load_hdf5 

9from ..typing import DTypeLike 

10 

11_version = '2' 

12 

13 

14@lru_cache(maxsize=1) 

15def _get_pooch() -> Any: 

16 try: 

17 import pooch 

18 except ImportError as err: 

19 raise ImportError( 

20 '''pooch is not installed. 

21It is required to use Scipp's bundled data files. 

22 

23Please install pooch either using conda: 

24 conda install pooch 

25or using pip: 

26 python -m pip install pooch 

27or install all optional components of Scipp: 

28 python -m pip install scipp[all] 

29''' 

30 ) from err 

31 

32 return pooch.create( 

33 path=pooch.os_cache('scipp'), 

34 env='SCIPP_DATA_DIR', 

35 retry_if_failed=3, 

36 base_url='https://public.esss.dk/groups/scipp/scipp/{version}/', 

37 version=_version, 

38 registry={ 

39 'rhessi_flares.h5': 'md5:b4fdc9508c6d1d7aab1c6ebdd13956f2', 

40 'VULCAN_221040_processed.h5': 'md5:626484b95372d3341b156dc2012722f9', 

41 }, 

42 ) 

43 

44 

45def get_path(name: str) -> str: 

46 """ 

47 Return the path to a data file bundled with Scipp. 

48 

49 This function only works with example data and cannot handle 

50 paths to custom files. 

51 """ 

52 return _get_pooch().fetch(name) # type: ignore[no-any-return] 

53 

54 

55def rhessi_flares() -> str: 

56 """ 

57 Return the path to the list of solar flares recorded by RHESSI 

58 in Scipp's HDF5 format. 

59 

60 The original is 

61 https://hesperia.gsfc.nasa.gov/rhessi3/data-access/rhessi-data/flare-list/index.html 

62 

63 Attention 

64 --------- 

65 This data has been manipulated! 

66 """ 

67 return get_path('rhessi_flares.h5') 

68 

69 

70def vulcan_steel_strain_data() -> DataGroup: 

71 return load_hdf5(get_path('VULCAN_221040_processed.h5')) # type: ignore[return-value] 

72 

73 

74def table_xyz( 

75 nrow: int, coord_max: float | None = None, coord_dtype: DTypeLike | None = None 

76) -> DataArray: 

77 """ 

78 Return a 1-D data array ("table") with x, y, and z coord columns. 

79 """ 

80 from numpy.random import default_rng 

81 

82 rng = default_rng(seed=1234) 

83 

84 def random_coordinate() -> Variable: 

85 return array( 

86 dims=['row'], 

87 unit='m', 

88 values=( 

89 rng.random(nrow) if coord_max is None else rng.random(nrow) * coord_max 

90 ), 

91 dtype=coord_dtype, 

92 ) 

93 

94 x = random_coordinate() 

95 y = random_coordinate() 

96 z = random_coordinate() 

97 data = ones(dims=['row'], unit='K', shape=[nrow]) 

98 data.values += 0.1 * rng.random(nrow) 

99 return DataArray(data=data, coords={'x': x, 'y': y, 'z': z}) 

100 

101 

102def binned_x(nevent: int, nbin: int) -> DataArray: 

103 """ 

104 Return data array binned along 1 dimension. 

105 """ 

106 return table_xyz(nevent).bin(x=nbin) 

107 

108 

109def binned_xy(nevent: int, nx: int, ny: int) -> DataArray: 

110 """ 

111 Return data array binned along 2 dimensions. 

112 """ 

113 return table_xyz(nevent).bin(x=nx, y=ny) 

114 

115 

116def data_xy() -> DataArray: 

117 from numpy.random import default_rng 

118 

119 rng = default_rng(seed=1234) 

120 da = DataArray(array(dims=['x', 'y'], values=rng.random((100, 100)))) 

121 da.coords['x'] = linspace('x', 0.0, 1.0, num=100, unit='mm') 

122 da.coords['y'] = linspace('y', 0.0, 5.0, num=100, unit='mm') 

123 return da