Esempio n. 1
0
def synth_model_plot_component_matching():

    with open('results/synth_model_component_robustness_190smp_4comps.bin', 'r') as f:
        d = cPickle.load(f)
        
    Uopt = d['Uopt']
    Ur = d['Ur']
    mean_comp = d['mean']
    var_comp = d['var']
    
    perm, sf = match_components_munkres(Uopt, Ur)
    perm = perm[:3]
    sf = sf[:, :3]
    rst = np.setdiff1d(np.arange(Ur.shape[1]), np.sort(perm[:3]), False)
    print rst
    Urm = np.hstack([Ur[:, perm] * sf, Ur[:, rst]])
    print Urm.shape
    
    perm, sf = match_components_munkres(Uopt, mean_comp)
    perm = perm[:3]
    sf = sf[:, :3]
    rst = np.setdiff1d(np.arange(mean_comp.shape[1]), np.sort(perm[:3]), False)
    print rst
    Umvm = np.hstack([mean_comp[:, perm] * sf, mean_comp[:, rst]])
    Umvv = np.hstack([var_comp[:, perm], var_comp[:, rst]]) 
    
    print Umvm.shape
    print Umvv.shape
    
    p2 = list(perm)
    for i in range(mean_comp.shape[1]):
        if not i in p2:
            p2.append(i)
    
    print estimate_snr(Uopt, Urm[:, :3])
    print estimate_snr(Uopt, Umvm[:, :3])
    
    print mse_error(Uopt, Urm[:, :3])
    print mse_error(Uopt, Umvm[:, :3])
    
    print marpe_error(Uopt, Urm[:, :3])
    print marpe_error(Uopt, Umvm[:, :3])
    
    print np.sum(Umvv, axis = 0)
    print np.sum(Umvm ** 2, axis = 0)
    print np.sum(Umvm ** 2, axis = 0) / np.sum(Umvv[:, p2], axis = 0)
    
    plt.figure(figsize = (12, 16))
    plt.subplots_adjust(left = 0.05, bottom = 0.05, right = 0.95, top = 0.95)
    for i in range(4):
        
        plt.subplot(4, 3, i*3+1)
        if(i < 3):
            plt.plot(Uopt[:,i], 'r-')
        plt.plot(Umvm[:,i], 'b-')
        plt.plot(np.ones((Uopt.shape[0],1)) * (1.0 / Uopt.shape[0]**0.5), 'g-', linewidth = 2)
        plt.title('Component %d [bootstrap mean]' % (i+1))
        
        plt.subplot(4, 3, i*3+2)
        if(i < 3):
            plt.plot(Uopt[:,i], 'r-')
        plt.plot(Urm[:,i], 'b-')
        plt.plot(np.ones((Uopt.shape[0],1)) * (1.0 / Uopt.shape[0]**0.5), 'g-', linewidth = 2)
        plt.title('Component %d [data]' % (i+1))
    
        plt.subplot(4, 3, i*3+3)
        plt.plot(Umvv[:,i], 'b-')
        plt.title('Component %d [variance]' % (i+1))
        
    plt.show()
    var_comp[var_comp == 0] = 1e-6
    
    # how do we now estimate the SNRs?
    # a. from data
    # b. from bootstrap means and stdevs -> T-values
    Urm = matched_components(Uopt, Ur)
#    Umvm = matched_components(Uopt, mean_comp / (var_comp ** 0.5))
    Umvm = matched_components(Uopt, mean_comp)
    
    print estimate_snr(Uopt, Urm)
    print estimate_snr(Uopt, Umvm)

    print mse_error(Uopt, Urm)
    print mse_error(Uopt, Umvm)
    
    print marpe_error(Uopt, Urm)
    print marpe_error(Uopt, Umvm)
    
    plt.figure(figsize = (12, 6))
    for i in range(3):

        plt.subplot(320+i*2+1)
        plt.plot(Uopt[:,i], 'r-')
        plt.plot(Umvm[:,i], 'b-')
        plt.plot(np.ones((Uopt.shape[0],1)) * (1.0 / Uopt.shape[0]**0.5), 'g-')
        plt.title('Component %d [bootstrap mean]' % i)
    
        plt.subplot(320+i*2+2)
        plt.plot(Uopt[:,i], 'r-')
        plt.plot(Urm[:,i], 'b-')
        plt.plot(np.ones((Uopt.shape[0],1)) * (1.0 / Uopt.shape[0]**0.5), 'g-')
    var_comp[var_comp == 0] = 1e-6

    # how do we now estimate the SNRs?
    # a. from data
    # b. from bootstrap means and stdevs -> T-values
    Urm = matched_components(Uopt, Ur)
    #    Umvm = matched_components(Uopt, mean_comp / (var_comp ** 0.5))
    Umvm = matched_components(Uopt, mean_comp)

    print estimate_snr(Uopt, Urm)
    print estimate_snr(Uopt, Umvm)

    print mse_error(Uopt, Urm)
    print mse_error(Uopt, Umvm)

    print marpe_error(Uopt, Urm)
    print marpe_error(Uopt, Umvm)

    plt.figure(figsize=(12, 6))
    for i in range(3):

        plt.subplot(320 + i * 2 + 1)
        plt.plot(Uopt[:, i], 'r-')
        plt.plot(Umvm[:, i], 'b-')
        plt.plot(
            np.ones((Uopt.shape[0], 1)) * (1.0 / Uopt.shape[0]**0.5), 'g-')
        plt.title('Component %d [bootstrap mean]' % i)

        plt.subplot(320 + i * 2 + 2)
        plt.plot(Uopt[:, i], 'r-')
        plt.plot(Urm[:, i], 'b-')