def plot(cls, V, W, Z, H, curriter=-1): WZH = cls.reconstruct(W, Z, H) plottools.plotall([V, WZH], subplot=(3, 1), align='xy', cmap=plt.cm.hot) plottools.plotall( 9 * [None] + [W, Z, H], subplot=(4, 3), clf=False, align='', cmap=plt.cm.hot, colorbar=False) plt.draw()
def plot(cls, V, W, Z, H, curriter=-1): WZH = cls.reconstruct(W, Z, H) plottools.plotall([V, WZH], subplot=(3, 1), align='xy', cmap=plt.cm.hot) plottools.plotall(9 * [None] + [W, Z, H], subplot=(4, 3), clf=False, align='', cmap=plt.cm.hot, colorbar=False) plt.draw()
def plot(self, V, W, Z, H, curriter=-1): rank = len(Z) nrows = rank + 2 WZH = self.reconstruct(W, Z, H, circular=self.circular) plottools.plotall([V, WZH] + [self.reconstruct(W[:,z,:], Z[z], H[z,:], circular=self.circular) for z in xrange(len(Z))], title=['V (Iteration %d)' % curriter, 'Reconstruction'] + ['Basis %d reconstruction' % x for x in xrange(len(Z))], colorbar=False, grid=False, cmap=plt.cm.hot, subplot=(nrows, 2), order='c', align='xy') plottools.plotall([None] + [Z], subplot=(nrows, 2), clf=False, plotfun=lambda x: plt.bar(np.arange(len(x)) - 0.4, x), xticks=[[], range(rank)], grid=False, colorbar=False, title='Z') plots = [None] * (3*nrows + 2) titles = plots + ['W%d' % x for x in range(rank)] wxticks = [[]] * (3*nrows + rank + 1) + [range(0, W.shape[2], 10)] plots.extend(W.transpose((1, 0, 2))) plottools.plotall(plots, subplot=(nrows, 6), clf=False, order='c', align='xy', cmap=plt.cm.hot, colorbar=False, ylabel=r'$\parallel$', grid=False, title=titles, yticks=[[]], xticks=wxticks) plots = [None] * (2*nrows + 2) titles=plots + ['H%d' % x for x in range(rank)] if np.squeeze(H).ndim < 4: plotH = np.squeeze(H) else: plotH = H.sum(2) if rank == 1: plotH = [plotH] plots.extend(plotH) plottools.plotall(plots, subplot=(nrows, 3), order='c', align='xy', grid=False, clf=False, title=titles, yticks=[[]], colorbar=False, cmap=plt.cm.hot, ylabel=r'$*$', xticks=[[]]*(3*nrows-1) + [range(0, V.shape[1], 100)]) plt.draw()
if __name__ == '__main__': btchroma145 = sio.loadmat( '/home/thierry/Columbia/covers80/coversongs/covers32k/Caroline_No/beach_boys+Pet_Sounds+13-Caroline_No.mp3.mat' )['btchroma'] mask, masked_cols = masking.random_col_mask(btchroma145, ncols=1, win=30) recon, lt = imputation.lintransform_cols(btchroma145, mask, masked_cols, win=1) pos1 = masked_cols[0] - 7 pos2 = masked_cols[0] + 7 im1 = btchroma145[:, pos1:pos2].copy() im2 = (btchroma145 * mask)[:, pos1:pos2].copy() im3 = recon[:, pos1:pos2].copy() # plot all this fig = P.figure() fig.subplots_adjust(hspace=0.4) blackbarsfun = lambda: P.gca().axvline(linewidth=2, color='0.', x=6.5 ) and P.gca().axvline( linewidth=2, color='0.', x=7.5) plotall([im1, im2, im3], subplot=(3, 1), cmap='gray_r', title=['original', 'original masked', 'reconstruction'], axvlines=blackbarsfun, colorbar=False, xticks=[()] * 3) P.show()
def compare_all(btchroma, mask, masked_cols, codebook=None): """ Compare all the algorithms we have so far. A lot of parameters hard-coded, but... It will get improved. We display: 1) original beat chromagram 2) masked beat chromagram 3) imputation by random 4) imputation by averaging nearby columns 5) imputation by knn on the rest of the song 6) imputation by linear prediction 7) imputation by NMF 8) imputation by codebook, if provided To init mask and masked_cols, something like: mask,masked_cols = masking.random_col_mask(btchroma,ncols=1,win=30) """ # for displaying purposes, display window size 50 pos1 = masked_cols[0] - 5 pos2 = masked_cols[0] + 5 allimages = [] titles = [] xlabels = [] # original beat chroma and masked one im1 = btchroma[:, pos1:pos2].copy() im2 = (btchroma * mask)[:, pos1:pos2].copy() allimages.append(im1) titles.append('original beat chroma') xlabels.append('') allimages.append(im2) titles.append('masked beat chroma') xlabels.append('') # 3) random recon = IMPUTATION.random_col(btchroma, mask, masked_cols) im3_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im3 = recon[:, pos1:pos2].copy() allimages.append(im3) titles.append('random') xlabels.append('err=' + str(im3_err)) # 4) average nearby columns recon = IMPUTATION.average_col(btchroma, mask, masked_cols, win=3) im4_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im4 = recon[:, pos1:pos2].copy() allimages.append(im4) titles.append('average 2 nearby cols') xlabels.append('err=' + str(im4_err)) # 5) knn recon, used_cols = IMPUTATION.eucldist_cols(btchroma, mask, masked_cols, win=7) im5_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im5 = recon[:, pos1:pos2].copy() allimages.append(im5) titles.append('knn for the whole song') xlabels.append('err=' + str(im5_err)) # 6) linear prediction recon, proj = IMPUTATION.lintransform_cols(btchroma, mask, masked_cols, win=1) im6_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im6 = recon[:, pos1:pos2].copy() allimages.append(im6) titles.append('linear prediction (1 col)') xlabels.append('err=' + str(im6_err)) # 7) SIPLCA res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma * mask).copy(), 4, mask, win=5) W, Z, H, norm, recon, logprob = res im7_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im7 = recon[:, pos1:pos2].copy() allimages.append(im7) titles.append('SIPLCA, rank=4, win=5') xlabels.append('err=' + str(im7_err)) # 7) SIPLCA 2 res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma * mask).copy(), 25, mask, win=10) W, Z, H, norm, recon, logprob = res im7_err_bis = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im7_bis = recon[:, pos1:pos2].copy() allimages.append(im7_bis) titles.append('SIPLCA, rank=25, win=10') xlabels.append('err=' + str(im7_err_bis)) # 8) codebook if codebook != None: cb = [p.reshape(12, p.size / 12) for p in codebook] recon, used_codes = IMPUTATION.codebook_cols(btchroma, mask, masked_cols, cb) im8_err = EVAL.recon_error(btchroma, mask, recon, measure='eucl') im8 = recon[:, pos1:pos2].copy() allimages.append(im8) titles.append('codebook, ' + str(codebook.shape[0]) + ' codes of length ' + str(codebook.shape[1] / 12)) xlabels.append('err=' + str(im8_err)) # ALL IMAGES CREATED fig = plt.figure() fig.subplots_adjust(hspace=0.4) blackbarsfun = lambda: plt.gca().axvline( linewidth=2, color='0.', x=4.5) and plt.gca().axvline( linewidth=2, color='0.', x=5.5) # plotall plotall(allimages, subplot=(3, 3), cmap='gray_r', title=titles, xlabel=xlabels, axvlines=blackbarsfun, colorbar=False)
def plot_oneexample(btchroma, mask, p1, p2, methods=(), methods_args=None, measures=(), subplot=None, plotrange=None, **kwargs): """ Utility function to plot the result of many methods on one example. Methods and arguments are the same as when we test on a whole dataset. First two plots are always original then original*mask. INPUT btchroma - btchroma mask - binary mask, 0 = hidden p1 - first masked column p2 - last masekdcolumn + 1 methods - list of methods (lintrans,random,....) bethods_args - list of dictionary containing arguments measures - list of measures, put in title (we sort according to first) subplot - if None, images in one column plotrange - if we dont want to plot everything set to (pos1,pos2) kwargs - extra arguments passed to plotall """ from plottools import plotall # inits and sanity checks for meas in measures: assert meas in ('eucl', 'kl', 'cos', 'dent', 'lhalf', 'ddiff', 'jdiff', 'thresh', 'leven', 'condent'), 'unknown measure: ' + meas nmethods = len(methods) assert nmethods > 0, 'we need at least one method...!' if methods_args is None: methods_args = [{}] * len(methods) if subplot is None: subplot = (nmethods + 2, 1) assert np.zeros(subplot).size == len( methods) + 2, 'wrong subplot, forgot original and masked original?' if plotrange is None: plotrange = (0, btchroma.shape[1]) # impute errs = [] recons = [] ########## ALGORITHM DEPENDENT for im, method in enumerate(methods): if method == 'random': recon = IMPUTATION.random_patch(btchroma, mask, p1, p2, **methods_args[im]) elif method == 'randomfromsong': recon = IMPUTATION.random_patch_from_song(btchroma, mask, p1, p2, **methods_args[im]) elif method == 'average': recon = IMPUTATION.average_patch(btchroma, mask, p1, p2, **methods_args[im]) elif method == 'averageall': recon = IMPUTATION.average_patch_all(btchroma, mask, p1, p2, **methods_args[im]) elif method == 'codebook': recon, used_codes = IMPUTATION.codebook_patch( btchroma, mask, p1, p2, **methods_args[im]) elif method == 'knn_eucl': recon, used_cols = IMPUTATION.knn_patch(btchroma, mask, p1, p2, measure='eucl', **methods_args[im]) elif method == 'knn_kl': recon, used_cols = IMPUTATION.knn_patch(btchroma, mask, p1, p2, measure='kl', **methods_args[im]) elif method == 'lintrans': recon, proj = IMPUTATION.lintransform_patch( btchroma, mask, p1, p2, **methods_args[im]) elif method == 'kalman': import kalman_toolbox as KALMAN recon = KALMAN.imputation(btchroma, p1, p2, **methods_args[im]) elif method == 'hmm': recon, recon2, hmm = IMPUTATION_HMM.imputation( btchroma * mask, p1, p2, **methods_args[im]) elif method == 'siplca': rank = methods_args[im]['rank'] res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma * mask).copy(), rank, mask, convergence_thresh=1e-15, **methods_args[im]) W, Z, H, norm, recon, logprob = res elif method == 'siplca2': rank = methods_args[im]['rank'] res = IMPUTATION_PLCA.SIPLCA2_mask.analyze( (btchroma * mask).copy(), rank, mask, convergence_thresh=1e-15, **methods_args[im]) W, Z, H, norm, recon, logprob = res else: print 'unknown method:', method return # compute errors recons.append(recon.copy()) errs.append(recon_error(btchroma, mask, recon, 'all')) # all methods computed # now we sort the results according to first measure main_errs = map(lambda x: x[measures[0]], errs) order = np.array(np.argsort(main_errs)) methods = map(lambda i: methods[i], order) errs = map(lambda i: errs[i], order) recons = map(lambda i: recons[i], order) # plot import pylab as P P.figure() pos1 = plotrange[0] pos2 = plotrange[1] ims = [btchroma[:, pos1:pos2], (btchroma * mask)[:, pos1:pos2]] ims.extend(map(lambda r: r[:, pos1:pos2], recons)) titles = ['original', 'original masked'] for imethod, method in enumerate(methods): t = method + ':' for m in measures: t += ' ' + m + '=' + nice_nums(errs[imethod][m]) titles.append(t) plotall(ims, subplot=subplot, title=titles, cmap='gray_r', colorbar=False) P.show()
def plot_oneexample(btchroma,mask,p1,p2,methods=(),methods_args=None, measures=(),subplot=None,plotrange=None,**kwargs): """ Utility function to plot the result of many methods on one example. Methods and arguments are the same as when we test on a whole dataset. First two plots are always original then original*mask. INPUT btchroma - btchroma mask - binary mask, 0 = hidden p1 - first masked column p2 - last masekdcolumn + 1 methods - list of methods (lintrans,random,....) bethods_args - list of dictionary containing arguments measures - list of measures, put in title (we sort according to first) subplot - if None, images in one column plotrange - if we dont want to plot everything set to (pos1,pos2) kwargs - extra arguments passed to plotall """ from plottools import plotall # inits and sanity checks for meas in measures: assert meas in ('eucl','kl','cos','dent','lhalf','ddiff','jdiff', 'thresh','leven','condent'),'unknown measure: '+meas nmethods = len(methods) assert nmethods > 0,'we need at least one method...!' if methods_args is None: methods_args = [{}]*len(methods) if subplot is None: subplot = (nmethods+2,1) assert np.zeros(subplot).size == len(methods)+2,'wrong subplot, forgot original and masked original?' if plotrange is None: plotrange = (0,btchroma.shape[1]) # impute errs = [] recons = [] ########## ALGORITHM DEPENDENT for im,method in enumerate(methods): if method == 'random': recon = IMPUTATION.random_patch(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'randomfromsong': recon = IMPUTATION.random_patch_from_song(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'average': recon = IMPUTATION.average_patch(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'averageall': recon = IMPUTATION.average_patch_all(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'codebook': recon,used_codes = IMPUTATION.codebook_patch(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'knn_eucl': recon,used_cols = IMPUTATION.knn_patch(btchroma,mask,p1,p2,measure='eucl',**methods_args[im]) elif method == 'knn_kl': recon,used_cols = IMPUTATION.knn_patch(btchroma,mask,p1,p2,measure='kl',**methods_args[im]) elif method == 'lintrans': recon,proj = IMPUTATION.lintransform_patch(btchroma,mask,p1,p2,**methods_args[im]) elif method == 'kalman': import kalman_toolbox as KALMAN recon = KALMAN.imputation(btchroma,p1,p2,**methods_args[im]) elif method == 'hmm': recon,recon2,hmm = IMPUTATION_HMM.imputation(btchroma*mask,p1,p2, **methods_args[im]) elif method == 'siplca': rank = methods_args[im]['rank'] res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma*mask).copy(), rank,mask, convergence_thresh=1e-15, **methods_args[im]) W, Z, H, norm, recon, logprob = res elif method == 'siplca2': rank = methods_args[im]['rank'] res = IMPUTATION_PLCA.SIPLCA2_mask.analyze((btchroma*mask).copy(), rank,mask, convergence_thresh=1e-15, **methods_args[im]) W, Z, H, norm, recon, logprob = res else: print 'unknown method:',method return # compute errors recons.append( recon.copy() ) errs.append( recon_error(btchroma,mask,recon,'all') ) # all methods computed # now we sort the results according to first measure main_errs = map(lambda x: x[measures[0]], errs) order = np.array(np.argsort(main_errs)) methods = map(lambda i: methods[i], order) errs = map(lambda i: errs[i], order) recons = map(lambda i: recons[i], order) # plot import pylab as P P.figure() pos1 = plotrange[0] pos2 = plotrange[1] ims = [btchroma[:,pos1:pos2],(btchroma*mask)[:,pos1:pos2]] ims.extend( map(lambda r: r[:,pos1:pos2], recons) ) titles = ['original', 'original masked'] for imethod,method in enumerate(methods): t = method + ':' for m in measures: t += ' ' + m + '=' + nice_nums(errs[imethod][m]) titles.append(t) plotall(ims,subplot=subplot,title=titles,cmap='gray_r',colorbar=False) P.show()
import numpy as np import scipy.io as sio from plottools import plotall import pylab as P sys.path.append('../PythonSrc') import evaluation import masking import imputation if __name__ == '__main__': btchroma145 = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32k/Caroline_No/beach_boys+Pet_Sounds+13-Caroline_No.mp3.mat')['btchroma'] mask,masked_cols = masking.random_col_mask(btchroma145,ncols=1,win=30) recon,lt = imputation.lintransform_cols(btchroma145,mask,masked_cols,win=1) pos1 = masked_cols[0] - 7 pos2 = masked_cols[0] + 7 im1 = btchroma145[:,pos1:pos2].copy() im2 = (btchroma145 * mask)[:,pos1:pos2].copy() im3 = recon[:,pos1:pos2].copy() # plot all this fig = P.figure() fig.subplots_adjust(hspace=0.4) blackbarsfun = lambda: P.gca().axvline(linewidth=2,color='0.',x=6.5) and P.gca().axvline(linewidth=2,color='0.',x=7.5) plotall([im1,im2,im3],subplot=(3,1),cmap='gray_r', title=['original','original masked','reconstruction'], axvlines=blackbarsfun,colorbar=False,xticks=[()]*3) P.show()
def compare_all(btchroma,mask,masked_cols,codebook=None): """ Compare all the algorithms we have so far. A lot of parameters hard-coded, but... It will get improved. We display: 1) original beat chromagram 2) masked beat chromagram 3) imputation by random 4) imputation by averaging nearby columns 5) imputation by knn on the rest of the song 6) imputation by linear prediction 7) imputation by NMF 8) imputation by codebook, if provided To init mask and masked_cols, something like: mask,masked_cols = masking.random_col_mask(btchroma,ncols=1,win=30) """ # for displaying purposes, display window size 50 pos1 = masked_cols[0] - 5 pos2 = masked_cols[0] + 5 allimages = [] titles = [] xlabels = [] # original beat chroma and masked one im1 = btchroma[:,pos1:pos2].copy() im2 = (btchroma * mask)[:,pos1:pos2].copy() allimages.append(im1) titles.append('original beat chroma') xlabels.append('') allimages.append(im2) titles.append('masked beat chroma') xlabels.append('') # 3) random recon = IMPUTATION.random_col(btchroma,mask,masked_cols) im3_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im3 = recon[:,pos1:pos2].copy() allimages.append(im3) titles.append('random') xlabels.append('err='+str(im3_err)) # 4) average nearby columns recon = IMPUTATION.average_col(btchroma,mask,masked_cols,win=3) im4_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im4 = recon[:,pos1:pos2].copy() allimages.append(im4) titles.append('average 2 nearby cols') xlabels.append('err='+str(im4_err)) # 5) knn recon,used_cols = IMPUTATION.eucldist_cols(btchroma,mask,masked_cols,win=7) im5_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im5 = recon[:,pos1:pos2].copy() allimages.append(im5) titles.append('knn for the whole song') xlabels.append('err='+str(im5_err)) # 6) linear prediction recon,proj = IMPUTATION.lintransform_cols(btchroma,mask,masked_cols,win=1) im6_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im6 = recon[:,pos1:pos2].copy() allimages.append(im6) titles.append('linear prediction (1 col)') xlabels.append('err='+str(im6_err)) # 7) SIPLCA res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma*mask).copy(), 4,mask,win=5) W, Z, H, norm, recon, logprob = res im7_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im7 = recon[:,pos1:pos2].copy() allimages.append(im7) titles.append('SIPLCA, rank=4, win=5') xlabels.append('err='+str(im7_err)) # 7) SIPLCA 2 res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma*mask).copy(), 25,mask,win=10) W, Z, H, norm, recon, logprob = res im7_err_bis = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im7_bis = recon[:,pos1:pos2].copy() allimages.append(im7_bis) titles.append('SIPLCA, rank=25, win=10') xlabels.append('err='+str(im7_err_bis)) # 8) codebook if codebook != None: cb = [p.reshape(12,p.size/12) for p in codebook] recon,used_codes = IMPUTATION.codebook_cols(btchroma,mask,masked_cols,cb) im8_err = EVAL.recon_error(btchroma,mask,recon,measure='eucl') im8 = recon[:,pos1:pos2].copy() allimages.append(im8) titles.append('codebook, '+str(codebook.shape[0])+' codes of length '+str(codebook.shape[1]/12)) xlabels.append('err='+str(im8_err)) # ALL IMAGES CREATED fig = plt.figure() fig.subplots_adjust(hspace=0.4) blackbarsfun = lambda: plt.gca().axvline(linewidth=2,color='0.',x=4.5) and plt.gca().axvline(linewidth=2,color='0.',x=5.5) # plotall plotall(allimages,subplot=(3,3),cmap='gray_r',title=titles,xlabel=xlabels,axvlines=blackbarsfun,colorbar=False)