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 # ==================================================================================
18 from mdclogpy import Logger
20 logger = Logger(name=__name__)
23 class modelling(object):
24 r""" Filter dataframe based on paramters that were used to train model
25 use transormer to transform the data
26 load model and predict the label(normal/anomalous)
32 def __init__(self, data=None):
40 with open('src/model', 'rb') as f:
41 self.model = joblib.load(f)
42 except FileNotFoundError:
43 logger.error("Model Does not exsist")
47 with open('src/num_params', 'rb') as f:
48 self.num = joblib.load(f)
50 except FileNotFoundError:
51 logger.error("Parameter file does not exsist")
55 with open('src/scale', 'rb') as f:
56 self.scale = joblib.load(f)
57 except FileNotFoundError:
58 logger.error("Scale file does not exsist")
60 def transformation(self):
61 self.data = self.scale.transform(self.data)
63 def predict(self, df):
64 """ Load the saved model and return predicted result.
73 predict label on a given sample
76 self.data = df.loc[:, self.num]
78 pred = self.model.predict(self.data)
79 pred = [1 if p == -1 else 0 for p in pred]
84 r""""Rule basd method to find degradation type of anomalous sample
88 Dataframe that contains only normal sample
94 def cause(self, df, db):
95 """ Filter normal data for a particular ue-id to compare with a given sample
96 Compare with normal data to find and return degradaton type
99 sample.index = range(len(sample))
100 for i in range(len(sample)):
101 if sample.iloc[i]['Anomaly'] == 1:
102 query = 'select * from {} where {} = \'{}\' and time<now() and time>now()-20s'.format(db.meas, db.ue, sample.iloc[i][db.ue])
103 normal = db.query(query)
105 normal = normal[db.meas][[db.thpt, db.rsrp, db.rsrq]]
106 deg = self.find(sample.loc[i, :], normal.max(), db)
108 sample.loc[i, 'Degradation'] = deg
109 if 'Throughput' in deg and ('RSRP' in deg or 'RSRQ' in deg):
110 sample.loc[i, 'Anomaly'] = 2
112 sample.loc[i, 'Anomaly'] = 0
113 return sample[['Anomaly', 'Degradation']].values.tolist()
115 def find(self, row, l, db):
116 """ store if a particular parameter is below threshold and return """
118 if row[db.thpt] < l[db.thpt]*0.5:
119 deg.append('Throughput')
120 if row[db.rsrp] < l[db.rsrp]-15:
122 if row[db.rsrq] < l[db.rsrq]-10: