[Scipy-svn] r2214 - trunk/Lib/io
scipy-svn at scipy.org
scipy-svn at scipy.org
Fri Sep 22 09:03:31 EDT 2006
Author: matthew.brett at gmail.com
Date: 2006-09-22 08:03:22 -0500 (Fri, 22 Sep 2006)
New Revision: 2214
Modified:
trunk/Lib/io/mio4.py
trunk/Lib/io/mio5.py
trunk/Lib/io/miobase.py
Log:
Removed some more unnecessary file oprations to improved speed
Modified: trunk/Lib/io/mio4.py
===================================================================
--- trunk/Lib/io/mio4.py 2006-09-22 13:00:18 UTC (rev 2213)
+++ trunk/Lib/io/mio4.py 2006-09-22 13:03:22 UTC (rev 2214)
@@ -77,15 +77,17 @@
remaining_bytes = header['dtype'].itemsize * product(header['dims'])
if header['is_complex'] and not header['mclass'] == mxSPARSE_CLASS:
remaining_bytes *= 2
- header['next_position'] = self.mat_stream.tell() + remaining_bytes
+ next_pos = self.mat_stream.tell() + remaining_bytes
if T == mxFULL_CLASS:
- return Mat4FullGetter(self, header)
+ getter = Mat4FullGetter(self, header)
elif T == mxCHAR_CLASS:
- return Mat4CharGetter(self, header)
+ getter = Mat4CharGetter(self, header)
elif T == mxSPARSE_CLASS:
- return Mat4SparseGetter(self, header)
+ getter = Mat4SparseGetter(self, header)
else:
raise TypeError, 'No reader for class code %s' % T
+ getter.next_position = next_pos
+ return getter
class Mat4MatrixGetter(MatMatrixGetter):
Modified: trunk/Lib/io/mio5.py
===================================================================
--- trunk/Lib/io/mio5.py 2006-09-22 13:00:18 UTC (rev 2213)
+++ trunk/Lib/io/mio5.py 2006-09-22 13:03:22 UTC (rev 2214)
@@ -165,7 +165,7 @@
buffer=raw_tag[4:])
byte_count = tag['byte_count']
if mdtype == miMATRIX:
- return self.getter_from_bytes(byte_count).get_array()
+ return self.current_getter().get_array()
if mdtype in self.codecs: # encoded char data
raw_str = self.mat_stream.read(byte_count)
codec = self.codecs[mdtype]
@@ -186,26 +186,31 @@
return el
def matrix_getter_factory(self):
- ''' Returns reader for next matrix '''
+ ''' Returns reader for next matrix at top level '''
tag = self.read_dtype(self.dtypes['tag_full'])
mdtype = tag['mdtype']
byte_count = tag['byte_count']
+ next_pos = self.mat_stream.tell() + byte_count
if mdtype == miCOMPRESSED:
- return Mat5ZArrayReader(self, byte_count).matrix_getter_factory()
- if not mdtype == miMATRIX:
+ getter = Mat5ZArrayReader(self, byte_count).matrix_getter_factory()
+ elif not mdtype == miMATRIX:
raise TypeError, \
'Expecting miMATRIX type here, got %d' % mdtype
- return self.getter_from_bytes(byte_count)
+ elif not byte_count: # an empty miMATRIX can contain no bytes
+ getter = Mat5EmptyMatrixGetter(self)
+ else:
+ getter = self.current_getter()
+ getter.next_position = next_pos
+ return getter
+
+ def current_getter(self):
+ ''' Return matrix getter for current stream position
- def getter_from_bytes(self, byte_count):
- ''' Return matrix getter for current stream position '''
- # Apparently an empty miMATRIX can contain no bytes
- if not byte_count:
- return Mat5EmptyMatrixGetter(self)
+ Returns matrix getters at top level and sub levels
+ '''
af = self.read_dtype(self.dtypes['array_flags'])
header = {}
flags_class = af['flags_class']
- header['next_position'] = self.mat_stream.tell() + byte_count
mc = flags_class & 0xFF
header['mclass'] = mc
header['is_logical'] = flags_class >> 9 & 1
@@ -233,9 +238,7 @@
''' Getter for compressed arrays
Reads and uncompresses gzipped stream on init, providing wrapper
- for this new sub-stream. Sets next_position for main stream to
- allow skipping over this variable (although we have to read and
- uncompress the whole thing anyway to get the name)
+ for this new sub-stream.
'''
def __init__(self, array_reader, byte_count):
'''Reads and uncompresses gzipped stream'''
@@ -246,19 +249,7 @@
array_reader.processor_func,
array_reader.codecs,
array_reader.class_dtypes)
- self._next_position = array_reader.mat_stream.tell()
- def getter_from_bytes(self, byte_count):
- ''' Set next_position to current position in parent stream
-
- self.next_position is only used by the get_variables routine
- of the main file reading loop, so must refer to the position
- in the main stream, not the compressed stream.
- '''
- getter = super(Mat5ZArrayReader, self).getter_from_bytes(byte_count)
- getter.header['next_position'] = self._next_position
- return getter
-
class Mat5MatrixGetter(MatMatrixGetter):
''' Base class for getting Mat5 matrices
Modified: trunk/Lib/io/miobase.py
===================================================================
--- trunk/Lib/io/miobase.py 2006-09-22 13:00:18 UTC (rev 2213)
+++ trunk/Lib/io/miobase.py 2006-09-22 13:03:22 UTC (rev 2214)
@@ -277,12 +277,9 @@
self.dtypes = array_reader.dtypes
self.header = header
self.name = header['name']
- self.data_position = self.mat_stream.tell()
def get_array(self):
''' Gets an array from matrix, and applies any necessary processing '''
- if not self.mat_stream.tell() == self.data_position:
- self.mat_stream.seek(self.data_position)
arr = self.get_raw_array()
return self.array_reader.processor_func(arr, self)
@@ -290,7 +287,7 @@
assert False, 'Not implemented'
def to_next(self):
- self.mat_stream.seek(self.header['next_position'])
+ self.mat_stream.seek(self.next_position)
class MatArrayReader(MatStreamAgent):
More information about the Scipy-svn
mailing list