Introducing Pymotif đ
Interactive graph exploration within the comforts of your Jupyter Notebook!
You mightâve heard of Motif, a no-code graph exploration tool that we recently launched. If you havenât⌠now you have đ
Check out our introductory post and play around with the demo to get an idea of the features available!
As described in the post, we also released Pymotif, a Python package that embeds Motif into a Jupyter widget. This offers the following advantages:
- Seamless integration into existing Jupyter workflows
- Multiple data import options
- Interactive graph manipulation
- Easy code sharing and reuse
This post describes these features in greater detail. If youâre already working within the Jupyter ecosystem, this may come in useful for you!
Installation
Before starting, itâs a good idea to set up a Python virtual environment (e.g. conda or venv).
Installing Pymotif is easy:
pip install pymotif
Or if you already have a Jupyter Notebook open, run this in a cell:
!pip install pymotif
To verify that the installation was successful, run this in a new cell:
from pymotif import Motif
motif = Motif()motif.plot()
If the Motif UI appears within your notebook, it worked!
.plot()
is actually optional, though itâs always nice to be clear.
Features
Now, we can see the different features in action.
1. Seamless integration into existing Jupyter workflows
You can already imagine how this works â if youâve been cleaning and munging your CSV graph data all this while, you can plot it immediately in the next cell.
Letâs try a simple example. Assume youâve been working on a CSV file called org_chart.csv
that represents your company's org chart in an edge-list format:
Run this in a cell:
org_chart = Motif(csv_path='org_chart.csv')
org_chart.state
As the name suggests, .state
tells you the current state of the graph, and may come in handy for debugging purposes.
The code above should return something like this:
Now, if you plot it with org_chart.plot()
, the output should look like this:
Play around with the graph and see if you are able to make it look more like a typical org chart:
Once youâre done, for bonus points, try clicking on the SAVE DATA AND STYLE
button, then run org_chart.state
again. Note how the value of the style
key in the dictionary is no longer empty!
As weâve seen, without needing to switch tabs or windows, you get a powerful graph exploration tool right at your fingertips! No disruptions to your Jupyter flow đ
2. Multiple data import options
Pymotif also accepts data in a variety of formats. Right now, these are the available data import options:
- JSON files (graphs exported from Motif are saved in JSON format)
- CSV files
- Neo4j database connections
- NetworkX graphs
Code examples of how these can be used are described in our introductory notebook on Github. Conveniently, if you already have a Neo4j graph database, it is easy to plot the data returned by your Cypher queries here!
Pymotifâs integration with NetworkX might be of particular interest if youâve been using it for your graph operations. Essentially, this means that as long as you can get your data into a NetworkX graph (and there are many ways to do so), you can plot it with Pymotif!
For example, check out this snippet from our introductory notebook on nbviewer, which plots a karate.gml
dataset that was downloaded and adapted from Mark Newmanâs website.
All example notebooks can be found here.
Explore these integrations yourself! If you find your data format doesnât work with Pymotif out of the box, see if a small detour to NetworkX fixes thatđ
3. Interactive graph manipulation
Pymotif makes it far easier to work with your graphs. If youâve followed along in your notebook or on nbviewer, youâll find that the output of motif_nx.plot()
above is fully-interactive. Compare that against the static image returned from something like networkx.draw()
:
Editing and labelling the graph is non-trivial as a lot of time needs to be spent exploring the docs for the right functions to call and parameters to pass.
In Pymotif, as shown in the org chart example above, you simply edit the graph via the UI (most actions are also possible via code), then click on the SAVE DATA AND STYLE
button at the bottom. It saves your changes and uses them again the next time you plot()
that graph in a different cell! Easy, isn't it?
Incidentally, this preservation of graph state implies a subtle yet crucial advantage: portability. Weâll discuss it in the last section!
4. Easy code sharing and reuse
As with most analyses, portability and replicability are important, since it allows others to investigate, understand, and verify your findings.
Since Pymotif directly integrates your graph exploration into your Notebook, it is much easier to describe your thought process and share this with fellow data analysts and scientists. Apart from sharing .ipynb
files, you can also embed a fully-interactive version of it within nbviewer and share the link, just like the introductory notebook linked above. With just a URL, your teammates can then explore your analysis and contribute their own discoveries!
If your audience doesnât need your code (business teams, executives, or a teammate that just wants to pick up where you left off), you can simply export your final graph as a JSON file and send that to them. Then, all they have to do is import that file into the web app and explore the graph themselves!
Thatâs all for now! Hopefully, you now have ideas on how to incorporate Pymotif into your own projects. Go forth, experiment, and let us know how it turns out! Feedback, suggestions, bug reports, etc. are always welcome đ
If you liked it, give us a Medium clap đ and a Github star â!
Happy graphing!
Links and resources:
- Motif Github repo (discussions, contributions, issues etc.)
- Pymotif source code
- Pymotif sample notebooks and datasets
- Motif demo site
- Cylynx website