示例#1
0
def kalman_pipeline(configs):
    """基于 Kalman 滤波器的各个算法的 pipeline

    Args:
        configs (dict): 配置字典
    """

    for data_type in configs.keys():
        config = configs[data_type]
        term_selector = Selector(
            f"{config['data_root']}{data_type}{config['term_path']}")
        terms_set = term_selector.make_terms()
        # get data
        normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term_selector.make_selection(
        )
        # 构造 Kalman Filter
        for algo_type in config['algorithm']:
            print(f"{data_type} {algo_type}")
            fname = f"{config['data_root']}{data_type}_{algo_type}_{config['est_fname']}"
            if algo_type == 'Kalman4ARX':
                kf = Kalman4ARX(normalized_signals,
                                config['max_lag'],
                                uc=config['uc'])
                # 估计系数
                y_coef, A_coef = kf.estimate_coef(config['threshold'])
                # 计算模型表达式并保存
                est_model = make_linear_func(A_coef, fname=fname)
                # 保存平均结果
            elif algo_type == 'Kalman4FROLS':
                kf = Kalman4FROLS(normalized_signals,
                                  Kalman_H=Kalman_H,
                                  uc=config['uc'])
                y_coef = kf.estimate_coef()
                est_model = make_func4K4FROLS(y_coef,
                                              candidate_terms,
                                              Kalman_S_No,
                                              fname=fname)
                # 保存平均结果
            elif algo_type == 'torch4FROLS':
                kf = torch4FROLS(normalized_signals,
                                 Kalman_H=Kalman_H,
                                 n_epoch=config['n_epoch'])
                y_coef = kf.estimate_coef()
                est_model = make_func4K4FROLS(y_coef,
                                              candidate_terms,
                                              Kalman_S_No,
                                              fname=fname)
                # 保存平均结果
            else:
                print('!Not Defined!')
            print(f"\n{data_type}_{algo_type} est model saved!\n")

            # 输出结果
            print(est_model)
示例#2
0
def torch4FROLS_pipeline(data_type, configs, n_trial):
    """基于 Kalman 滤波器的各个算法的 pipeline

    Args:
        data_type: 数据类型
        configs (dict): 配置字典
        n_trial: 试验次数
    """

    config = configs[data_type]
    fname = f"{config['data_root']}{data_type}_torch4FROLS100_{config['est_fname']}"
    term_selector = Selector(
        f"{config['data_root']}{data_type}{config['term_path']}")
    terms_set = term_selector.make_terms()
    # get data
    normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term_selector.make_selection(
    )
    y_coef = 0
    for trial in range(n_trial):
        print(f'data_type: {data_type}, trial: ### {trial+1}')
        kf = torch4FROLS(normalized_signals,
                         Kalman_H=Kalman_H,
                         n_epoch=config['n_epoch'])
        y_coef += kf.estimate_coef()
    est_model = make_func4K4FROLS(y_coef / n_trial,
                                  candidate_terms,
                                  Kalman_S_No,
                                  fname=fname)
    # 输出结果
    print(est_model)
示例#3
0
def torch4FROLS_pipeline(data_type, configs, n_trial, id_correct, n_correct):
    """基于 Kalman 滤波器的各个算法的 pipeline

    Args:
        data_type: 数据类型
        configs (dict): 配置字典
        n_trial: 试验次数
    """

    config = configs[data_type]
    y_coef100 = np.zeros((100, 5, 5))
    y_coef9 = np.zeros((100, 9))
    for trial in range(n_trial):
        fname = f"{config['data_root']}{data_type}_torch4FROLS100_{config['est_fname']}{trial+1}.txt"
        term_selector = Selector(
            f"{config['data_root']}{data_type}{config['term_path']}{trial+1}.mat"
        )
        terms_set = term_selector.make_terms()
        # get data
        normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term_selector.make_selection(
        )
        print(f'data_type: {data_type}, trial: ### {trial+1}')
        kf = torch4FROLS(normalized_signals,
                         Kalman_H=Kalman_H,
                         n_epoch=config['n_epoch'])
        y_coef = kf.estimate_coef()
        print(kf.y_error, kf.y_error.shape)
        y_coef100[trial] = y_coef
        est_model = make_func4K4FROLS(y_coef,
                                      candidate_terms,
                                      Kalman_S_No,
                                      fname=fname)
        coef9 = []
        Kalman_S_No_order = np.sort(Kalman_S_No)
        for row in range(5):
            for t in range(n_correct[row]):
                idx = np.argwhere(
                    Kalman_S_No_order[row, :] == id_correct[data_type][row][t])
                value = y_coef[row, idx]
                coef9.append(value[0, 0])
        y_coef9[trial] = np.array(coef9)

        # 输出结果
        # print(est_model)
    fname1 = f"{config['data_root']}{data_type}_torch4FROLS100_{config['est_fname']}log.txt"
    save_3Darray(fname1, y_coef100)
    mean_y = np.mean(y_coef9, 0)
    var_y = np.var(y_coef9, 0)
    print(mean_y, var_y, sep='\n')
    fname1 = f"{config['data_root']}{data_type}_torch4FROLS100_{config['est_fname']}log100.txt"
    save_2Darray(fname1, np.array([mean_y, var_y]).T)
示例#4
0
fname = './data/linear_candidate_terms.txt'
np.savetxt(fname, terms_repr, fmt='%s')

# *selection
print(term)
normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term.make_selection(
)

# *构造 Kalman Filter
kf = Kalman4FROLS(normalized_signals, Kalman_H=Kalman_H, uc=0.01)
y_coef = kf.estimate_coef()
print(y_coef)

# *估计模型生成
est_model = make_func4K4FROLS(y_coef,
                              candidate_terms,
                              Kalman_S_No,
                              fname='./data/K4FROLS_est_model.txt')
print(est_model)

# !非线性模型
terms_path = './data/nor_nonlinear_terms.mat'
term = Selector(terms_path)
terms_repr = term.make_terms()

# *保存候选项集合
fname = './data/nonlinear_candidate_terms.txt'
np.savetxt(fname, terms_repr, fmt='%s')

# *selection
normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term.make_selection(
)
示例#5
0
# *selection
print(term)
normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term.make_selection(
)

# *构造 Kalman Filter
kf = Kalman4FROLS(normalized_signals, Kalman_H=Kalman_H, uc=0.01)
y_coef = kf.estimate_coef()
print(y_coef)

# *估计模型生成
# est_model = make_func4K4FROLS(y_coef, candidate_terms, Kalman_S_No, fname='./data/linear_Kalmal4FROLS_est_model.txt')
est_model = make_func4K4FROLS(
    y_coef,
    candidate_terms,
    Kalman_S_No,
    fname='./data/longlag_linear_Kalmal4FROLS_est_model.txt')
print(est_model)

# !线性模型 torch4FROLS 算法(matlab 标准化)
# terms_path = './data/nor_linear_terms.mat'
terms_path = './data/longlag_linear_terms.mat'
term = Selector(terms_path)
terms_repr = term.make_terms()

# *保存候选项集合
# fname = './data/linear_candidate_terms.txt'
fname = './data/longlag_linear_candidate_terms.txt'
np.savetxt(fname, terms_repr, fmt='%s')