1 # ==================================================================================
2 # Copyright (c) 2020 HCL Technologies Limited.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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 # ==================================================================================
17 from statsmodels.tsa.api import VAR
18 from statsmodels.tsa.stattools import adfuller
22 class PROCESS(object):
24 def __init__(self, data):
28 def adfuller_test(self, series, thresh=0.05, verbose=False):
29 """ADFuller test for Stationarity of given series and return True or False"""
30 r = adfuller(series, autolag='AIC')
31 output = {'test_statistic': round(r[0], 4), 'pvalue': round(r[1], 4), 'n_lags': round(r[2], 4), 'n_obs': r[3]}
32 p_value = output['pvalue']
38 def make_stationary(self):
39 """ call adfuller_test() to check for stationary
40 If the column is stationary, perform 1st differencing and return data"""
43 for name, column in df.iteritems():
44 res_adf.append(self.adfuller_test(column)) # Perform ADF test
46 self.data = df.diff().dropna()
49 def invert_transformation(self, inp, forecast):
50 """Revert back the differencing to get the forecast to original scale."""
56 df[col] = inp[col].iloc[-1] + df[col].cumsum()
61 """ Filter throughput parameters, call make_stationary() to check for Stationarity time series
64 df = df[['pdcpBytesDl', 'pdcpBytesUl']]
65 self.data = df.loc[:, (df != 0).any(axis=0)]
66 self.make_stationary() # check for Stationarity and make the Time Series Stationary
70 df = df.loc[:, (df != 0).any(axis=0)]
71 if len(df) != 0 and df.shape[1] == 2:
79 Read the input file(based on cell id received from the main program)
80 call process() to forecast the downlink and uplink of the input cell id
81 Make a VAR model, call the fit method with the desired lag order.
83 db.read_data(meas='liveCell', cellid=cid)
87 model = VAR(md.data) # Make a VAR model
88 model_fit = model.fit(10) # call fit method with lag order
89 file_name = 'qp/'+cid.replace('/', '')
90 with open(file_name, 'wb') as f:
91 joblib.dump(model_fit, f) # Save the model with the cell id name