Last active
May 2, 2022 14:51
-
-
Save j6k4m8/d02259dfedc2321973be4d2e665653f4 to your computer and use it in GitHub Desktop.
DotMotif-Search-in-Custom-NetworkX.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "DotMotif-Search-in-Custom-NetworkX.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMpnafhK2FKFbVkGc1l97oA", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/j6k4m8/d02259dfedc2321973be4d2e665653f4/dotmotif-search-in-custom-networkx.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Using DotMotif to search for motifs in a custom graph\n", | |
"\n", | |
"[DotMotif](https://github.com/aplbrain/dotmotif) is a performant, powerful query framework to search for network motifs.\n" | |
], | |
"metadata": { | |
"id": "ZjweaPr1LtEk" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%%capture\n", | |
"# Install dotmotif with one line.\n", | |
"# On your own computer, you can run this in the terminal.\n", | |
"!pip3 install dotmotif networkx\n", | |
"\n", | |
"import networkx as nx\n", | |
"from dotmotif import Motif, GrandIsoExecutor" | |
], | |
"metadata": { | |
"id": "qVIoPW6UK3rG" | |
}, | |
"execution_count": 44, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Create a new graph from scratch:\n", | |
"host = nx.fast_gnp_random_graph(100, 0.1, directed=True)" | |
], | |
"metadata": { | |
"id": "-4U9mEOQK7rG" | |
}, | |
"execution_count": 45, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Create the search engine.\n", | |
"E = GrandIsoExecutor(graph=host)" | |
], | |
"metadata": { | |
"id": "ooxAE9DuLSyB" | |
}, | |
"execution_count": 46, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Build your own motif here!\n", | |
"motif = Motif(\"\"\"\n", | |
"\n", | |
"# Example motif:\n", | |
"\n", | |
"onewayEdge(a, b) {\n", | |
" # An edge that only points in one direction,\n", | |
" # with no reciprocal edge: \n", | |
" a -> b\n", | |
" b !> a\n", | |
"}\n", | |
"\n", | |
"# A triangle that only has edges pointing\n", | |
"# in one direction:\n", | |
"onewayEdge(A, B)\n", | |
"onewayEdge(B, C)\n", | |
"onewayEdge(C, A)\n", | |
"\n", | |
"\"\"\")" | |
], | |
"metadata": { | |
"id": "B8_jzbUvLUHd" | |
}, | |
"execution_count": 47, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"results = E.find(motif)" | |
], | |
"metadata": { | |
"id": "kTRYjBSDLWzY" | |
}, | |
"execution_count": 48, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(len(results))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "b7paIFMBLYiZ", | |
"outputId": "5599e144-c252-410c-ee36-5f8826ebca6b" | |
}, | |
"execution_count": 49, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"639\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "7tqVQfx8O7cJ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment