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

39 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-04-28 01:28 +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 

5 

6from ..core import DataArray, DataGroup, array, linspace, ones 

7from ..io import load_hdf5 

8 

9_version = '2' 

10 

11 

12@lru_cache(maxsize=1) 

13def _get_pooch(): 

14 try: 

15 import pooch 

16 except ImportError as err: 

17 raise ImportError( 

18 '''pooch is not installed. 

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

20 

21Please install pooch either using conda: 

22 conda install pooch 

23or using pip: 

24 python -m pip install pooch 

25or install all optional components of Scipp: 

26 python -m pip install scipp[all] 

27''' 

28 ) from err 

29 

30 return pooch.create( 

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

32 env='SCIPP_DATA_DIR', 

33 retry_if_failed=3, 

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

35 version=_version, 

36 registry={ 

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

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

39 }, 

40 ) 

41 

42 

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

44 """ 

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

46 

47 This function only works with example data and cannot handle 

48 paths to custom files. 

49 """ 

50 return _get_pooch().fetch(name) 

51 

52 

53def rhessi_flares() -> str: 

54 """ 

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

56 in Scipp's HDF5 format. 

57 

58 The original is 

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

60 

61 Attention 

62 --------- 

63 This data has been manipulated! 

64 """ 

65 return get_path('rhessi_flares.h5') 

66 

67 

68def vulcan_steel_strain_data() -> DataGroup: 

69 return load_hdf5(get_path('VULCAN_221040_processed.h5')) 

70 

71 

72def table_xyz(nrow: int, coord_max=None, coord_dtype=None) -> DataArray: 

73 """ 

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

75 """ 

76 from numpy.random import default_rng 

77 

78 rng = default_rng(seed=1234) 

79 

80 def random_coordinate(): 

81 return array( 

82 dims=['row'], 

83 unit='m', 

84 values=( 

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

86 ), 

87 dtype=coord_dtype, 

88 ) 

89 

90 x = random_coordinate() 

91 y = random_coordinate() 

92 z = random_coordinate() 

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

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

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

96 

97 

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

99 """ 

100 Return data array binned along 1 dimension. 

101 """ 

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

103 

104 

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

106 """ 

107 Return data array binned along 2 dimensions. 

108 """ 

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

110 

111 

112def data_xy() -> DataArray: 

113 from numpy.random import default_rng 

114 

115 rng = default_rng(seed=1234) 

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

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

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

119 return da