Introducing Pymotif 🐍

Interactive graph exploration within the comforts of your Jupyter Notebook!

Jason Ho
Cylynx
5 min readJun 23, 2021

--

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:

  1. Seamless integration into existing Jupyter workflows
  2. Multiple data import options
  3. Interactive graph manipulation
  4. 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:

Adapted from the sample org chart dataset here

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:

`state` is a Python dictionary with 2 keys: `data` and `style`

Now, if you plot it with org_chart.plot(), the output should look like this:

It’s that simple!

Play around with the graph and see if you are able to make it look more like a typical org chart:

Hint: Explore the settings in the Styles panel (the palette icon on the left bar).

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 🙂

Best practice for working in Jupyter Notebooks

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.

As stated in Mark Newman’s website, the original source of this data is “W. W. Zachary, An information flow model for conflict and fission in small groups, Journal of Anthropological Research 33, 452–473 (1977).”

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!

Anyone can interact with the graph directly!

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!

Export and send -> Import -> Explore

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:

--

--