[Scipy-svn] r5578 - in trunk/scipy/io/matlab: . tests

scipy-svn@scip... scipy-svn@scip...
Thu Feb 19 22:01:15 CST 2009


Author: matthew.brett@gmail.com
Date: 2009-02-19 22:01:10 -0600 (Thu, 19 Feb 2009)
New Revision: 5578

Modified:
   trunk/scipy/io/matlab/mio.py
   trunk/scipy/io/matlab/mio4.py
   trunk/scipy/io/matlab/mio5.py
   trunk/scipy/io/matlab/miobase.py
   trunk/scipy/io/matlab/tests/test_mio.py
Log:
Added oned_as option to matlab 4 files.  The default behavior was to save as row vectors anyway, so the behavior has now changed

Modified: trunk/scipy/io/matlab/mio.py
===================================================================
--- trunk/scipy/io/matlab/mio.py	2009-02-20 03:37:55 UTC (rev 5577)
+++ trunk/scipy/io/matlab/mio.py	2009-02-20 04:01:10 UTC (rev 5578)
@@ -156,7 +156,7 @@
     if format == '4':
         if long_field_names:
             raise ValueError("Long field names are not available for version 4 files")
-        MW = MatFile4Writer(file_stream)
+        MW = MatFile4Writer(file_stream, oned_as)
     elif format == '5':
         MW = MatFile5Writer(file_stream,
                             do_compression=do_compression,

Modified: trunk/scipy/io/matlab/mio4.py
===================================================================
--- trunk/scipy/io/matlab/mio4.py	2009-02-20 03:37:55 UTC (rev 5577)
+++ trunk/scipy/io/matlab/mio4.py	2009-02-20 04:01:10 UTC (rev 5578)
@@ -1,13 +1,14 @@
 ''' Classes for read / write of matlab (TM) 4 files
 '''
 import sys
+import warnings
 
 import numpy as np
 
 import scipy.sparse
 
 from miobase import MatFileReader, MatArrayReader, MatMatrixGetter, \
-     MatFileWriter, MatStreamWriter, docfiller
+     MatFileWriter, MatStreamWriter, docfiller, matdims
 
 
 SYS_LITTLE_ENDIAN = sys.byteorder == 'little'
@@ -244,8 +245,8 @@
         self.write_string(self.name + '\0')
 
     def arr_to_2d(self):
-        self.arr = np.atleast_2d(self.arr)
-        dims = self.arr.shape
+        dims = matdims(self.arr, self.oned_as)
+        self.arr.shape = dims
         if len(dims) > 2:
             self.arr = self.arr.reshape(-1,dims[-1])
 
@@ -319,14 +320,14 @@
         self.write_bytes(ijv)
 
 
-def matrix_writer_factory(stream, arr, name):
+def matrix_writer_factory(stream, arr, name, oned_as):
     ''' Factory function to return matrix writer given variable to write
     stream      - file or file-like stream to write to
     arr         - array to write
     name        - name in matlab (TM) workspace
     '''
     if scipy.sparse.issparse(arr):
-        return Mat4SparseWriter(stream, arr, name)
+        return Mat4SparseWriter(stream, arr, name, oned_as)
     arr = np.array(arr)
     dtt = arr.dtype.type
     if dtt is np.object_:
@@ -334,16 +335,26 @@
     elif dtt is np.void:
         raise TypeError, 'Cannot save void type arrays'
     elif dtt in (np.unicode_, np.string_):
-        return Mat4CharWriter(stream, arr, name)
+        return Mat4CharWriter(stream, arr, name, oned_as)
     else:
-        return Mat4NumericWriter(stream, arr, name)
+        return Mat4NumericWriter(stream, arr, name, oned_as)
 
 
 class MatFile4Writer(MatFileWriter):
     ''' Class for writing matlab 4 format files '''
-    def __init__(self, file_stream):
+    def __init__(self, file_stream, oned_as=None):
         self.file_stream = file_stream
+        # deal with deprecations
+        if oned_as is None:
+            warnings.warn("Using oned_as default value ('column')" +
+                          " This will change to 'row' in future versions",
+                          FutureWarning, stacklevel=2)
+            oned_as = 'column'
+        self.oned_as = oned_as
 
     def put_variables(self, mdict):
         for name, var in mdict.items():
-            matrix_writer_factory(self.file_stream, var, name).write()
+            matrix_writer_factory(self.file_stream, 
+                                  var, 
+                                  name, 
+                                  self.oned_as).write()

Modified: trunk/scipy/io/matlab/mio5.py
===================================================================
--- trunk/scipy/io/matlab/mio5.py	2009-02-20 03:37:55 UTC (rev 5577)
+++ trunk/scipy/io/matlab/mio5.py	2009-02-20 04:01:10 UTC (rev 5578)
@@ -653,7 +653,10 @@
                  unicode_strings=False,
                  long_field_names=False,
                  oned_as='column'):
-        super(Mat5MatrixWriter, self).__init__(file_stream, arr, name)
+        super(Mat5MatrixWriter, self).__init__(file_stream, 
+                                               arr, 
+                                               name,
+                                               oned_as)
         self.is_global = is_global
         self.unicode_strings = unicode_strings
         self.long_field_names = long_field_names

Modified: trunk/scipy/io/matlab/miobase.py
===================================================================
--- trunk/scipy/io/matlab/miobase.py	2009-02-20 03:37:55 UTC (rev 5577)
+++ trunk/scipy/io/matlab/miobase.py	2009-02-20 04:01:10 UTC (rev 5578)
@@ -513,13 +513,14 @@
 
 class MatStreamWriter(object):
     ''' Base object for writing to mat files '''
-    def __init__(self, file_stream, arr, name):
+    def __init__(self, file_stream, arr, name, oned_as):
         self.file_stream = file_stream
         self.arr = arr
         dt = self.arr.dtype
         if not dt.isnative:
             self.arr = self.arr.astype(dt.newbyteorder('='))
         self.name = name
+        self.oned_as = oned_as
 
     def rewind(self):
         self.file_stream.seek(0)

Modified: trunk/scipy/io/matlab/tests/test_mio.py
===================================================================
--- trunk/scipy/io/matlab/tests/test_mio.py	2009-02-20 03:37:55 UTC (rev 5577)
+++ trunk/scipy/io/matlab/tests/test_mio.py	2009-02-20 04:01:10 UTC (rev 5578)
@@ -470,24 +470,29 @@
 
 
 def test_1d_shape():
-    # Current behavior is 1D -> column vector
-    arr = np.arange(5)
-    stream = StringIO()
-    savemat(stream, {'oned':arr})
-    vals = loadmat(stream)
-    yield assert_equal, vals['oned'].shape, (5,1)
-    # which is the same as 'column' for oned_as
-    stream = StringIO()
-    savemat(stream, {'oned':arr}, oned_as='column')
-    vals = loadmat(stream)
-    yield assert_equal, vals['oned'].shape, (5,1)
-    # but different from 'row'
-    stream = StringIO()
-    savemat(stream, {'oned':arr}, oned_as='row')
-    vals = loadmat(stream)
-    yield assert_equal, vals['oned'].shape, (1,5)
+    for format in ('4', '5'):
+        # Current behavior is 1D -> column vector
+        arr = np.arange(5)
+        stream = StringIO()
+        savemat(stream, {'oned':arr}, format=format)
+        vals = loadmat(stream)
+        yield assert_equal, vals['oned'].shape, (5,1)
+        # which is the same as 'column' for oned_as
+        stream = StringIO()
+        savemat(stream, {'oned':arr}, 
+                format=format,
+                oned_as='column')
+        vals = loadmat(stream)
+        yield assert_equal, vals['oned'].shape, (5,1)
+        # but different from 'row'
+        stream = StringIO()
+        savemat(stream, {'oned':arr}, 
+                format=format,
+                oned_as='row')
+        vals = loadmat(stream)
+        yield assert_equal, vals['oned'].shape, (1,5)
+    
 
-
 def test_compression():
     arr = np.zeros(100).reshape((5,20))
     arr[2,10] = 1



More information about the Scipy-svn mailing list