Skip to content

Commit

Permalink
updated viewer to be more sensible
Browse files Browse the repository at this point in the history
  • Loading branch information
leobrowning92 committed May 9, 2018
1 parent 01f8bc7 commit b83b49b
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions viewnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,21 @@
from percolation import StickCollection

class Netviewer(StickCollection):
def __init__(self,fname,directory='data',scaling=5):
StickCollection.__init__(self,fname=fname,directory=directory,scaling=scaling)
# from network
def show_network(self,v=False):

fig = plt.figure(figsize=(10,10),facecolor='white')
ax1=plt.subplot(111)
self.plot_network(ax1,v=v)
plt.show()
def show_device(self,v=False,ax=False):
if not(ax):
fig = plt.figure(figsize=(10,10),facecolor='white')
ax=plt.subplot(111)
self.plot_network(ax,v=v)
self.plot_regions(ax)

# ax.legend()
if not(ax):
plt.show()
def plot_regions(self,ax):
for a in self.cnet.gate_areas:
ax.add_patch(patches.Rectangle( (a[0][0]-a[0][2]/2,a[0][1]-a[0][3]/2), a[0][2],a[0][3], edgecolor='b', fill=False, label="Local $V_G$ = {} V".format(a[1])))
ax.add_patch(patches.Rectangle( (-0.02,.48), 0.04,0.04, edgecolor='r', fill=False,label="Source = {} V".format(self.cnet.vds)))
ax.add_patch(patches.Rectangle( (.98,0.48), 0.04,0.04, edgecolor='k',
fill=False, label="GND = 0 V"))
def plot_network(self,ax1,v=False):
pos={k:self.cnet.graph.nodes[k]['pos'] for k in self.cnet.graph.nodes}
# for i in range(self.network_rows):
# for j in range(self.network_columns):
# pos[(i,j)]=j,i
edges,currents = zip(*nx.get_edge_attributes(self.cnet.graph,'current').items())
def __init__(self,**kwargs):
super(Netviewer, self).__init__(**kwargs)
self.label_clusters()

nodes,voltages = zip(*nx.get_node_attributes(self.cnet.graph,'voltage').items())
nodes=nx.draw_networkx_nodes(self.cnet.graph, pos, width=2,nodelist=nodes, node_color=voltages, cmap=plt.get_cmap('YlOrRd'), node_size=30, ax=ax1)
edges=nx.draw_networkx_edges(self.cnet.graph, pos, width=2, edgelist=edges, edge_color=currents, edge_cmap=plt.get_cmap('YlGn'), ax=ax1)\

if v:
nodelabels=nx.get_node_attributes(self.graph,'voltage')
nx.draw_networkx_labels(self.graph,pos,labels={k:'{}\n {:.1e}V'.format(k,nodelabels[k]) for k in nodelabels})
edgecurrents=nx.get_edge_attributes(self.graph,'current')
edgeresistance=nx.get_edge_attributes(self.graph,'resistance')
nx.draw_networkx_edge_labels(self.graph,pos, edge_labels={k:'{:.1e}A\n{:.1e}$\Omega$'.format(edgecurrents[k], edgeresistance[k]) for k in edgecurrents})

# from percolation
def show_system(self,clustering=True, junctions=True, conduction=True, show=True, save=False):
fig = plt.figure(figsize=(15,5))
axes=[fig.add_subplot(1,3,i+1) for i in range(3)]
self.label_clusters()
if clustering:
self.show_clusters(ax=axes[0])
self.show_sticks(ax=axes[0],junctions=False, clusters=True)
axes[0].set_title("Cluster labeling")
if junctions:
self.show_sticks(sticks=self.sticks,intersects=self.intersects, ax=axes[1])
self.show_sticks(ax=axes[1],junctions=True, clusters=False)
axes[1].set_title("ms labeling and junctions")
if conduction and self.percolating:
self.show_device(ax=axes[2])
Expand All @@ -77,38 +39,78 @@ def show_system(self,clustering=True, junctions=True, conduction=True, show=True
plt.savefig(self.fname+'_'+save+'_plots.png')
if show:
plt.show()
def show_clusters(self,intersects=True,ax=False):

def show_sticks(self,ax=False, clusters=False, junctions=True):
sticks=self.sticks
intersects=self.intersects
if not(ax):
fig=plt.figure(figsize=(5,5))
fig = plt.figure(figsize=(5,5),facecolor='white')
ax=fig.add_subplot(111)
colors=np.append([[0,0,0]], np.random.rand(len(sticks),3), axis=0)
colorpattern=[colors[i] for i in sticks.cluster.values]
collection=LineCollection(sticks.endarray.values,linewidth=0.5,colors=colorpattern)
if clusters:
colors=np.append([[0,0,0]], np.random.rand(len(sticks),3), axis=0)
stick_colors=[colors[i] for i in sticks.cluster.values]
else:
stick_cmap={'s':'b','m':'r','v':'k'}
stick_colors=[stick_cmap[i] for i in sticks.kind]
collection=LineCollection(sticks.endarray.values,linewidth=1,colors=stick_colors)
ax.add_collection(collection)
# if intersects:
# ax.scatter(self.intersects.x, self.intersects.y, c="r", s=30, linewidth=0.8, marker="x")
if junctions:
isect_cmap={'ms':'g','sm':'g', 'mm':'k','ss':'k','vs':'k','sv':'k','vm':'k','mv':'k'}
isect_colors=[isect_cmap[i] for i in self.intersects.kind]
ax.scatter(intersects.x, intersects.y, c=isect_colors, s=20, linewidth=1, marker="x")
ax.set_xlim((-0.02,1.02))
ax.set_ylim((-0.02,1.02))
# ax.set_title("$n_{{clusters}}$={}\nConnected={}".format(len(self.sticks.cluster.drop_duplicates()),str(self.percolating)))
if not(ax):
plt.show()
def show_sticks(self,sticks,intersects,ax=False):

# from network
def show_cnet(self,ax=False,v=False, current = True, voltage=True):
if not(ax):
fig = plt.figure(figsize=(5,5),facecolor='white')
ax=plt.subplot(111)
self.plot_cnet(ax,v=v, current = current, voltage=voltage)
if not(ax):
plt.show()
def show_device(self,v=False,ax=False,current = True, voltage=True,legend=False):
if not(ax):
fig=plt.figure(figsize=(5,5))
ax=fig.add_subplot(111)
stick_cmap={'s':'b','m':'r','v':'k'}
stick_colors=[stick_cmap[i] for i in sticks.kind]
collection=LineCollection(sticks.endarray.values,linewidth=0.5,colors=stick_colors)
ax.add_collection(collection)
isect_cmap={'ms':'g','sm':'g', 'mm':'k','ss':'k','vs':'k','sv':'k','vm':'k','mv':'k'}
isect_colors=[isect_cmap[i] for i in self.intersects.kind]
ax.scatter(intersects.x, intersects.y, c=isect_colors, s=20, linewidth=1, marker="x")
ax.set_xlim((-0.02,1.02))
ax.set_ylim((-0.02,1.02))
fig = plt.figure(figsize=(5,5),facecolor='white')
ax=plt.subplot(111)
self.plot_cnet(ax,v=v, current = current, voltage=voltage)
self.plot_regions(ax)
if legend:
ax.legend()
if not(ax):
plt.show()
def plot_regions(self,ax):
for a in self.cnet.gate_areas:
ax.add_patch(patches.Rectangle( (a[0][0]-a[0][2]/2,a[0][1]-a[0][3]/2), a[0][2],a[0][3], edgecolor='b', fill=False, label="Local $V_G$ = {} V".format(a[1])))
ax.add_patch(patches.Rectangle( (-0.02,.48), 0.04,0.04, edgecolor='r', fill=False,label="Source = {} V".format(self.cnet.vds)))
ax.add_patch(patches.Rectangle( (.98,0.48), 0.04,0.04, edgecolor='k',
fill=False, label="GND = 0 V"))
def plot_cnet(self,ax1,v=False,current=True,voltage=True):
pos={k:self.cnet.graph.nodes[k]['pos'] for k in self.cnet.graph.nodes}
# for i in range(self.network_rows):
# for j in range(self.network_columns):
# pos[(i,j)]=j,i
edges,currents = zip(*nx.get_edge_attributes(self.cnet.graph,'current').items())

nodes,voltages = zip(*nx.get_node_attributes(self.cnet.graph,'voltage').items())
if voltage:
nodes=nx.draw_networkx_nodes(self.cnet.graph, pos, width=2,nodelist=nodes, node_color=voltages, cmap=plt.get_cmap('YlOrRd'), node_size=30, ax=ax1,edgecolors='k')
else:
nodes=nx.draw_networkx_nodes(self.cnet.graph, pos, width=2,nodelist=nodes, node_color='r', node_size=30, ax=ax1)

if current:
edges=nx.draw_networkx_edges(self.cnet.graph, pos, width=2, edgelist=edges, edge_color=currents, edge_cmap=plt.get_cmap('YlGn'), ax=ax1)
else:
edges=nx.draw_networkx_edges(self.cnet.graph, pos, width=2, edgelist=edges, edge_color='b', ax=ax1)

if v:
nodelabels=nx.get_node_attributes(self.graph,'voltage')
nx.draw_networkx_labels(self.graph,pos,labels={k:'{}\n {:.1e}V'.format(k,nodelabels[k]) for k in nodelabels})
edgecurrents=nx.get_edge_attributes(self.graph,'current')
edgeresistance=nx.get_edge_attributes(self.graph,'resistance')
nx.draw_networkx_edge_labels(self.graph,pos, edge_labels={k:'{:.1e}A\n{:.1e}$\Omega$'.format(edgecurrents[k], edgeresistance[k]) for k in edgecurrents})

if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit b83b49b

Please sign in to comment.