Commit d27838a2 by Yoann Dufresne

### add tests and correct the values from them

parent 8555474c
 import networkx as nx import d_graph as dgm import itertools from d_graph import compute_all_max_d_graphs class D2Graph(object): ... ... @@ -9,7 +11,7 @@ class D2Graph(object): self.graph = graph # Compute all the d-graphs self.d_graphs = dgm.compute_all_max_d_graphs(self.graph) self.d_graphs = compute_all_max_d_graphs(self.graph) # Index all the d-graphes self.index = self.create_index() ... ... @@ -17,6 +19,33 @@ class D2Graph(object): def create_index(self): index = {} perfect = 0 for node in self.d_graphs: for dg in self.d_graphs[node]: lst = dg.to_ordered_lists() # Generate all dmers without the first node # pull all the values concat = [el for l in lst[1:] for el in l] # generate dmers for idx in range(len(lst[0])): dmer = frozenset(concat + lst[0][:idx] + lst[0][idx+1:]) if not dmer in index: index[dmer] = [dg] else: index[dmer].append(dg) # Generate all dmers without the last node # pull all the values concat = [el for l in lst[:-1] for el in l] # generate dmers for idx in range(len(lst[-1])): dmer = frozenset(concat + lst[-1][:idx] + lst[-1][idx+1:]) if not dmer in index: index[dmer] = [dg] else: index[dmer].append(dg) return index ... ...
 ... ... @@ -42,7 +42,7 @@ class Dgraph(object): self.halves[1].sort(reverse=True, key=lambda v: connex[1][v]) def get_link_ratio(self): def get_link_divergence(self): return abs((self.score / self.get_optimal_score()) - 1) ... ... @@ -51,29 +51,35 @@ class Dgraph(object): return max_len * (max_len - 1) / 2 def to_ordered_list(self): # TODO : Can't be uniq (see for corrections) return self.halves[0][::-1] + [self.center] + self.halves[1] def to_ordered_lists(self): hands = [[],[]] for idx in range(2): prev_connectivity = -1 for node in self.halves[idx]: # group nodes by similar connectivity value = self.connexity[idx][node] if value != prev_connectivity: hands[idx].append([]) prev_connectivity = value hands[idx][-1].append(node) return hands[0][::-1] + [[self.center]] + hands[1] def __eq__(self, other): my_tuple = (self.get_link_ratio(), self.get_optimal_score()) other_tuple = (other.get_link_ratio(), other.get_optimal_score()) my_tuple = (self.get_link_divergence(), self.get_optimal_score()) other_tuple = (other.get_link_divergence(), other.get_optimal_score()) return (my_tuple == other_tuple) def __ne__(self, other): return not (self == other) def __lt__(self, other): my_tuple = (self.get_link_ratio(), self.get_optimal_score()) other_tuple = (other.get_link_ratio(), other.get_optimal_score()) my_tuple = (self.get_link_divergence(), self.get_optimal_score()) other_tuple = (other.get_link_divergence(), other.get_optimal_score()) return (my_tuple < other_tuple) def __hash__(self): return frozenset(self.to_ordered_list()).__hash__() def __repr__(self): # print(self.halves) representation = self.center + " " + str(self.score) + "/" + str(self.get_optimal_score()) + " " ... ...
 ... ... @@ -20,7 +20,7 @@ def main(): G = nx.read_gexf(filename) d2g = d2.D2Graph(G) d2g.save_to_file("data/d2_graph.gexf") # d2g.save_to_file("data/d2_graph.gexf") if __name__ == "__main__": main()
tests/__init__.py 0 → 100644
 import unittest from d2_graph import D2Graph from d_graph import Dgraph from tests.d_graph_data import unit_d_graph, unit_overlapp_d_graph, complete_graph class TestD2Graph(unittest.TestCase): def test_construction(self): d2 = D2Graph(complete_graph) # Evaluate the number of candidate unit d_graphs generated for node, candidates in d2.d_graphs.items(): if node == "C" or node == "B2": self.assertEquals(1, len(candidates)) else: self.assertEquals(0, len(candidates)) # Evaluate the hashes self.assertEquals(3, len(d2.index)) udg = Dgraph(unit_d_graph[0]) udg.put_halves(unit_d_graph[1], unit_d_graph[2], unit_d_graph[3]) uodg = Dgraph(unit_overlapp_d_graph[0]) uodg.put_halves(unit_overlapp_d_graph[1], unit_overlapp_d_graph[2], unit_overlapp_d_graph[3]) key = frozenset({'A2', 'A1', 'B1', 'C', 'B0', 'B2'}) self.assertEquals(2, len(d2.index[key])) self.assertTrue(udg in d2.index[key]) self.assertTrue(uodg in d2.index[key]) key = frozenset({'A0', 'A2', 'A1', 'B1', 'C', 'B2'}) self.assertEquals(1, len(d2.index[key])) self.assertEquals(udg, d2.index[key][0]) key = frozenset({'A2', 'B-1', 'B1', 'C', 'B2', 'B0'}) self.assertEquals(1, len(d2.index[key])) self.assertEquals(uodg, d2.index[key][0]) if __name__ == "__main__": unittest.main()