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()
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 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