changeset 39:0ff08ae833be

utils.py: Mark the areas with less than 10% error in the figures. Joined loadsavematrices() and loadshowmatrices() together in the same function loadmatrices()
author nikcleju
date Fri, 18 Nov 2011 15:12:34 +0000
parents aa3e89435a2a
children 9bea0b50b170
files scripts/utils.py
diffstat 1 files changed, 157 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/utils.py	Thu Nov 17 17:41:31 2011 +0000
+++ b/scripts/utils.py	Fri Nov 18 15:12:34 2011 +0000
@@ -5,32 +5,69 @@
 @author: ncleju
 """
 
+import numpy
 import scipy.io
 import matplotlib.pyplot as plt
 import matplotlib.cm as cm
 
-def loadshowmatrices(filename, algonames = None):
-    mdict = scipy.io.loadmat(filename)
-    if algonames == None:
-      algonames = mdict['algonames']
+# Sample call
+utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf'))
+
+#def loadshowmatrices(filename, algonames = None):
+#    mdict = scipy.io.loadmat(filename)
+#    if algonames == 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')            
+#        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."  
     
-    for algonameobj in algonames:
-        algoname = algonames[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')            
-        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 loadmatrices(filename, algonames=None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
     
-def loadsavematrices(filename, saveplotbase, saveplotexts, algonames = None):
+    if dosave and (saveplotbase is None or saveplotexts is None):
+      print('Error: please specify name and extensions for saving')
+      raise Exception('Name or extensions for saving not specified')
     
     mdict = scipy.io.loadmat(filename)
-    lambdas = mdict['lambdas']
+
+    if dosave:
+      lambdas = mdict['lambdas']
 
     if algonames is None:
       algonames = mdict['algonames']
@@ -41,18 +78,114 @@
         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')
+            if dosave:
+              for ext in saveplotexts:
+                plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
         elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
-            ilbd = 0
+            if dosave:
+              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
+                if dosave:
+                  for ext in saveplotexts:
+                    plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
+                  ilbd = ilbd + 1
+    
+    if doshow:
+      plt.show()
     print "Finished."    
+
+    
+def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
+  
+    if dosave and (saveplotbase is None or saveplotexts is None):
+      print('Error: please specify name and extensions for saving')
+      raise Exception('Name or extensions for saving not specified')
+      
+    mdict = scipy.io.loadmat(filename)
+    
+    if dosave:
+      lambdas = mdict['lambdas']
+      
+    if algonames == None:
+      algonames = mdict['algonames']
+    
+    thresh = 0.90
+    N = 10
+    border = 2
+    bordercolor = 0 # black
+    
+    for algonameobj in algonames:
+        algoname = algonameobj[0][0]
+        print algoname
+        if mdict['meanmatrix'][algoname][0,0].ndim == 2:
             
+            # Prepare bigger matrix
+            rows,cols = mdict['meanmatrix'][algoname][0,0].shape
+            bigmatrix = numpy.zeros((N*rows,N*cols))
+            for i in numpy.arange(rows):
+              for j in numpy.arange(cols):
+                bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j]
+                # Mark 95% border
+                if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
+                  # Top border
+                  if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
+                    bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
+                  # Bottom border
+                  if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i<rows-1:
+                    bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor                
+                  # Left border
+                  if mdict['meanmatrix'][algoname][0,0][i,j-1] < thresh and j>0:
+                    bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
+                  # Right border (not very probable)
+                  if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
+                    bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
+                    
+            plt.figure()
+            #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')            
+            plt.imshow(bigmatrix, cmap=cm.gray, interpolation='nearest',origin='lower')        
+            if dosave:
+              for ext in saveplotexts:
+                plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')            
+        elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
+            if dosave:
+              ilbd = 0          
+              
+            for matrix in mdict['meanmatrix'][algoname][0,0]:
+              
+                # Prepare bigger matrix
+                rows,cols = matrix.shape
+                bigmatrix = numpy.zeros((N*rows,N*cols))
+                for i in numpy.arange(rows):
+                  for j in numpy.arange(cols):
+                    bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
+                    # Mark 95% border
+                    if matrix[i,j] > thresh:
+                      # Top border
+                      if matrix[i-1,j] < thresh and i>0:
+                        bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
+                      # Bottom border
+                      if matrix[i+1,j] < thresh and i<rows-1:
+                        bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor                
+                      # Left border
+                      if matrix[i,j-1] < thresh and j>0:
+                        bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
+                      # Right border (not very probable)
+                      if j<cols-1 and matrix[i,j+1] < thresh:
+                        bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
+                        
+                plt.figure()
+                #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
+                plt.imshow(bigmatrix, cmap=cm.gray, interpolation='nearest',origin='lower')
+                if dosave:
+                  for ext in saveplotexts:
+                    plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
+                  ilbd = ilbd + 1                
+    if doshow:
+      plt.show()
+    print "Finished."    
+    
 def appendtomatfile(filename, toappend, toappendname):
   mdict = scipy.io.loadmat(filename)
   mdict[toappendname] = toappend