Skip to content

Commit 19a7ecb

Browse files
committed
gexf export colors
1 parent 5488675 commit 19a7ecb

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

csnanalysis/csn.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ def __init__(self, counts, symmetrize=False):
3939

4040
# initialize networkX directed graph
4141
self.graph = nx.DiGraph()
42-
labels = [{'label' : i, 'ID' : i, 'count' : int(totcounts[i][0])} for i in range(self.nnodes)]
42+
labels = [{'label' : i, 'count' : int(totcounts[i][0])} for i in range(self.nnodes)]
4343
self.graph.add_nodes_from(zip(range(self.nnodes),labels))
4444
self.graph.add_weighted_edges_from(zip(self.transmat.col,self.transmat.row,self.transmat.data))
4545

46-
def to_gephi(self, cols='all', node_name='node.csv', edge_name='edge.csv', directed=False):
46+
# remove self edges from graph
47+
self_edges = [(i,i) for i in range(self.nnodes)]
48+
self.graph.remove_edges_from(self_edges)
49+
50+
def to_gephi_csv(self, cols='all', node_name='node.csv', edge_name='edge.csv', directed=False):
4751
"""
4852
Writes node and edge files for import into the Gephi network visualization program.
4953
@@ -94,7 +98,39 @@ def add_attr(self, name, values):
9498
attr[i] = v
9599

96100
nx.set_node_attributes(self.graph,values=attr,name=name)
101+
102+
def set_colors(self, rgb):
103+
"""
104+
Adds colors to each node for gexf export of the graph.
105+
106+
rgb: A dict that stores the rgb values of each node.
107+
108+
Example: rgb['0']['r'] = 255
109+
rgb['0']['g'] = 0
110+
rgb['0']['b'] = 0
111+
"""
112+
for node in rgb:
113+
self.graph.node[node]['viz'] = {'color': {'r': rgb[node]['r'], 'g': rgb[node]['g'], 'b': rgb[node]['b'], 'a': 0}}
114+
115+
def colors_from_committors(self,comm):
116+
"""
117+
Returns rgb dict using values of committor probabilities.
118+
Very useful for 3-basin committors!
119+
120+
comm: Numpy array of committors, as returns from self.calc_committors
121+
"""
122+
highc = 255
123+
nbasin = comm.shape[1]
124+
rgb = {}
125+
colors = ['r','g','b']
126+
for node in range(self.nnodes):
127+
maxc = comm[node,:].max()
128+
for i in range(min(3,nbasin)):
129+
rgb[node][colors[i]] = highc*comm[node,i]/maxc
130+
131+
return rgb
97132

133+
98134
def trim(self, by_inflow=True, by_outflow=True, min_count=0):
99135
"""
100136
Trims a graph to delete nodes that are not connected to the main
@@ -179,14 +215,15 @@ def calc_eig_weights(self,label='eig_weights'):
179215
if self.trim_transmat is None:
180216
# use full transition matrix
181217
full_wts = eig_weights(self.transmat)
182-
self.add_attr(label, full_wts)
183218
else:
184219
# use trimmed transition matrix
185220
wts = eig_weights(self.trim_transmat)
186221
full_wts = np.zeros(self.nnodes,dtype=float)
187222
for i,ind in enumerate(self.trim_indices):
188223
full_wts[ind] = wts[i]
189-
self.add_attr(label, full_wts)
224+
225+
fw_float = [float(i) for i in full_wts]
226+
self.add_attr(label, fw_float)
190227

191228
return full_wts
192229

@@ -203,15 +240,16 @@ def calc_mult_weights(self,label='mult_weights',tol=1e-6):
203240
if self.trim_transmat is None:
204241
# use full transition matrix
205242
full_wts = mult_weights(self.transmat,tol)
206-
self.add_attr(label, full_wts)
207243
else:
208244
# use trimmed transition matrix
209245
wts = mult_weights(self.trim_transmat,tol)
210246
full_wts = np.zeros(self.nnodes,dtype=float)
211247
for i,ind in enumerate(self.trim_indices):
212248
full_wts[ind] = wts[i]
213-
self.add_attr(label, full_wts)
214249

250+
fw_float = [float(i) for i in full_wts]
251+
self.add_attr(label, fw_float)
252+
215253
return full_wts
216254

217255
def calc_committors(self,basins,labels=None,basin_labels=None,add_basins=False,tol=1e-6,maxstep=20):
@@ -245,15 +283,17 @@ def calc_committors(self,basins,labels=None,basin_labels=None,add_basins=False,t
245283
if labels is None:
246284
labels = ['p' + str(i) for i in range(len(basins))]
247285
for i,b in enumerate(basins):
248-
self.add_attr(labels[i], full_comm[:,i])
286+
fc_float = [float(i) for i in full_comm[:,i]]
287+
self.add_attr(labels[i], fc_float)
249288

250289
if add_basins:
251290
if basin_labels is None:
252291
basin_labels = [str(i) for i in range(len(basins))]
253292
for i,b in enumerate(basins):
254293
bvec = np.zeros(self.nnodes,dtype=int)
255294
bvec[b] = 1
256-
self.add_attr(basin_labels[i],bvec)
295+
bv_int = [int(i) for i in bvec]
296+
self.add_attr(basin_labels[i],bv_int)
257297

258298
return full_comm
259299

csnanalysis/matrix.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def committor(transmat,basins,tol=1e-6,maxstep=20):
137137
"""
138138

139139
# make sink_matrix
140-
sink_mat = make_sink(transmat,list(np.array(basins).flatten()))
140+
flat_sink = [i for b in basins for i in b]
141+
sink_mat = make_sink(transmat,flat_sink)
141142

142143
sink_results = trans_mult_iter(sink_mat,tol,maxstep)
143144

examples/state_U.dat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4,11,29,56,57,67,115,213,251,252,299,301,303,305,327,340,404,504,537,560,573,583,627,631,639,683,704,708,717,765,844,867,923,940,941,1010,1068,1070,1105,1138,1151,1157,1162,1243,1256,1287,1344,1348,1382,1412,1428,1455,1490,1569,1596,1640,1690,1744,1758,1776,1805,1815,1817,1825,1893,1922,1930,1933,1936,1960,1989,1993,1994,2062,2066,2076,2097,2147,2171,2183,2185,2199,2212,2302,2320,2345,2389,2408,2430,2470,2481,2513,2517,2536,2542,2606,2608,2618,2723,2749,2811,2813,2830,2870,2917,2931,2963,2995,3075,3087,3169,3183,3204,3212,3214,3247,3249,3251,3269,3270,3278,3305,3357,3466,3475,3480,3501,3536,3593,3600,3604,3611,3740,3781,3794,3810,3868,3897,3907,3962,3967,3969,3979,3989
1+
4 11 29 56 57 67 115 213 251 252 299 301 303 305 327 340 404 504 537 560 573 583 627 631 639 683 704 708 717 765 844 867 923 940 941 1010 1068 1070 1105 1138 1151 1157 1162 1243 1256 1287 1344 1348 1382 1412 1428 1455 1490 1569 1596 1640 1690 1744 1758 1776 1805 1815 1817 1825 1893 1922 1930 1933 1936 1960 1989 1993 1994 2062 2066 2076 2097 2147 2171 2183 2185 2199 2212 2302 2320 2345 2389 2408 2430 2470 2481 2513 2517 2536 2542 2606 2608 2618 2723 2749 2811 2813 2830 2870 2917 2931 2963 2995 3075 3087 3169 3183 3204 3212 3214 3247 3249 3251 3269 3270 3278 3305 3357 3466 3475 3480 3501 3536 3593 3600 3604 3611 3740 3781 3794 3810 3868 3897 3907 3962 3967 3969 3979 3989

0 commit comments

Comments
 (0)