def errs_to_str(errs): s = 'eucl. = ' + nice_nums(errs['eucl']) s += ', delta diff. = ' + nice_nums(errs['ddiff']) s += ', D-ENT = ' + nice_nums(errs['dent']) return s # load btchroma, create mask btchroma = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32kENmats/john_lennon+Double_Fantasy+05-I_m_Losing_You.mp3.mat')['btchroma'] p1=185;p2=p1+15;mask=np.ones(btchroma.shape);mask[:,p1:p2]=0. # method: lintrans recon_lintrans,tmp = IMPUTATION.lintransform_patch(btchroma,mask,p1,p2,win=1) err_lintrans = evaluation.recon_error(btchroma,mask,recon_lintrans) # method: random recon_rand = IMPUTATION.random_patch(btchroma,mask,p1,p2) err_rand = evaluation.recon_error(btchroma,mask,recon_rand) # method: average recon_avg = IMPUTATION.average_patch(btchroma,mask,p1,p2,win=1) err_avg = evaluation.recon_error(btchroma,mask,recon_avg) # method: NN recon_nn,tmp = IMPUTATION.knn_patch(btchroma,mask,p1,p2,win=8,measure='eucl') err_nn = evaluation.recon_error(btchroma,mask,recon_nn) # method: SIPLCA recon_siplca = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma*mask).copy(), 50,mask,win=40, convergence_thresh=1e-15)[4] err_siplca = evaluation.recon_error(btchroma,mask,recon_siplca) # PLOT pargs = {'origin':'lower','interpolation':'nearest','cmap':P.cm.gray_r,
'/home/thierry/Columbia/covers80/coversongs/covers32kENmats/john_lennon+Double_Fantasy+05-I_m_Losing_You.mp3.mat' )['btchroma'] p1 = 185 p2 = p1 + 15 mask = np.ones(btchroma.shape) mask[:, p1:p2] = 0. # method: lintrans recon_lintrans, tmp = IMPUTATION.lintransform_patch(btchroma, mask, p1, p2, win=1) err_lintrans = evaluation.recon_error(btchroma, mask, recon_lintrans) # method: random recon_rand = IMPUTATION.random_patch(btchroma, mask, p1, p2) err_rand = evaluation.recon_error(btchroma, mask, recon_rand) # method: average recon_avg = IMPUTATION.average_patch(btchroma, mask, p1, p2, win=1) err_avg = evaluation.recon_error(btchroma, mask, recon_avg) # method: NN recon_nn, tmp = IMPUTATION.knn_patch(btchroma, mask, p1, p2, win=8, measure='eucl') err_nn = evaluation.recon_error(btchroma, mask, recon_nn) # method: SIPLCA recon_siplca = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma * mask).copy(), 50,
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 test_maskedpatch_on_dataset(dataset, method='random', ncols=2, win=1, rank=4, codebook=None, nstates=1, verbose=1, **kwargs): """ Dataset is either dir, or list of files General method to test a method on a whole dataset for one masked column Methods are: - random - randomfromsong - average - averageall - codebook - knn_eucl - knn_kl - knn_eucl_delta - lintrans - kalman - hmm - siplca - siplca2 Used arguments vary based on the method. For SIPLCA, we can use **kwargs to set priors. RETURN - all errors, as a list of dict """ # get all matfiles if type(dataset).__name__ == 'str': matfiles = get_all_matfiles(dataset) else: matfiles = dataset # init total_cnt = 0 all_errs = [] # some specific inits if codebook != None and not type(codebook) == type([]): codebook = [p.reshape(12, codebook.shape[1] / 12) for p in codebook] print 'codebook in ndarray format transformed to list' # iterate for matfile in matfiles: btchroma = sio.loadmat(matfile)['btchroma'] if len(btchroma.shape) < 2: continue if btchroma.shape[1] < MINLENGTH or np.isnan(btchroma).any(): continue mask, p1, p2 = MASKING.random_patch_mask(btchroma, ncols=ncols, win=25) ########## ALGORITHM DEPENDENT if method == 'random': recon = IMPUTATION.random_patch(btchroma, mask, p1, p2) elif method == 'randomfromsong': recon = IMPUTATION.random_patch_from_song(btchroma, mask, p1, p2) elif method == 'average': recon = IMPUTATION.average_patch(btchroma, mask, p1, p2, win=win) elif method == 'averageall': recon = IMPUTATION.average_patch_all(btchroma, mask, p1, p2) elif method == 'codebook': recon, used_codes = IMPUTATION.codebook_patch( btchroma, mask, p1, p2, codebook) elif method == 'knn_eucl': recon, used_cols = IMPUTATION.knn_patch(btchroma, mask, p1, p2, win=win, measure='eucl') elif method == 'knn_kl': recon, used_cols = IMPUTATION.knn_patch(btchroma, mask, p1, p2, win=win, measure='kl') elif method == 'knn_eucl_delta': recon, used_cols = IMPUTATION.knn_patch_delta(btchroma, mask, p1, p2, win=win, measure='eucl') elif method == 'lintrans': recon, proj = IMPUTATION.lintransform_patch(btchroma, mask, p1, p2, win=win) elif method == 'kalman': import kalman_toolbox as KALMAN recon = KALMAN.imputation(btchroma, p1, p2, dimstates=nstates, **kwargs) elif method == 'hmm': recon, recon2, hmm = IMPUTATION_HMM.imputation(btchroma * mask, p1, p2, nstates=nstates, **kwargs) elif method == 'siplca': res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma * mask).copy(), rank, mask, win=win, convergence_thresh=1e-15, **kwargs) W, Z, H, norm, recon, logprob = res elif method == 'siplca2': res = IMPUTATION_PLCA.SIPLCA2_mask.analyze( (btchroma * mask).copy(), rank, mask, win=win, convergence_thresh=1e-15, **kwargs) W, Z, H, norm, recon, logprob = res else: print 'unknown method:', method return ########## ALGORITHM DEPENDENT END # measure recon errs = recon_error(btchroma, mask, recon, measure='all') all_errs.append(errs) total_cnt += 1 # done if verbose > 0: print 'number of songs tested:', total_cnt for meas in np.sort(all_errs[0].keys()): errs = map(lambda d: d[meas], all_errs) print 'average', meas, '=', np.mean(errs), '(', np.std(errs), ')' return all_errs
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 test_maskedpatch_on_dataset(dataset,method='random',ncols=2,win=1,rank=4,codebook=None,nstates=1,verbose=1,**kwargs): """ Dataset is either dir, or list of files General method to test a method on a whole dataset for one masked column Methods are: - random - randomfromsong - average - averageall - codebook - knn_eucl - knn_kl - knn_eucl_delta - lintrans - kalman - hmm - siplca - siplca2 Used arguments vary based on the method. For SIPLCA, we can use **kwargs to set priors. RETURN - all errors, as a list of dict """ # get all matfiles if type(dataset).__name__ == 'str': matfiles = get_all_matfiles(dataset) else: matfiles = dataset # init total_cnt = 0 all_errs = [] # some specific inits if codebook != None and not type(codebook) == type([]): codebook = [p.reshape(12,codebook.shape[1]/12) for p in codebook] print 'codebook in ndarray format transformed to list' # iterate for matfile in matfiles: btchroma = sio.loadmat(matfile)['btchroma'] if len(btchroma.shape) < 2: continue if btchroma.shape[1] < MINLENGTH or np.isnan(btchroma).any(): continue mask,p1,p2 = MASKING.random_patch_mask(btchroma,ncols=ncols,win=25) ########## ALGORITHM DEPENDENT if method == 'random': recon = IMPUTATION.random_patch(btchroma,mask,p1,p2) elif method == 'randomfromsong': recon = IMPUTATION.random_patch_from_song(btchroma,mask,p1,p2) elif method == 'average': recon = IMPUTATION.average_patch(btchroma,mask,p1,p2,win=win) elif method == 'averageall': recon = IMPUTATION.average_patch_all(btchroma,mask,p1,p2) elif method == 'codebook': recon,used_codes = IMPUTATION.codebook_patch(btchroma,mask,p1,p2,codebook) elif method == 'knn_eucl': recon,used_cols = IMPUTATION.knn_patch(btchroma,mask,p1,p2,win=win,measure='eucl') elif method == 'knn_kl': recon,used_cols = IMPUTATION.knn_patch(btchroma,mask,p1,p2,win=win,measure='kl') elif method == 'knn_eucl_delta': recon,used_cols = IMPUTATION.knn_patch_delta(btchroma,mask,p1,p2,win=win,measure='eucl') elif method == 'lintrans': recon,proj = IMPUTATION.lintransform_patch(btchroma,mask,p1,p2,win=win) elif method == 'kalman': import kalman_toolbox as KALMAN recon = KALMAN.imputation(btchroma,p1,p2, dimstates=nstates,**kwargs) elif method == 'hmm': recon,recon2,hmm = IMPUTATION_HMM.imputation(btchroma*mask,p1,p2, nstates=nstates, **kwargs) elif method == 'siplca': res = IMPUTATION_PLCA.SIPLCA_mask.analyze((btchroma*mask).copy(), rank,mask,win=win, convergence_thresh=1e-15, **kwargs) W, Z, H, norm, recon, logprob = res elif method == 'siplca2': res = IMPUTATION_PLCA.SIPLCA2_mask.analyze((btchroma*mask).copy(), rank,mask,win=win, convergence_thresh=1e-15, **kwargs) W, Z, H, norm, recon, logprob = res else: print 'unknown method:',method return ########## ALGORITHM DEPENDENT END # measure recon errs = recon_error(btchroma,mask,recon,measure='all') all_errs.append( errs ) total_cnt += 1 # done if verbose > 0: print 'number of songs tested:',total_cnt for meas in np.sort(all_errs[0].keys()): errs = map(lambda d: d[meas], all_errs) print 'average',meas,'=',np.mean(errs),'(',np.std(errs),')' return all_errs
s = 'method: NN\n' s += 'eucl = ' + nice_nums(errs['eucl']) + '\n' s += 'd1/2 = ' + nice_nums(errs['lhalf']) + '\n' s += 'cond. ent. = ' + nice_nums(errs['condent']) + '\n' s += 'delta diff. = ' + nice_nums(errs['ddiff']) + '\n' s += 'D-ENT = ' + nice_nums(errs['dent']) + '\n' s += 'Jensen diff. = ' + nice_nums(errs['jdiff']) + '\n' originals.append( bt[:,p1:p2].copy() ) recons.append( recon[:,p1:p2].copy() ) texts.append(s) # RANDOM RECON bt = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32k/Caroline_No/beach_boys+Pet_Sounds+13-Caroline_No.mp3.mat')['btchroma'] mask,p1,p2 = masking.random_patch_mask(bt,ncols=15,win=25) recon = IMPUTATION.random_patch(bt,mask,p1,p2) errs = evaluation.recon_error(bt,mask,recon) s = 'method: Random\n' s += 'eucl = ' + nice_nums(errs['eucl']) + '\n' s += 'd1/2 = ' + nice_nums(errs['lhalf']) + '\n' s += 'cond. ent. = ' + nice_nums(errs['condent']) + '\n' s += 'delta diff. = ' + nice_nums(errs['ddiff']) + '\n' s += 'D-ENT = ' + nice_nums(errs['dent']) + '\n' s += 'Jensen diff. = ' + nice_nums(errs['jdiff']) + '\n' originals.append( bt[:,p1:p2].copy() ) recons.append( recon[:,p1:p2].copy() ) texts.append(s) # SIPLCA bt = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32kENmats/garth_brooks+The_Chase+05-Walking_After_Midnight.mp3.mat')['btchroma'] mask,p1,p2 = masking.random_patch_mask(bt,ncols=15,win=25)