# HG changeset patch # User nikcleju # Date 1321629154 0 # Node ID 0ff08ae833be41ad9ca79a334b0a7440ae6d468a # Parent aa3e89435a2af3897b5ce7d9dadfae4159bdf3a0 utils.py: Mark the areas with less than 10% error in the figures. Joined loadsavematrices() and loadshowmatrices() together in the same function loadmatrices() diff -r aa3e89435a2a -r 0ff08ae833be scripts/utils.py --- 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 i0: + bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor + # Right border (not very probable) + if 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 i0: + bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor + # Right border (not very probable) + if j