-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_network.py
More file actions
231 lines (190 loc) · 7.4 KB
/
plot_network.py
File metadata and controls
231 lines (190 loc) · 7.4 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
import matplotlib.pylab as pylab
import numpy as np
import sys
class Plot:
"""
Plot procrystalline lattice.
"""
def __init__(self,nodes=False,cnxs=False,rings=False,periodic=False):
"""
Initialise with plot options.
"""
# Get options
self.nodes = nodes
self.cnxs = cnxs
self.rings = rings
self.periodic = periodic
# Set up empty figure
params = {"figure.figsize": (6, 6)}
pylab.rcParams.update(params)
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.ax.set_axis_off()
def __call__(self,network,**kwargs):
"""
Plot procrystalline lattice.
"""
# Images to generate based on periodicity
if self.periodic:
images = [-1,0,1]
else:
images = [0]
# Unpack options
self.lw = kwargs.get("lw",1.0)
self.lc = kwargs.get("lc","k")
self.ms = kwargs.get("ms",10)
self.mc = kwargs.get("mc","k")
save = kwargs.get("save",False)
# Load data
self.load_network(network)
# Add images to plot
for y in images:
for x in images:
self.plot_rings(x,y)
self.plot_nodes(x,y)
self.plot_cnxs(x,y)
# Display
if save:
plt.savefig("config.png",dpi=400)
plt.show()
def load_network(self,network):
"""
Load coordinates and network information.
"""
# Node coordination, periodicity and node crds
self.pbc = network.pbc
self.mic = network.mic
self.node_crds = network.get_node_crds()
self.mean_ring_size = 6 # self.node_cnxs = []
# Make connections accounting for periodicity
self.node_cnxs = network.get_edges(unique=False)
self.node_cnx_crds = np.zeros((self.node_cnxs.shape[0],4))
crd_i = np.zeros(2)
crd_j = np.zeros(2)
for i,p in enumerate(self.node_cnxs):
crd_i[:] = self.node_crds[p[0]][:]
crd_j[:] = self.node_crds[p[1]][:]
x = crd_j[0]-crd_i[0]
y = crd_j[1]-crd_i[1]
if x>self.mic[0]: x-=self.pbc[0]
elif x<-self.mic[0]: x+=self.pbc[0]
if y>self.mic[1]: y-=self.pbc[1]
elif y<-self.mic[1]: y+=self.pbc[1]
self.node_cnx_crds[i,0] = crd_i[0]
self.node_cnx_crds[i,1] = crd_i[0]+x/2
self.node_cnx_crds[i,2] = crd_i[1]
self.node_cnx_crds[i,3] = crd_i[1]+y/2
# Make rings accounting for periodicity
self.node_rings = network.get_rings()
self.ring_crds = []
self.max_ring_size = 0
for ring in self.node_rings:
crds = np.zeros((ring.size,2))
for i,j in enumerate(ring):
crds[i,:] = self.node_crds[j,:]
for i in range(1,ring.size):
x = crds[i,0] - crds[i-1,0]
y = crds[i,1] - crds[i-1,1]
if x>self.mic[0]: x -= self.pbc[0]
elif x<-self.mic[0]: x += self.pbc[0]
if y>self.mic[1]: y -= self.pbc[1]
elif y<-self.mic[1]: y += self.pbc[1]
crds[i,0] = crds[i-1,0] + x
crds[i,1] = crds[i-1,1] + y
self.ring_crds.append(crds)
if ring.size > self.max_ring_size:
self.max_ring_size = ring.size
self.perimeter_ring = network.get_rings(True)
self.perimeter_ring_crds = []
for i in self.perimeter_ring:
self.perimeter_ring_crds.append(self.node_crds[i,:])
self.perimeter_ring_crds = np.array(self.perimeter_ring_crds)
self.init_ring_colours(self.mean_ring_size,self.max_ring_size)
def plot_nodes(self,x_shift,y_shift):
"""
Plot image nodes as scatter.
"""
if not self.nodes: return # Bounce if option not selected
self.ax.scatter(self.node_crds[:,0]+x_shift*self.pbc[0],self.node_crds[:,1]+y_shift*self.pbc[1],
marker="o",s=self.ms,c=self.mc,zorder=1)
# for i,c in enumerate(self.node_crds):
# self.ax.text(c[0],c[1],i,size=8)
def plot_cnxs(self,x_shift,y_shift):
"""
Plot image connections as lines.
"""
if not self.cnxs: return # Bounce if option not selected
self.node_cnx_crds[:,:2] += x_shift*self.pbc[0]
self.node_cnx_crds[:,2:] += y_shift*self.pbc[1]
for cnx_crd in self.node_cnx_crds:
self.ax.plot(cnx_crd[:2],cnx_crd[2:],c=self.lc,lw=self.lw)
self.node_cnx_crds[:,:2] -= x_shift*self.pbc[0]
self.node_cnx_crds[:,2:] -= y_shift*self.pbc[1]
def plot_rings(self,x_shift,y_shift):
"""
Plot rings as polygons.
"""
if not self.rings: return # Bounce if option not selected
patches = []
colours = []
for ring in self.ring_crds:
ring[:,0] += x_shift*self.pbc[0]
ring[:,1] += y_shift*self.pbc[1]
patches.append(Polygon(np.array(ring), True))
colours.append(self.ring_colours[ring[:,0].size])
ring[:,0]-=x_shift*self.pbc[0]
ring[:,1]-=y_shift*self.pbc[1]
self.ax.add_collection(PatchCollection(patches,facecolor=colours,linewidths=self.lw,edgecolor="k",zorder=0))
patches = []
for ring in self.perimeter_ring_crds:
ring[:,0] += x_shift*self.pbc[0]
ring[:,1] += y_shift*self.pbc[1]
patches.append(Polygon(np.array(ring), True))
ring[:,0]-=x_shift*self.pbc[0]
ring[:,1]-=y_shift*self.pbc[1]
self.ax.add_collection(PatchCollection(patches,facecolor=(0,0,0,0),linewidths=self.lw*3,edgecolor="orange",zorder=0))
def init_ring_colours(self,av_ring_size=6,max_ring_size=10):
"""
Initialise colouring for rings.
"""
map_lower = cm.get_cmap('Blues_r', 128)
map_upper = cm.get_cmap('Reds', 128)
map_mean=cm.get_cmap("Greys")
map_lower=ListedColormap(map_lower(np.arange(20,100)))
map_upper=ListedColormap(map_upper(np.arange(20,100)))
norm_lower=Normalize(vmin=av_ring_size-3,vmax=av_ring_size)
norm_upper=Normalize(vmin=av_ring_size,vmax=av_ring_size+6)
colour_mean=map_mean(50)
self.ring_colours=[]
for i in range(max_ring_size+1):
if i < 3:
self.ring_colours.append("white")
elif np.abs(i-av_ring_size)<1e-6:
self.ring_colours.append(colour_mean)
elif i<av_ring_size:
self.ring_colours.append(map_lower(norm_lower(i)))
else:
self.ring_colours.append(map_upper(norm_upper(i)))
if __name__ == "__main__":
prefix = sys.argv[1]
sample = int(sys.argv[2])
if len(sys.argv) <= 3:
nodes = True
cnxs = False
rings = True
periodic = False
save = False
else:
flags = sys.argv[3]
nodes = 'n' in flags
cnxs = 'c' in flags
rings = 'r' in flags
periodic = 'p' in flags
save = 's' in flags
plot=Plot(nodes=nodes,cnxs=cnxs,rings=rings,periodic=periodic)
plot(prefix,sample,save=save)