def __init__(self, gpu=False, device=0): self.NODE_LEVEL = { "attractor_basin": FeatureMeta(AttractorBasinCalculator, {"ab"}), # Directed "average_neighbor_degree": FeatureMeta(AverageNeighborDegreeCalculator, {"avg_nd"}), # Any "betweenness_centrality": FeatureMeta(BetweennessCentralityCalculator, {"betweenness"}), # Any "bfs_moments": FeatureMeta(BfsMomentsCalculator, {"bfs"}), # Any "closeness_centrality": FeatureMeta(ClosenessCentralityCalculator, {"closeness"}), # Any "communicability_betweenness_centrality": FeatureMeta(CommunicabilityBetweennessCentralityCalculator, {"communicability"}), # Undirected "eccentricity": FeatureMeta(EccentricityCalculator, {"ecc"}), # Any "fiedler_vector": FeatureMeta(FiedlerVectorCalculator, {"fv"}), # Undirected (due to a code limitation) "flow": FeatureMeta(FlowCalculator, {}), # Directed # General - calculating degrees. Directed will get (in_deg, out_deg) and undirected will get degree only per vertex. "general": FeatureMeta(GeneralCalculator, {"gen"}), # Any "hierarchy_energy": FeatureMeta(HierarchyEnergyCalculator, {"hierarchy"}), # Directed (but works for any) "k_core": FeatureMeta(KCoreCalculator, {"kc"}), # Any "load_centrality": FeatureMeta(LoadCentralityCalculator, {"load_c"}), # Any "louvain": FeatureMeta(LouvainCalculator, {"lov"}), # Undirected "motif3": FeatureMeta(nth_nodes_motif(3, gpu, device), {"m3"}), # Any "page_rank": FeatureMeta(PageRankCalculator, {"pr"}), # Directed (but works for any) "motif4": FeatureMeta(nth_nodes_motif(4, gpu, device), {"m4"}), # Any } self.MOTIFS = { "motif3": FeatureMeta(nth_nodes_motif(3, gpu, device), {"m3"}), "motif4": FeatureMeta(nth_nodes_motif(4, gpu, device), {"m4"}) } """
def __init__(self, params, graph, dir_path, gpu, device): self._params = params self._graph = graph self._dir_path = dir_path self._mp = MotifProbability(self._params["vertices"], self._params["probability"], self._params["clique_size"], self._params["directed"]) self._motif_mat = None self._motif_features = { "motif3": FeatureMeta(nth_nodes_motif(3, gpu=gpu, device=device), {"m3"}), "motif4": FeatureMeta(nth_nodes_motif(4, gpu=gpu, device=device), {"m4"}) }
def __init__(self, gpu=False): self.NODE_LEVEL = { "attractor_basin": FeatureMeta(AttractorBasinCalculator, {"ab"}), "average_neighbor_degree": FeatureMeta(AverageNeighborDegreeCalculator, {"avg_nd"}), "betweenness_centrality": FeatureMeta(BetweennessCentralityCalculator, {"betweenness"}), "bfs_moments": FeatureMeta(BfsMomentsCalculator, {"bfs"}), "closeness_centrality": FeatureMeta(ClosenessCentralityCalculator, {"closeness"}), "communicability_betweenness_centrality": FeatureMeta(CommunicabilityBetweennessCentralityCalculator, {"communicability"}), "eccentricity": FeatureMeta(EccentricityCalculator, {"ecc"}), "fiedler_vector": FeatureMeta(FiedlerVectorCalculator, {"fv"}), "flow": FeatureMeta(FlowCalculator, {}), "general": FeatureMeta(GeneralCalculator, {"gen"}), "hierarchy_energy": FeatureMeta(HierarchyEnergyCalculator, {"hierarchy"}), "k_core": FeatureMeta(KCoreCalculator, {"kc"}), "load_centrality": FeatureMeta(LoadCentralityCalculator, {"load_c"}), "louvain": FeatureMeta(LouvainCalculator, {"lov"}), "motif3": FeatureMeta(nth_nodes_motif(3, gpu), {"m3"}), "page_rank": FeatureMeta(PageRankCalculator, {"pr"}), "motif4": FeatureMeta(nth_nodes_motif(4, gpu), {"m4"}), } self.MOTIFS = { "motif3": FeatureMeta(nth_nodes_motif(3, gpu), {"m3"}), "motif4": FeatureMeta(nth_nodes_motif(4, gpu), {"m4"}) }
def _calc_motif3(self): raw_ftr = GraphFeatures(self._graph, { "motif3": FeatureMeta(nth_nodes_motif(3, gpu=self._gpu, device=self._device), {"m3"}) }, dir_path=self._dir_path) raw_ftr.build(should_dump=self._dump) feature = raw_ftr['motif3']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature return self._log_norm(motif_matrix)
def _calc_motif4(self): # FOR NOW, NO GPU FOR US if os.path.exists(os.path.join(self._dir_path, "motif4.pkl")): pkl4 = pickle.load( open(os.path.join(self._dir_path, "motif4.pkl"), "rb")) if type(pkl4) == dict: motif4 = self._to_matrix(pkl4) elif type(pkl4) == MotifsNodeCalculator: motif4 = np.array(pkl4._features) else: motif4 = np.array(pkl4) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['clique_size'], self._params['directed']) motif3_count = 1 + mp.get_3_clique_motifs(3)[ -1] # The full 3 clique is the last motif 3. clique_motifs = [ m - motif3_count for m in mp.get_3_clique_motifs(4) ] return motif4[:, clique_motifs] else: return motif4 raw_ftr = GraphFeatures(self._graph, { "motif4": FeatureMeta(nth_nodes_motif(4, gpu=self._gpu, device=self._device), {"m4"}) }, dir_path=self._dir_path) raw_ftr.build(should_dump=True) feature = raw_ftr['motif4']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature normed_matrix = self._log_norm(motif_matrix) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['clique_size'], self._params['directed']) motif3_count = 1 + mp.get_3_clique_motifs(3)[ -1] # The full 3 clique is the last motif 3. clique_motifs = [ m - motif3_count for m in mp.get_3_clique_motifs(4) ] return normed_matrix[:, clique_motifs] else: return normed_matrix
def _calc_motif4(self): raw_ftr = GraphFeatures(self._graph, { "motif4": FeatureMeta(nth_nodes_motif(4, gpu=self._gpu, device=self._device), {"m4"}) }, dir_path="") raw_ftr.build(should_dump=False) feature = raw_ftr['motif4']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature normed_matrix = self._log_norm(motif_matrix) return normed_matrix
def _calc_motif3(self): raw_ftr = GraphFeatures(self._graph, {"motif3": FeatureMeta(nth_nodes_motif(3, gpu=self._gpu, device=self._device), {"m3"})}, dir_path=self._dir_path) raw_ftr.build(should_dump=True) feature = raw_ftr['motif3']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature normed_matrix = self._log_norm(motif_matrix) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['subgraph_size'], self._params['directed']) clique_motifs = mp.get_3_clique_motifs(3) return normed_matrix[:, clique_motifs] else: return normed_matrix
def _calc_motif4(self): raw_ftr = GraphFeatures(self._graph, {"motif4": FeatureMeta(nth_nodes_motif(4, gpu=self._gpu, device=self._device), {"m4"})}, dir_path=self._dir_path) raw_ftr.build(should_dump=True) feature = raw_ftr['motif4']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature normed_matrix = self._log_norm(motif_matrix) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['subgraph_size'], self._params['directed']) motif3_count = 1 + mp.get_3_clique_motifs(3)[-1] # The full 3 clique is the last motif 3. clique_motifs = [m - motif3_count for m in mp.get_3_clique_motifs(4)] return normed_matrix[:, clique_motifs] else: return normed_matrix
def _calc_motif3(self): # FOR NOW, NO GPU FOR US if os.path.exists(os.path.join(self._dir_path, "motif3.pkl")): pkl3 = pickle.load( open(os.path.join(self._dir_path, "motif3.pkl"), "rb")) if type(pkl3) == dict: motif3 = self._to_matrix(pkl3) elif type(pkl3) == MotifsNodeCalculator: motif3 = np.array(pkl3._features) else: motif3 = np.array(pkl3) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['clique_size'], self._params['directed']) clique_motifs = mp.get_3_clique_motifs(3) return motif3[:, clique_motifs] else: return motif3 raw_ftr = GraphFeatures(self._graph, { "motif3": FeatureMeta(nth_nodes_motif(3, gpu=self._gpu, device=self._device), {"m3"}) }, dir_path=self._dir_path) raw_ftr.build(should_dump=True) feature = raw_ftr['motif3']._features if type(feature) == dict: motif_matrix = self._to_matrix(feature) else: motif_matrix = feature normed_matrix = self._log_norm(motif_matrix) if self._motif_choice == "All_Motifs": mp = MotifProbability(self._params['vertices'], self._params['probability'], self._params['clique_size'], self._params['directed']) clique_motifs = mp.get_3_clique_motifs(3) return normed_matrix[:, clique_motifs] else: return normed_matrix