X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ad%2Fad_model%2Fad_model.py;h=eee9b677dc056d1fb74e381448269cc16d0a0869;hb=cd532f9e1a4b470719f85b237b409be8a09311e1;hp=cf517b0b789f99b475ac034bf92f780c6314f3a1;hpb=f98ee76af036d60b8f5077105830ed61a13ed5aa;p=ric-app%2Fad.git diff --git a/ad/ad_model/ad_model.py b/ad/ad_model/ad_model.py index cf517b0..eee9b67 100644 --- a/ad/ad_model/ad_model.py +++ b/ad/ad_model/ad_model.py @@ -14,59 +14,60 @@ # limitations under the License. # ================================================================================== +import hdbscan +import pandas as pd +import numpy as np import joblib import random import json - - + class modelling(object): - def __init__(self, data): + def __init__(self,data): """ Separating UEID and timestamp features to be mapped later after prediction """ self.time = data.MeasTimestampRF self.id = data.UEID - self.data = data.drop(['UEID', 'MeasTimestampRF'], axis=1) - + self.data = data.drop(['UEID', 'MeasTimestampRF'], axis = 1) + def predict(self, name): - """ + """ Load the saved model and map the predicted category into Category field. - Map UEID, MeasTimestampRF with the predicted result. + Map UEID, MeasTimestampRF with the predicted result. """ - model = joblib.load('ad/' + name) + model = joblib.load('/tmp/ad/' + name) pred = model.predict(self.data) data = self.data.copy() - le = joblib.load('ad/LabelEncoder') + le = joblib.load('/tmp/ad/LabelEncoder') data['Category'] = le.inverse_transform(pred) data['MeasTimestampRF'] = self.time data['UEID'] = self.id return data - def compare(df): """ If the category of UEID is present in the segment file, it is considered as normal(0) otherwise, the sample is considered as anomaly. - """ - with open("ad/ue_seg.json", "r") as json_data: + """ + with open("/tmp/ad/ue_seg.json", "r") as json_data: segment = json.loads(json_data.read()) anomaly = [] for i in df.index: - if df.loc[i, 'Category'] in segment[str(df.loc[i, 'UEID'])]: + if df.loc[i, 'Category'] in segment[str(df.loc[i,'UEID'])]: anomaly.append(0) else: anomaly.append(1) return anomaly - - + def HDB_PREDICT(df): """ - Extract all the unique UEID + Extract all the unique UEID Call Predict method to get the final data for the randomly selected UEID """ ue_list = df.UEID.unique() # Extract unique UEIDs - ue = random.choice(ue_list) # Randomly selected the ue list + ue = random.choice(ue_list) # Randomly selected the ue list df = df[df['UEID'] == ue] db = modelling(df) - db_df = db.predict('RF') # Calls predict module and store the result into db_df + db_df = db.predict('RF')# Calls predict module and store the result into db_df del db + db_df['Anomaly'] = compare(db_df) return db_df