nikcleju@0: # -*- coding: utf-8 -*- nikcleju@0: """ nikcleju@17: Some utility functions. nikcleju@0: nikcleju@17: Author: Nicolae Cleju 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@19: def replot_exact(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): nikcleju@19: """ nikcleju@19: Replot exact recovery results from mat file, with better axis ticks and nikcleju@19: other custom tweaked options. nikcleju@19: """ nikcleju@19: nikcleju@19: mdict = scipy.io.loadmat(filename) nikcleju@19: nikcleju@19: if algonames == None: nikcleju@19: if 'algonames' in mdict: nikcleju@19: algonames = mdict['algonames'] nikcleju@19: else: nikcleju@19: print "No algonames given, and couldn't find them in mat file." nikcleju@19: print "Exiting." nikcleju@19: return nikcleju@19: nikcleju@21: withticks = ['GAP'] nikcleju@21: withnoaxes = [algoname[0][0] for algoname in algonames if algoname not in withticks] nikcleju@21: loadshowmatrices_multipixels(filename, algonames, [], [], withticks, withnoaxes, doshow, dosave, saveplotbase, saveplotexts) nikcleju@19: nikcleju@19: def replot_approx(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): nikcleju@19: """ nikcleju@19: Replot exact recovery results from mat file, with better axis ticks and nikcleju@19: other custom tweaked options. nikcleju@19: """ nikcleju@19: nikcleju@19: mdict = scipy.io.loadmat(filename) nikcleju@19: nikcleju@19: if algonames == None: nikcleju@19: if 'algosNnames' in mdict and 'algosLnames' in mdict: nikcleju@21: nikcleju@21: if mdict['algosLnames'].size is 0: nikcleju@21: algonames = mdict['algosNnames'] nikcleju@21: elif mdict['algosNnames'].size is 0: nikcleju@21: algonames = mdict['algosLnames'] nikcleju@21: else: nikcleju@21: algonames = numpy.vstack((mdict['algosNnames'], mdict['algosLnames'])) nikcleju@19: else: nikcleju@19: print "No algonames given, and couldn't find them in mat file." nikcleju@19: print "Exiting." nikcleju@19: return nikcleju@19: nikcleju@19: if dosave: nikcleju@19: lambdas = mdict['lambdas'] nikcleju@19: threshs = [(0.85,2,0),(0.8,2,0.4),(0.5,2,1)] nikcleju@19: withticks = ['GAP'] nikcleju@19: withnoaxes = [algoname[0][0] for algoname in algonames if algoname not in withticks] nikcleju@19: #withnoaxes.remove('GAP') nikcleju@19: loadshowmatrices_multipixels(filename, algonames, lambdas, threshs, withticks, withnoaxes, doshow, dosave, saveplotbase, saveplotexts) nikcleju@19: nikcleju@19: def loadshowmatrices_multipixels(filename, algonames, lambdas, threshs = [], withticks = [], withnoaxes = [], 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@19: nikcleju@19: N = 10 # one data box = NxN nikcleju@19: 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@19: nikcleju@19: for thrval,width,color in threshs: nikcleju@19: bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,N,thrval,width,color) nikcleju@19: nikcleju@19: #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0) nikcleju@19: #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4) nikcleju@19: #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1) nikcleju@19: 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