Skip to content

Instantly share code, notes, and snippets.

@ellisonbg
Forked from shoke/colorgraph.py
Created October 5, 2011 16:35
Show Gist options
  • Save ellisonbg/1264945 to your computer and use it in GitHub Desktop.
Save ellisonbg/1264945 to your computer and use it in GitHub Desktop.
Graphene
import networkx as nx
import matplotlib as mat
# Builds a graphene sheet with the given number of rows and columns.
# The sheet has a defected column running through the optionally passed
# arg. Otherwise it defaults to the center.
def build_defected_graph(rows, columns, defect = 0):
g = nx.Graph()
if( columns <= 2 ):
print "Must have more than two columns."
return g
if( rows < 2 ):
print "Must have more than 1 row."
return g
if defect == 1:
print "Defect wire cannot be on edge column."
if defect == columns:
print "Defect wire cannot be on edge column."
if (defect == 0) :
defect = int(columns/2) + 1
# creates the first complete hexagon
for i in range(6):
if (i+1) > 1:
g.add_edge(i+1,i)
g.add_edge(6,1)
# top of the first hexagon, start and end of the next hexagon
start = 4
end = 5
numNodes = 6
# if the number of hexagons in the row is greater than 1
if rows > 1:
for j in range(rows-1):
s = start
# 2 nodes already placed, adds the next 4
for addnodes in range(4):
g.add_edge(s, numNodes+1)
s = numNodes + 1
numNodes += 1
g.add_edge(numNodes, end)
start += 4
end += 4
evenColumn = True
secondColumn = True
connection = 1
# startingNode is the node the NEXT column will start at
# (aka the bottom of the one being drawn)
startingNode = numNodes + 1
# start is your current postion
start = startingNode
afterDefect = False
after2 = False
# if there is more than one column
if columns > 1:
for k in range(columns-1):
if( k+2 == defect ):
if evenColumn:
connection += 2
g.add_edge(connection, start)
g.add_edge(start, start+1)
g.add_edge(start+1, start+2)
g.add_edge(start+1, start+2)
g.add_edge(start+2, start+3)
g.add_edge(start+3, start+4)
numNodes += 5
# the second column is a special case
if secondColumn:
connection += 4
else:
connection += 2
g.add_edge(connection, start+4)
start += 4
evenRow = True
for l in range(rows -2):
if evenRow:
# the second column is a special case
if secondColumn:
connection += 4
else:
connection += 2
g.add_edge(start, start+1)
g.add_edge(start+1, connection)
start = start -1
g.add_edge(start, start+3)
g.add_edge(start+3, start + 4)
g.add_edge(start+2, start + 4)
start += 4
numNodes += 3
evenRow = False
else:
# the second column is a special case
if secondColumn:
connection += 4
else:
connection += 2
g.add_edge(start, start+1)
g.add_edge(start+2, start+1)
g.add_edge(start+2, start+3)
g.add_edge(connection, start+3)
numNodes += 3
start += 3
evenRow = True
if not evenColumn:
if evenRow:
# the second column is a special case
if secondColumn:
connection += 4
else:
connection += 2
g.add_edge(start, start+1)
g.add_edge(start+1, connection)
start = start -1
g.add_edge(start, start+3)
g.add_edge(start+3, start + 4)
g.add_edge(start+2, start + 4)
start += 4
numNodes += 3
evenRow = False
else:
connection += 2
g.add_edge(start, start+1)
start += 1
g.add_edge(start, start+1)
start += 1
g.add_edge(start, start+1)
start += 1
g.add_edge(start, connection)
numNodes += 3
connection = startingNode + 1
start = numNodes +1
evenColumn = not evenColumn
startingNode = numNodes
secondColumn = False
afterDefect = True
# even vs. odd columns start and end differently
elif evenColumn:
# the connection to the last finished column
# the first hexagon in each new column has one extra node
connection += 2
if not(k == (columns-2)):
g.add_edge(start, start+1)
start += 1
numNodes +=1
g.add_edge(start, start+1)
if afterDefect:
#connection += 1
evenRow = True
g.add_edge(start, connection)
numNodes += 1
for l in range(rows -1):
# the second column is a special case
if secondColumn:
connection += 4
elif afterDefect:
if evenRow:
connection += 4
evenRow = False
else:
connection += 2
evenRow = True
else:
connection += 2
start += 1
g.add_edge(start-1, start)
g.add_edge(start, start+1)
start += 1
g.add_edge(start, connection)
numNodes += 2
# finish column
if not(k == (columns-2)):
g.add_edge(numNodes , numNodes +1)
numNodes +=1
evenColumn = False
secondColumn = False
connection = startingNode
if afterDefect:
connection += 1
elif after2:
g.add_edge(numNodes, numNodes + 1)
numNodes += 1
startingNode = numNodes
start = numNodes +1
afterDefect = False
else:
g.add_edge(start, connection)
g.add_edge(start, start+1)
start += 1
g.add_edge(start, start+1)
start += 1
if afterDefect:
g.add_edge(start, start+1)
start += 1
connection += 1
evenRow = True
else:
connection += 2
g.add_edge(start, connection)
numNodes += 3
for l in range(rows -1):
if afterDefect:
if evenRow:
connection += 4
evenRow = False
else:
connection += 2
evenRow = True
else:
connection += 2
g.add_edge(start+1, start)
start += 1
g.add_edge(start, start+1)
start += 1
if( l == (rows-2) ):
if afterDefect:
g.add_edge(start +1, start)
if evenRow:
g.add_edge(start +1, connection-1)
else:
g.add_edge(start +1, connection-3)
numNodes += 3
else:
g.add_edge(start, connection)
numNodes += 2
else:
g.add_edge(start, connection)
numNodes += 2
# finish column
start += 1
evenColumn = True
if after2:
after2 = False
if afterDefect:
startingNode += 1
after2 = True
start += 1
connection = startingNode
startingNode = start
afterDefect = False
return g
import graphene_graph as gg
# <codecell>
import networkx as nx
import numpy as np
import matplotlib as mat
# builds a graph, colors it and exports it to a file
#g = gg.build_graph(5,5,False)
#data = list(np.linspace(-10,10,len(g)))
#newg = gg.color_graph(g, data, mat.cm.Blues)
#nx.draw(newg)
#nx.write_gexf(newg, 'test.gexf')
# adds defects
#g = gg.add_defects(g,1)
#data = list(np.linspace(-10,10,len(g)))
#newg = gg.color_graph(g, data, mat.cm.jet)
#nx.write_gexf(newg, 'test.gexf')
# builds a small graph and uses the draw_networkx function
#g = gg.build_graph(4,4,False)
#data = list(np.linspace(-10,10,len(g)))
#gg.draw_networkx(g,data)
#gg.draw_networkx(g,data, plt.cm.Accent)
gg.create_graph(6,6, False, list(np.linspace(-10,10,100)), map = mat.cm.Accent, filename = "creategraph.gexf")
import networkx as nx
import matplotlib as mat
import matplotlib.pyplot as plt
import random
# Builds a graphene sheet with the given number of rows and columns.
# If wrap is set to true it will roll it up
def build_graph(rows, columns, wrap):
g = nx.Graph()
# creates the first complete hexagon
for i in range(6):
if (i+1) > 1:
g.add_edge(i+1,i)
g.add_edge(6,1)
# top of the first hexagon, start and end of the next hexagon
start = 4
end = 5
numNodes = 6
# if the number of hexagons in the row is greater than 1
if rows > 1:
for j in range(rows-1):
s = start
# 2 nodes already placed, adds the next 4
for addnodes in range(4):
g.add_edge(s, numNodes+1)
numNodes += 1
s = numNodes
g.add_edge(numNodes, end)
start += 4
end += 4
evenColumn = True
secondColumn = True
connection = 1
# startingNode is the node the NEXT column will start at
# (aka the bottom of the one being drawn)
startingNode = numNodes + 1
# start is your current postion
start = startingNode
# if there is more than one column
if columns > 1:
for k in range(columns-1):
# even vs. odd columns start and end differently
if evenColumn:
# the connection to the last finished column
# the first hexagon in each new column has one extra node
connection += 2
print k
if not(k == (columns-2)):
g.add_edge(start, start+1)
start += 1
numNodes +=1
g.add_edge(start, start+1)
g.add_edge(start, connection)
numNodes += 1
for l in range(rows -1):
# the second column is a special case
if secondColumn:
connection += 4
else:
connection += 2
start += 1
g.add_edge(start-1, start)
g.add_edge(start, start+1)
start += 1
g.add_edge(start, connection)
numNodes += 2
# finish column
if not(k == (columns-2)):
g.add_edge(numNodes , numNodes +1)
numNodes +=1
evenColumn = False
secondColumn = False
connection = startingNode
startingNode = numNodes
start = numNodes +1
else:
g.add_edge(start, connection)
g.add_edge(start, start+1)
start += 1
g.add_edge(start, start+1)
start += 1
connection += 2
g.add_edge(start, connection)
numNodes += 3
for l in range(rows -1):
connection += 2
g.add_edge(start+1, start)
start += 1
g.add_edge(start, start+1)
start += 1
g.add_edge(start, connection)
numNodes += 2
# finish column
start += 1
evenColumn = True
connection = startingNode
startingNode = start
# are we rolling the sheet?
if wrap and (rows > 0):
g.remove_node( 2 + (rows * 4) - 1 )
g.add_edge( 2 + (rows * 4) , 1 )
g.remove_node( 2 + (rows * 4) - 2 )
g.add_edge( 2 + (rows * 4) - 3, 2 )
start = 3 + (rows * 4)
for k in range(columns-1):
end = 2 * rows + start
g.remove_node( end )
g.add_edge( end - 1, start )
start = end + 1
return g
# Builds a complete two-node graph
def build_two_node():
g = nx.Graph()
g.add_edge(1,2)
return g
# Builds a complete three-node graph
def build_three_node():
g = nx.Graph()
g.add_edge(1,2)
g.add_edge(2,3)
g.add_edge(3,1)
return g
# Builds a complete four-node graph
def build_four_node():
g = nx.Graph()
g.add_edge(1,2)
g.add_edge(2,3)
g.add_edge(3,4)
g.add_edge(1,4)
g.add_edge(3,1)
g.add_edge(2,4)
return g
# Colors each node in the given graph based on the data
# Set provided
def color_graph(graph, d, cmap):
col_graph = nx.Graph()
#normalize the data and create an array of colors that will
#correspond to the nodes in the graph
data = (normalize(d))
color_array = []
for j in range(len(data)):
color = cmap(data[j])
color_array.append( color )
# add the color to each node
for i in range(len(graph.node)-1):
red = int(255*color_array[i][0])
blu = int(255*color_array[i][1])
gre = int(255*color_array[i][2])
alp = color_array[i][3]
col_graph.add_node(i,{'viz':{'color':dict(r=red,b=blu,g=gre,a=alp)}})
col_graph.add_edges_from(graph.edges())
col_graph.remove_node(0)
return col_graph
# Normalizes the given data between 0 and 1
def normalize(data):
d = []
min = float(data[0])
max = float(data[0])
for i in range(len(data) ):
if ( float(data[i]) > max):
max = float(data[i])
if ( float(data[i]) < min):
min = float(data[i])
data_range = max - min
for j in range(len(data)):
d.append( ( float(data[j]) - min) / data_range )
return d
# Utilizes the networkx draw_networkx function
# Useful for small graphs
def draw_networkx(g, data, map = plt.cm.Blues):
min = float(data[0])
max = float(data[0])
# gathers the datas min and max
for i in range(len(data) ):
if ( float(data[i]) > max):
max = float(data[i])
if ( float(data[i]) < min):
min = float(data[i])
nx.draw_networkx(g, cmap = map, vmin = min, vmax = max, node_color = data)
plt.show()
# Adds the number of defects specified to the graph. Removes the nodes randomly
# and all adjacent edges
def add_defects(graph, number):
# do we have enough nodes?
if (number >= len(graph.nodes()) ):
print "The Number of Defects must be Less than the Size of the Graph"
return graph
# randomly generate nodes to pull
defects = random.sample(graph.nodes(), number)
for i in range(len(defects)):
graph.remove_node(defects[i])
return graph
def create_graph(rows,columns,wrap,data,map = plt.cm.Blues, filename = "test.gexf"):
g = build_graph(rows,columns,wrap)
g = color_graph(g,data,map)
nx.write_gexf(g,filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment