changeset 1394:48e8292e22e7

new version _read_header for filetensor that allow to work gzip file.
author Frederic Bastien <nouiz@nouiz.org>
date Fri, 07 Jan 2011 11:42:38 -0500
parents 8ecc6da87350
children 54b2268db0d7
files pylearn/io/filetensor.py
diffstat 1 files changed, 6 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/io/filetensor.py	Thu Jan 06 13:33:47 2011 -0500
+++ b/pylearn/io/filetensor.py	Fri Jan 07 11:42:38 2011 -0500
@@ -52,17 +52,14 @@
     s_array = numpy.fromstring(s, dtype='int32')
     return s_array.item()
 
-def _read_header(f, debug=False, fromstring=False):
+def _read_header(f, debug=False, fromgzip=False):
     """
     :returns: data type, element size, rank, shape, size
     """
     #what is the data type of this matrix?
     #magic_s = f.read(4)
     #magic = numpy.fromstring(magic_s, dtype='int32')
-    if fromstring:
-        magic = numpy.fromstring(f[0:4], dtype='int32').item()
-    else:
-        magic = _read_int32(f)
+    magic = _read_int32(f)
     magic_t, elsize = _magic_dtype[magic]
     if debug: 
         print 'header magic', magic, magic_t, elsize
@@ -70,15 +67,13 @@
         raise NotImplementedError('packed matrix not supported')
 
     #what is the rank of the tensor?
-    if fromstring:
-        ndim = numpy.fromstring(f[4:8], dtype='int32').item()
-    else:
-        ndim = _read_int32(f)
+    ndim = _read_int32(f)
     if debug: print 'header ndim', ndim
 
     #what are the dimensions of the tensor?
-    if fromstring:
-        dim = numpy.fromstring(f[8:8+max(ndim,3)*4], dtype='int32')[:ndim]
+    if fromgzip:
+        d = f.read(max(ndim,3)*4)
+        dim = numpy.fromstring(d, dtype='int32')[:ndim]
     else:
         dim = numpy.fromfile(f, dtype='int32', count=max(ndim,3))[:ndim]
     dim_size = _prod(dim)