• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wiheto / netplotbrain / b94012c8-28c1-4e28-bf7f-af294f9dc361

18 Aug 2025 06:45AM UTC coverage: 78.819%. Remained the same
b94012c8-28c1-4e28-bf7f-af294f9dc361

push

circleci

web-flow
Merge pull request #76 from seblome/seblome-patch-1

Update plot_cm.py

1001 of 1270 relevant lines covered (78.82%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.15
/netplotbrain/plotting/plot_cm.py
1
import numpy as np
1✔
2
from matplotlib import transforms
1✔
3
from matplotlib.collections import LineCollection
1✔
4
import pandas as pd
1✔
5

6
def _plot_connectivitymatrix(ax, edges, nodes=None, node_color=None, node_colorby=None, **kwargs):
1✔
7
    """Plot connectivity matrix
8

9
    Args:
10
        ax (matplotlib ax)
11
        edges (pandas datafram): edges.
12
    """
13

14
    # Dataframe to array
15
    if isinstance(nodes, pd.DataFrame):
1✔
16
        number_of_nodes=len(nodes)
1✔
17
    else:
18
        number_of_nodes=edges[['i', 'j']].max().max()+1
×
19

20
    cm_order = kwargs.get('cm_order')
1✔
21
    cm_boundary = kwargs.get('cm_boundary')
1✔
22
    cm_boundarywidth = kwargs.get('cm_boundarywidth')
1✔
23
    cm_boundaryalpha = kwargs.get('cm_boundaryalpha')
1✔
24
    cm_border = kwargs.get('cm_border')
1✔
25
    cm_borderwidth = kwargs.get('cm_borderwidth')
1✔
26
    cm_bordercolor = kwargs.get('cm_bordercolor')
1✔
27

28

29
    vmin = kwargs.get('cm_vmin')
1✔
30
    vmax = kwargs.get('cm_vmax')
1✔
31
    #if cm_boundary == 'Auto' and (cm)
32
    #if cm_boundarycolor == 'auto' and cm_boundary is not None:
33
    #    cm_boundarycolor = 'black'
34
    #else:
35
    #    cm_boundarycolor = None
36
    # Set cm_order to node_colorby if it is set to auto and node_color is set
37
    if cm_order == 'auto' and node_colorby is not None:
1✔
38
        cm_order = node_colorby
1✔
39
        if cm_boundary == 'auto':
1✔
40
            cm_boundary = True
1✔
41
    elif cm_order == 'auto':
×
42
        cm_order = None
×
43
    if cm_order is not None:
1✔
44
        # Check if cm_order consists of unqiue indicies or are communities/groups
45
        if len(np.unique(cm_order))==len(cm_order):
1✔
46
            nodeorder = cm_order
×
47
        else:
48
            communities = nodes[cm_order].astype('category').cat.codes.values
1✔
49
            nodeorder = np.argsort(communities)
1✔
50
    else:
51
        nodeorder = np.arange(number_of_nodes)
×
52
    # If it is still set to auto, set ot None
53
    if cm_boundary == 'auto':
1✔
54
        cm_boundary = None
×
55

56
    adj = np.zeros((number_of_nodes, number_of_nodes))
1✔
57
    if 'weight' in edges:
1✔
58
        adj[edges['i'], edges['j']] = edges['weight']
1✔
59
        # TODO Assume the matrix is symmetric, fix later in kwargs
60
        adj[edges['j'], edges['i']] = edges['weight']
1✔
61
    else:
62
        adj[edges['i'], edges['j']] = 1
×
63
        # TODO Assume the matrix is symmetric, fix later in kwargs
64
        adj[edges['j'], edges['i']] = 1
×
65

66
    # rotate the plot
67
    rotate = kwargs.get('cm_rotate')
1✔
68
    if rotate:
1✔
69
        tr = transforms.Affine2D().rotate_deg(45) + ax.transData
1✔
70
    else:
71
        tr = ax.transData
1✔
72
    ax.pcolormesh(adj[nodeorder][:, nodeorder], cmap='RdBu_r', vmin=vmin, vmax=vmax, transform=tr, rasterized=True, zorder=1)
1✔
73

74
    # Plot
75
    # Add squares for different communities to the connectivity plot
76
    if cm_border:
1✔
77
        if cm_bordercolor == 'networks':
1✔
78
            line_blank = np.zeros(number_of_nodes+1)
×
79
            line_fill = np.zeros(number_of_nodes+1)
×
80
            for i in range(number_of_nodes+1):
×
81
                line_fill[i] = i-0.5
×
82
            points_x = np.array([line_fill, line_blank]).T.reshape((-1, 1, 2))
×
83
            points_y = np.array([line_blank, line_fill]).T.reshape((-1, 1, 2))
×
84
            segments_x  = np.concatenate([points_x[:-1], points_x[1:]], axis=1)
×
85
            segments_y  = np.concatenate([points_y[:-1], points_y[1:]], axis=1)
×
86
            lc_x = LineCollection   (segments_x,
×
87
                                    colors      = node_color[nodeorder],
88
                                    linewidth   = cm_borderwidth*2,
89
                                    transform   = tr,
90
                                    clip_on     = False,
91
                                    zorder      = -1)
92
            lc_y = LineCollection   (segments_y,
×
93
                                    colors      = node_color[nodeorder],
94
                                    linewidth   = cm_borderwidth*2,
95
                                    transform   = tr,
96
                                    clip_on     = False,
97
                                    zorder      = -1)
98
            ax.add_collection(lc_x)
×
99
            ax.add_collection(lc_y)
×
100

101
        else:
102
            ax.plot([0, 0], [0, number_of_nodes], color=cm_bordercolor, linewidth=cm_borderwidth, transform=tr,zorder=5)
1✔
103
            ax.plot([number_of_nodes, 0], [number_of_nodes, number_of_nodes], color=cm_bordercolor, linewidth=cm_borderwidth, transform=tr,zorder=5)
1✔
104
            ax.plot([0, number_of_nodes], [0, 0], color=cm_bordercolor, linewidth=cm_borderwidth, transform=tr,zorder=5)
1✔
105
            ax.plot([number_of_nodes, number_of_nodes], [0, number_of_nodes], color=cm_bordercolor, linewidth=cm_borderwidth, transform=tr,zorder=5)
1✔
106

107
    if cm_boundary:
1✔
108
        for coms in np.unique(communities):
1✔
109
            no = np.where(communities[nodeorder] == coms)[0]
1✔
110
            ax.plot([no[0], no[0]], [no[0], no[-1]+1], color=node_color[nodeorder[no[0]]][:3], linewidth=cm_boundarywidth, transform=tr,zorder=10, alpha = cm_boundaryalpha)
1✔
111
            ax.plot([no[-1]+1, no[0]], [no[-1]+1, no[-1]+1], color=node_color[nodeorder[no[0]]][:3], linewidth=cm_boundarywidth, transform=tr,zorder=10, alpha = cm_boundaryalpha)
1✔
112
            ax.plot([no[0], no[-1]+1], [no[0], no[0]], color=node_color[nodeorder[no[0]]][:3], linewidth=cm_boundarywidth, transform=tr,zorder=10, alpha = cm_boundaryalpha)
1✔
113
            ax.plot([no[-1]+1, no[-1]+1], [no[0], no[-1]+1], color=node_color[nodeorder[no[0]]][:3], linewidth=cm_boundarywidth, transform=tr,zorder=10, alpha = cm_boundaryalpha)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc