class CliqueInERDetector: def __init__(self, v, p, cs, d, num_run=0): self._params = { 'vertices': v, 'probability': p, 'clique_size': cs, 'directed': d, 'load_graph': False, 'load_labels': False, 'load_motifs': False } self._key_name = f"n_{v}_p_{p}_size_{cs}_{'d' if d else 'ud'})" self._dir_path = os.path.join(os.path.dirname(__file__), '..', 'graph_calculations', 'pkl', self._key_name + '_runs', self._key_name + "_run_" + str(num_run)) self._data = GraphBuilder(self._params, self._dir_path) self._graph = self._data.graph() self._labels = self._data.labels() self._motif_calc = MotifCalculator(self._params, self._graph, self._dir_path, gpu=True, device=2) self._motif_matrix = self._motif_calc.motif_matrix( motif_picking=self._motif_calc.clique_motifs()) # self.detect_clique() def detect_clique(self): detector = DetectClique(graph=self._graph, matrix=self._motif_matrix, labels=self._labels, dir_path=self._dir_path) suspected_vertices = detector.irregular_vertices(to_scale=False) vertex_label = [(v, self._labels[v]) for v in suspected_vertices] print(vertex_label)
def _load_other_things(self): graph_ids = os.listdir(self._head_path) self._gnxs = [] self._labels_by_run = [] self._all_labels = [] for run in range(len(graph_ids)): dir_path = os.path.join(self._head_path, self._key_name + "_run_" + str(run)) data = GraphBuilder(self._params, dir_path) gnx = data.graph() self._gnxs.append(gnx) labels = data.labels() self._labels_by_run.append(labels) if type(labels) == dict: new_labels = [y for x, y in labels.items()] self._all_labels += new_labels else: self._all_labels += labels self._mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['clique_size'], self._params['directed']) self._clique_motifs = self._mp.get_3_clique_motifs(3) + self._mp.get_3_clique_motifs(4) \ if self._motifs_picked is None else self._motifs_picked
def _load_data(self, check): graph_ids = os.listdir(self._head_path) if len(graph_ids) == 0: if self._num_runs == 0: raise ValueError( f"No runs of G({self._params['vertices']}, {self._params['probability']}) " f"with a clique of size {self._params['clique_size']} were saved, " f"and no new runs were requested.") self._feature_matrices = [] self._labels = [] motifs_picked = [] for run in range(0, len(graph_ids) + self._num_runs): dir_path = os.path.join(self._head_path, self._key_name + "_run_" + str(run)) data = GraphBuilder(self._params, dir_path) gnx = data.graph() labels = data.labels() mc = MotifCalculator(self._params, gnx, dir_path, gpu=True, device=1) motifs_picked = [ i for i in range(mc.mp.get_3_clique_motifs(3)[0] + 1) ] mc.build_all(motifs_picked) motif_matrix = mc.motif_matrix() self._feature_matrices.append(motif_matrix) if type(labels) == dict: new_labels = [[y for x, y in labels.items()]] self._labels += new_labels else: self._labels += [labels] self._extra_parameters(motifs=motifs_picked) self._scale_matrices() if check == -1: # Training-test split or cross-validation, where in CV the left-out graph index is given. rand_test_indices = np.random.choice( len(graph_ids) + self._num_runs, round((len(graph_ids) + self._num_runs) * 0.2), replace=False) train_indices = np.delete( np.arange(len(graph_ids) + self._num_runs), rand_test_indices) self._test_features = [ self._feature_matrices[j] for j in rand_test_indices ] self._test_labels = [self._labels[j] for j in rand_test_indices] self._training_features = [ self._feature_matrices[j] for j in train_indices ] self._training_labels = [self._labels[j] for j in train_indices] else: one_out = check train_indices = np.delete( np.arange(len(graph_ids) + self._num_runs), one_out) self._test_features = [self._feature_matrices[one_out]] self._test_labels = [self._labels[one_out]] self._training_features = [ self._feature_matrices[j] for j in train_indices ] self._training_labels = [self._labels[j] for j in train_indices]