def cal_lf0(config):

    base_path = config['base_path']
    label_path = config['label_path']
    name = config['name']
    outfilepath = config['outfilepath']
    var_path = config['var_path']
    syllable_base_path = config['syllable_base_path']
    syllable_var_path = config['syllable_var_path']
    original = config['original']
    koriyama_gen = config['koriyama_gen']
    figure_path = config['figure_path']
    ph_in_syl_object_path = config['phone_in_syllable_object_path']
    stress = config['stress']
    original_vuv = config['original_vuv']

    p_in_s_file = Utility.load_obj(ph_in_syl_object_path)

    vuv = np.load('{}/class.npy'.format(config['vuv_path']))
    # vuv = original_vuv

    #--------Frame-------#

    lf0_mean = np.load('{}/mean.npy'.format(base_path))
    lf0_cov = np.load('{}/cov.npy'.format(base_path))

    var = np.load('{}'.format(var_path))

    if len(lf0_cov) > len(vuv):
        for i in range(len(lf0_cov) - len(vuv)):
            vuv.append(-1, axis=0)
    elif len(lf0_cov) < len(vuv):
        vuv = vuv[0:len(lf0_cov)]

    lf0_var = np.sum(var, axis=0)

    lf0_mean = np.array([lf0_mean[:, 0], lf0_mean[:, 1], lf0_mean[:, 2]])
    lf0_w = PoGUtility.generate_W_for_GPR_generate_features(len(lf0_cov), vuv)

    frame_B = alpha * PoGUtility.cal_sum_of_mean_part(lf0_var, lf0_w, lf0_cov,
                                                      lf0_mean)
    frame_A = alpha * PoGUtility.cal_sum_of_weight_part(
        lf0_var, lf0_w, lf0_cov)

    L = linalg.cholesky(frame_A, lower=True)
    lf0 = linalg.cho_solve((L, True), frame_B)

    # lf0 = lf0_gen_with_vuv(lf0, vuv)
    print lf0.shape

    frame_lf0_nomask = np.copy(lf0)

    # lf0 = lf0_gen_with_vuv(lf0, vuv)

    lf0[lf0 < 1] = np.nan

    frame_lf0 = np.copy(lf0)

    #----------Syllable level--------#

    dur_list, names = PoGUtility.gen_dur_and_name_list(label_path, name)

    # print np.sum(dur_list)
    if np.sum(dur_list) < len(original):
        dur_list[0] = dur_list[0] + len(original) - np.sum(dur_list)
    # print np.sum(dur_list)

    syl_mean = np.load('{}/mean.npy'.format(syllable_base_path))
    syl_cov = np.load('{}/cov.npy'.format(syllable_base_path))

    s_mean = syl_mean

    var = np.load('{}'.format(syllable_var_path))
    syl_var = np.sum(var, axis=0)

    temp_mean = []
    for i in range(len(syl_mean[0])):
        temp_mean.append(syl_mean[:, i])
    syl_mean = np.array(temp_mean)

    syl_w = PoGUtility.generate_DCT_W_without_consonant_on_stress(
        len(lf0_cov), dur_list, num_coeff, p_in_s_file, stress)

    syl_B = beta * PoGUtility.cal_sum_of_mean_part(syl_var, syl_w, syl_cov,
                                                   syl_mean)
    syl_A = beta * PoGUtility.cal_sum_of_weight_part(syl_var, syl_w, syl_cov)

    #----------Combine Model--------#

    L = linalg.cholesky(frame_A + syl_A, lower=True)
    lf0 = linalg.cho_solve((L, True), frame_B + syl_B)

    # print lf0.shape

    lf0[lf0 < 1] = np.nan

    PlotUtility.plot([lf0, original, frame_lf0_nomask],
                     ['Multi', 'original', 'Single'],
                     '{}/{}_no_mask.eps'.format(figure_path, name))

    lf0 = lf0_gen_with_vuv(lf0, vuv)
    lf0[lf0 < 1] = np.nan

    frame_lf0 = lf0_gen_with_vuv(frame_lf0, vuv)
    frame_lf0[frame_lf0 < 1] = np.nan

    np.save(outfilepath, lf0)

    print min(lf0)

    PlotUtility.plot([lf0, original, frame_lf0],
                     ['Multi', 'original', 'Single'],
                     '{}/{}_multi.eps'.format(figure_path, name))

    #----------Combine Model--------#

    o = []
    for data_dct, dur in zip(s_mean, dur_list):
        i_dct = PoGUtility.generate_inverse_DCT(data_dct, dur)
        o = o + i_dct

    o = np.concatenate((np.zeros(len(original) - len(o)), np.array(o)), axis=0)

    o = lf0_gen_with_vuv(o, vuv)
    o[o <= 1] = np.nan
    # print o.shape

    PlotUtility.plot([o, original, lf0, frame_lf0],
                     ['dct', 'original', 'Multi', 'frame_lf0'],
                     '{}/{}_dct.eps'.format(figure_path, name))

    pass
def cal_lf0(config):

    base_path = config['base_path']
    label_path = config['label_path']
    name = config['name']
    outfilepath = config['outfilepath']
    var_path = config['var_path']
    syllable_base_path = config['syllable_base_path']
    syllable_var_path = config['syllable_var_path']
    original = config['original']
    koriyama_gen = config['koriyama_gen']
    figure_path = config['figure_path']

    vuv = np.load('{}/class.npy'.format(config['vuv_path']))

    #--------Frame-------#

    lf0_mean = np.load('{}/mean.npy'.format(base_path))
    lf0_cov = np.load('{}/cov.npy'.format(base_path))

    var = np.load('{}'.format(var_path))

    lf0_var = np.sum(var, axis=0)

    lf0_mean = np.array( [ lf0_mean[:,0], lf0_mean[:,1], lf0_mean[:,2] ] )
    lf0_w = PoGUtility.generate_W_for_GPR_generate_features(len(lf0_cov), vuv)

    frame_B = alpha * PoGUtility.cal_sum_of_mean_part(lf0_var, lf0_w, lf0_cov, lf0_mean)
    frame_A = alpha * PoGUtility.cal_sum_of_weight_part(lf0_var, lf0_w, lf0_cov)

    L = linalg.cholesky(frame_A, lower=True)
    lf0 = linalg.cho_solve((L, True) , frame_B)

    lf0 = lf0_gen_with_vuv(lf0, vuv)
    print lf0.shape

    lf0[lf0<0] = np.nan

    frame_lf0 = lf0

    #----------Syllable level--------#

    dur_list, names = PoGUtility.gen_dur_and_name_list(label_path, name)

    # print np.sum(dur_list)
    if np.sum(dur_list) < len(original):
        dur_list[0] = dur_list[0] + len(original)-np.sum(dur_list)
    # print np.sum(dur_list)

    syl_mean = np.load('{}/mean.npy'.format(syllable_base_path))
    syl_cov = np.load('{}/cov.npy'.format(syllable_base_path))

    s_mean = syl_mean

    var = np.load('{}'.format(syllable_var_path))
    syl_var = np.sum(var, axis=0)

    temp_mean = []
    for i in range( len(syl_mean[0]) ):
        temp_mean.append( syl_mean[:,i] )
    syl_mean = np.array(temp_mean)

    syl_w = PoGUtility.generate_DCT_W_with_vuv(len(lf0_cov), dur_list, num_coeff, vuv)

    syl_B = beta * PoGUtility.cal_sum_of_mean_part(syl_var, syl_w, syl_cov, syl_mean)
    syl_A = beta * PoGUtility.cal_sum_of_weight_part(syl_var, syl_w, syl_cov)

    # print syl_B
    # Utility.write_to_file_line_by_line('./syl_B.txt', syl_B)
    # Utility.write_to_file_line_by_line('./syl_A.txt', syl_A)

    #----------Combine Model--------#

    L = linalg.cholesky(frame_A + syl_A, lower=True)
    lf0 = linalg.cho_solve((L, True) , frame_B + syl_B)

    # lf0 = np.dot(
    #     inv(frame_A + syl_A),
    #     frame_B + syl_B
    #     )

    lf0 = lf0_gen_with_vuv(lf0, vuv)
    # print lf0.shape

    lf0[lf0<0] = np.nan

    np.save(outfilepath, lf0)

    PlotUtility.plot([lf0, original, frame_lf0], ['Multi', 'original', 'Single'], '{}/{}_multi.eps'.format(figure_path, name))

    #----------Combine Model--------#

    o = []
    for data_dct, dur in zip(s_mean, dur_list):
        i_dct = PoGUtility.generate_inverse_DCT(data_dct, dur)
        o = o + i_dct

    o = np.concatenate(
        (np.zeros(len(original)-len(o)), 
                 np.array(o)) , axis=0)

    o = lf0_gen_with_vuv(o, vuv)
    o[o<0] = np.nan
    # print o.shape

    PlotUtility.plot([o, original, lf0], ['dct', 'original', 'Multi'], '{}/{}_dct.eps'.format(figure_path, name))

    pass