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.4.1, created at 2024-02-14 06:37 +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', 

66# octave-maintainers, April-May 2007 

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

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

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

70mxOBJECT_CLASS_FROM_MATRIX_H = 18 

71 

72mdtypes_template = { 

73 miINT8: 'i1', 

74 miUINT8: 'u1', 

75 miINT16: 'i2', 

76 miUINT16: 'u2', 

77 miINT32: 'i4', 

78 miUINT32: 'u4', 

79 miSINGLE: 'f4', 

80 miDOUBLE: 'f8', 

81 miINT64: 'i8', 

82 miUINT64: 'u8', 

83 miUTF8: 'u1', 

84 miUTF16: 'u2', 

85 miUTF32: 'u4', 

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

87 ('subsystem_offset', 'i8'), 

88 ('version', 'u2'), 

89 ('endian_test', 'S2')], 

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

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

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

93 ('byte_count', 'u4'), 

94 ('flags_class','u4'), 

95 ('nzmax', 'u4')], 

96 'U1': 'U1', 

97 } 

98 

99mclass_dtypes_template = { 

100 mxINT8_CLASS: 'i1', 

101 mxUINT8_CLASS: 'u1', 

102 mxINT16_CLASS: 'i2', 

103 mxUINT16_CLASS: 'u2', 

104 mxINT32_CLASS: 'i4', 

105 mxUINT32_CLASS: 'u4', 

106 mxINT64_CLASS: 'i8', 

107 mxUINT64_CLASS: 'u8', 

108 mxSINGLE_CLASS: 'f4', 

109 mxDOUBLE_CLASS: 'f8', 

110 } 

111 

112mclass_info = { 

113 mxINT8_CLASS: 'int8', 

114 mxUINT8_CLASS: 'uint8', 

115 mxINT16_CLASS: 'int16', 

116 mxUINT16_CLASS: 'uint16', 

117 mxINT32_CLASS: 'int32', 

118 mxUINT32_CLASS: 'uint32', 

119 mxINT64_CLASS: 'int64', 

120 mxUINT64_CLASS: 'uint64', 

121 mxSINGLE_CLASS: 'single', 

122 mxDOUBLE_CLASS: 'double', 

123 mxCELL_CLASS: 'cell', 

124 mxSTRUCT_CLASS: 'struct', 

125 mxOBJECT_CLASS: 'object', 

126 mxCHAR_CLASS: 'char', 

127 mxSPARSE_CLASS: 'sparse', 

128 mxFUNCTION_CLASS: 'function', 

129 mxOPAQUE_CLASS: 'opaque', 

130 } 

131 

132NP_TO_MTYPES = { 

133 'f8': miDOUBLE, 

134 'c32': miDOUBLE, 

135 'c24': miDOUBLE, 

136 'c16': miDOUBLE, 

137 'f4': miSINGLE, 

138 'c8': miSINGLE, 

139 'i8': miINT64, 

140 'i4': miINT32, 

141 'i2': miINT16, 

142 'i1': miINT8, 

143 'u8': miUINT64, 

144 'u4': miUINT32, 

145 'u2': miUINT16, 

146 'u1': miUINT8, 

147 'S1': miUINT8, 

148 'U1': miUTF16, 

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

150 } 

151 

152 

153NP_TO_MXTYPES = { 

154 'f8': mxDOUBLE_CLASS, 

155 'c32': mxDOUBLE_CLASS, 

156 'c24': mxDOUBLE_CLASS, 

157 'c16': mxDOUBLE_CLASS, 

158 'f4': mxSINGLE_CLASS, 

159 'c8': mxSINGLE_CLASS, 

160 'i8': mxINT64_CLASS, 

161 'i4': mxINT32_CLASS, 

162 'i2': mxINT16_CLASS, 

163 'i1': mxINT8_CLASS, 

164 'u8': mxUINT64_CLASS, 

165 'u4': mxUINT32_CLASS, 

166 'u2': mxUINT16_CLASS, 

167 'u1': mxUINT8_CLASS, 

168 'S1': mxUINT8_CLASS, 

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

170 } 

171 

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

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

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

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

176 

177codecs_template = { 

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

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

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

181 } 

182 

183 

184def _convert_codecs(template, byte_order): 

185 ''' Convert codec template mapping to byte order 

186 

187 Set codecs not on this system to None 

188 

189 Parameters 

190 ---------- 

191 template : mapping 

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

193 (without byte order suffix) 

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

195 code for little or big endian 

196 

197 Returns 

198 ------- 

199 codecs : dict 

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

201 ''' 

202 codecs = {} 

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

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

205 codec = v['codec'] 

206 try: 

207 " ".encode(codec) 

208 except LookupError: 

209 codecs[k] = None 

210 continue 

211 if v['width'] > 1: 

212 codec += postfix 

213 codecs[k] = codec 

214 return codecs.copy() 

215 

216 

217MDTYPES = {} 

218for _bytecode in '<>': 

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

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

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

222 MDTYPES[_bytecode] = _def 

223 

224 

225class mat_struct: 

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

227 

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

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

230 """ 

231 pass 

232 

233 

234class MatlabObject(np.ndarray): 

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

236 

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

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

239 """ 

240 

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

242 # Input array is an already formed ndarray instance 

243 # We first cast to be our class type 

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

245 # add the new attribute to the created instance 

246 obj.classname = classname 

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

248 return obj 

249 

250 def __array_finalize__(self,obj): 

251 # reset the attribute from passed original object 

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

253 # We do not need to return anything 

254 

255 

256class MatlabFunction(np.ndarray): 

257 """Subclass for a MATLAB function. 

258 

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

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

261 """ 

262 

263 def __new__(cls, input_array): 

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

265 return obj 

266 

267 

268class MatlabOpaque(np.ndarray): 

269 """Subclass for a MATLAB opaque matrix. 

270 

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

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

273 """ 

274 

275 def __new__(cls, input_array): 

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

277 return obj 

278 

279 

280OPAQUE_DTYPE = np.dtype( 

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