def nice_nums(val):
    return evaluation.nice_nums(val)

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)
# 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
    # go
    res = learn_kalman_missing_patch(btchroma*mask,p1,p2,A,C,Q,R,initx,initV,
                                     niter,diagQ=1,diagR=1)
    recon = res[0]
    recon[:,:p1] = btchroma[:,:p1]
    recon[:,p2:] = btchroma[:,p2:]
    assert not np.isnan(recon).any(),'recon has NaN'
    assert not np.isinf(recon).any(),'recon has inf'
    recon[np.where(recon>1.)] = 1.
    recon[np.where(recon<0.)] = 0.


    # analysis
    print 'recon shape:',recon.shape
    div_eucl = evaluation.recon_error(btchroma,mask,recon,measure='eucl')
    div_kl = evaluation.recon_error(btchroma,mask,recon,measure='kl')
    print 'eucl div:',div_eucl
    print 'kl div:',div_kl
    
    # comparison
    import imputation as IMPUTATION
    print 'average, win=3'
    recon = IMPUTATION.average_patch(btchroma,mask,p1,p2,win=3)
    div_eucl = evaluation.recon_error(btchroma,mask,recon,measure='eucl')
    div_kl = evaluation.recon_error(btchroma,mask,recon,measure='kl')
    print 'eucl div:',div_eucl
    print 'kl div:',div_kl
    print 'kkn eucl:'
    recon,used_cols = IMPUTATION.knn_patch(btchroma,mask,p1,p2,win=7,measure='eucl')
    div_eucl = evaluation.recon_error(btchroma,mask,recon,measure='eucl')
Example #4
0
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 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)
originals = []
recons = []
texts = []



def nice_nums(val):
    return evaluation.nice_nums(val)


# EASY RECONSTRUCTION
bt = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32kENmats/beatles+Revolver+14-Tomorrow_Never_Knows.mp3.mat')['btchroma']
mask,p1,p2 = masking.random_patch_mask(bt,ncols=15,win=25)
recon,tmp = IMPUTATION.lintransform_patch(bt,mask,p1,p2,win=1)
errs = evaluation.recon_error(bt,mask,recon)
s = 'method: linear transform\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)


# SMOOTH RECONSTRUCTION
bt = sio.loadmat('/home/thierry/Columbia/covers80/coversongs/covers32kENmats/metallica+Live_Shit_Binge_And_Purge_Disc_3_+09-Stone_Cold_Crazy.mp3.mat')['btchroma']
mask,p1,p2 = masking.random_patch_mask(bt,ncols=15,win=25)
Example #7
0
                                     initx,
                                     initV,
                                     niter,
                                     diagQ=1,
                                     diagR=1)
    recon = res[0]
    recon[:, :p1] = btchroma[:, :p1]
    recon[:, p2:] = btchroma[:, p2:]
    assert not np.isnan(recon).any(), 'recon has NaN'
    assert not np.isinf(recon).any(), 'recon has inf'
    recon[np.where(recon > 1.)] = 1.
    recon[np.where(recon < 0.)] = 0.

    # analysis
    print 'recon shape:', recon.shape
    div_eucl = evaluation.recon_error(btchroma, mask, recon, measure='eucl')
    div_kl = evaluation.recon_error(btchroma, mask, recon, measure='kl')
    print 'eucl div:', div_eucl
    print 'kl div:', div_kl

    # comparison
    import imputation as IMPUTATION
    print 'average, win=3'
    recon = IMPUTATION.average_patch(btchroma, mask, p1, p2, win=3)
    div_eucl = evaluation.recon_error(btchroma, mask, recon, measure='eucl')
    div_kl = evaluation.recon_error(btchroma, mask, recon, measure='kl')
    print 'eucl div:', div_eucl
    print 'kl div:', div_kl
    print 'kkn eucl:'
    recon, used_cols = IMPUTATION.knn_patch(btchroma,
                                            mask,