Skip to content

Commit

Permalink
Add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
phamquiluan committed Aug 10, 2024
1 parent afa582b commit 4e30833
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions RCAEval/utility/__init__.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4e30833

Please sign in to comment.