diff --git a/RCAEval/utility/__init__.py b/RCAEval/utility/__init__.py new file mode 100644 index 0000000..d3a4ef2 --- /dev/null +++ b/RCAEval/utility/__init__.py @@ -0,0 +1,52 @@ +import json +import sys + +import numpy as np +import pandas as pd +from sklearn.preprocessing import StandardScaler + +ENCODING = "utf-8" + + +def is_py310(): + return sys.version_info.major == 3 and sys.version_info.minor == 10 + + +def dump_json(filename: str, data): + """ + Dump data into a json file + """ + with open(filename, "w", encoding=ENCODING) as obj: + json.dump(data, obj, ensure_ascii=False, indent=2, sort_keys=True) + + +def load_json(filename: str): + """ + Load data from a json file + """ + with open(filename, encoding=ENCODING) as obj: + return json.load(obj) + + +def convert_adjacency_matrix(adj, node_names): + """ + convert metrics adj to service adj + """ + services = list(set([name.split("_")[0] for name in node_names])) + # print(services) + num_services = len(services) + + service_adj = np.zeros((num_services, num_services)) + + for i in range(adj.shape[0]): + for j in range(adj.shape[0]): + if adj[i][j] == 1: + service_adj[services.index(node_names[i].split("_")[0])][ + services.index(node_names[j].split("_")[0]) + ] = 1 + + # remove cycles + for i in range(num_services): + service_adj[i][i] = 0 + + return service_adj, services # services is node_names but for services \ No newline at end of file