[RICAPP-181] Update Prediction message (from QP to TS) with serving cell prediciton
[ric-app/qp.git] / src / prediction.py
1 # ==================================================================================
2 #  Copyright (c) 2020 HCL Technologies Limited.
3 #
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15 # ==================================================================================
16 # import pandas as pd
17 import os
18 import joblib
19 import pandas as pd
20 from qptrain import PROCESS
21
22
23 def forecast(data, cid, nobs=1):
24     """
25      forecast the time series using the saved model.
26     """
27     ps = PROCESS(data.copy())
28     if ps.data is None:
29         return
30     data = ps.data
31     pred = data.tail(1).values
32     if os.path.isfile('src/'+cid) and not ps.constant():
33         model = joblib.load('src/'+cid)
34         pred = model.forecast(y=data.values, steps=nobs)
35
36     df_f = pd.DataFrame(pred, columns=data.columns)
37     df_f.index = pd.date_range(start=data.index[-1], freq='10ms', periods=len(df_f))
38     df_f = df_f[data.columns].fillna(0).astype(int)
39     df_f = ps.invert_transformation(data, df_f)
40     return df_f