Commit 2cb22aca authored by Yoann Dufresne's avatar Yoann Dufresne

reduction + evaluation ok

parent c6ef39a8
......@@ -141,7 +141,7 @@ class D2Graph(nx.Graph):
self.bidict_nodes = {}
for idx, node in enumerate(self.nodes(data=True)):
node, data = node
dg = Dgraph.load(data["udg"], self.barcode_graph)
dg = Dgraph.load(data["udg"], data["score"], data["barcode_edges"])
self.bidict_nodes[node] = dg
self.all_d_graphs.append(dg)
if dg.idx == -1:
......@@ -189,7 +189,7 @@ class D2Graph(nx.Graph):
self.nodes[nodes[dg]]["barcode_edges"] = barcode_edges
self.nodes[nodes[dg]]["score"] = f"{dg.score}/{dg.get_optimal_score()}"
self.nodes[nodes[dg]]["udg"] = str(dg)
self.nodes[nodes[dg]]["central_node_barcode"] = str(dg).split(']')[0]+']'
self.nodes[nodes[dg]]["central_node_barcode"] = str(dg.center)
# Create the edges from neighbor edges
for dg in self.all_d_graphs:
......
......@@ -19,7 +19,7 @@ class Dgraph(object):
self.edges = set()
@staticmethod
def load(lcp_txt, variables):
def load(lcp_txt, score, variables):
""" Static method to load a dgraph from a text
:param lcp_txt: the saved d-graph
:return: a new d-graph object corresponding to the test
......@@ -43,6 +43,12 @@ class Dgraph(object):
dg.node_set.update(h2)
dg.nodes.extend(h2)
# Score parsing
dg.score = int(score.split('/')[0])
# covering variable loading
dg.edges = {int(x) for x in variables.split(' ')}
return dg
def put_halves(self, h1, h2, graph):
......
#!/usr/bin/env python3
import sys
with open(sys.argv[1]) as file:
header = file.readline()
nb_nodes, nb_variables = [int(x) for x in header.split()]
present_variables = [False]*nb_variables
for _ in range(nb_nodes):
line = file.readline()
variables = [int(x) for x in line.split()[1:]]
for var in variables:
present_variables[var] = True
for idx, val in enumerate(present_variables):
if not val:
print(f"{idx} not present")
\ No newline at end of file
#!/usr/bin/env python3
import networkx as nx
import argparse
def parse_arguments():
parser = argparse.ArgumentParser(description='Analysis graph cliques complexity')
parser.add_argument('graph', help='The input graph to analyse')
args = parser.parse_args()
return args
def analyse_neighbors_cliques(graph):
clique_sizes = []
for node in graph.nodes():
neighbors = list(graph.neighbors(node))
subgraph = graph.subgraph(neighbors)
cliques = list(nx.find_cliques(subgraph))
print(node, " \t", len(neighbors), " \t", len(cliques))
clique_sizes.append(len(cliques))
print(max(clique_sizes))
def main():
args = parse_arguments()
graph = nx.read_gexf(args.graph)
analyse_neighbors_cliques(graph)
if __name__ == "__main__":
main()
......@@ -10,12 +10,15 @@ from deconvolution.d2graph import d2_algorithms as d2a
def parse_arguments():
parser = argparse.ArgumentParser(description='Reduce the number of edges of a d2 graph. The reduction is performed on edges that are redundant.')
parser.add_argument('barcode_graph', help='The barcode graph file. Must be a gefx formated file.')
parser.add_argument('d2_graph', help='d2 graph to reduce. Must be a gefx formated file.')
parser.add_argument('--output_d2_name', '-o', default="transitively_reduced_d2.gexf", help="Output file prefix.")
parser.add_argument('--component_min_size', '-c', type=int, default=10, help="Minimum size of a component to keep it in the d2-graph, after the edge simplifications.")
parser.add_argument('lcp_graph', help='lcp graph to reduce. Must be a gefx formated file.')
parser.add_argument('--output_lcpg_name', '-o', default="", help="Output file prefix.")
parser.add_argument('--component_min_size', '-c', type=int, default=10, help="Minimum size of a component to keep it in the lcp graph, after the edge simplifications.")
args = parser.parse_args()
if args.output_lcpg_name == "":
args.output_lcpg_name = args.lcp_graph[:args.lcp_graph.rfind('.')] + "_reduced.gexf"
return args
......@@ -23,32 +26,30 @@ def main():
# Parsing the arguments and validate them
args = parse_arguments()
barcode_file = args.barcode_graph
d2_file = args.d2_graph
if (not barcode_file.endswith('.gexf')) or (not d2_file.endswith(".gexf")):
lcpg_name = args.lcp_graph
if not lcpg_name.endswith(".gexf"):
print("Inputs file must be gexf formatted", file=sys.stderr)
exit(1)
# Loading
print("--- d2 graph loading ---")
G = nx.read_gexf(barcode_file)
d2g = d2.D2Graph(G)
d2g.load(d2_file)
print("--- lcp graph loading ---")
lcpg = d2.D2Graph()
lcpg.load(lcpg_name)
# Algorithms for reduction
print("--- 3-reduction processing ---")
d2a.transitive_reduction(d2g)
d2a.transitive_reduction(lcpg)
# Take the principal component
print("--- Extract and write the graph ---")
components = [c for c in nx.connected_components(d2g) if len(c) > args.component_min_size]
components = [c for c in nx.connected_components(lcpg) if len(c) > args.component_min_size]
nodes = set()
for comp in components:
nodes.update(comp)
subgraph = d2g.subgraph(nodes)
subgraph = lcpg.subgraph(nodes)
# Write the simplified graph
nx.write_gexf(subgraph, args.output_d2_name)
nx.write_gexf(subgraph, args.output_lcpg_name)
if __name__ == "__main__":
......
#!/usr/bin/env python3
import networkx as nx
import argparse
import sys
import random
from deconvolution.d2graph import d2_graph as d2
from barcodes.partialorder import greedy_partial_order, bb_partial_order
def parse_arguments():
parser = argparse.ArgumentParser(description='Greedy construction of a path through the d2 graph.')
parser.add_argument('barcode_graph', help='The barcode graph file. Must be a gefx formatted file.')
parser.add_argument('d2_graph', help='d2 graph to reduce. Must be a gexf formatted file.')
parser.add_argument('--out_prefix', '-o', default="", help="Output file prefix.")
args = parser.parse_args()
if args.out_prefix == "":
args.out_prefix = '.'.join(args.d2_graph.split('.')[:-1])
return args
def main():
# Parsing the arguments and validate them
args = parse_arguments()
barcode_file = args.barcode_graph
d2_file = args.d2_graph
if (not barcode_file.endswith('.gexf')) or (not d2_file.endswith(".gexf")):
print("Inputs file must be gexf formatted", file=sys.stderr)
exit(1)
# Loading
G = nx.read_gexf(barcode_file)
d2g = d2.D2Graph(G)
d2g.load(d2_file)
# Take the principal component
largest_component_nodes = max(nx.connected_components(d2g), key=len)
largest_component = d2g.subgraph(largest_component_nodes)
all_nodes = list(largest_component.nodes())
rnd_node = all_nodes[random.randint(0, len(all_nodes)-1)]
# po = greedy_partial_order(largest_component, rnd_node)
for po in bb_partial_order(largest_component, rnd_node):
print("barcodes", len(po), "sets", po.len_sets, "udgs", po.len_udgs, "score", po.score)
if __name__ == "__main__":
main()
......@@ -25,24 +25,6 @@ def parse_args():
return args
def load_graph(filename):
if filename.endswith('.graphml'):
return nx.read_graphml(filename)
elif filename.endswith('.gexf'):
return nx.read_gexf(filename)
else:
print("Wrong file format. Require graphml or gefx format", file=sys.stderr)
exit()
def save_graph(g, filename):
if filename.endswith('.graphml'):
return nx.write_graphml(g,filename)
elif filename.endswith('.gexf'):
return nx.write_gexf(g,filename)
else:
print("Wrong file format. Require graphml or gefx format", file=sys.stderr)
exit()
def transform_bg(graph):
idx = 0
node_names = {}
......@@ -610,14 +592,14 @@ def verify_graph_edges(d2_component):
def main():
args = parse_args()
graph = load_graph(args.filename)
graph = nx.read_gexf(args.filename)
if args.type == "path":
if args.barcode_graph is None:
print("--barcode_graph is required for path analysis", file=sys.stderr)
exit(0)
barcode_graph = load_graph(args.barcode_graph)
barcode_graph = nx.read_gexf(args.barcode_graph)
# if len(list(nx.connected_components(graph))) != 1:
# print([len(x) for x in list(nx.connected_components(graph))])
# exit("when running evaluate.py --type path, the graph should have a single connected component (it's supposed to be a path, after all)")
......@@ -660,7 +642,7 @@ def main():
extension = args.filename.split('.')[-1]
base_filename = '.'.join(args.filename.split('.')[:-1])
save_graph(component, base_filename+".verified."+extension)
nx.write_gexf(component, base_filename+".verified."+extension)
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment