X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ad%2Fad_model%2Fad_model.py;h=cf517b0b789f99b475ac034bf92f780c6314f3a1;hb=588acf17f5f14399b2bad62bda8106fef7dac063;hp=eee9b677dc056d1fb74e381448269cc16d0a0869;hpb=4f8b2a6fc8581b8227489d857f06d7734883700a;p=ric-app%2Fad.git diff --git a/ad/ad_model/ad_model.py b/ad/ad_model/ad_model.py index eee9b67..cf517b0 100644 --- a/ad/ad_model/ad_model.py +++ b/ad/ad_model/ad_model.py @@ -14,60 +14,59 @@ # 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('/tmp/ad/' + name) + model = joblib.load('ad/' + name) pred = model.predict(self.data) data = self.data.copy() - le = joblib.load('/tmp/ad/LabelEncoder') + le = joblib.load('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("/tmp/ad/ue_seg.json", "r") as json_data: + """ + with open("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