Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/scipy/io/matlab/_mio5_params.py: 86%

77 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-23 06:43 +0000

1''' Constants and classes for matlab 5 read and write 

2 

3See also mio5_utils.pyx where these same constants arise as c enums. 

4 

5If you make changes in this file, don't forget to change mio5_utils.pyx 

6''' 

7import numpy as np 

8 

9from ._miobase import convert_dtypes 

10 

11 

12__all__ = [ 

13 'MDTYPES', 'MatlabFunction', 'MatlabObject', 'MatlabOpaque', 

14 'NP_TO_MTYPES', 'NP_TO_MXTYPES', 'OPAQUE_DTYPE', 'codecs_template', 

15 'mat_struct', 'mclass_dtypes_template', 'mclass_info', 'mdtypes_template', 

16 'miCOMPRESSED', 'miDOUBLE', 'miINT16', 'miINT32', 'miINT64', 'miINT8', 

17 'miMATRIX', 'miSINGLE', 'miUINT16', 'miUINT32', 'miUINT64', 'miUINT8', 

18 'miUTF16', 'miUTF32', 'miUTF8', 'mxCELL_CLASS', 'mxCHAR_CLASS', 

19 'mxDOUBLE_CLASS', 'mxFUNCTION_CLASS', 'mxINT16_CLASS', 'mxINT32_CLASS', 

20 'mxINT64_CLASS', 'mxINT8_CLASS', 'mxOBJECT_CLASS', 

21 'mxOBJECT_CLASS_FROM_MATRIX_H', 'mxOPAQUE_CLASS', 'mxSINGLE_CLASS', 

22 'mxSPARSE_CLASS', 'mxSTRUCT_CLASS', 'mxUINT16_CLASS', 'mxUINT32_CLASS', 

23 'mxUINT64_CLASS', 'mxUINT8_CLASS' 

24] 

25miINT8 = 1 

26miUINT8 = 2 

27miINT16 = 3 

28miUINT16 = 4 

29miINT32 = 5 

30miUINT32 = 6 

31miSINGLE = 7 

32miDOUBLE = 9 

33miINT64 = 12 

34miUINT64 = 13 

35miMATRIX = 14 

36miCOMPRESSED = 15 

37miUTF8 = 16 

38miUTF16 = 17 

39miUTF32 = 18 

40 

41mxCELL_CLASS = 1 

42mxSTRUCT_CLASS = 2 

43# The March 2008 edition of "Matlab 7 MAT-File Format" says that 

44# mxOBJECT_CLASS = 3, whereas matrix.h says that mxLOGICAL = 3. 

45# Matlab 2008a appears to save logicals as type 9, so we assume that 

46# the document is correct. See type 18, below. 

47mxOBJECT_CLASS = 3 

48mxCHAR_CLASS = 4 

49mxSPARSE_CLASS = 5 

50mxDOUBLE_CLASS = 6 

51mxSINGLE_CLASS = 7 

52mxINT8_CLASS = 8 

53mxUINT8_CLASS = 9 

54mxINT16_CLASS = 10 

55mxUINT16_CLASS = 11 

56mxINT32_CLASS = 12 

57mxUINT32_CLASS = 13 

58# The following are not in the March 2008 edition of "Matlab 7 

59# MAT-File Format," but were guessed from matrix.h. 

60mxINT64_CLASS = 14 

61mxUINT64_CLASS = 15 

62mxFUNCTION_CLASS = 16 

63# Not doing anything with these at the moment. 

64mxOPAQUE_CLASS = 17 # This appears to be a function workspace 

65# Thread 'saving/loading symbol table of annymous functions', octave-maintainers, April-May 2007 

66# https://lists.gnu.org/archive/html/octave-maintainers/2007-04/msg00031.html 

67# https://lists.gnu.org/archive/html/octave-maintainers/2007-05/msg00032.html 

68# (Was/Deprecated: https://www-old.cae.wisc.edu/pipermail/octave-maintainers/2007-May/002824.html) 

69mxOBJECT_CLASS_FROM_MATRIX_H = 18 

70 

71mdtypes_template = { 

72 miINT8: 'i1', 

73 miUINT8: 'u1', 

74 miINT16: 'i2', 

75 miUINT16: 'u2', 

76 miINT32: 'i4', 

77 miUINT32: 'u4', 

78 miSINGLE: 'f4', 

79 miDOUBLE: 'f8', 

80 miINT64: 'i8', 

81 miUINT64: 'u8', 

82 miUTF8: 'u1', 

83 miUTF16: 'u2', 

84 miUTF32: 'u4', 

85 'file_header': [('description', 'S116'), 

86 ('subsystem_offset', 'i8'), 

87 ('version', 'u2'), 

88 ('endian_test', 'S2')], 

89 'tag_full': [('mdtype', 'u4'), ('byte_count', 'u4')], 

90 'tag_smalldata':[('byte_count_mdtype', 'u4'), ('data', 'S4')], 

91 'array_flags': [('data_type', 'u4'), 

92 ('byte_count', 'u4'), 

93 ('flags_class','u4'), 

94 ('nzmax', 'u4')], 

95 'U1': 'U1', 

96 } 

97 

98mclass_dtypes_template = { 

99 mxINT8_CLASS: 'i1', 

100 mxUINT8_CLASS: 'u1', 

101 mxINT16_CLASS: 'i2', 

102 mxUINT16_CLASS: 'u2', 

103 mxINT32_CLASS: 'i4', 

104 mxUINT32_CLASS: 'u4', 

105 mxINT64_CLASS: 'i8', 

106 mxUINT64_CLASS: 'u8', 

107 mxSINGLE_CLASS: 'f4', 

108 mxDOUBLE_CLASS: 'f8', 

109 } 

110 

111mclass_info = { 

112 mxINT8_CLASS: 'int8', 

113 mxUINT8_CLASS: 'uint8', 

114 mxINT16_CLASS: 'int16', 

115 mxUINT16_CLASS: 'uint16', 

116 mxINT32_CLASS: 'int32', 

117 mxUINT32_CLASS: 'uint32', 

118 mxINT64_CLASS: 'int64', 

119 mxUINT64_CLASS: 'uint64', 

120 mxSINGLE_CLASS: 'single', 

121 mxDOUBLE_CLASS: 'double', 

122 mxCELL_CLASS: 'cell', 

123 mxSTRUCT_CLASS: 'struct', 

124 mxOBJECT_CLASS: 'object', 

125 mxCHAR_CLASS: 'char', 

126 mxSPARSE_CLASS: 'sparse', 

127 mxFUNCTION_CLASS: 'function', 

128 mxOPAQUE_CLASS: 'opaque', 

129 } 

130 

131NP_TO_MTYPES = { 

132 'f8': miDOUBLE, 

133 'c32': miDOUBLE, 

134 'c24': miDOUBLE, 

135 'c16': miDOUBLE, 

136 'f4': miSINGLE, 

137 'c8': miSINGLE, 

138 'i8': miINT64, 

139 'i4': miINT32, 

140 'i2': miINT16, 

141 'i1': miINT8, 

142 'u8': miUINT64, 

143 'u4': miUINT32, 

144 'u2': miUINT16, 

145 'u1': miUINT8, 

146 'S1': miUINT8, 

147 'U1': miUTF16, 

148 'b1': miUINT8, # not standard but seems MATLAB uses this (gh-4022) 

149 } 

150 

151 

152NP_TO_MXTYPES = { 

153 'f8': mxDOUBLE_CLASS, 

154 'c32': mxDOUBLE_CLASS, 

155 'c24': mxDOUBLE_CLASS, 

156 'c16': mxDOUBLE_CLASS, 

157 'f4': mxSINGLE_CLASS, 

158 'c8': mxSINGLE_CLASS, 

159 'i8': mxINT64_CLASS, 

160 'i4': mxINT32_CLASS, 

161 'i2': mxINT16_CLASS, 

162 'i1': mxINT8_CLASS, 

163 'u8': mxUINT64_CLASS, 

164 'u4': mxUINT32_CLASS, 

165 'u2': mxUINT16_CLASS, 

166 'u1': mxUINT8_CLASS, 

167 'S1': mxUINT8_CLASS, 

168 'b1': mxUINT8_CLASS, # not standard but seems MATLAB uses this 

169 } 

170 

171''' Before release v7.1 (release 14) matlab (TM) used the system 

172default character encoding scheme padded out to 16-bits. Release 14 

173and later use Unicode. When saving character data, R14 checks if it 

174can be encoded in 7-bit ascii, and saves in that format if so.''' 

175 

176codecs_template = { 

177 miUTF8: {'codec': 'utf_8', 'width': 1}, 

178 miUTF16: {'codec': 'utf_16', 'width': 2}, 

179 miUTF32: {'codec': 'utf_32','width': 4}, 

180 } 

181 

182 

183def _convert_codecs(template, byte_order): 

184 ''' Convert codec template mapping to byte order 

185 

186 Set codecs not on this system to None 

187 

188 Parameters 

189 ---------- 

190 template : mapping 

191 key, value are respectively codec name, and root name for codec 

192 (without byte order suffix) 

193 byte_order : {'<', '>'} 

194 code for little or big endian 

195 

196 Returns 

197 ------- 

198 codecs : dict 

199 key, value are name, codec (as in .encode(codec)) 

200 ''' 

201 codecs = {} 

202 postfix = byte_order == '<' and '_le' or '_be' 

203 for k, v in template.items(): 

204 codec = v['codec'] 

205 try: 

206 " ".encode(codec) 

207 except LookupError: 

208 codecs[k] = None 

209 continue 

210 if v['width'] > 1: 

211 codec += postfix 

212 codecs[k] = codec 

213 return codecs.copy() 

214 

215 

216MDTYPES = {} 

217for _bytecode in '<>': 

218 _def = {'dtypes': convert_dtypes(mdtypes_template, _bytecode), 

219 'classes': convert_dtypes(mclass_dtypes_template, _bytecode), 

220 'codecs': _convert_codecs(codecs_template, _bytecode)} 

221 MDTYPES[_bytecode] = _def 

222 

223 

224class mat_struct: 

225 """Placeholder for holding read data from structs. 

226 

227 We use instances of this class when the user passes False as a value to the 

228 ``struct_as_record`` parameter of the :func:`scipy.io.loadmat` function. 

229 """ 

230 pass 

231 

232 

233class MatlabObject(np.ndarray): 

234 """Subclass of ndarray to signal this is a matlab object. 

235 

236 This is a simple subclass of :class:`numpy.ndarray` meant to be used 

237 by :func:`scipy.io.loadmat` and should not be instantiated directly. 

238 """ 

239 

240 def __new__(cls, input_array, classname=None): 

241 # Input array is an already formed ndarray instance 

242 # We first cast to be our class type 

243 obj = np.asarray(input_array).view(cls) 

244 # add the new attribute to the created instance 

245 obj.classname = classname 

246 # Finally, we must return the newly created object: 

247 return obj 

248 

249 def __array_finalize__(self,obj): 

250 # reset the attribute from passed original object 

251 self.classname = getattr(obj, 'classname', None) 

252 # We do not need to return anything 

253 

254 

255class MatlabFunction(np.ndarray): 

256 """Subclass for a MATLAB function. 

257 

258 This is a simple subclass of :class:`numpy.ndarray` meant to be used 

259 by :func:`scipy.io.loadmat` and should not be directly instantiated. 

260 """ 

261 

262 def __new__(cls, input_array): 

263 obj = np.asarray(input_array).view(cls) 

264 return obj 

265 

266 

267class MatlabOpaque(np.ndarray): 

268 """Subclass for a MATLAB opaque matrix. 

269 

270 This is a simple subclass of :class:`numpy.ndarray` meant to be used 

271 by :func:`scipy.io.loadmat` and should not be directly instantiated. 

272 """ 

273 

274 def __new__(cls, input_array): 

275 obj = np.asarray(input_array).view(cls) 

276 return obj 

277 

278 

279OPAQUE_DTYPE = np.dtype( 

280 [('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])