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)
Esempio n. 2
0
def test_maskedcol_on_dataset(datasetdir,method='random',ncols=1,win=3,rank=4,codebook=None,**kwargs):
    """
    General method to test a method on a whole dataset for one masked column
    Methods are:
      - random
      - randomfromsong
      - average
      - codebook
      - knn_eucl
      - knn_kl
      - lintrans
      - siplca
      - siplca2
    Used arguments vary based on the method. For SIPLCA, we can use **kwargs
    to set priors.
    """
    # get all matfiles
    matfiles = get_all_matfiles(datasetdir)
    # init
    total_cnt = 0
    errs_eucl = []
    errs_kl = []
    # 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,masked_cols = MASKING.random_col_mask(btchroma,ncols=ncols,win=25)
        ########## ALGORITHM DEPENDENT
        if method == 'random':
            recon = IMPUTATION.random_col(btchroma,mask,masked_cols)
        elif method == 'randomfromsong':
            recon = IMPUTATION.random_col_from_song(btchroma,mask,masked_cols)
        elif method == 'average':
            recon = IMPUTATION.average_col(btchroma,mask,masked_cols,win=win)
        elif method == 'codebook':
            recon,used_codes = IMPUTATION.codebook_cols(btchroma,mask,masked_cols,codebook)
        elif method == 'knn_eucl':
            recon,used_cols = IMPUTATION.knn_cols(btchroma,mask,masked_cols,win=win,measure='eucl')
        elif method == 'knn_kl':
            recon,used_cols = IMPUTATION.knn_cols(btchroma,mask,masked_cols,win=win,measure='kl')
        elif method == 'lintrans':
            recon,proj = IMPUTATION.lintransform_cols(btchroma,mask,masked_cols,win=win)
        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
        err = recon_error(btchroma,mask,recon,measure='eucl')
        if err > 100:
            print 'huge EUCL error:',err,', method =',method,',file =',matfile
        errs_eucl.append( err )
        err = recon_error(btchroma,mask,recon,measure='kl')
        if err > 100:
            print 'huge KL error:',err,', method =',method,',file =',matfile
        errs_kl.append( err )
        total_cnt += 1
    # done
    print 'number of songs tested:',total_cnt
    print 'average sq euclidean dist:',np.mean(errs_eucl),'(',np.std(errs_eucl),')'
    print 'average kl divergence:',np.mean(errs_kl),'(',np.std(errs_kl),')'
Esempio n. 3
0
def test_maskedcol_on_dataset(datasetdir,
                              method='random',
                              ncols=1,
                              win=3,
                              rank=4,
                              codebook=None,
                              **kwargs):
    """
    General method to test a method on a whole dataset for one masked column
    Methods are:
      - random
      - randomfromsong
      - average
      - codebook
      - knn_eucl
      - knn_kl
      - lintrans
      - siplca
      - siplca2
    Used arguments vary based on the method. For SIPLCA, we can use **kwargs
    to set priors.
    """
    # get all matfiles
    matfiles = get_all_matfiles(datasetdir)
    # init
    total_cnt = 0
    errs_eucl = []
    errs_kl = []
    # 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, masked_cols = MASKING.random_col_mask(btchroma,
                                                    ncols=ncols,
                                                    win=25)
        ########## ALGORITHM DEPENDENT
        if method == 'random':
            recon = IMPUTATION.random_col(btchroma, mask, masked_cols)
        elif method == 'randomfromsong':
            recon = IMPUTATION.random_col_from_song(btchroma, mask,
                                                    masked_cols)
        elif method == 'average':
            recon = IMPUTATION.average_col(btchroma,
                                           mask,
                                           masked_cols,
                                           win=win)
        elif method == 'codebook':
            recon, used_codes = IMPUTATION.codebook_cols(
                btchroma, mask, masked_cols, codebook)
        elif method == 'knn_eucl':
            recon, used_cols = IMPUTATION.knn_cols(btchroma,
                                                   mask,
                                                   masked_cols,
                                                   win=win,
                                                   measure='eucl')
        elif method == 'knn_kl':
            recon, used_cols = IMPUTATION.knn_cols(btchroma,
                                                   mask,
                                                   masked_cols,
                                                   win=win,
                                                   measure='kl')
        elif method == 'lintrans':
            recon, proj = IMPUTATION.lintransform_cols(btchroma,
                                                       mask,
                                                       masked_cols,
                                                       win=win)
        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
        err = recon_error(btchroma, mask, recon, measure='eucl')
        if err > 100:
            print 'huge EUCL error:', err, ', method =', method, ',file =', matfile
        errs_eucl.append(err)
        err = recon_error(btchroma, mask, recon, measure='kl')
        if err > 100:
            print 'huge KL error:', err, ', method =', method, ',file =', matfile
        errs_kl.append(err)
        total_cnt += 1
    # done
    print 'number of songs tested:', total_cnt
    print 'average sq euclidean dist:', np.mean(errs_eucl), '(', np.std(
        errs_eucl), ')'
    print 'average kl divergence:', np.mean(errs_kl), '(', np.std(errs_kl), ')'
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()