nikcleju@0: # -*- coding: utf-8 -*- nikcleju@0: """ nikcleju@0: Created on Wed Nov 09 12:28:55 2011 nikcleju@0: nikcleju@0: @author: ncleju nikcleju@0: """ nikcleju@0: nikcleju@0: import numpy nikcleju@0: import scipy.io nikcleju@0: import matplotlib.pyplot as plt nikcleju@0: import matplotlib.cm as cm nikcleju@0: import matplotlib.colors as mcolors nikcleju@0: nikcleju@0: # Sample call nikcleju@0: #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: nikcleju@0: #def loadshowmatrices(filename, algonames = None): nikcleju@0: # mdict = scipy.io.loadmat(filename) nikcleju@0: # if algonames == None: nikcleju@0: # algonames = mdict['algonames'] nikcleju@0: # nikcleju@0: # for algonameobj in algonames: nikcleju@0: # algoname = algonameobj[0][0] nikcleju@0: # print algoname nikcleju@0: # if mdict['meanmatrix'][algoname][0,0].ndim == 2: nikcleju@0: # plt.figure() nikcleju@0: # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@0: # elif mdict['meanmatrix'][algoname][0,0].ndim == 3: nikcleju@0: # for matrix in mdict['meanmatrix'][algoname][0,0]: nikcleju@0: # plt.figure() nikcleju@0: # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@0: # plt.show() nikcleju@0: # print "Finished." nikcleju@0: # nikcleju@0: # nikcleju@0: #def loadsavematrices(filename, saveplotbase, saveplotexts, algonames = None): nikcleju@0: # nikcleju@0: # mdict = scipy.io.loadmat(filename) nikcleju@0: # lambdas = mdict['lambdas'] nikcleju@0: # nikcleju@0: # if algonames is None: nikcleju@0: # algonames = mdict['algonames'] nikcleju@0: # nikcleju@0: # for algonameobj in algonames: nikcleju@0: # algoname = algonameobj[0][0] nikcleju@0: # print algoname nikcleju@0: # if mdict['meanmatrix'][algoname][0,0].ndim == 2: nikcleju@0: # plt.figure() nikcleju@0: # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@0: # for ext in saveplotexts: nikcleju@0: # plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') nikcleju@0: # elif mdict['meanmatrix'][algoname][0,0].ndim == 3: nikcleju@0: # ilbd = 0 nikcleju@0: # for matrix in mdict['meanmatrix'][algoname][0,0]: nikcleju@0: # plt.figure() nikcleju@0: # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@0: # for ext in saveplotexts: nikcleju@0: # plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') nikcleju@0: # ilbd = ilbd + 1 nikcleju@0: # print "Finished." nikcleju@0: nikcleju@0: def loadmatrices(filename, algonames=None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): nikcleju@0: nikcleju@0: if dosave and (saveplotbase is None or saveplotexts is None): nikcleju@0: print('Error: please specify name and extensions for saving') nikcleju@0: raise Exception('Name or extensions for saving not specified') nikcleju@0: nikcleju@0: mdict = scipy.io.loadmat(filename) nikcleju@0: nikcleju@0: if dosave: nikcleju@0: lambdas = mdict['lambdas'] nikcleju@0: nikcleju@0: if algonames is None: nikcleju@0: algonames = mdict['algonames'] nikcleju@0: nikcleju@0: for algonameobj in algonames: nikcleju@0: algoname = algonameobj[0][0] nikcleju@0: print algoname nikcleju@0: if mdict['meanmatrix'][algoname][0,0].ndim == 2: nikcleju@0: plt.figure() nikcleju@0: plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') nikcleju@0: if dosave: nikcleju@0: for ext in saveplotexts: nikcleju@0: plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') nikcleju@0: elif mdict['meanmatrix'][algoname][0,0].ndim == 3: nikcleju@0: if dosave: nikcleju@0: ilbd = 0 nikcleju@0: for matrix in mdict['meanmatrix'][algoname][0,0]: nikcleju@0: plt.figure() nikcleju@0: plt.imshow(matrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') nikcleju@0: if dosave: nikcleju@0: for ext in saveplotexts: nikcleju@0: plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') nikcleju@0: ilbd = ilbd + 1 nikcleju@0: nikcleju@0: if doshow: nikcleju@0: plt.show() nikcleju@0: print "Finished." nikcleju@0: nikcleju@0: nikcleju@0: def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): nikcleju@0: nikcleju@0: if dosave and (saveplotbase is None or saveplotexts is None): nikcleju@0: print('Error: please specify name and extensions for saving') nikcleju@0: raise Exception('Name or extensions for saving not specified') nikcleju@0: nikcleju@0: mdict = scipy.io.loadmat(filename) nikcleju@0: nikcleju@0: if dosave: nikcleju@0: lambdas = mdict['lambdas'] nikcleju@0: nikcleju@0: if algonames == None: nikcleju@0: algonames = mdict['algonames'] nikcleju@0: nikcleju@0: # thresh = 0.90 nikcleju@0: N = 10 nikcleju@0: # border = 2 nikcleju@0: # bordercolor = 0 # black nikcleju@0: nikcleju@0: for algonameobj in algonames: nikcleju@0: algoname = algonameobj[0][0] nikcleju@0: print algoname nikcleju@0: if mdict['meanmatrix'][algoname][0,0].ndim == 2: nikcleju@0: nikcleju@0: # Prepare bigger matrix nikcleju@0: rows,cols = mdict['meanmatrix'][algoname][0,0].shape nikcleju@0: bigmatrix = numpy.zeros((N*rows,N*cols)) nikcleju@0: for i in numpy.arange(rows): nikcleju@0: for j in numpy.arange(cols): nikcleju@0: bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j] nikcleju@5: bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0) nikcleju@6: #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.9, 2,0.2) nikcleju@5: bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4) nikcleju@5: bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1) nikcleju@0: # # Mark 95% border nikcleju@0: # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh: nikcleju@0: # # Top border nikcleju@0: # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0: nikcleju@0: # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor nikcleju@0: # # Bottom border nikcleju@0: # if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i0: nikcleju@0: # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor nikcleju@0: # # Right border (not very probable) nikcleju@0: # if j thresh: nikcleju@0: # # Top border nikcleju@0: # if matrix[i-1,j] < thresh and i>0: nikcleju@0: # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor nikcleju@0: # # Bottom border nikcleju@0: # if matrix[i+1,j] < thresh and i0: nikcleju@0: # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor nikcleju@0: # # Right border (not very probable) nikcleju@0: # if j>> obj_arr = np.zeros((2,), dtype=np.object) nikcleju@0: # >>> obj_arr[0] = 1 nikcleju@0: # >>> obj_arr[1] = 'a string' nikcleju@0: nikcleju@0: def int_drawseparation(matrix,bigmatrix,N,thresh,border,bordercolor): nikcleju@0: rows,cols = matrix.shape nikcleju@0: for i in numpy.arange(rows): nikcleju@0: for j in numpy.arange(cols): nikcleju@0: # Mark border nikcleju@0: # Use top-left corner of current square for reference nikcleju@0: if matrix[i,j] > thresh: nikcleju@0: # Top border nikcleju@0: if matrix[i-1,j] < thresh and i>0: nikcleju@0: bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor nikcleju@0: # Bottom border nikcleju@0: if i0: nikcleju@0: bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor nikcleju@0: # Right border (not very probable) nikcleju@0: if j