def test_isomorphisms_on_directed_random_graph(self, host, motif):
     assert find_motifs(motif,
                        host,
                        directed=True,
                        isomorphisms_only=True,
                        count_only=True) == len([
                            i for i in DiGraphMatcher(
                                host, motif).subgraph_isomorphisms_iter()
                        ])
Exemple #2
0
    def test_subgraph_isomorphism_directed(self, backend):
        G = Graph(backend=backend(directed=True))
        nxG = nx.DiGraph()

        G.nx.add_edge("A", "B")
        nxG.add_edge("A", "B")
        G.nx.add_edge("B", "C")
        nxG.add_edge("B", "C")
        G.nx.add_edge("C", "A")
        nxG.add_edge("C", "A")

        from networkx.algorithms.isomorphism import DiGraphMatcher

        assert len([
            i
            for i in DiGraphMatcher(G.nx, G.nx).subgraph_monomorphisms_iter()
        ]) == len([
            i for i in DiGraphMatcher(nxG, nxG).subgraph_monomorphisms_iter()
        ])
    def mapping(self, other):
        if not isinstance(other, GraphPattern):
            return None

        my_wire = self.wire
        others_wire = other.wire

        def __node_match(n1, n2):
            return n1 == n2

        def __edge_match(e1, e2):
            return e1 == e2

        matcher = DiGraphMatcher(my_wire, others_wire, node_match=__node_match, edge_match=__edge_match)
        mapping = list(matcher.isomorphisms_iter())
        if len(mapping) == 1:
            return mapping.pop()
        else:
            return None
Exemple #4
0
def check_isomorphism(Arg_list):

    # Find unique argument structures
    # checking graphs for isomorphism
    # TODO: check for isomorphism taking into account edges (rel, sem_rel)

    unclassified_graphs = Arg_list
    graph_families = []

    for unclassified_graph in unclassified_graphs:
        for graph_family in graph_families:
            family_member = graph_family[0]
            dm = DiGraphMatcher(unclassified_graph, family_member)
            if dm.is_isomorphic():
                graph_family.append(unclassified_graph)
                break
        else:
            graph_families.append([unclassified_graph])

    return graph_families
Exemple #5
0
def group_into_isomorphic_families(argument_graphs):
    '''
    Find unique argument structures and group them
    into families
    TODO: check for isomorphism taking into account edges (rel, sem_rel)
    '''

    unclassified_graphs = argument_graphs
    graph_families = []
    for unclassified_graph in unclassified_graphs:
        for graph_family in graph_families:
            family_member = graph_family[0]
            dm = DiGraphMatcher(unclassified_graph, family_member)
            if dm.is_isomorphic():
                graph_family.append(unclassified_graph)
                break
        else:
            graph_families.append([unclassified_graph])

    return graph_families
    def test_two_hop_count_matches_nx(self):

        host = nx.fast_gnp_random_graph(10, 0.5, directed=True)

        motif = nx.DiGraph()
        motif.add_edge("A", "B")
        motif.add_edge("B", "C")

        assert len(find_motifs(motif, host)) == len([
            i
            for i in DiGraphMatcher(host, motif).subgraph_monomorphisms_iter()
        ])
Exemple #7
0
    def mapping(self, other):
        # type: (AGP) -> dict
        """
        :return: If there is any, the mapping with another graph pattern
        """
        if not isinstance(other, AGP):
            return {}

        my_wire = self.wire
        others_wire = other.wire

        def __node_match(n1, n2):
            return n1 == n2

        def __edge_match(e1, e2):
            return e1 == e2

        matcher = DiGraphMatcher(my_wire, others_wire, node_match=__node_match, edge_match=__edge_match)
        mapping = list(matcher.isomorphisms_iter())
        if len(mapping) > 0:
            return mapping.pop()
        else:
            return dict()
Exemple #8
0
    def match(self, pattern):
        """

        :param subgraph:
        :return:
        """
        def node_match(node, pattern_node):
            """

            :param node:
            :param pattern_node:
            :return:
            """
            node = node["Node"]
            pattern_node = pattern_node["Node"]

            result = True
            fields = [
                'FORM', 'LEMMA', 'UPOS', 'XPOS', 'HEAD', 'DEPREL', 'DEPS',
                'MISC'
            ]
            for field in fields:
                if getattr(pattern_node, field) is not None:
                    if getattr(node, field) is None:
                        result = False
                    else:
                        pattern_field_value = getattr(pattern_node, field)
                        instance_field_value = getattr(node, field)
                        if not any(
                                re.match(pattern_field_value, x)
                                for x in instance_field_value.split("|")):
                            result = False

            if pattern_node.FEATS:
                for key, value in pattern_node.FEATS.items():
                    if key not in node.FEATS or value not in node.FEATS[key]:
                        result = False

            # print("Node Matched")
            # print("In node match", node.ID, pattern_node.ID, result)
            return result

        def edge_match(edges, pattern_edges):
            """

            :param edge:
            :param pattern_edge:
            :return:
            """

            edges = edges["Edge"].rels
            pattern_edges = pattern_edges["Edge"].rels

            result = False
            for edge, pattern_edge in itertools.product(edges, pattern_edges):
                match = re.match(pattern_edge, edge)
                if match:
                    result = True

            return result

        match_iter = DiGraphMatcher(
            self.g, pattern.g, node_match=node_match,
            edge_match=edge_match).subgraph_isomorphisms_iter()

        for result in match_iter:
            result = dict((pattern.get_node(v), self.get_node(k))
                          for k, v in result.items())

            yield result