-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.py
92 lines (72 loc) · 2.49 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import detrend as dd
from time_series_nx import ts_corr_network
import networkx as nx
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#TODO: build a light weight plotting package for ts_corr_network()
#TODO: ts_corr_network().plot(figsize=(), cmap=True or cmap=False)
# .node_style(inherits draw_networkx_nodes)
# .edge_style(inherits draw_networkx_edges)
# .label_style(inherits draw_networkx_labels)
def test():
df = dd.detrend()
df.dropna(inplace=True)
H = ts_corr_network(data=df, corr_param='dcor')
# function to display the network from the correlation matrix
def show_corr_nx(H):
# creates a list of the edges of G and their corresponding weights
edges, weights = zip(*nx.get_edge_attributes(H, "weight").items())
# draw the network with the Kamada-Kawai path-length cost-function
pos = nx.kamada_kawai_layout(H)
# figure size
plt.figure(figsize=(20, 20))
# computes the degree (number of connections) of each node
deg = H.degree
# list of node names
nodelist = []
# list of node sizes
node_sizes = []
# iterates over deg and appends the node names and degrees
for n, d in deg:
nodelist.append(n)
node_sizes.append(d)
# draw nodes
nx.draw_networkx_nodes(
H,
pos,
node_color="#DA70D6",
nodelist=nodelist,
node_size=np.power(node_sizes, 2.5),
alpha=0.8,
font_weight="bold",
)
# node label styles
nx.draw_networkx_labels(H, pos, font_size=8, font_family="sans-serif")
# color map
cmap = sns.cubehelix_palette(3, as_cmap=True, reverse=True)
# draw edges
nx.draw_networkx_edges(
H,
pos,
edge_list=edges,
style="solid",
edge_color=weights,
edge_cmap=cmap,
edge_vmin=min(weights),
edge_vmax=max(weights),
)
# builds a colorbar
sm = plt.cm.ScalarMappable(
cmap=cmap, norm=plt.Normalize(vmin=min(weights), vmax=max(weights))
)
sm._A = []
plt.colorbar(sm)
# displays network without axes
plt.axis("off")
# plt.savefig('dija_correlation_network.jpg')
plt.show()
return show_corr_nx(H)
if __name__ == "__main__":
test()