changeset 1517:a6e634b83d88

allow to read filetensor compressed with bz2
author Frederic Bastien <nouiz@nouiz.org>
date Wed, 09 May 2012 11:56:28 -0400
parents 57feab73c783
children 18c638290706
files pylearn/io/filetensor.py
diffstat 1 files changed, 4 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/io/filetensor.py	Mon Mar 05 17:35:15 2012 -0500
+++ b/pylearn/io/filetensor.py	Wed May 09 11:56:28 2012 -0400
@@ -20,6 +20,7 @@
 @todo: add complex type support
 
 """
+import bz2
 import gzip
 
 import numpy
@@ -64,7 +65,7 @@
     :returns: data type, element size, rank, shape, size
     """
     if fromgzip is None:
-        fromgzip = isinstance(f, gzip.GzipFile)
+        fromgzip = isinstance(f, (gzip.GzipFile, bz2.BZ2File))
 
     #what is the data type of this matrix?
     #magic_s = f.read(4)
@@ -183,6 +184,7 @@
     """Load all or part of file 'f' into a numpy ndarray
 
     @param f: file from which to read
+              file can be opended with open(), gzip.open() and bz2.BZ2File()
     @type f: file-like object. Can be a gzip open file.
 
     If subtensor is not None, it should be like the argument to
@@ -200,7 +202,7 @@
     f_start = f.tell()
 
     rval = None
-    if isinstance(f, gzip.GzipFile):
+    if isinstance(f, (gzip.GzipFile, bz2.BZ2File)):
         assert subtensor is None, "Not implemented the subtensor case for gzip file"
         d = f.read(_prod(dim)*elsize)
         rval = numpy.fromstring(d, dtype=magic_t).reshape(dim)