Spaces:
Sleeping
Sleeping
File size: 7,298 Bytes
1215771 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import numpy as np
import pandas as pd
import pickle
import os
import sys
# append the path of the parent directory
sys.path.append("..")
from abstractions import *
from runtime_monitors import *
# def monitors_offline_construction(network_name, network_folder_path, classes, layers_indexes, taus):
# appendixes = ["_correctly_classified_features.csv", "_incorrectly_classified_features.csv"]
# product = ((i, y) for i in layers_indexes for y in classes)
# for i, y in product:
# # load obtained features to creat reference
# path_bad_features = network_folder_path +"Layer_minus_" + str(i) + "/class_" + str(y) + appendixes[1]
# path_good_features = network_folder_path +"Layer_minus_" + str(i) + "/class_" + str(y) + appendixes[0]
# bad_feat_clustering_results = []
# good_feat_clustering_results = []
# if os.path.exists(path_bad_features):
# df_bad_features = pd.read_csv(path_bad_features)
# bad_features_to_cluster = df_bad_features[df_bad_features.columns[3:]].to_numpy()
# bad_features_index = df_bad_features["index"].to_numpy()
# # load clustering results to partition the features
# bad_feat_clustering_results_path = network_folder_path + "Layer_minus_" + str(i) + "/clustering_results_class_" + str(y) + appendixes[1]
# if os.path.exists(bad_feat_clustering_results_path):
# bad_feat_clustering_results = pd.read_csv(bad_feat_clustering_results_path)
# if os.path.exists(path_good_features):
# df_good_features = pd.read_csv(path_good_features)
# good_features_to_cluster = df_good_features[df_good_features.columns[3:]].to_numpy()
# good_features_index = df_good_features["index"].to_numpy()
# # load clustering results to partition the features
# good_feat_clustering_results_path = network_folder_path + "Layer_minus_" + str(i) + "/clustering_results_class_" + str(y) + appendixes[0]
# n_dim = good_features_to_cluster.shape[1]
# if os.path.exists(good_feat_clustering_results_path):
# good_feat_clustering_results = pd.read_csv(good_feat_clustering_results_path)
# for tau in taus:
# good_loc_boxes = []
# bad_loc_boxes = []
# if len(bad_feat_clustering_results):
# # load clustering result related to tau
# bad_feat_clustering_result = bad_feat_clustering_results[str(tau)]
# # determine the labels of clusters
# bad_num_clusters = np.amax(bad_feat_clustering_result) + 1
# bad_clustering_labels = np.arange(bad_num_clusters)
# # extract the indices of vectors in a cluster
# bad_clusters_indices = []
# for k in bad_clustering_labels:
# bad_indices_cluster_k, = np.where(bad_feat_clustering_result == k)
# bad_clusters_indices.append(bad_indices_cluster_k)
# # creat local box for each cluster
# bad_loc_boxes = [Box() for i in bad_clustering_labels]
# for j in range(len(bad_loc_boxes)):
# bad_points_j = [(bad_features_index[i], bad_features_to_cluster[i]) for i in bad_clusters_indices[j]]
# bad_loc_boxes[j].build(n_dim, bad_points_j)
# if len(good_feat_clustering_results):
# # load clustering result related to tau
# good_feat_clustering_result = good_feat_clustering_results[str(tau)]
# # determine the labels of clusters
# good_num_clusters = np.amax(good_feat_clustering_result) + 1
# good_clustering_labels = np.arange(good_num_clusters)
# # extract the indices of vectors in a cluster
# good_clusters_indices = []
# for k in good_clustering_labels:
# good_indices_cluster_k, = np.where(good_feat_clustering_result == k)
# good_clusters_indices.append(good_indices_cluster_k)
# # creat local box for each cluster
# good_loc_boxes = [Box() for i in good_clustering_labels]
# for j in range(len(good_loc_boxes)):
# good_points_j = [(good_features_index[i], good_features_to_cluster[i]) for i in good_clusters_indices[j]]
# good_loc_boxes[j].build(n_dim, good_points_j)
# # creat the monitor for class y at layer i
# monitor_y_i = Monitor("Box", network_name, y, i, good_ref=good_loc_boxes, bad_ref=bad_loc_boxes)
# # save the created monitor
# monitor_stored_folder_path = network_folder_path + "Monitors/"
# if not os.path.exists(monitor_stored_folder_path):
# os.makedirs(monitor_stored_folder_path)
# monitor_stored_path = monitor_stored_folder_path + network_name + "_monitor_for_class_" + str(y) + "_at_layer_minus_" + str(i) + "_tau_" + str(tau) + ".pkl"
# with open(monitor_stored_path, 'wb') as f:
# pickle.dump(monitor_y_i, f)
def monitor_construction_from_features(features, taus, clustering_results, class_name, monitor_saving_folder):
# if os.path.exists(clustering_result_path):
# clustering_results = pd.read_csv(clustering_result_path)
# else:
# raise RuntimeError("Please partition your data first!")
for tau in taus:
loc_boxes = []
if len(features):
n_dim = features.shape[1]
# load clustering result related to tau
clustering_result = clustering_results[str(tau)]
# determine the labels of clusters
num_clusters = np.amax(clustering_result) + 1
clustering_labels = np.arange(num_clusters)
# extract the indices of vectors in a cluster
clusters_indices = []
for k in clustering_labels:
indices_cluster_k, = np.where(clustering_result == k)
clusters_indices.append(indices_cluster_k)
# creat local box for each cluster
loc_boxes = [Box() for i in clustering_labels]
for j in range(len(loc_boxes)):
points_j = [(i, features[i]) for i in clusters_indices[j]]
loc_boxes[j].build(n_dim, points_j)
else:
raise RuntimeError("There exists no feature for building monitor!!")
# creat the monitor for class y at layer i
monitor = Monitor(good_ref=loc_boxes)
# save the created monitor
if not os.path.exists(monitor_saving_folder):
os.makedirs(monitor_saving_folder)
monitor_saving_path = monitor_saving_folder + "monitor_for_clustering_parameter" + "_tau_" + str(tau) + ".pkl"
with open(monitor_saving_path, 'wb') as f:
pickle.dump(monitor, f)
|