def test_dm_parser_limit(self): dm = dotmotif.Motif(limit=3) dm.from_motif(_DEMO_G_MIN) self.assertTrue("LIMIT 3" in Neo4jExecutor.motif_to_cypher(dm).strip()) dm = dotmotif.Motif() dm.from_motif(_DEMO_G_MIN) self.assertFalse("LIMIT" in Neo4jExecutor.motif_to_cypher(dm).strip())
def test_alphanumeric_variables(self): exp = """\ edge(A, B) { A -> B } dualedge(A1, B) { # Nested-inside comment! edge(A1, B) # inline comment B -> A1 } dualedge(foo_1, bar_2) # inline comment # standalone comment foo_1 -> bar_2 # inline comment """ dm = dotmotif.Motif(exp) edges = list(dm._g.edges(data=True)) self.assertEqual(len(edges), 2) self.assertEqual(list(dm._g.nodes()), ["foo_1", "bar_2"]) self.assertEqual(type(list(dm._g.nodes())[0]), str) new_exp = """ L1 -> Mi1 L1 -> Tm3 L3 -> Mi9 """ dm = dotmotif.Motif(new_exp) self.assertEqual(list(dm._g.nodes()), ["L1", "Mi1", "Tm3", "L3", "Mi9"])
def test_node_macro_attr(self): exp = """\ macro(A) { A.type = "excitatory" A.size >= 4.0 } Aaa -> Ba macro(Aaa) """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm.list_node_constraints()), 1) self.assertEqual(list(dm.list_node_constraints().keys()), ["Aaa"]) exp = """\ macro(A) { A.type = "excitatory" A.size >= 4.0 } Aaa -> Ba macro(Aaa) macro(Ba) """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm.list_node_constraints()), 2) self.assertEqual(list(dm.list_node_constraints().keys()), ["Aaa", "Ba"])
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 = GrandIsoExecutor(graph=G).find(motif) self.assertEqual(len(res), 2) motif = dotmotif.Motif(exclude_automorphisms=True).from_motif(""" A -> C B -> C A === B """) res = GrandIsoExecutor(graph=G).find(motif) self.assertEqual(len(res), 1)
def test_dm_parser_actions(self): dm = dotmotif.Motif(_THREE_CYCLE) self.assertEqual([e[2]["action"] for e in dm._g.edges(data=True)], ["SYN"] * 3) dm = dotmotif.Motif(_THREE_CYCLE_INH) self.assertEqual([e[2]["action"] for e in dm._g.edges(data=True)], ["INH"] * 3)
def test_dm_parser_edge_exists(self): dm = dotmotif.Motif(_THREE_CYCLE) self.assertEqual([e[2]["exists"] for e in dm._g.edges(data=True)], [True] * 3) dm = dotmotif.Motif(_THREE_CYCLE_NEG) self.assertEqual([e[2]["exists"] for e in dm._g.edges(data=True)], [False] * 3) dm = dotmotif.Motif(_THREE_CYCLE_NEG_INH) self.assertEqual([e[2]["exists"] for e in dm._g.edges(data=True)], [False] * 3)
def test_dm_parser_no_direction(self): dm = dotmotif.Motif(ignore_direction=True) dm.from_motif(_DEMO_G_MIN) self.assertEqual( Neo4jExecutor.motif_to_cypher(dm).strip(), _DEMO_G_MIN_CYPHER.strip().replace("->", "-"), )
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 = GrandIsoExecutor(graph=H).find(motif) self.assertEqual(len(res), 3) H.add_edge("Z", "C", weight=7) res = GrandIsoExecutor(graph=H).find(motif) self.assertEqual(len(res), 4) H.add_edge("Z", "D", weight="no") res = GrandIsoExecutor(graph=H).find(motif) self.assertEqual(len(res), 4) H.add_edge("y", "a") self.assertEqual(len(GrandIsoExecutor(graph=H).find(motif)), 4) H.add_edge("y", "a", other_weight=7, weight=8) self.assertEqual(len(GrandIsoExecutor(graph=H).find(motif)), 5)
def test_dm_parser_no_pretty_print(self): dm = dotmotif.Motif(pretty_print=False) dm.from_motif(_DEMO_G_MIN) self.assertEqual( Neo4jExecutor.motif_to_cypher(dm).strip(), _DEMO_G_MIN_CYPHER.strip().replace("\n", " "), )
def test_edge_multi_attr(self): exp = """\ Aa -> Ba [type != 1, type != 12] """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm._g.edges()), 1) u, v, d = list(dm._g.edges(data=True))[0] self.assertEqual(d["constraints"]["type"], {"!=": [1, 12]})
def test_macro_not_added(self): exp = """\ edge(A, B) { A -> B } """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm._g.edges()), 0)
def test_automatic_autos(self): exp = """\ A -> C B -> C """ dm = dotmotif.Motif(exp, exclude_automorphisms=True) cypher = Neo4jExecutor.motif_to_cypher(dm) self.assertIn("id(A) < id(B)", cypher)
def test_simple_macro(self): exp = """\ edge(A, B) { A -> B } edge(C, D) """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm._g.edges()), 1)
def test_dm_parser_inequality(self): dm = dotmotif.Motif(enforce_inequality=True) dm.from_motif(_DEMO_G_MIN) self.assertTrue("A<>B" in Neo4jExecutor.motif_to_cypher(dm).strip() or "B<>A" in Neo4jExecutor.motif_to_cypher(dm).strip()) self.assertTrue("B<>C" in Neo4jExecutor.motif_to_cypher(dm).strip() or "C<>B" in Neo4jExecutor.motif_to_cypher(dm).strip()) self.assertTrue("A<>C" in Neo4jExecutor.motif_to_cypher(dm).strip() or "C<>A" in Neo4jExecutor.motif_to_cypher(dm).strip())
def test_basic_node_attr(self): exp = """\ A -> C B -> C A === B """ dm = dotmotif.Motif(exp) cypher = Neo4jExecutor.motif_to_cypher(dm) self.assertIn("id(A) < id(B)", cypher)
def test_basic_node_attr(self): exp = """\ Aa -> Ba Aa.type = "excitatory" """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm.list_node_constraints()), 1) self.assertEqual(list(dm.list_node_constraints().keys()), ["Aa"])
def test_multi_node_attr(self): exp = """\ Aa -> Ba Aa.type = "excitatory" Ba.size=4.0 """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm.list_node_constraints()), 2) self.assertEqual(list(dm.list_node_constraints().keys()), ["Aa", "Ba"])
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(GrandIsoExecutor(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_cypher_edge_many_attributes(self): dm = dotmotif.Motif() dm.from_motif(""" A -> B [weight=4, area<=10, area<=20] X -> Y [weight=2] """) self.assertEqual( Neo4jExecutor.motif_to_cypher(dm).strip(), _DEMO_EDGE_ATTR_CYPHER_2.strip())
def test_basic_edge_attr(self): exp = """\ Aa -> Ba [type == 1] """ dm = dotmotif.Motif(exp) self.assertEqual(len(dm._g.edges()), 1) u, v, d = list(dm._g.edges(["Aa", "Bb"], data=True))[0] self.assertEqual(type(list(dm._g.nodes())[0]), str) self.assertEqual(type(list(dm._g.nodes())[1]), str) self.assertEqual(d["constraints"]["type"], {"==": [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_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(GrandIsoExecutor(graph=H).find(motif)), 0)
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.Motif(ignore_direction=True).from_nx(g) E = NetworkXExecutor(graph=G) self.assertEqual(len(E.find(dm)), 4)
def test_wrong_args_macro(self): exp = """\ edge(A, B) { A -> B B -> A } edge(C, D, E) """ # with self.assertRaises(ValueError): with self.assertRaises(Exception): dotmotif.Motif(exp)
def test_simple_macro_construction(self): exp = """\ edge(A, B) { A -> B } edge(C, D) """ dm = dotmotif.Motif(exp) exp_edge = list(dm._g.edges(data=True))[0] self.assertEqual(exp_edge[0], "C") self.assertEqual(exp_edge[1], "D")
def test_dynamic_constraints_in_cypher(self): dm = dotmotif.Motif(enforce_inequality=True) dm.from_motif(""" A -> B A.radius >= B.radius A.zorf != B.zorf """) self.assertIn( "WHERE A.radius >= B.radius AND A.zorf <> B.zorf", Neo4jExecutor.motif_to_cypher(dm).strip(), )
def test_undefined_macro(self): exp = """\ dualedge(A, B) { A -> B B -> A } foo(C, D) """ # with self.assertRaises(ValueError): with self.assertRaises(Exception): dotmotif.Motif(exp)
def test_more_complex_macro(self): exp = """\ tri(A, B, C) { A -> B B -> C C -> A } tri(C, D, E) """ dm = dotmotif.Motif(exp) edges = list(dm._g.edges(data=True)) self.assertEqual(len(edges), 3)
def test_cypher_node_attributes(self): dm = dotmotif.Motif() dm.from_motif(""" A -> B A.size = "big" """) self.assertEqual( Neo4jExecutor.motif_to_cypher(dm).strip(), """MATCH (A:Neuron)-[A_B:SYN]->(B:Neuron)\nWHERE A.size = "big"\nRETURN DISTINCT A,B""" .strip(), )