Coverage for /usr/local/lib/python3.8/site-packages/spam/datasets.py: 77%

84 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-11-22 13:26 +0000

1import pkg_resources as pkg 

2import tifffile 

3import pickle 

4import sys 

5import numpy 

6 

7# Should avoid following error in python3 

8# UnicodeDecodeError: 'ascii' codec can't decode byte 0xba in position 1: ordinal not in range(128) 

9# Generic functions 

10 

11 

12def loadtiff(filepath): 

13 data_file_path = 'data/' + filepath 

14 if (pkg.resource_exists('spam', data_file_path)): 

15 f = pkg.resource_filename('spam', data_file_path) 

16 return tifffile.imread(f) 

17 else: 

18 # raise IOError("File %s not found" % data_file_path) 

19 print(IOError("File %s not found" % data_file_path)) 

20 return 0 

21 

22 

23def load(filepath): 

24 data_file_path = 'data/' + filepath 

25 if (pkg.resource_exists('spam', data_file_path)): 

26 f = pkg.resource_filename('spam', data_file_path) 

27 return open(f, 'rb') 

28 else: 

29 # raise IOError("File %s not found" % data_file_path) 

30 print(IOError("File %s not found" % data_file_path)) 

31 return 0 

32 

33 

34def loadPickle(filepath): 

35 data_file_path = 'data/' + filepath 

36 if (pkg.resource_exists('spam', data_file_path)): 

37 f = pkg.resource_filename('spam', data_file_path) 

38 # Different pickle-loading options for different python versions 

39 if sys.version[0] == '2': 

40 # on 3 lines to close pickle 

41 with open(f, 'r') as fh: 

42 ret = pickle.load(fh) 

43 return ret 

44 else: 

45 with open(f, 'rb') as fh: 

46 ret = pickle.load(fh, encoding='latin1') 

47 return ret 

48 else: 

49 # raise IOError("File %s not found" % data_file_path) 

50 print(IOError("File %s not found" % data_file_path)) 

51 return 0 

52 

53 

54# Usage example ## 

55 

56# def loadtiffexample(): 

57# print("print something funny") 

58# return loadtiff('image.tiff') 

59 

60 

61# def loadsomething(): 

62# print("print something funny") 

63# return load('placeholder.txt') 

64 

65 

66def loadSnow(): 

67 # print("spam.datasets.snow(): Brrr, enjoy this cold data") 

68 return loadtiff("snow/snow.tif") 

69 

70 

71def loadStructuredMesh(): 

72 return loadPickle("mesh/structuredMesh.p") 

73 

74 

75def loadUnstructuredMesh(): 

76 return loadPickle("mesh/unstructuredMesh.p") 

77 

78 

79def loadConcreteXr(): 

80 return loadtiff("concrete/concrete_x-ray-bin16.tif") 

81 

82 

83def loadConcreteNe(): 

84 return loadtiff("concrete/concrete_neutron-bin16.tif") 

85 

86 

87def loadDEMboxsizeCentreRadius(): 

88 filepath = "DEM/spheres.txt" 

89 data_file_path = 'data/' + filepath 

90 if (pkg.resource_exists('spam', data_file_path)): 

91 f = pkg.resource_filename('spam', data_file_path) 

92 # loading the dem file 

93 boxSizeDEM = numpy.genfromtxt(f, max_rows=1, comments='%', usecols=(1,2,3)) 

94 centres = numpy.loadtxt(f, skiprows=0, usecols=(4,3,2)) #! x,y,z 

95 radii = numpy.loadtxt(f, skiprows=0, usecols=(5)) 

96 return [boxSizeDEM, centres, radii] 

97 else: 

98 # raise IOError("File %s not found" % data_file_path) 

99 print(IOError("File %s not found" % data_file_path)) 

100 return 0 

101 

102def loadUniformDEMboxsizeCentreRadius(): 

103 filepath = "DEM/uniformSpheres.txt" 

104 data_file_path = 'data/' + filepath 

105 if (pkg.resource_exists('spam', data_file_path)): 

106 f = pkg.resource_filename('spam', data_file_path) 

107 # loading the dem file 

108 centres = numpy.loadtxt(f, skiprows=1, usecols=(2, 1, 0)) #! x,y,z 

109 # remove negative numbers 

110 centres -= numpy.min(centres, axis=0) 

111 radii = numpy.loadtxt(f, skiprows=1, usecols=(3)) 

112 boxSizeDEM = (numpy.max(centres[:, :], axis=0) - numpy.min(centres[:, :], axis=0))+2*numpy.max(radii) 

113 return [boxSizeDEM, centres, radii] 

114 else: 

115 # raise IOError("File %s not found" % data_file_path) 

116 print(IOError("File %s not found" % data_file_path)) 

117 return 0 

118 

119def loadDEMtouchingGrainsAndBranchVector(): 

120 filepath = "DEM/contacts.txt" 

121 data_file_path = 'data/' + filepath 

122 if (pkg.resource_exists('spam', data_file_path)): 

123 f = pkg.resource_filename('spam', data_file_path) 

124 # loading the dem file 

125 labels = numpy.loadtxt(f, skiprows=1, usecols=(0, 1)) 

126 zyxBranch = numpy.loadtxt(f, skiprows=1, usecols=(4, 3, 2)) 

127 return [labels.astype(int), zyxBranch] 

128 else: 

129 # raise IOError("File %s not found" % data_file_path) 

130 print(IOError("File %s not found" % data_file_path)) 

131 return 0 

132 return loadtxt("DEM/spheres.txt") 

133 

134def loadDEMspheresMMR(): 

135 filepath = "DEM/SpheresMMR.txt" 

136 data_file_path = 'data/' + filepath 

137 if (pkg.resource_exists('spam', data_file_path)): 

138 f = pkg.resource_filename('spam', data_file_path) 

139 # loading the dem file 

140 return numpy.genfromtxt(f) 

141 else: 

142 # raise IOError("File %s not found" % data_file_path) 

143 print(IOError("File %s not found" % data_file_path)) 

144 return 0