annotate utils.py @ 7:b4a0b4dfe206

utils.py: added function int_setticks() to set ticks and ticklabels
author nikcleju
date Fri, 13 Jan 2012 18:44:26 +0000
parents 09651b934691
children 4d1bfd404f6a
rev   line source
nikcleju@0 1 # -*- coding: utf-8 -*-
nikcleju@0 2 """
nikcleju@0 3 Created on Wed Nov 09 12:28:55 2011
nikcleju@0 4
nikcleju@0 5 @author: ncleju
nikcleju@0 6 """
nikcleju@0 7
nikcleju@0 8 import numpy
nikcleju@0 9 import scipy.io
nikcleju@0 10 import matplotlib.pyplot as plt
nikcleju@0 11 import matplotlib.cm as cm
nikcleju@0 12 import matplotlib.colors as mcolors
nikcleju@0 13
nikcleju@0 14 # Sample call
nikcleju@0 15 #utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf'))
nikcleju@0 16
nikcleju@0 17 def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
nikcleju@0 18
nikcleju@0 19 if dosave and (saveplotbase is None or saveplotexts is None):
nikcleju@0 20 print('Error: please specify name and extensions for saving')
nikcleju@0 21 raise Exception('Name or extensions for saving not specified')
nikcleju@0 22
nikcleju@0 23 mdict = scipy.io.loadmat(filename)
nikcleju@0 24
nikcleju@0 25 if dosave:
nikcleju@0 26 lambdas = mdict['lambdas']
nikcleju@0 27
nikcleju@0 28 if algonames == None:
nikcleju@0 29 algonames = mdict['algonames']
nikcleju@0 30
nikcleju@0 31 # thresh = 0.90
nikcleju@0 32 N = 10
nikcleju@0 33 # border = 2
nikcleju@0 34 # bordercolor = 0 # black
nikcleju@0 35
nikcleju@0 36 for algonameobj in algonames:
nikcleju@0 37 algoname = algonameobj[0][0]
nikcleju@0 38 print algoname
nikcleju@0 39 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
nikcleju@0 40
nikcleju@0 41 # Prepare bigger matrix
nikcleju@0 42 rows,cols = mdict['meanmatrix'][algoname][0,0].shape
nikcleju@0 43 bigmatrix = numpy.zeros((N*rows,N*cols))
nikcleju@0 44 for i in numpy.arange(rows):
nikcleju@0 45 for j in numpy.arange(cols):
nikcleju@0 46 bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j]
nikcleju@5 47 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0)
nikcleju@6 48 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.9, 2,0.2)
nikcleju@5 49 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4)
nikcleju@5 50 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1)
nikcleju@0 51 # # Mark 95% border
nikcleju@0 52 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
nikcleju@0 53 # # Top border
nikcleju@0 54 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
nikcleju@0 55 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 56 # # Bottom border
nikcleju@0 57 # if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i<rows-1:
nikcleju@0 58 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 59 # # Left border
nikcleju@0 60 # if mdict['meanmatrix'][algoname][0,0][i,j-1] < thresh and j>0:
nikcleju@0 61 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 62 # # Right border (not very probable)
nikcleju@0 63 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
nikcleju@0 64 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 65
nikcleju@0 66 plt.figure()
nikcleju@0 67 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
nikcleju@7 68 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@7 69 #if algoname == 'GAP':
nikcleju@7 70 int_setticks()
nikcleju@7 71 #else:
nikcleju@7 72 # plt.gca().get_xaxis().set_visible(False)
nikcleju@7 73 # plt.gca().get_yaxis().set_visible(False)
nikcleju@7 74
nikcleju@0 75 if dosave:
nikcleju@0 76 for ext in saveplotexts:
nikcleju@0 77 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
nikcleju@0 78 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
nikcleju@0 79 if dosave:
nikcleju@0 80 ilbd = 0
nikcleju@0 81
nikcleju@0 82 for matrix in mdict['meanmatrix'][algoname][0,0]:
nikcleju@0 83
nikcleju@0 84 # Prepare bigger matrix
nikcleju@0 85 rows,cols = matrix.shape
nikcleju@0 86 bigmatrix = numpy.zeros((N*rows,N*cols))
nikcleju@0 87 for i in numpy.arange(rows):
nikcleju@0 88 for j in numpy.arange(cols):
nikcleju@0 89 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
nikcleju@5 90 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.95,2,0)
nikcleju@6 91 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.9, 2,0.2)
nikcleju@5 92 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8, 2,0.4)
nikcleju@5 93 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5, 2,1)
nikcleju@0 94 # # Mark 95% border
nikcleju@0 95 # if matrix[i,j] > thresh:
nikcleju@0 96 # # Top border
nikcleju@0 97 # if matrix[i-1,j] < thresh and i>0:
nikcleju@0 98 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 99 # # Bottom border
nikcleju@0 100 # if matrix[i+1,j] < thresh and i<rows-1:
nikcleju@0 101 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 102 # # Left border
nikcleju@0 103 # if matrix[i,j-1] < thresh and j>0:
nikcleju@0 104 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 105 # # Right border (not very probable)
nikcleju@0 106 # if j<cols-1 and matrix[i,j+1] < thresh:
nikcleju@0 107 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 108
nikcleju@0 109 plt.figure()
nikcleju@0 110 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
nikcleju@5 111 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@7 112 plt.gca().get_xaxis().set_visible(False)
nikcleju@7 113 plt.gca().get_yaxis().set_visible(False)
nikcleju@7 114 #int_setticks()
nikcleju@0 115 if dosave:
nikcleju@0 116 for ext in saveplotexts:
nikcleju@0 117 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
nikcleju@0 118 ilbd = ilbd + 1
nikcleju@0 119 if doshow:
nikcleju@0 120 plt.show()
nikcleju@0 121 print "Finished."
nikcleju@0 122
nikcleju@0 123 def appendtomatfile(filename, toappend, toappendname):
nikcleju@0 124 mdict = scipy.io.loadmat(filename)
nikcleju@0 125 mdict[toappendname] = toappend
nikcleju@0 126 try:
nikcleju@0 127 scipy.io.savemat(filename, mdict)
nikcleju@0 128 except:
nikcleju@0 129 print "Save error"
nikcleju@0 130
nikcleju@0 131 # To save to a cell array, create an object array:
nikcleju@0 132 # >>> obj_arr = np.zeros((2,), dtype=np.object)
nikcleju@0 133 # >>> obj_arr[0] = 1
nikcleju@0 134 # >>> obj_arr[1] = 'a string'
nikcleju@0 135
nikcleju@0 136 def int_drawseparation(matrix,bigmatrix,N,thresh,border,bordercolor):
nikcleju@0 137 rows,cols = matrix.shape
nikcleju@0 138 for i in numpy.arange(rows):
nikcleju@0 139 for j in numpy.arange(cols):
nikcleju@0 140 # Mark border
nikcleju@0 141 # Use top-left corner of current square for reference
nikcleju@0 142 if matrix[i,j] > thresh:
nikcleju@0 143 # Top border
nikcleju@0 144 if matrix[i-1,j] < thresh and i>0:
nikcleju@0 145 bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 146 # Bottom border
nikcleju@0 147 if i<rows-1 and matrix[i+1,j] < thresh:
nikcleju@0 148 bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 149 # Left border
nikcleju@0 150 if matrix[i,j-1] < thresh and j>0:
nikcleju@0 151 bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 152 # Right border (not very probable)
nikcleju@0 153 if j<cols-1 and matrix[i,j+1] < thresh:
nikcleju@0 154 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 155
nikcleju@7 156 return bigmatrix
nikcleju@7 157
nikcleju@7 158 def int_setticks():
nikcleju@7 159
nikcleju@7 160 ticks = [10, 94, 179]
nikcleju@7 161 ticklabels = ["0.05", "0.5", "0.95"]
nikcleju@7 162
nikcleju@7 163 ax = plt.gca()
nikcleju@7 164 ax.set_xticks(ticks)
nikcleju@7 165 ax.set_xticklabels(ticklabels)
nikcleju@7 166 ax.set_yticks(ticks)
nikcleju@7 167 ax.set_yticklabels(ticklabels)
nikcleju@7 168
nikcleju@7 169 for label in ax.get_xticklabels():
nikcleju@7 170 label.set_fontsize(42)
nikcleju@7 171 for label in ax.get_yticklabels():
nikcleju@7 172 label.set_fontsize(42)
nikcleju@7 173
nikcleju@7 174 ax.set_xlabel('\delta')