def compute_bootstrap_sample_components(x):
    gf, Urd = x
    
    # commo operation - generate new bootstrap sample
    b = gf.sample_temporal_bootstrap()
    
    # custom method to compute the components
    Ur = COMPONENT_ESTIMATOR(b)
    
    # match, flip sign and permute the discovered components    
    perm, sign_flip = match_components_munkres(Urd, Ur)
    Ur = Ur[:, perm]
    Ur *= sign_flip[:Ur.shape[1]]
    
    return Ur
def compute_bootstrap_sample_components(x):
    gf, Urd = x

    # commo operation - generate new bootstrap sample
    b = gf.sample_temporal_bootstrap()

    # custom method to compute the components
    Ur = COMPONENT_ESTIMATOR(b)

    # match, flip sign and permute the discovered components
    perm, sign_flip = match_components_munkres(Urd, Ur)
    Ur = Ur[:, perm]
    Ur *= sign_flip[:Ur.shape[1]]

    return Ur
def compute_bootstrap_sample_components(x):
    gf, Urd = x
    
    Nc = Urd.shape[1]
    
    # common operation - generate new bootstrap sample
    b = gf.sample_temporal_bootstrap()
    
    if COSINE_REWEIGHTING:
        b *= gf.qea_latitude_weights()
    
    # custom method to compute the components
    Ur = COMPONENT_ESTIMATOR(b)
    if Ur is None:
        return None
    
    # match, flip sign and permute the discovered components    
    perm, sign_flip = match_components_munkres(Urd, Ur)
    Ur = Ur[:, perm[:Nc]]
    Ur *= sign_flip[:, :Nc]
    
    return Ur
def compute_bootstrap_sample_components(x):
    gf, Urd = x

    Nc = Urd.shape[1]

    # common operation - generate new bootstrap sample
    b = gf.sample_temporal_bootstrap()

    if COSINE_REWEIGHTING:
        b *= gf.qea_latitude_weights()

    # custom method to compute the components
    Ur = COMPONENT_ESTIMATOR(b)
    if Ur is None:
        return None

    # match, flip sign and permute the discovered components
    perm, sign_flip = match_components_munkres(Urd, Ur)
    Ur = Ur[:, perm[:Nc]]
    Ur *= sign_flip[:, :Nc]

    return Ur
def compute_bootstrap_sample_components(gf, Urd, jobq, resq):
    """
    Estimate the components from a temporally bootstrapped dataset.
    """
    Nc = Urd.shape[1]
    while jobq.get() is not None:
    
        # common operation - generate new bootstrap sample
        b, ndx = gf.sample_temporal_bootstrap()
    
        if COSINE_REWEIGHTING:
            b *= gf.qea_latitude_weights()
    
        # custom method to compute the components
        Res = COMPONENT_ESTIMATOR(b)
        if Res is None:
            resq.put(None)
        else:
            Ur, _ = Res
            # match, flip sign and permute the discovered components    
            perm, sign_flip = match_components_munkres(Urd, Ur)
            Ur = Ur[:, perm[:Nc]]
            Ur *= sign_flip[:, :Nc]
            resq.put(Ur)
Пример #6
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()