[RICAPP-181] Update Prediction message (from QP to TS) with serving cell prediciton
[ric-app/qp.git] / qp / 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     data = data[['pdcpBytesUl', 'pdcpBytesDl']]
28     ps = PROCESS(data.copy())
29     ps.make_stationary()
30
31     if not ps.valid():
32         df_f = data.tail(1)
33     elif os.path.isfile('qp/'+cid):
34         model = joblib.load('qp/'+cid)
35         pred = model.forecast(y=ps.data.values, steps=nobs)
36
37         if pred is not None:
38             df_f = pd.DataFrame(pred, columns=data.columns)
39             df_f.index = pd.date_range(start=data.index[-1], freq='10ms', periods=len(df_f))
40             df_f = df_f[data.columns].astype(int)
41             df_f = ps.invert_transformation(data, df_f)
42     else:
43         return None
44     df_f = df_f[data.columns].astype(int)
45     return df_f