comparison filetensor.py @ 35:2508c373cf29

bug fix for reading byte matrices
author bergstrj@iro.umontreal.ca
date Fri, 18 Apr 2008 01:36:56 -0400
parents bb92087cb0f6
children 2b6656b2ef52
comparison
equal deleted inserted replaced
34:1b152f46ad0c 35:2508c373cf29
29 _magic_dtype = { 29 _magic_dtype = {
30 0x1E3D4C51 : ('float32', 4), 30 0x1E3D4C51 : ('float32', 4),
31 0x1E3D4C52 : ('packed matrix', 0), #what is a packed matrix? 31 0x1E3D4C52 : ('packed matrix', 0), #what is a packed matrix?
32 0x1E3D4C53 : ('float64', 8), 32 0x1E3D4C53 : ('float64', 8),
33 0x1E3D4C54 : ('int32', 4), 33 0x1E3D4C54 : ('int32', 4),
34 0x1E3D4C55 : ('int8', 1), 34 0x1E3D4C55 : ('uint8', 1),
35 0x1E3D4C56 : ('int16', 2), 35 0x1E3D4C56 : ('int16', 2),
36 } 36 }
37 _dtype_magic = { 37 _dtype_magic = {
38 'float32': 0x1E3D4C51, 38 'float32': 0x1E3D4C51,
39 'packed matrix': 0x1E3D4C52, 39 'packed matrix': 0x1E3D4C52,
40 'float64': 0x1E3D4C53, 40 'float64': 0x1E3D4C53,
41 'int32': 0x1E3D4C54, 41 'int32': 0x1E3D4C54,
42 'int8': 0x1E3D4C55, 42 'uint8': 0x1E3D4C55,
43 'int16': 0x1E3D4C56 43 'int16': 0x1E3D4C56
44 } 44 }
45 45
46 def _unused(): 46 def _unused():
47 f.seek(0,2) #seek to end 47 f.seek(0,2) #seek to end
148 _write_int32(f, sh) 148 _write_int32(f, sh)
149 mat.tofile(f) 149 mat.tofile(f)
150 150
151 if __name__ == '__main__': 151 if __name__ == '__main__':
152 #a small test script, starts by reading sys.argv[1] 152 #a small test script, starts by reading sys.argv[1]
153 rval = read(sys.argv[1], slice(400), debug=True) #load from filename 153 rval = read(sys.argv[1], None, debug=True) #load from filename
154 print 'rval', rval.shape, rval.size 154 print 'rval', rval.shape, rval.size
155 155
156 f = file('/tmp/some_mat', 'w'); 156 if 0:
157 write(f, rval) 157 f = file('/tmp/some_mat', 'w');
158 print '' 158 write(f, rval)
159 f.close() 159 print ''
160 f = file('/tmp/some_mat', 'r'); 160 f.close()
161 rval2 = read(f) #load from file handle 161 f = file('/tmp/some_mat', 'r');
162 print 'rval2', rval2.shape, rval2.size 162 rval2 = read(f) #load from file handle
163 print 'rval2', rval2.shape, rval2.size
163 164
164 assert rval.dtype == rval2.dtype 165 assert rval.dtype == rval2.dtype
165 assert rval.shape == rval2.shape 166 assert rval.shape == rval2.shape
166 assert numpy.all(rval == rval2) 167 assert numpy.all(rval == rval2)
167 print 'ok' 168 print 'ok'
168 169