def decompose_RNN_forecasting(ts, dataset, freq, lag, epoch=20, hidden_num=64,
                              batch_size=32, lr=1e-3, unit="GRU", varFlag=False, maxLen=48, minLen=24, step=8):

    # 序列分解
    trend, seasonal, residual = decompose.ts_decompose(ts, freq)
    print("trend shape:", trend.shape)
    print("peroid shape:", seasonal.shape)
    print("residual shape:", residual.shape)

    # 分别预测
    resWin = trendWin = lag
    t1 = time.time()
    trTrain, trTest, MAE1, MRSE1, SMAPE1 = RNN_forecasting(trend, lookBack=lag, epoch=epoch, batchSize=batch_size, hiddenNum=hidden_num,
                                            varFlag=varFlag, minLen=minLen, maxLen=maxLen, step=step, unit=unit, lr=lr)
    resTrain, resTest, MAE2, MRSE2, SMAPE2 = RNN_forecasting(residual, lookBack=lag, epoch=epoch, batchSize=batch_size, hiddenNum=hidden_num,
                                            varFlag=varFlag, minLen=minLen, maxLen=maxLen, step=step, unit=unit, lr=lr)
    t2 = time.time()
    print(t2-t1)

    print("trTrain shape:", trTrain.shape)
    print("resTrain shape:", resTrain.shape)

    # 数据对齐
    trendPred, resPred = util.align(trTrain, trTest, trendWin, resTrain, resTest, resWin)
    print("trendPred shape is", trendPred.shape)
    print("resPred shape is", resPred.shape)

    # 获取最终预测结果
    # finalPred = trendPred+seasonal+resPred

    trainPred = trTrain+seasonal[trendWin:trendWin+trTrain.shape[0]]+resTrain
    testPred = trTest+seasonal[2*resWin+resTrain.shape[0]:]+resTest

    # 获得ground-truth数据
    data = dataset[freq//2:-(freq//2)]
    trainY = data[trendWin:trendWin+trTrain.shape[0]]
    testY = data[2*resWin+resTrain.shape[0]:]

    # 评估指标
    MAE = eval.calcMAE(testY, testPred)
    print("test MAE", MAE)
    MRSE = eval.calcRMSE(testY, testPred)
    print("test RMSE", MRSE)
    MAPE = eval.calcMAPE(testY, testPred)
    print("test MAPE", MAPE)
    SMAPE = eval.calcSMAPE(testY, testPred)
    print("test SMAPE", SMAPE)

    plt.plot(testY, label='ground-truth')
    plt.plot(testPred, label='prediction')
    plt.xlabel("Time", fontsize=10)
    plt.ylabel("CPU Utilization(%)", fontsize=10)
    plt.legend()
    foo_fig = plt.gcf()
    foo_fig.savefig('M_1955_CPU.eps', format='eps', dpi=1000, bbox_inches='tight')
    plt.show()

    return trainPred, testPred, MAE, MRSE, SMAPE
def decompose_MLP_forecasting(ts,
                              dataset,
                              freq,
                              lag,
                              epoch=20,
                              hidden_num=64,
                              batch_size=32,
                              lr=1e-3):

    # 序列分解
    trend, seasonal, residual = decompose.ts_decompose(ts, freq=freq)
    print("trend shape:", trend.shape)
    print("peroid shape:", seasonal.shape)
    print("residual shape:", residual.shape)

    # 分别预测
    resWin = trendWin = lag
    t1 = time.time()
    trTrain, trTest, mae1, mrse1, smape1 = \
        ANNFORECAST.MLP_forecasting(trend, inputDim=trendWin, epoch=epoch, hiddenNum=hidden_num,
                                    batchSize=batch_size, lr=lr)
    resTrain, resTest, mae2, mrse2, smape2 = \
        ANNFORECAST.MLP_forecasting(residual, inputDim=trendWin, epoch=epoch, hiddenNum=hidden_num,
                                    batchSize=batch_size, lr=lr)
    t2 = time.time()
    print("time:", t2 - t1)

    # 数据对齐
    # trendPred, resPred = util.align(trTrain, trTest, trendWin, resTrain, resTest, resWin)

    # 获取最终预测结果
    # finalPred = trendPred + seasonal + resPred

    trainPred = trTrain + seasonal[trendWin:trendWin +
                                   trTrain.shape[0]] + resTrain
    testPred = trTest + seasonal[2 * resWin + resTrain.shape[0]:] + resTest

    # 获得ground-truth数据
    data = dataset[freq // 2:-(freq // 2)]
    trainY = data[trendWin:trendWin + trTrain.shape[0]]
    testY = data[2 * resWin + resTrain.shape[0]:]

    # 评估指标
    MAE = eval.calcMAE(testY, testPred)
    print("test MAE", MAE)
    MRSE = eval.calcRMSE(testY, testPred)
    print("test RMSE", MRSE)
    MAPE = eval.calcMAPE(testY, testPred)
    print("test MAPE", MAPE)
    SMAPE = eval.calcSMAPE(testY, testPred)
    print("test SMAPE", SMAPE)

    # plt.plot(data)
    # plt.plot(finalPred)
    # plt.show()

    return trainPred, testPred, MAE, MRSE, SMAPE
def decompose_SVR_forecasting(ts, dataset, freq, lag, C=0.1, epsilon=0.01):

    # 序列分解
    trend, seasonal, residual = decompose.ts_decompose(ts, freq=freq)
    print("trend shape:", trend.shape)
    print("peroid shape:", seasonal.shape)
    print("residual shape:", residual.shape)

    # 分别预测
    resWin = trendWin = lag
    t1 = time.time()
    trTrain, trTest, mae1, mrse1, mape1 = SVR_forecasting(trend,
                                                          lookBack=lag,
                                                          C=C,
                                                          epsilon=epsilon)
    resTrain, resTest, mae2, mrse2, mape2 = SVR_forecasting(residual,
                                                            lookBack=lag,
                                                            C=C,
                                                            epsilon=epsilon)
    t2 = time.time()
    print(t2 - t1)

    # 数据对齐
    trendPred, resPred = util.align(trTrain, trTest, trendWin, resTrain,
                                    resTest, resWin)

    # 获取最终预测结果
    finalPred = trendPred + seasonal + resPred

    trainPred = trTrain + seasonal[trendWin:trendWin +
                                   trTrain.shape[0]] + resTrain
    testPred = trTest + seasonal[2 * resWin + resTrain.shape[0]:] + resTest

    # 获得ground-truth数据
    data = dataset[freq // 2:-(freq // 2)]
    trainY = data[trendWin:trendWin + trTrain.shape[0]]
    testY = data[2 * resWin + resTrain.shape[0]:]

    # 评估指标
    MAE = eval.calcMAE(testY, testPred)
    print("test MAE", MAE)
    MRSE = eval.calcRMSE(testY, testPred)
    print("test RMSE", MRSE)
    MAPE = eval.calcMAPE(testY, testPred)
    print("test MAPE", MAPE)
    SMAPE = eval.calcSMAPE(testY, testPred)
    print("test SMAPE", SMAPE)

    # plt.plot(data)
    # plt.plot(finalPred)
    # plt.show()

    return trainPred, testPred, MAE, MRSE, SMAPE