SimilarityFusion.py
def doSimilarityFusionWs(Ws, K = 5, NIters = 20, reg = 1, PlotNames = [], verboseTimes = False):
    """
    Perform similarity fusion between a set of exponentially
    weighted similarity matrices
    :param Ws: An array of NxN affinity matrices for N songs
    :param K: Number of nearest neighbors
    :param NIters: Number of iterations
    :param reg: Identity matrix regularization parameter for
        self-similarity promotion
    :param PlotNames: Strings describing different similarity
        measurements. If this array is specified, an
        animation will be saved of the cross-diffusion process
    :return D: A fused NxN similarity matrix
    """
    tic = time.time()
    #Full probability matrices
    Ps = [getP(W) for W in Ws]
    #Nearest neighbor truncated matrices
    Ss = [getS(W, K) for W in Ws]

    #Now do cross-diffusion iterations
    Pts = [np.array(P) for P in Ps]
    nextPts = [np.zeros(P.shape) for P in Pts]
    if verboseTimes:
        print("Time getting Ss and Ps: %g"%(time.time() - tic))

    N = len(Pts)
    AllTimes = []
    for it in range(NIters):
        if len(PlotNames) == N:
            k = int(np.ceil(np.sqrt(N)))
            for i in range(N):
                plt.subplot(k, k, i+1)
                Im = 1.0*Pts[i]
                Idx = np.arange(Im.shape[0], dtype=np.int64)
                Im[Idx, Idx] = 0
                if Im.shape[0] > 400:
                    Im = interp.zoom(Im, 400.0/Im.shape[0])
                plt.imshow(Im, interpolation = 'none', cmap = 'afmhot')
                plt.title(PlotNames[i])
                plt.axis('off')
            plt.savefig("SSMFusion%i.png"%it, dpi=150, bbox_inches='tight')
        for i in range(N):
            nextPts[i] *= 0
            tic = time.time()
            for k in range(N):
                if i == k:
                    continue
                nextPts[i] += Pts[k]
            nextPts[i] /= float(N-1)

            #tic = time.time()
            #nextPts[i] = SsD[i].dot(nextPts[i].dot(SsD[i].T))
            #toc = time.time()
            #print(toc - tic, " ")

            #Need S*P*S^T, but have to multiply sparse matrix on the left
            tic = time.time()
            A = Ss[i].dot(nextPts[i].T)
            nextPts[i] = Ss[i].dot(A.T)
            toc = time.time()
            AllTimes.append(toc - tic)

            if reg > 0:
                nextPts[i] += reg*np.eye(nextPts[i].shape[0])

        Pts = nextPts
    if verboseTimes:
        print("Total Time multiplying: %g"%np.sum(np.array(AllTimes)))
    FusedScores = np.zeros(Pts[0].shape)
    for Pt in Pts:
        FusedScores += Pt
    return FusedScores/N

    Perform similarity fusion between a set of exponentially
    weighted similarity matrices
    :param Ws: An array of NxN affinity matrices for N songs
    :param K: Number of nearest neighbors
    :param NIters: Number of iterations
    :param reg: Identity matrix regularization parameter for
        self-similarity promotion
    :param PlotNames: Strings describing different similarity
        measurements. If this array is specified, an
        animation will be saved of the cross-diffusion process
    :return D: A fused NxN similarity matrix
    
Perform similarity fusion between a set of exponentially
weighted similarity matrices
Perform similarity fusion between a set of exponentially weighted similarity matrices
def doSimilarityFusionWs(Ws, K = 5, NIters = 20, reg = 1, PlotNames = [], verboseTimes = False):
    tic = time.time()
    Ps = [getP(W) for W in Ws]
    Ss = [getS(W, K) for W in Ws]
    Pts = [np.array(P) for P in Ps]
    nextPts = [np.zeros(P.shape) for P in Pts]
    if verboseTimes:
        print("Time getting Ss and Ps: %g"%(time.time() - tic))
    N = len(Pts)
    AllTimes = []
    for it in range(NIters):
        if len(PlotNames) == N:
            k = int(np.ceil(np.sqrt(N)))
            for i in range(N):
                plt.subplot(k, k, i+1)
                Im = 1.0*Pts[i]
                Idx = np.arange(Im.shape[0], dtype=np.int64)
                Im[Idx, Idx] = 0
                if Im.shape[0] > 400:
                    Im = interp.zoom(Im, 400.0/Im.shape[0])
                plt.imshow(Im, interpolation = 'none', cmap = 'afmhot')
                plt.title(PlotNames[i])
                plt.axis('off')
            plt.savefig("SSMFusion%i.png"%it, dpi=150, bbox_inches='tight')
        for i in range(N):
            nextPts[i] *= 0
            tic = time.time()
            for k in range(N):
                if i == k:
                    continue
                nextPts[i] += Pts[k]
            nextPts[i] /= float(N-1)
            tic = time.time()
            A = Ss[i].dot(nextPts[i].T)
            nextPts[i] = Ss[i].dot(A.T)
            toc = time.time()
            AllTimes.append(toc - tic)
            if reg > 0:
                nextPts[i] += reg*np.eye(nextPts[i].shape[0])
        Pts = nextPts
    if verboseTimes:
        print("Total Time multiplying: %g"%np.sum(np.array(AllTimes)))
    FusedScores = np.zeros(Pts[0].shape)
    for Pt in Pts:
        FusedScores += Pt
    return FusedScores/N
def doSimilarityFusionWs ( Ws , K = 5 , NIters = 20 , reg = 1 , PlotNames = [ ] , verboseTimes = False ) : tic = time . time ( ) Ps = [ getP ( W ) for W in Ws ] Ss = [ getS ( W , K ) for W in Ws ] Pts = [ np . array ( P ) for P in Ps ] nextPts = [ np . zeros ( P . shape ) for P in Pts ] if verboseTimes : print ( "Time getting Ss and Ps: %g" % ( time . time ( ) - tic ) ) N = len ( Pts ) AllTimes = [ ] for it in range ( NIters ) : if len ( PlotNames ) == N : k = int ( np . ceil ( np . sqrt ( N ) ) ) for i in range ( N ) : plt . subplot ( k , k , i + 1 ) Im = 1.0 * Pts [ i ] Idx = np . arange ( Im . shape [ 0 ] , dtype = np . int64 ) Im [ Idx , Idx ] = 0 if Im . shape [ 0 ] > 400 : Im = interp . zoom ( Im , 400.0 / Im . shape [ 0 ] ) plt . imshow ( Im , interpolation = 'none' , cmap = 'afmhot' ) plt . title ( PlotNames [ i ] ) plt . axis ( 'off' ) plt . savefig ( "SSMFusion%i.png" % it , dpi = 150 , bbox_inches = 'tight' ) for i in range ( N ) : nextPts [ i ] *= 0 tic = time . time ( ) for k in range ( N ) : if i == k : continue nextPts [ i ] += Pts [ k ] nextPts [ i ] /= float ( N - 1 ) tic = time . time ( ) A = Ss [ i ] . dot ( nextPts [ i ] . T ) nextPts [ i ] = Ss [ i ] . dot ( A . T ) toc = time . time ( ) AllTimes . append ( toc - tic ) if reg > 0 : nextPts [ i ] += reg * np . eye ( nextPts [ i ] . shape [ 0 ] ) Pts = nextPts if verboseTimes : print ( "Total Time multiplying: %g" % np . sum ( np . array ( AllTimes ) ) ) FusedScores = np . zeros ( Pts [ 0 ] . shape ) for Pt in Pts : FusedScores += Pt return FusedScores / N
Perform similarity fusion between a set of exponentially
weighted similarity matrices
Perform similarity fusion between a set of exponentially weighted similarity matrices
"""
    Perform similarity fusion between a set of exponentially
    weighted similarity matrices
    :param Ws: An array of NxN affinity matrices for N songs
    :param K: Number of nearest neighbors
    :param NIters: Number of iterations
    :param reg: Identity matrix regularization parameter for
        self-similarity promotion
    :param PlotNames: Strings describing different similarity
        measurements. If this array is specified, an
        animation will be saved of the cross-diffusion process
    :return D: A fused NxN similarity matrix
    """ #Full probability matrices #Nearest neighbor truncated matrices #Now do cross-diffusion iterations #tic = time.time() #nextPts[i] = SsD[i].dot(nextPts[i].dot(SsD[i].T)) #toc = time.time() #print(toc - tic, " ") #Need S*P*S^T, but have to multiply sparse matrix on the left
A fused NxN similarity matrix
A fused NxN similarity matrix
D
Ws
An array of NxN affinity matrices for N songs
An array of NxN affinity matrices for N songs
K
Number of nearest neighbors
Number of nearest neighbors
NIters
Number of iterations
Number of iterations
reg
Identity matrix regularization parameter for
self-similarity promotion
Identity matrix regularization parameter for self - similarity promotion
PlotNames
Strings describing different similarity
measurements. If this array is specified, an
animation will be saved of the cross-diffusion process
Strings describing different similarity measurements . If this array is specified an animation will be saved of the cross - diffusion process
verboseTimes