def generate_output_matrix(self): M1 = self.input_matrix[0:3, :] M2 = self.input_matrix[3:6, :] G1 = nx.from_numpy_matrix(M1, create_using=nx.DiGraph()) G2 = nx.from_numpy_matrix(M2, create_using=nx.DiGraph()) OG = compose(G1, G2) output_matrix = nx.to_numpy_array(OG, dtype=int) output_matrix[output_matrix == 0] = 2 output_matrix[output_matrix != 2] = 0 return output_matrix
def extract_subgraph(graph, object, task, hop=3): """ This function extract n-hop neighbors for an object and task pairs. ToDo: using the subgraph is slightly more involved since node index in the subgraph needs to be adjusted :param object: :param task: :param hop: :return: """ object_subgraph = ego_graph(graph, object, radius=hop, undirected=True) task_subgraph = ego_graph(graph, task, radius=hop, undirected=True) subgraph = compose(object_subgraph, task_subgraph) return subgraph
def path_contradiction_free_merge(graph_clusts, trace=False, trace_lemma=None): def filter_merge(weight): def filtered_graphs(): for g in graph_clusts: def filter_edge(n1, n2): try: return g.edges[n1, n2]['weight'] == weight except KeyError: return False yield subgraph_view(g, filter_edge=filter_edge) return reduce(compose, filtered_graphs()) same_merged = filter_merge(1) diff_merged = filter_merge(-1) rm = set() # Iterate through all diff edges for u, v in diff_merged.edges: # Find all same paths between them found_contradiction = False for path in all_simple_paths(same_merged, u, v): # Found some? Okay delete the negative edge and all positive paths found_contradiction = True rm.update(pairwise(path)) if trace: print(f"Contradiction in clustering for {trace_lemma}", file=sys.stderr) print((u, v), file=sys.stderr) print(path, file=sys.stderr) if found_contradiction: rm.add((u, v)) # Put them back together and remove all the invalid edges merged = compose(same_merged, diff_merged) merged.remove_edges_from(rm) return merged
def union(self, other_graph): """Union two graphs - add all vertices and edges of other graph :type other_graph: NXGraph """ self._g = compose(self._g, other_graph._g)
def get_composed(self, other): # TODO: asssert same config return Graph(agraph=binary_op.compose(self.g, other.g), contraction=self.get_composed_contraction( other.contraction))
def inoutgraph(g, vertex, distance=None): i = ingraph(g, vertex, distance=distance) o = outgraph(g, vertex, distance=distance) return compose(i, o)
SEM_adult.add_nodes_from(white_adult.nodes()) white_adult.add_nodes_from(SEM_adult.nodes()) # Compare datasets. print 'Edges in both datasets:', binary.intersection(SEM_adult, white_adult).number_of_edges() print 'Edges not annotated by John White:', binary.difference(SEM_adult, white_adult).number_of_edges() print 'Edges only annotated by John White:', binary.difference(white_adult, SEM_adult).number_of_edges() for (pre, post) in binary.difference(SEM_adult, white_adult).edges(): print pre, post, SEM_adult[pre][post]['weight'] # Export for Cytoscape. gml.write_gml(SEM_adult, 'data/SEM_adult.gml') combined_graph = binary.compose(SEM_adult, white_adult) specific_to_SEM_adult = binary.difference(SEM_adult, white_adult) specific_to_white_adult = binary.difference(white_adult, SEM_adult) for (pre, post) in combined_graph.edges(): if specific_to_SEM_adult.has_edge(pre, post): combined_graph[pre][post]['dataset'] = 'only_SEM_adult' elif specific_to_white_adult.has_edge(pre, post): combined_graph[pre][post]['dataset'] = 'only_white_adult' else: combined_graph[pre][post]['dataset'] = 'shared' gml.write_gml(combined_graph, 'data/combined_graph.gml')