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 # ==================================================================================
20 class modelling(object):
21 r""" Filter dataframe based on paramters that were used to train model
22 use transormer to transform the data
23 load model and predict the label(normal/anomalous)
29 def __init__(self, data):
31 with open('params', 'rb') as f:
33 if all(cols.isin(data.columns)):
34 self.data = data[cols]
36 print(data.columns, cols)
40 def transformation(self):
41 """ load transformer to transform data """
43 sc = joblib.load('scale')
44 self.data = sc.transform(self.data)
46 def predict(self, name):
47 """ Load the saved model and return predicted result.
56 predict label on a given sample
60 model = joblib.load(name)
61 pred = model.predict(self.data)
62 pred = [1 if p == -1 else 0 for p in pred]
67 r""""Rule basd method to find degradation type of anomalous sample
71 Dataframe that contains only normal sample
74 def __init__(self, db):
76 self.normal = db.data[['rsrp', 'rsrq', 'rssinr', 'throughput', 'prb_usage', 'ue-id']]
78 def cause(self, sample):
79 """ Filter normal data for a particular ue-id to compare with a given sample
80 Compare with normal data to find and return degradaton type
82 normal = self.normal[self.normal['ue-id'] == sample.iloc[0]['ue-id']].drop('ue-id', axis=1)
83 param = self.find(sample, normal.max())
86 def find(self, sample, Range):
87 """ store if a particular parameter is below threshold and return """
90 if sample.iloc[0]['throughput'] < Range['throughput']*0.5:
91 deg.append('Throughput')
92 if sample.iloc[0]['rsrp'] <= Range['rsrp']-20:
94 if sample.iloc[0]['rsrq'] <= Range['rsrq']-20:
96 if sample.iloc[0]['rssinr'] <= Range['rssinr']-25:
98 if sample.iloc[0]['prb_usage'] <= Range['prb_usage']*0.5:
99 deg.append('prb_usage')
109 Call Predict method to predict whether a given sample is normal or anomalous
113 db_df = db.predict('model') # Calls predict module and store the result into db_df