import networkx as nx from graph_measures.features_infra.feature_calculators import NodeFeatureCalculator, FeatureMeta class LoadCentralityCalculator(NodeFeatureCalculator): def is_relevant(self): return True def _calculate(self, include: set, is_regression=False): self._features = nx.load_centrality(self._gnx) feature_entry = { "load_centrality": FeatureMeta(LoadCentralityCalculator, {"load_c"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(LoadCentralityCalculator, is_max_connected=True)
for node in self._gnx} max_b_u = float(max(b_u.values())) for node in self._gnx: # the delta determines whether this node is to be considered if (b_u[node] / max_b_u) <= self._threshold: self._features[node] = 0 continue udists = undirected_dists[node] dists = directed_dists[node] # getting coordinated values from two dictionaries with the same keys # saving the data as np.array type num, denom = map(np.array, zip(*((udists[n], dists[n]) for n in dists))) num = num[denom != 0] denom = denom[denom != 0] self._features[node] = np.sum(num / denom) / float(b_u[node]) feature_entry = { "flow": FeatureMeta(FlowCalculator, {}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(FlowCalculator, is_max_connected=True)
import networkx as nx from graph_measures.features_infra.feature_calculators import NodeFeatureCalculator, FeatureMeta class AverageNeighborDegreeCalculator(NodeFeatureCalculator): def is_relevant(self): return True def _calculate(self, include: set, is_regression=False): self._features = nx.average_neighbor_degree(self._gnx) feature_entry = { "average_neighbor_degree": FeatureMeta(AverageNeighborDegreeCalculator, {"avg_nd"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(AverageNeighborDegreeCalculator, is_max_connected=True)
import networkx as nx from graph_measures.features_infra.feature_calculators import NodeFeatureCalculator, FeatureMeta class PageRankCalculator(NodeFeatureCalculator): def __init__(self, *args, alpha=0.9, **kwargs): super(PageRankCalculator, self).__init__(*args, **kwargs) self._alpha = alpha def is_relevant(self): # Undirected graphs will be converted to a directed # graph with two directed edges for each undirected edge. return True def _calculate(self, include: set, is_regression=False): self._features = nx.pagerank(self._gnx, alpha=self._alpha) feature_entry = { "page_rank": FeatureMeta(PageRankCalculator, {"pr"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(PageRankCalculator, is_max_connected=True)
from collections import Counter import community from graph_measures.features_infra.feature_calculators import NodeFeatureCalculator, FeatureMeta class LouvainCalculator(NodeFeatureCalculator): def is_relevant(self): # relevant only for undirected graphs return not self._gnx.is_directed() def _calculate(self, include: set, is_regression=False): partition = community.best_partition(self._gnx) com_size_dict = Counter(partition.values()) self._features = {node: com_size_dict[partition[node]] for node in self._gnx} feature_entry = { "louvain": FeatureMeta(LouvainCalculator, {"lov"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(LouvainCalculator, is_max_connected=True)
class EccentricityCalculator(NodeFeatureCalculator): def _calculate(self, include: set, is_regression=False): dists = { src: neighbors for src, neighbors in nx.all_pairs_shortest_path_length(self._gnx) } self._features = { node: max(neighbors.values()) for node, neighbors in dists.items() } def _calculate_dep(self, include: set): # Not using eccentricity to handle disconnected graphs. (If a graph has more than 1 connected components, # the eccentricty will raise an exception) self._features = { node: nx.eccentricity(self._gnx, node) for node in self._gnx } def is_relevant(self): return True feature_entry = { "eccentricity": FeatureMeta(EccentricityCalculator, {"ecc"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(EccentricityCalculator, is_max_connected=True)
class FiedlerVectorCalculator(NodeFeatureCalculator): def _calculate_dep(self, include: set): # Working on every connected component by itself self._features = dict(zip(self._gnx, alg_connectivity.fiedler_vector(self._gnx))) def _calculate(self, include: set, is_regression=False): self._features = {} for graph in nx.connected_component_subgraphs(self._gnx): if len(graph) < 2: self._features.update(zip(graph.nodes(), [0.] * len(graph))) else: self._features.update(zip(graph.nodes(), map(float, alg_connectivity.fiedler_vector(graph)))) def is_relevant(self): # Fiedler vector also works only on connected undirected graphs # so if gnx is not connected we shall expect an exception: networkx.exception.NetworkXError # return (not self._gnx.is_directed()) and (nx.is_connected(self._gnx.to_undirected())) return not self._gnx.is_directed() feature_entry = { "fiedler_vector": FeatureMeta(FiedlerVectorCalculator, {"fv"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(FiedlerVectorCalculator, is_max_connected=True)
def test_all(): for cls in FEATURE_CLASSES: test_specific_feature(cls, is_max_connected=True)
d = r return laplacian, y, tol, r, d @staticmethod def _initialize_vars_from_laplacian_matrix1(g): # creating laplacian matrix w = g + g.T d = np.diag(sum(w)) l = d - w _id = np.sum(g, 0) od = np.sum(g, 1) # initialize_vars b = np.subtract((np.array([od])).T, (np.array([_id])).T) tol = 0.001 n = np.size(g, 1) y = np.random.rand(n, 1) y = np.subtract(y, (1 / n) * sum(y)) k = np.dot(l, y) r = np.subtract(b, k) d = r return l, y, tol, r, d feature_entry = { "hierarchy_energy": FeatureMeta(HierarchyEnergyCalculator, {"hierarchy"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(HierarchyEnergyCalculator, is_max_connected=True)
from graph_measures.features_infra.feature_calculators import NodeFeatureCalculator, FeatureMeta class GeneralCalculator(NodeFeatureCalculator): def is_relevant(self): return True def _calculate(self, include: set, is_regression=False): if self._gnx.is_directed(): self._features = {node: (in_deg, out_deg) for (node, out_deg), (_, in_deg) in zip(self._gnx.out_degree(), self._gnx.in_degree())} else: self._features = {node: deg for node, deg in self._gnx.degree()} feature_entry = { "general": FeatureMeta(GeneralCalculator, {"gen"}), } if __name__ == "__main__": from graph_measures.measure_tests.specific_feature_test import test_specific_feature test_specific_feature(GeneralCalculator, is_max_connected=True)