changeset 34:e8c4672e9de4

Save algonames (not tested) in output mat file Matplotlib saves tight images (bbox_inches = 'tight') utils.py: added loadsavematrices, modified loadshowmatrices
author nikcleju
date Tue, 15 Nov 2011 14:50:58 +0000
parents 116dcfacd1cc
children d4f9c906dc78
files scripts/ABSapprox.py scripts/utils.py
diffstat 2 files changed, 54 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ABSapprox.py	Fri Nov 11 16:12:17 2011 +0000
+++ b/scripts/ABSapprox.py	Tue Nov 15 14:50:58 2011 +0000
@@ -307,6 +307,7 @@
     tosave['numvects'] = numvects
     tosave['SNRdb'] = SNRdb
     tosave['lambdas'] = lambdas
+    tosave['algonames'] = [algotuple[1] for algotuple in algosN+algosL]
     try:
       scipy.io.savemat(savedataname, tosave)
     except:
@@ -319,7 +320,7 @@
       plt.imshow(meanmatrix[algoname], cmap=cm.gray, interpolation='nearest',origin='lower')
       if dosaveplot:
         for ext in saveplotexts:
-          plt.savefig(saveplotbase + algoname + '.' + ext)
+          plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
     for algotuple in algosL:
       algoname = algotuple[1]
       for ilbd in np.arange(lambdas.size):
@@ -327,7 +328,7 @@
         plt.imshow(meanmatrix[algoname][ilbd], cmap=cm.gray, interpolation='nearest',origin='lower')
         if dosaveplot:
           for ext in saveplotexts:
-            plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext)
+            plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
     if doshowplot:
       plt.show()
     
--- a/scripts/utils.py	Fri Nov 11 16:12:17 2011 +0000
+++ b/scripts/utils.py	Tue Nov 15 14:50:58 2011 +0000
@@ -9,17 +9,61 @@
 import matplotlib.pyplot as plt
 import matplotlib.cm as cm
 
-def loadshowmatrices(filename, algostr = ('GAP','SL0_approx')):
+def loadshowmatrices(filename, algonames = None):
     mdict = scipy.io.loadmat(filename)
-    for strname in algostr:
-        print strname
-        if mdict['meanmatrix'][strname][0,0].ndim == 2:
+    if algonames == None:
+      algonames = ('GAP','SL0_approx')
+    else:
+      algonames = mdict['algonames']
+    
+    for algonameobj in algonames:
+        algoname = algonames[0,0][0]
+        print algoname
+        if mdict['meanmatrix'][algoname][0,0].ndim == 2:
             plt.figure()
-            plt.imshow(mdict['meanmatrix'][strname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')            
-        elif mdict['meanmatrix'][strname][0,0].ndim == 3:
-            for matrix in mdict['meanmatrix'][strname][0,0]:
+            plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')            
+        elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
+            for matrix in mdict['meanmatrix'][algoname][0,0]:
                 plt.figure()
                 plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
     plt.show()
     print "Finished."
+    
+def loadsavematrices(filename, saveplotbase, saveplotexts, algonames = None):
+    
+    mdict = scipy.io.loadmat(filename)
+    lambdas = mdict['lambdas']
+
+    if algonames is None:
+      algonames = mdict['algonames']
+    
+    for algonameobj in algonames:
+        algoname = algonameobj[0][0]
+        print algoname
+        if mdict['meanmatrix'][algoname][0,0].ndim == 2:
+            plt.figure()
+            plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
+            for ext in saveplotexts:
+              plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
+        elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
+            ilbd = 0
+            for matrix in mdict['meanmatrix'][algoname][0,0]:
+                plt.figure()
+                plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
+                for ext in saveplotexts:
+                  plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
+                ilbd = ilbd + 1
+    print "Finished."    
             
+def appendtomatfile(filename, toappend, toappendname):
+  mdict = scipy.io.loadmat(filename)
+  mdict[toappendname] = toappend
+  try:
+    scipy.io.savemat(filename, mdict)
+  except:
+    print "Save error"  
+  
+  # To save to a cell array, create an object array:
+  #  >>> obj_arr = np.zeros((2,), dtype=np.object)
+  #  >>> obj_arr[0] = 1
+  #  >>> obj_arr[1] = 'a string'    
\ No newline at end of file