Skip to content

Getting Started

Jordan Matelsky edited this page Mar 18, 2021 · 1 revision

To start using GrandIso with NetworkX, first install the library:

pip3 install grandiso

(If you are using a different network science library, see Not Using NetworkX.)

Writing your first motif search

Let's count the number of triangles in a large Erdős–Rényi graph. First, we'll generate the host graph:

import networkx as nx

host = nx.fast_gnp_random_graph(50, 0.1)

Next we'll generate the motif graph:

motif = nx.complete_graph(3)

These graphs are both undirected. (For directed search, see below.) GrandIso will automatically determine if a motif search should be directed or undirected based upon the host and motif you pass to it.

Finally, we can perform the motif search:

from grandiso import find_motifs

results = find_motifs(motif, host)

Now, results is a List of Dictionaries, where each item in the list is a mapping from motif to host:

[
    {
        "motif_id": "corresponding_host_id",
        "motif_id": "corresponding_host_id",
        "motif_id": "corresponding_host_id", 
    }
]

If you don't care about the node matches and just want to have the total count of motif instances, you can actually save a little bit of RAM! Just pass count_only=True:

results = find_motifs(motif, host, count_only=True)

Now instead of a list of mappings, results is an integer count.

Clone this wiki locally