I’m interning for my final year dissertation. My main goal is to find associations between medical data and connectomes (map of neural connections in the brain). After lot of processing on the MRI Images, I end up having a square matrix with the estimated connectivity between the different brain areas.
A square matrix full of numbers is not the easiest thing to interpret for a human, so I decided that I needed a tool to be able to iterate over the brains in the different studies and quickly “understand” them. The classic approach that they suggested me was to do a correlation matrix with a heatmap. I find them difficult to understand so I wanted to copy some of the chord diagrams I saw before in some articles and posters. Most of the chord graphs visualizations I found on google were done with d3.js; which is not easy to integrate on a Python workflow.
Matplotlib, Seaborn or Bokeh are the libraries I’m used to work with, so I took it as a personal challenge and I started the adventure of creating a chord graph with one of those libraries. I like interactivity a lot, so I finally end up using Bokeh.
Creating a circle of dots
The first idea I came up with was to assign each area a dot in a circle an then draw the connections between the areas. I made a function to create dinamically the x and y of each point in a circle given a n number of areas; as I might use different approaches sometimes.
Creating the lines
Now that I had each area as a point in the graph, I needed to add the lines from one area to the other using the x,y array I just got. In order to remove the connections that were very weak, I added a hardness parameter to the function that will ignore those area connections connected by less than 20 estimated connections.
The lines array was generated and it needed to be transposed to be easier to work with it.
Let’s plot this !
Time to make Bokeh work. As I was going to plot lot of lines, curved lines (bezier lines) seemed a better option than straight lines. I gave a fixed curvature to all of them so I didn’t have to worry about that.
So we had a bunch of lines in the form of a chord diagram, but nothing easy to understand. I thought that giving each line the color of the area it was originated from could be a great idea. Other thing that the graph was not considering at the moment was the number of connections between the areas; zero to thousands depending in the case. Standardization of the number of connections and plotting them as the difference in the width of a connection seemed like a good approach to quantify the strength of the connection.
Counting the number of connections
To modify the width of the lines, we just need add one parameter to our bezier object.
As the standardized values were too low for bokeh, I broadcasted a multiplication to be able to have some visible lines.
In order to create the glyphs information only once and reuse it in different plots, ColumnDataSource let’s you define the data in a variable and then use it when you create a glyph in a plot.
Now the plot, the glyphs and the data source were three different things defined independently.
When adding the curve glyphs to the plot
line_width set the width of each line.
Coloring the brain
Coloring the brain wasn’t something new for me, but in this case assigning so many areas a unique color required some research in color theory.
Creating random color values without too dark or too bright colors is not an easy task. There are different ways of defining a color but the most usual one is using RGB, that stands for red, green, blue. If you use RGB to create random color, the usual output is to have some awful colors that variate a lot in their brightness. I found out that using the HSV method (hue, saturation, value), you can set two of the parameters fixed and generate random colors in a similar color space just by modifying the hue.
I needed now to iterate over the matrix to assign each line the correct color depending in the area it was originated from.
Now let’s plot the same chord diagram with colors
Adding interactivity to the plot
Having such a cool plot needed a way to interact with it. Bokeh offers some tools like HoverTool for that. The hover tool displays informational tooltips whenever the cursor is directly over a glyph, in this case I used the circles to show the area it was representing.
The dot’s data was modified again, as we needed to include the data to be shown when the mouse is hover the point.
We plot again.
We include the new tool and Voilá !
NOTE: The order of the labels in this specific case is not the correct one. I’ll correct it soon.
And this is so far what I did but this still work on progress and I’ll show more soon. Thanks for passing by !