def prediction(self): highest_betweenness = dict() if not self.values: for community in self.communities: print("Getting betweenness for community {}".format(community)) subgraph = nx.subgraph(self.graph, self.communities[community]) highest_betweenness[community] = self.__get_betweenness( subgraph) print("Betweenness done") write_dict_to_json(highest_betweenness, self.filename, "../betweenness/") print("Betweenness values written") else: print("Betweenness provided") highest_betweenness = self.values highest_betweenness_left = highest_betweenness["0"] highest_betweenness_right = highest_betweenness["1"] non_connected_nodes = list(nx.non_edges(self.graph)) n_possible_new_connections = len(non_connected_nodes) non_connected_nodes = list( filter( lambda x: (x[0] in highest_betweenness_right and x[1] in highest_betweenness_left) or (x[0] in highest_betweenness_left and x[1] in highest_betweenness_right), non_connected_nodes)) possible_new_edges = self.__get_highest_betweenness( non_connected_nodes, highest_betweenness_left, highest_betweenness_right) possible_new_edges = { k: v for k, v in sorted(possible_new_edges.items(), key=lambda item: item[1], reverse=True) } max_links = len(possible_new_edges) number_edges = round(self.k * self.n_edges) all_possible_new_edges = possible_new_edges.keys() if number_edges < max_links: edges_to_add = islice(all_possible_new_edges, number_edges) else: edges_to_add = all_possible_new_edges self.percentage_edges_added = self.k print("% of edges added: {}".format(self.percentage_edges_added)) print("Adding {} edges".format(number_edges)) for edge in edges_to_add: self.link_nodes(edge[0], edge[1])
def prediction(self): if not self.values: left = self.communities["0"] right = self.communities["1"] non_connected_nodes = list(nx.non_edges(self.graph)) n_possible_new_connections = len(non_connected_nodes) non_connected_nodes = list( filter( lambda x: (x[0] in right and x[1] in left) or (x[0] in left and x[1] in right), non_connected_nodes)) algorithm = None print("Adding edges using {}".format(self.algorithm.lower())) if self.algorithm == TypeOfAlgorithm.ADAMIC_ADAR.value: algorithm = nx.adamic_adar_index elif self.algorithm == TypeOfAlgorithm.JACCARD_COEFFICIENT.value: algorithm = nx.jaccard_coefficient elif self.algorithm == TypeOfAlgorithm.RESOURCE_ALLOCATION.value: algorithm = nx.resource_allocation_index elif self.algorithm == TypeOfAlgorithm.PREFERENTIAL_ATTACHMENT.value: algorithm = nx.preferential_attachment edges_to_add = list( sorted(algorithm(self.graph, non_connected_nodes), key=lambda element: element[2], reverse=True)) write_dict_to_json({"value": edges_to_add}, self.filename, f"../{self.algorithm.lower()}/") print(f"{self.algorithm} values written") else: print(f"{self.algorithm.lower()} provided") edges_to_add = list(self.values.values())[0] max_links = len(edges_to_add) number_edges = round(self.k * self.n_edges) all_possible_new_edges = edges_to_add if number_edges < max_links: edges_to_add = islice(all_possible_new_edges, number_edges) else: edges_to_add = all_possible_new_edges self.percentage_edges_added = self.k print("% of edges added: {}".format(self.percentage_edges_added)) print("Adding {} edges".format(number_edges)) for edge in edges_to_add: self.link_nodes(edge[0], edge[1])
def prediction(self): highest_betweenness = dict() if not self.betweeness_value: for community in self.communities: print("Getting betweenness for community {}".format(community)) subgraph = nx.subgraph(self.graph, self.communities[community]) highest_betweenness[ community] = self._LinkWithBetweenness__get_betweenness( subgraph) print("Betweenness done") write_dict_to_json(highest_betweenness, self.filename, "../betweenness/") print("Betweenness values written") else: print("Betweenness provided") highest_betweenness = self.betweeness_value highest_betweenness_left = highest_betweenness["0"] highest_betweenness_right = highest_betweenness["1"] non_connected_nodes = list(nx.non_edges(self.graph)) n_possible_new_connections = len(non_connected_nodes) non_connected_nodes = list( filter( lambda x: (x[0] in highest_betweenness_right and x[1] in highest_betweenness_left) or (x[0] in highest_betweenness_left and x[1] in highest_betweenness_right), non_connected_nodes)) ranked_betweenness_nodes = self._LinkWithBetweenness__get_highest_betweenness( non_connected_nodes, highest_betweenness_left, highest_betweenness_right) if not self.values: algorithm = None print("Combining betweenness with {}".format( self.algorithm.lower())) if self.algorithm.upper() == TypeOfAlgorithm.ADAMIC_ADAR.value: algorithm = nx.adamic_adar_index elif self.algorithm == TypeOfAlgorithm.JACCARD_COEFFICIENT.value: algorithm = nx.jaccard_coefficient elif self.algorithm == TypeOfAlgorithm.RESOURCE_ALLOCATION.value: algorithm = nx.resource_allocation_index elif self.algorithm == TypeOfAlgorithm.PREFERENTIAL_ATTACHMENT.value: algorithm = nx.preferential_attachment ranked_similarity_nodes = list( sorted(algorithm(self.graph, non_connected_nodes), key=lambda element: element[2], reverse=True)) write_dict_to_json({"values": ranked_similarity_nodes}, self.filename, f"../{self.algorithm.lower()}/") print(f"{self.algorithm} values written") else: print(f"{self.algorithm.lower()} provided") ranked_similarity_nodes = list(self.values.values())[0] ranked_similarity_nodes = list(map(tuple, ranked_similarity_nodes)) scores = self.__combine_scores(ranked_betweenness_nodes, ranked_similarity_nodes) scores = { k: v for k, v in sorted( scores.items(), key=lambda item: item[1], reverse=True) } max_links = len(scores) number_edges = round(self.k, self.n_edges) all_possible_new_edges = scores.keys() if number_edges < max_links: edges_to_add = islice(all_possible_new_edges, number_edges) else: edges_to_add = all_possible_new_edges self.percentage_edges_added = self.k print("% of edges added: {}".format(self.percentage_edges_added)) print("Adding {} edges".format(number_edges)) for edge in edges_to_add: self.link_nodes(edge[0], edge[1])
path_preferential_attachment) else: given_preferential_attachment_value = None if communities_to_write in communities_written: given_communities = read_json_to_dict(communities_to_write, path_community) communities = CommunityDetection( graph=graph, algorithm=algorithm, given_communities=given_communities) print('Getting communities from file') else: communities = CommunityDetection(graph=graph, algorithm=algorithm) write_dict_to_json(communities.communities, communities_to_write, path_community) result = results( g=graph, communities=communities.communities, per_edges=percentage_edges, filename=graph_name, link_prediction_alg=[ "BETWEENNESS", "EFFECTIVE_SIZE", "JACCARD_COEFFICIENT", "ADAMIC_ADAR", "RESOURCE_ALLOCATION", "PREFERENTIAL_ATTACHMENT" ], hybrid=hybrid, given_betweenness_value=given_betweenness_value, given_effective_size=given_effective_size_value, given_jaccard_value=given_jaccard_value,