def test_node_and_edge_full_example(self): H = nx.DiGraph() H.add_edge("X", "Y", weight=10) H.add_edge("Y", "Z", weight=9) H.add_edge("Z", "X", weight=8) motif = dotmotif.Motif(""" A -> B [weight>=7] """.strip()) res = NetworkXExecutor(graph=H).find(motif) self.assertEqual(len(res), 3) H.add_edge("Z", "C", weight=7) res = NetworkXExecutor(graph=H).find(motif) self.assertEqual(len(res), 4) H.add_edge("Z", "D", weight="no") res = NetworkXExecutor(graph=H).find(motif) self.assertEqual(len(res), 4) H.add_edge("y", "a") self.assertEqual(len(NetworkXExecutor(graph=H).find(motif)), 4) H.add_edge("y", "a", other_weight=7, weight=8) self.assertEqual(len(NetworkXExecutor(graph=H).find(motif)), 5)
def test_automorphism_reduction(self): G = nx.DiGraph() G.add_edge("X", "Z") G.add_edge("Y", "Z") motif = dotmotif.Motif(""" A -> C B -> C A === B """) res = NetworkXExecutor(graph=G).find(motif) self.assertEqual(len(res), 2) motif = dotmotif.Motif(exclude_automorphisms=True).from_motif(""" A -> C B -> C A === B """) res = NetworkXExecutor(graph=G).find(motif) self.assertEqual(len(res), 1)
def test_edgecount_motif(self): dm = dotmotif.Motif("""A->B""") H = nx.DiGraph() H.add_edge("x", "y") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 1) H.add_edge("x", "y") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 1) H.add_edge("x", "z") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 2)
def test_from_nx_import(self): G = nx.Graph() G.add_edge("A", "B") G.add_edge("B", "C") g = nx.Graph() g.add_edge("A", "B") dm = dotmotif.dotmotif(ignore_direction=True).from_nx(g) E = NetworkXExecutor(graph=G) self.assertEqual(len(E.find(dm)), 4)
def test_fullyconnected_triangle_motif(self): dm = dotmotif.Motif(""" A->B B->C C->A """) H = nx.DiGraph() H.add_edge("x", "y") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 0) H.add_edge("y", "z") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 0) H.add_edge("z", "x") self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 3)
def test_mini_example(self): H = nx.DiGraph() H.add_edge("y", "x", ATTRIBUTE=7) H.add_edge("y", "z", ATTRIBUTE=7) motif = dotmotif.Motif(""" A -> B [ATTRIBUTE>=7] """.strip()) self.assertEqual(len(NetworkXExecutor(graph=H).find(motif)), 2)
def test_edge_attribute_equality(self): dm = dotmotif.Motif(""" A->B [weight==10, area==4] """) H = nx.DiGraph() H.add_edge("z", "x", weight=10, area=4) H.add_edge("x", "y") H.add_edge("y", "z", weight=5) self.assertEqual(len(NetworkXExecutor(graph=H).find(dm)), 1)
def test_one_instance(self): H = nx.DiGraph() H.add_edge("x", "y", weight=1) H.add_edge("y", "z", weight=10) H.add_edge("z", "x", weight=5) motif = dotmotif.Motif(""" A -> B [weight>=11] """.strip()) self.assertEqual(len(NetworkXExecutor(graph=H).find(motif)), 0)
def test_not_in(self): m = dotmotif.Motif(""" A -> B A.name !in "foo" """) host = nx.DiGraph() host.add_edge("A", "B") host.add_node("A", name="the foobar") host.add_node("B", name="Barbaz") res = NetworkXExecutor(graph=host).find(m) self.assertEqual(len(res), 1)
def test_automorphism_flag_triangle(self): G = nx.DiGraph() G.add_edge("A", "B") G.add_edge("B", "C") G.add_edge("C", "A") motif = dotmotif.Motif(""" A -> B B -> C C -> A """) res = NetworkXExecutor(graph=G).find(motif) self.assertEqual(len(res), 3) motif = dotmotif.Motif(exclude_automorphisms=True).from_motif(""" A -> B B -> C C -> A """) res = NetworkXExecutor(graph=G).find(motif) self.assertEqual(len(res), 1)
def test_automorphism_notauto(self): G = nx.DiGraph() G.add_edge("X", "Z") G.add_edge("Y", "Z") motif = dotmotif.Motif(""" A -> C B -> C """) res = NetworkXExecutor(graph=G).find(motif) self.assertEqual(len(res), 2)
def test_two_instance(self): H = nx.DiGraph() H.add_edge("x", "y", weight=1) H.add_edge("y", "z", weight=10) H.add_edge("z", "x", weight=5) H.add_edge("z", "a", weight=5) H.add_edge("a", "b", weight=1) H.add_edge("b", "c", weight=10) H.add_edge("c", "a", weight=5) motif = dotmotif.dotmotif().from_motif(""" A -> B [weight>=7] """.strip()) self.assertEqual(len(NetworkXExecutor(graph=H).find(motif)), 2)
def find_motif(self): # find motif and write out motif_df use dotmotif ''' dotmotif need to cite @article{matelsky_2020_dotmotif, doi = {10.1101/2020.06.08.140533}, url = {https://www.biorxiv.org/content/10.1101/2020.06.08.140533v1}, year = 2020, month = {june}, publisher = {BiorXiv}, author = {Matelsky, Jordan K. and Reilly, Elizabeth P. and Johnson,Erik C. and Wester, Brock A. and Gray-Roncal, William}, title = {{Connectome subgraph isomorphisms and graph queries with DotMotif}}, journal = {BiorXiv} } ''' if self.dataset == 'cn': G = self.cn_G() if self.dataset == 'mo': G = self.mo_G() if self.dataset == 'db': G = self.db_G() for root, _, files in os.walk('./data/' + self.dataset + '/motif'): for file in files: start = time.process_time() motif_file_name = root + '/' + file dm = dotmotif.dotmotif().from_motif(motif_file_name) tmp_df = pd.DataFrame() tmp_list = [] for i in NetworkXExecutor(graph=G).find(dm): tmp_list.append(i) if tmp_list: tmp_df = tmp_df.append(tmp_list, ignore_index=True) tmp_df = tmp_df.reindex(columns=['A', 'B', 'C' ]) # see floder '/motif' tmp_df.to_csv('./data/' + self.dataset + '/motif_unprocess/' + file, sep='\t', index=0, header=0) else: print(file, ' not exsit') elapsed = time.process_time() - start print(file, 'motif created. time cost:', elapsed) print(time.strftime('%y-%m-%d %I:%M:%S %p'))
def test_dynamic_constraints_one_result(self): """ Test that comparisons may be made between variables, e.g.: A.type != B.type """ G = nx.DiGraph() G.add_edge("A", "B") G.add_edge("B", "C") G.add_edge("C", "A") G.add_node("A", radius=25) G.add_node("B", radius=10) exp = """\ A -> B A.radius > B.radius """ dm = dotmotif.Motif(parser=ParserV2) res = NetworkXExecutor(graph=G).find(dm.from_motif(exp)) self.assertEqual(len(res), 1)