Circular layout

Example illustrating the circular layout

import os

import numpy as np
import pandas as pd

from netchos import circular

import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots

pio.templates.default = 'plotly_white'

Load the data

# load the connectivity 2D matrix
ufc = pd.read_excel('ufc.xlsx', index_col=0)
print(ufc)

# load a table that contains informations about the nodes
ma = pd.read_excel('ma.xlsx')
print(ma)

# computes nodes' degree
ma['degree'] = (~np.isnan(ufc.values)).sum(0)
# compute node's strength
ma['strength'] = np.nansum(ufc, axis=0)

Out:

/home/runner/work/netchos/netchos/examples/plot_circular.py:27: FutureWarning:

Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.

             aINS      pINS  ACC  PFCvm  ...        Cu  VCs       VCl      VCcm
sources                                  ...
aINS     0.005500  0.001185  NaN    NaN  ...       NaN  NaN       NaN       NaN
pINS     0.001185       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
ACC           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFCvm         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
vmPFC         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
OFCvm         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
OFCv          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
lOFC          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
OFCvl         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFrm          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFrd          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Pfrdls        NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
dlPFC    0.001219       NaN  NaN    NaN  ...       NaN  NaN  0.001760  0.012980
Pfrdli        NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFrvl    0.003716       NaN  NaN    NaN  ...       NaN  NaN  0.003139  0.007489
MCC           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFcdm         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PFcdl         NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PMdm          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PMdl          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PMrv          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Mdm           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Mdl           NaN  0.002862  NaN    NaN  ...  0.008246  NaN  0.002460       NaN
Mv       0.005842       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Sdm           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Sdl           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Sv            NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PCC           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
PCm           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
SPCm          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
SPC           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
IPCd          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
IPCv          NaN       NaN  NaN    NaN  ...       NaN  NaN  0.001141  0.002808
ICC           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
MTCr          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
STCr          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
STCc          NaN       NaN  NaN    NaN  ...       NaN  NaN  0.001088  0.002290
MTCc     0.002699       NaN  NaN    NaN  ...  0.020681  NaN  0.000745  0.001717
ITCr     0.002164       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
ITCm     0.001082       NaN  NaN    NaN  ...       NaN  NaN  0.002184  0.006301
VCrm          NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
Cu            NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
VCs           NaN       NaN  NaN    NaN  ...       NaN  NaN       NaN       NaN
VCl           NaN       NaN  NaN    NaN  ...       NaN  NaN  0.001851  0.005478
VCcm          NaN       NaN  NaN    NaN  ...       NaN  NaN  0.005478  0.014947

[45 rows x 45 columns]
/home/runner/work/netchos/netchos/examples/plot_circular.py:31: FutureWarning:

Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.

    Unnamed: 0    Name LR_Name  ... ycoord_3D zcoord_3D lobe_color
0            0    aINS    aINS  ...      33.0      49.0    #636EFA
1            1    pINS    pINS  ...      33.0      49.0    #636EFA
2            2     ACC     ACC  ...       5.0      51.0    #636EFA
3            3   PFCvm   PFCvm  ...       7.0      37.0    #636EFA
4            4   vmPFC   vmPFC  ...      11.0      34.0    #636EFA
5            5   OFCvm   OFCvm  ...      15.0      31.0    #636EFA
6            6    OFCv    OFCv  ...      27.0      33.0    #636EFA
7            7    lOFC    lOFC  ...      31.0      33.0    #636EFA
8            8   OFCvl   OFCvl  ...      35.0      33.0    #636EFA
9            9    PFrm    PFrm  ...       5.0      68.0    #636EFA
10          10    PFrd    PFrd  ...      11.0      69.0    #636EFA
11          11  Pfrdls  Pfrdls  ...      24.0      62.0    #636EFA
12          12   dlPFC   dlPFC  ...      29.5      58.5    #636EFA
13          13  Pfrdli  Pfrdli  ...      35.0      55.0    #636EFA
14          14   PFrvl   PFrvl  ...      41.0      49.0    #636EFA
15          15     MCC     MCC  ...       4.0      79.0    #636EFA
16          16   PFcdm   PFcdm  ...       9.0      87.0    #636EFA
17          17   PFcdl   PFcdl  ...      32.0      78.0    #636EFA
18          18    PMdm    PMdm  ...      10.0      99.0    #636EFA
19          19    PMdl    PMdl  ...      30.0      89.0    #636EFA
20          20    PMrv    PMrv  ...      42.0      58.0    #636EFA
21          21     Mdm     Mdm  ...       9.0     104.0    #636EFA
22          22     Mdl     Mdl  ...      37.0      90.0    #636EFA
23          23      Mv      Mv  ...      48.0      61.0    #636EFA
24          24     Sdm     Sdm  ...      10.0     106.0    #EF553B
25          25     Sdl     Sdl  ...      37.0      96.0    #EF553B
26          26      Sv      Sv  ...      50.0      70.0    #EF553B
27          27     PCC     PCC  ...       3.0      80.0    #EF553B
28          28     PCm     PCm  ...       5.0      88.0    #EF553B
29          29    SPCm    SPCm  ...      13.0     104.0    #EF553B
30          30     SPC     SPC  ...      27.0      99.0    #EF553B
31          31    IPCd    IPCd  ...      41.0      94.0    #EF553B
32          32    IPCv    IPCv  ...      48.0      76.0    #EF553B
33          33     ICC     ICC  ...      11.0      59.0    #EF553B
34          34    MTCr    MTCr  ...      40.0      22.0    #00CC96
35          35    STCr    STCr  ...      43.0      36.0    #00CC96
36          36    STCc    STCc  ...      50.0      57.0    #00CC96
37          37    MTCc    MTCc  ...      51.0      54.0    #00CC96
38          38    ITCr    ITCr  ...      37.0      25.0    #00CC96
39          39    ITCm    ITCm  ...      26.0      37.0    #00CC96
40          40    VCrm    VCrm  ...      15.0      57.0    #AB63FA
41          41      Cu      Cu  ...      11.0      78.0    #AB63FA
42          42     VCs     VCs  ...      33.0      83.0    #AB63FA
43          43     VCl     VCl  ...      42.0      57.0    #AB63FA
44          44    VCcm    VCcm  ...      20.0      55.0    #AB63FA

[45 rows x 13 columns]

Default layout

title = "Default layout (nodes degree represented by marker size and color)"
fig = circular(
    ufc
)
fig.update_layout(title=title)
pio.show(fig)

Passing data to the nodes

"""
Here, we use the `nodes_data` input to provide a pandas DataFrame containing
nodes informations, namely :
- The name of the nodes ('Name')
- The number of connections per node ('degree')
- The connectivity strength ('strength')
- Additional categorization (each node is a brain region that belong to a lobe)
"""
fig = circular(
    ufc, nodes_data=ma, nodes_name='Name', nodes_size='degree',
    nodes_color='strength', categories='Lobe'
)
pio.show(fig)

Control of aesthetics

kw_circ = dict()

# nodes settings
kw_circ['nodes_size_min'] = 0.2
kw_circ['nodes_size_max'] = 10
kw_circ['nodes_cmap'] = 'plasma'    # colormap for the nodes

# edges settings
kw_circ['edges_width_min'] = .5
kw_circ['edges_width_max'] = 9.
kw_circ['edges_opacity_min'] = 0.2  # opacity for weakest connections
kw_circ['edges_opacity_max'] = 1.   # opacity for strongest connections
kw_circ['edges_cmap'] = 'agsunset'  # colormap for the edges

# layout settings
kw_circ['angle_start'] = 90         # start circle at 90°
kw_circ['angle_range'] = 180        # use only half of the circle
kw_circ['cbar_title'] = 'Significant links (p<0.05)'

# sphinx_gallery_thumbnail_number = 3
fig = circular(
    ufc, nodes_data=ma, nodes_name='Name', nodes_size='degree',
    nodes_color='strength', categories='Lobe', **kw_circ
)
fig.update_layout(width=600, height=700, title='<b>Control of aesthetics</b>',
                  title_x=0.5, template='plotly_dark')
pio.show(fig)

Circular layout in subplots

fig = make_subplots(rows=1, cols=2, subplot_titles=('Subplot 1', 'Subplot 2'))

# configuring the first subplot
circular(
    ufc, nodes_data=ma, nodes_name='Name', nodes_size='degree',
    nodes_color='degree', categories='Lobe', nodes_cmap='plasma_r',
    edges_cmap='plasma_r', angle_range=180, fig=fig,
    kw_trace=dict(row=1, col=1), kw_cbar=dict(x=0.4)
)

# configuring the second subplot
circular(
    ufc, nodes_data=ma, nodes_name='Name', nodes_size='strength',
    nodes_color='strength', categories='Lobe', nodes_cmap='magma_r',
    edges_cmap='magma_r', angle_range=180, fig=fig,
    kw_trace=dict(row=1, col=2), kw_cbar=dict(x=0.95)
)

title = "<b>Illustration of adding circular layouts to subplots</b>"
fig.update_layout(width=1000, height=800, title=title, title_x=0.5)
pio.show(fig)

Total running time of the script: ( 0 minutes 10.179 seconds)

Gallery generated by Sphinx-Gallery