Beispiel #1
0
def planning(env, path_nodes, start, goal):
    """
    env : environment parameter
    path_node : list of [x, y] represents the path nodes
    """
    # create the nodes
    G = nx.graph()
    G.add_node(0, loc = start)
    for i in len(path_nodes):
        G.add_node(i+1, loc = path_nodes[i])
    G.add_node(len(path_nodes) + 1, loc = path_nodes[i])

    # create the edges
    for i in range(len(path_nodes) + 2):
        for j in range(len(path_nodes) + 2):
            if collision_detection(env = env, graph = G , node1 = i, node2 =  j, step = 0.1):
                G.add_edge(i, j)
    
    # use bellman_ford algorithm to compute the distance
    


    


    return distance, node_list
Beispiel #2
0
    def get_networkx_graph(self):
        """
        将网络拓扑转化为图的形式表示
        :return:
        """
        graph = nx.graph()

        return graph
Beispiel #3
0
	def get_future_link_graph(self):
		Graph = nx.graph()
		for line in self.future_dataset.readlines():
			line = line.strip()
			col = line.split(';')
			node1_id = int(col[0])
			node2_id = "p%s" %(int(col[1]))
			Graph.add_edge(node1_id, node2_id)
		return Graph
Beispiel #4
0
def create_graph(data):
	G = nx.graph() #empty graph
	
	for i, elrow in data.iterrows():
    	g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
    
if __name__ == "__main__":
	a = import_data('https://gist.githubusercontent.com/brooksandrew/e570c38bcc72a8d102422f2af836513b/raw/89c76b2563dbc0e88384719a35cba0dfc04cd522/edgelist_sleeping_giant.csv')
	b = import_data('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv')
	create_graph(a)
Beispiel #5
0
def graph(next_ids):
    g = nx.graph()
    g.add_nodes_from(next_ids) #creates nodes from the users ids
    for j in next_ids.items():
        g.add_edges_from([ (j,k) for k in y ]) #adds edges to the id nodes
    data = json_graph.node_link_data(G)
    with open('data.json', 'w', encoding ='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4)
    diameter = nx.diameter(n_graph) #calculates diameter
    avg_distance = nx.average_shortest_path_length(n_graph)  #calculates the average distance
    print("Diameter: ", diameter, "\nAverage Distance: ", avg_distance)
def pdist(x, dist=distance, threshold=7):
    
    #D = np.zeros(len(x), len(x))
    G = nx.graph()
    for i in range(len(x)):
        for j in range(i+1, len(x)):
            d = dist(x[i], x[j])
            if d<threshold:
                G.add_edge(i,j,weight)
    return G
Beispiel #7
0
def load_graph(graph_name):
  if graph_name=='':
    return
  if graph_name == 'Board Ex':
    ## Board example - Toy Graph
    G = nx.Graph()
    G.add_edge(1,2)
    G.add_edge(1,5)
    G.add_edge(2,3)
    G.add_edge(2,4)
    G.add_edge(3,4)
    G.add_edge(3,5)
    G.add_edge(4,6)
    G.add_edge(5,6)
    # G.add_edge(5,7)
    # G.add_edge(6,7)
    #print '--- Board Example ---'
  elif graph_name == 'Karate Club':
    G = nx.karate_club_graph()
  elif graph_name == "Erdos-Renyi":
    n=100 # 10 nodes
    p= 0.5# 20 edges
    G=nx.gnp_random_graph(n,p) #binomial_graph which is also somtims called the Erdos-Renyi grap    h.
  elif graph_name == "NewWatStr SW":
    k=2
    p=0.5
    G = nx.newman_watts_strogatz_graph(n, k, p)
  elif graph_name == "Kronecker":
    ## TODO: implement Kronecker
    G = nx.graph()
    #
  elif graph_name == "edgelist":
    G = nx.read_edgelist('../demo_graphs/out.contact',comments="%",delimiter="\t")
    G = nx.read_edgelist('../demo_graphs/netscience.txt')
    #LCCG = sorted(nx.connected_components(G), key = len, reverse=True)
    cc = sorted(list(nx.connected_component_subgraphs(G)), key = len, reverse=True)
    G = cc[0] 
  else:
    G = nx.graph()

  return (graph_name,G)
 def visualize(self, ajmatrix, ntype="bit"):
     """
     ajmatrix -> adjacency matrix of graph(array like)
     ntype -> default: bit(string) or add list of node(list)
     """
     if ntype == "bit":
         edge = [
             format(i, "0%sb" % (self.n_qubit)) for i in range(self.nodes)
         ]
         print(edge)
     else:
         edge = ntype
     G = nx.graph()
Beispiel #9
0
def get_stations():
        with open("tube.csv",mode="r",encoding ="utf-8") as stations:
            reader = csv.reader(stations)
            for line in reader:
                print(line[0])
                print(line[1])
                print(line[2:])
                print()
                data = {"line name":line[0],"edge colour":line[1]}
                print(data)
                print()
                print()
                tube_graph = nx.graph()
                tube_graph.add_path(line[2:],data)
Beispiel #10
0
def get_stations():
    with open("tube.csv", mode="r", encoding="utf-8") as stations:
        reader = csv.reader(stations)
        for line in reader:
            print(line[0])
            print(line[1])
            print(line[2:])
            print()
            data = {"line name": line[0], "edge colour": line[1]}
            print(data)
            print()
            print()
            tube_graph = nx.graph()
            tube_graph.add_path(line[2:], data)
def report(antcompend):
	G=nx.graph()
	G=from_numpy_matrix(antcompend)
	noden=nx.number_of_nodes(G)
	for n in range(noden):
		for nn in range(noden):
			if n==nn:
				pass
			G.add_edge(n,nn,weigh=route[n][nn])
	pos=nx.spring_layout(T)
	
	plt.figure(figsize=(8,8))
	nx.draw_networkx_nodes(G,pos)
	nx.draw_networkx_edges(G,pos,)
	plt.show()
Beispiel #12
0
def load_graph(graph_name):
    if graph_name == '':
        return
    if graph_name == 'BoardEx':
        ## Board example - Toy Graph
        G = nx.Graph()
        G.add_edge(1, 2)
        G.add_edge(2, 3)
        G.add_edge(2, 4)
        G.add_edge(3, 4)
        G.add_edge(3, 5)
        G.add_edge(4, 6)
        G.add_edge(5, 6)
        G.add_edge(5, 7)
        G.add_edge(6, 7)
        #print '--- Board Example ---'
    elif graph_name == 'KarateClub':
        G = nx.karate_club_graph()
    elif graph_name == "Erdos-Renyi":
        n = 68  # 10 nodes
        m = 68  # 20 edges
        G = nx.gnm_random_graph(
            n, m
        )  #binomial_graph which is also somtims called the Erdos-Renyi grap    h.
    elif graph_name == "NewWatStrSW":
        k = 2
        p = 0.5
        G = nx.newman_watts_strogatz_graph(n, k, p)
    elif graph_name == "Kronecker":
        ## TODO: implement Kronecker
        G = nx.graph()
        #
    else:
        G = nx.graph()

    return (graph_name, G)
Beispiel #13
0
def report(antcompend):
    G = nx.graph()
    G = from_numpy_matrix(antcompend)
    noden = nx.number_of_nodes(G)
    for n in range(noden):
        for nn in range(noden):
            if n == nn:
                pass
            G.add_edge(n, nn, weigh=route[n][nn])
    pos = nx.spring_layout(T)

    plt.figure(figsize=(8, 8))
    nx.draw_networkx_nodes(G, pos)
    nx.draw_networkx_edges(
        G,
        pos,
    )
    plt.show()
Beispiel #14
0
def gen_independent_graph():

   global ind_graph
   global followers_dict

   ind_graph=Ddict(dict)
   big_chunk=read_timeline()
   
   for chunk in big_chunk:
       first=chunk[0].split(' ')
       hashtag=first[0].strip()
       time=first[1].strip()
       ind_graph[hashtag][time]=nx.Graph()
       chunk=chunk[1:]
       for user in chunk:
           user=user.split(' ')
           time=user[0].strip()
           id=user[1].strip()
           if time in ind_graph[hashtag]:
              graph=ind_graph[hashtag][time]
              graph.add_node(user)
           else:
              graph=nx.graph()
              graph.add_node(user)
              ind_graph[hashtag][time]=graph
   
   for hashtag in ind_graph:
       for time in ind_graph[hashtag]:
           graph=ind_graph[hashtag][time]
           for node in graph.nodes():
               for other in graph.nodes():
                   if node!=other:
                      if node in  followers_dict[other]:
                         if other in followers_dict[node]:
                            graph.add_edge(node,other)
           ind_graph[hashtag][time]=graph
   
    
   
   
   gen_dependent_graph(big_chunk)
Beispiel #15
0
def gen_dependent_graph(big_chunk):

    global dep_graph
    dep_graph = {}
    for chunk in big_chunk:
        hashtag = chunk[0].split(' ').strip()
        chunk = chunk[1:]
        graph = nx.graph()
        for user in chunk:
            id = user.split(' ')[1]
            graph.add_node(id)
        dep_graph[hashtag] = graph

    for hashtag in dep_graph:
        graph = dep_graph[hashtag]
        for node in graph.nodes():
            for other in graph.nodes():
                if node != other:
                    if node in followers_dict[other]:
                        if other in followers_dict[node]:
                            graph.add_edge(node, other)
        dep_graph[hashtag] = graph
Beispiel #16
0
def gen_dependent_graph(big_chunk):
   
   global dep_graph
   dep_graph={}
   for chunk in big_chunk:
       hashtag=chunk[0].split(' ').strip()
       chunk=chunk[1:]
       graph=nx.graph()
       for user in chunk:
           id=user.split(' ')[1]
           graph.add_node(id)
       dep_graph[hashtag]=graph
   
   for hashtag in dep_graph:
      graph=dep_graph[hashtag]
      for node in graph.nodes():
               for other in graph.nodes():
                   if node!=other:
                      if node in  followers_dict[other]:
                         if other in followers_dict[node]:
                            graph.add_edge(node,other)
      dep_graph[hashtag]=graph
Beispiel #17
0
def gen_independent_graph():

    global ind_graph
    global followers_dict

    ind_graph = Ddict(dict)
    big_chunk = read_timeline()

    for chunk in big_chunk:
        first = chunk[0].split(' ')
        hashtag = first[0].strip()
        time = first[1].strip()
        ind_graph[hashtag][time] = nx.Graph()
        chunk = chunk[1:]
        for user in chunk:
            user = user.split(' ')
            time = user[0].strip()
            id = user[1].strip()
            if time in ind_graph[hashtag]:
                graph = ind_graph[hashtag][time]
                graph.add_node(user)
            else:
                graph = nx.graph()
                graph.add_node(user)
                ind_graph[hashtag][time] = graph

    for hashtag in ind_graph:
        for time in ind_graph[hashtag]:
            graph = ind_graph[hashtag][time]
            for node in graph.nodes():
                for other in graph.nodes():
                    if node != other:
                        if node in followers_dict[other]:
                            if other in followers_dict[node]:
                                graph.add_edge(node, other)
            ind_graph[hashtag][time] = graph

    gen_dependent_graph(big_chunk)
Beispiel #18
0
 def __init__(self):
     self.graph = nx.graph()
     self.userList = []
Beispiel #19
0
 def __init__(self):
     self.G = networkx.graph()
Beispiel #20
0
import networkx as nx

def dfs(graph, s):
  """
  graph - networkx graph. Which graph would you like to search?

  s - where is your starting node?
  """
  return None
  

if __name__ == "__main__":
  g = nx.graph()
  g.add_edges_from()
    best = 0  # best graph split
    while True:  # iterate until we use all the number of edges
        gnStep(g)
        q = gnModularity(g, orig, m)
        if q > best:  # if the modularity of this method of partitioning is better
            best = q
            bestcom = nx.connected_components(
                g)  # best way of partitioning the graph
        if g.number_of_edges() == 0:
            break  # break the loop when we get to this part of the algorithm
    if best > 0:
        print("Modularity (Q): %f " % best)
        print("Graph communities:", bestcom)
    else:
        print("Modularity (Q): %f" % best)


gr = sys.argv[1]  # read input graph file
g = nx.graph()  # initialize is using networkx
buildGraph(g, gr, ",")  # build the graph

n = g.number_of_nodes()
a = nx.adj_matrix(g)  # adjacency matrix
m = 0  # number of edges
for i in range(n):
    for j in range(n):
        m += a[i, j]
m = m / 2  # normalize
gnRun(g, Deg(a, g.nodes()),
      m)  # run the algorithm after getting the weighted degree for each node
Beispiel #22
0
def growgraph(usenx= True, force_connected = True, sparse = True, plot = False, directed = True, randomgrowth=False, wholegrowth=False,growthfactor=100, num_measurements = 10, verbose = True, connected = True, plotx = 'nodegrowth', ploty = 'maxclique', ploty2 = 'modularity',drawgraph = 'moral', draw= True):
	# initialize database 
	graph_db = neo4j.GraphDatabaseService()

	#get the pickled dictionary of nodes and node names that is made while the csv file is loaded.
	try:
		nodes = open('nodes.p','r')
		nodes = pickle.load(nodes)
	except:
		print 'Your graph is empty. Please loab a graph into the database.'
		1/0
	data = []
	if graph_db.size < 2:
		print 'Your graph is empty. Please load a graph into the database.'
		1/0
	#make a list of all the nodes in the database to randomly choose from, but only if force connected graph option is false. 
	if force_connected == False:
		possiblenodes = nodes
		print 'Graph will not be fully connected, use "force_connected = True" if you want it to be fully connected'

	# here we figure out at what points to measure if the user wants a spare measumement or to measure every time a node is added. This speeds up big graph growths a lot! 
	if sparse == True:
	    sparsemeasurements = [6,7,8,9,growthfactor]
	    measurements = np.logspace(1, (int(len(str(growthfactor))))-1, num_measurements)
	    for x in measurements:
	        sparsemeasurements.append(int(x))
	else:
		sparsemeasurements = range(2,growthfactor)

	# this will actually only work for directed graph because we are moralizing, but I want to leave the option for later. Perhaps I can just skip moralization for undirected graph.
	if directed:
		graph = nx.DiGraph()
	else:
		graph = nx.graph()


	# this will grow the whole graph. Takes a while for big ones! 
	if wholegrowth == True:
	    growthfactor = len(nodes)

	# pick an initial node to start from
	initial_node = random.choice(nodes.keys()) #randomly get a node to add from dictionary
	if verbose:
	    print 'Starting from ' + initial_node
	used = 0
	#measure the nodegrowth 
	nodegrowth = graph.size()
	while nodegrowth < growthfactor: # make sure we aren't above how many nodes we want to measure in the graph    
		if force_connected == True:
			if nodegrowth > 1:
				possiblenodes = graph.nodes() # get all nodes in graph.
				fromnode = random.choice(possiblenodes) #pick random node in graph to grow from.
				if verbose:
				    print 'Using ' + str(fromnode) + ' to find new node'
			else: #this is because you can't do random from one node at the start.
			    fromnode = initial_node
			    if verbose:
			        print 'using initial node'
			        used = used+1
			        if used > 5:
			        	1/0

			fromnode = nodes[fromnode] #get DB version of the node.
			#get all relationpships in graph DB for that node
			new_node_rels = list(graph_db.match(end_node = fromnode, bidirectional=True))
			new_rel = random.choice(new_node_rels)

			# is the new node a parentt or child of the node we are growing from?
			if new_rel.end_node == fromnode:
			    new_node = new_rel.start_node
			if new_rel.start_node == fromnode:
			    new_node = new_rel.end_node
			assert new_node != fromnode
		
		if force_connected == False:
			new_node = random.choice(possiblenodes.values())
			
		print 'adding' + str(new_node)
	    # go through the list of edges that have the new node as a part of it, and only add the edge if they are between the new node and a node in the graph already.
		rels = list(graph_db.match(start_node=new_node)) #query graph for edges from that node
		for edge in rels:
		    newnodename = edge.start_node.get_properties()
		    newnodename = newnodename.get('name')
		    newnodename = newnodename.encode()
		    endnodename = edge.end_node.get_properties()
		    endnodename = endnodename.get('name')
		    endnodename = endnodename.encode()
		    if newnodename not in graph: #check to see if new node is in graph
		        graph.add_node(newnodename) # add if not
		        if verbose:
		            print 'added ' + str(newnodename)
		    if endnodename in graph: #check to see if end node is in graph
		        graph.add_edge(newnodename, endnodename) #add it if it is
		        if verbose:
		            print 'connected ' + newnodename +' to '+ endnodename

		rels = list(graph_db.match(end_node=new_node)) #query graph for edges to that node
		for edge in rels:
		    newnodename = edge.end_node.get_properties()
		    newnodename = newnodename.get('name')
		    newnodename = newnodename.encode()
		    startnodename = edge.start_node.get_properties()
		    startnodename = startnodename.get('name')
		    startnodename = startnodename.encode()
		    if newnodename not in graph: #check to see if new node is in graph
		        graph.add_node(newnodename) # add if not
		        if verbose:
		            print 'added ' + str(newnodename)
		    if startnodename in graph: #check to see if end node is in graph
		        graph.add_edge(startnodename, newnodename) #add it if it is
		        if verbose:
		            print 'connected ' + startnodename +' to '+ newnodename
		nodegrowth = len(graph.nodes())
		if verbose:
			print 'Graph has ' + str(nodegrowth) + ' nodes.'
		edgegrowth = len(graph.edges())
		if verbose:
		    	print 'Graph has ' + str(edgegrowth) + ' edges.'

			if nodegrowth in sparsemeasurements: 
				start_time = time()
				try:
					modgraph = nx.Graph(graph) #make it into a networkx graph
					partition = community.best_partition(modgraph) #find the partition with maximal modularity
					modularity = community.modularity(partition,modgraph) #calculate modularity with that partition
				except:
					modularity = 0.0 
				moral = nx.to_numpy_matrix(graph)
				if usenx == True:
					moralized = pybayes.moralize(graph)
				moralized, moral_edges = octave.moralize(moral)	
				ismoral = False
				tries = 0
				while ismoral == False and tries < 5: #sometimes oct2py takes too long to return I think
				    try:
				        moralized, moral_edges = octave.moralize(moral)
				        ismoral = True
				    except:
				        ismoral = False
				        print 'Octave Error, trying again!'
				        tries = tries + 1
				order = range(len(moralized)+1)
				order = order[1:]
				triangulated, cliques, fill_ins = octave.triangulate(moralized,order)
				istriangulated = False
				tries = 0
				while istriangulated == False and tries < 5: #sometimes oct2py takes too long to return I think
				    try:
				        triangulated, cliques, fill_ins = octave.triangulate(moralized,order)
				        istriangulated = True
				        tries = 5
				    except:
				        istriangulated = False
				        print 'Octave Error, trying again!'
				        tries = tries + 1 
				cliquesizes = []
				for x in cliques:
				    size = len(x)
				    cliquesizes.append(size)
				maxclique = max(cliquesizes)
				avgclique = np.mean(cliquesizes)
				end_time = time()
				run_time = end_time - start_time
				data.append([nodegrowth, len(graph.edges()), modularity, maxclique,avgclique,run_time])
				sparsemeasurements.remove(nodegrowth) #so we don't calculate clique size more than once!
				if verbose:
				    print 'took ' + str(run_time) + ' to run last computation'
				print 'Growing graph with ' + str(nodegrowth) + ' nodes, ' + str(modularity) + ' modularity, max clique of ' + str(maxclique) + ', ' + str(len(graph.edges())) + ' edges.'
				if plot == True:
					df = pd.DataFrame(data, columns= ('nodegrowth','edgegrowth', 'modularity','maxclique','avgclique','run_time'))
					plt.close()
					fig = plt.figure(figsize=(24,16))
					ax = fig.add_subplot(1,1,1)
					ax2 = ax.twinx()
					y1 = df[ploty]
					y2 = df[ploty2]
					x = df[plotx]
					ax.set_xlabel('%s in Graph' %(plotx),fontsize=20)
					line1, = ax.plot(x, y1, label = ploty)
					line2, = ax2.plot(x, y2, label = ploty2, color = 'red')
					ax.set_ylabel(ploty,fontsize=20)
					ax2.set_ylabel(ploty2, fontsize=20)
					plt.suptitle('Graph Growth', fontsize=30)
					plt.legend((line1,line2),(ploty , ploty2), loc='upper center', frameon=False, fontsize=20)
					plt.show()
				if draw == True:
					if drawgraph == 'triangulated':
						G = nx.from_numpy_matrix(triangulated)
					elif drawgraph == 'moral':
						G = nx.from_numpy_matrix(moralized)
					elif drawgraph == 'directed':
						G = graph()
				# position is stored as node attribute data for random_geometric_graph
					plt.close()
					pos = nx.random_layout(G)
					# find node near center (0.5,0.5)
					dmin=1
					ncenter=0
					for n in pos:
					    x,y=pos[n]
					    d=(x-0.5)**2+(y-0.5)**2
					    if d<dmin:
					        ncenter=n
					        dmin=d

					# color by path length from node near center
					p=nx.single_source_shortest_path_length(G,ncenter)
					plt.figure(figsize=(15,15))
					nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.2)
					nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),
					                       node_size=20,
					                       node_color=p.values(),
					                       cmap=plt.cm.Reds_r)

					plt.xlim(-0.05,1.05)
					plt.ylim(-0.05,1.05)
					plt.axis('off')
					plt.show()
	df = pd.DataFrame(data, columns= ('nodegrowth','edgegrowth', 'modularity','maxclique','avgclique','run_time'))
	return(df)
Beispiel #23
0
import networkx as nx
import random

edge_list = [(1, 2), (1, 4), (1, 5), (2, 4), (2, 6), (3, 5), (3, 6), (4, 5),
             (5, 6), (7, 8), (7, 10), (8, 9), (9, 10)]
# create G2 and Gc using edge_list

n_edges_added_for_weak_connectedness = 0
n_edges_added_for_strong_connectedness = 0
G2 = nx.graph()
G2.add_node(range(1, 11))
G2.add_edge(x for x in edge_list)
while (nx.is_strongly_connected(G2) == False):
    u = random.choice(list(G2.nodes()))
    v = random.choice(list(G2.nodes()))
    if (u != v):
        if (nx.is_weakly_connected(G2) == False):
            G2.add_edge(u, v)
            n_edges_added_for_strong_connectedness += 1
            n_edges_added_for_weak_connectedness += 1
        else:
            G2.add_edge(u, v)
            n_edges_added_for_strong_connectedness += 1

Gc = G2.copy()
Beispiel #24
0
 def __init__(self, populace, home_infectivity, school_infectivity,
              work_infectivity):
     self.graph = nx.graph()
     self.home_infectivity = home_infectivity
     self.school_infectivity = school_infectivity
     self.work_infectivity = work_infectivity
Beispiel #25
0
def grow_graph(reverserandom = False, outgoingrandom = False, incomingrandom = False, totalrandom = False, usenx= True, force_connected = True, sparse = True, plot = False, directed = True, randomgrowth=False, wholegrowth=False,growthfactor=100, num_measurements = 10, verbose = True, connected = True, plotx = 'nodegrowth', ploty = 'maxclique', ploty2 = 'modval',drawgraph = 'triangulated', draw= True, drawspectral = True, getgraph = True):
	
	random = False #set as false, gets made to truth later if the person passes a type of random graph they want

	#make sure user does not want to draw and plot at the same time. 
	if plot == True:
		draw = False
	if draw == True:
		plot = False
	#you cannot plot and draw at the same time

	# initialize database 
	graph_db = database()

	#get the pickled dictionary of nodes and node names (for the database) that is made while the csv file is loaded.
	try:
		nodes = open('nodes.p','r')
		nodes = pickle.load(nodes)
	except:
		print 'Your graph is empty. Please load a graph into the database.'
		1/0
	data = []
	if graph_db.size < 2:
		print 'Your graph is empty. Please load a graph into the database.'
		1/0
	# this will grow the whole graph. Takes a while for big ones! 
	if wholegrowth == True:
	    growthfactor = len(nodes)

	#make a list of all the nodes in the database to randomly choose from, but only if force connected graph option is false. 
	if force_connected == False:
		possiblenodes = nodes
		print 'Graph will not be fully connected, use "force_connected = True" if you want it to be fully connected'

	# here we figure out at what points to measure if the user wants a spare measumement or to measure every time a node is added. This speeds up big graph growths a lot! 
	if sparse == True:
	    sparsemeasurements = [10,15,growthfactor]
	    measurements = np.logspace(1, (int(len(str(growthfactor))))-1, num_measurements)
	    for x in measurements:
	        sparsemeasurements.append(int(x))
	else:
		sparsemeasurements = range(2,growthfactor)

	# this will actually only work for directed graph because we are moralizing, but I want to leave the option for later. Perhaps I can just skip moralization for undirected graphs.
	if directed:
		graph = nx.DiGraph()
	else:
		graph = nx.graph()




	# pick an initial node to start from
	initial_node = np.random.choice(nodes.keys()) #randomly get a node to add from dictionary
	if verbose:
	    print 'Starting from ' + initial_node
	initial_used = 0 #keep track of how many times the initial node was used
	nodegrowth = graph.number_of_nodes()
	edgegrowth = graph.number_of_edges()
	while nodegrowth < growthfactor: # make sure we aren't above how many nodes we want to measure in the graph    
		#start off finding a node to add to the graph.
		if force_connected == True:
			try:
				possiblenodes = graph.nodes() # get all nodes in graph.
				fromnode = Random.choice(possiblenodes) #pick random node in graph to grow from(add one of its nieghbors). This uses the random module, not np.random
				if verbose:
				    print 'Using ' + str(fromnode) + ' to find new node'
			except: #this is because you can't do random from one node at the start.
			    fromnode = initial_node
			    graph.add_node(fromnode)
			    if verbose:
			        print 'using initial node'
			        initial_used = initial_used+1
			        if initial_used > 5:
			        	1/0

			fromnode = nodes[fromnode] #get DB version of the node.
			#get all relationships in graph DB for that node so we can pick 
			new_node_rels = list(graph_db.match(end_node = fromnode, bidirectional=True))
			new_rel = Random.choice(new_node_rels) #randomly pick one of them, thus picking a node to add to graph. This uses the random module, not np.random

			# is the new node a parent or child of the node we are growing from?
			if new_rel.end_node == fromnode:
			    new_node = new_rel.start_node
			if new_rel.start_node == fromnode:
			    new_node = new_rel.end_node
			assert new_node != fromnode
		
		if force_connected == False: # if not connected, we can just pick from the pickled dictionary of nodes in the database
			new_node = np.random.choice(possiblenodes.values())
			
		print 'adding' + str(new_node)
		#add the nodes to the graph, connecting it to nodes in the graph that it is connected to.
	    # go through the list of edges that have the new node as a part of it, and only add the edge if they are between the new node and a node in the graph already.
		rels = list(graph_db.match(start_node=new_node)) #query graph for edges from that node
		for edge in rels:
			#get the string name of the node
		    newnodename = edge.start_node.get_properties()
		    newnodename = newnodename.get('name')
		    newnodename = newnodename.encode()
		    endnodename = edge.end_node.get_properties()
		    endnodename = endnodename.get('name')
		    endnodename = endnodename.encode()
		    if newnodename not in graph: #check to see if new node is in graph
		        graph.add_node(newnodename) # add if not
		        if verbose:
		            print 'added ' + str(newnodename)
		    if endnodename in graph: #check to see if end node is in graph
		        graph.add_edge(newnodename, endnodename) #add it if it is
		        if verbose:
		            print 'connected ' + newnodename +' to '+ endnodename

		rels = list(graph_db.match(end_node=new_node)) #query graph for edges to that node
		for edge in rels:
		    newnodename = edge.end_node.get_properties()
		    newnodename = newnodename.get('name')
		    newnodename = newnodename.encode()
		    startnodename = edge.start_node.get_properties()
		    startnodename = startnodename.get('name')
		    startnodename = startnodename.encode()
		    if newnodename not in graph: #check to see if new node is in graph
		        graph.add_node(newnodename) # add if not
		        if verbose:
		            print 'added ' + str(newnodename)
		    if startnodename in graph: #check to see if end node is in graph
		        graph.add_edge(startnodename, newnodename) #add it if it is
		        if verbose:
		            print 'connected ' + startnodename +' to '+ newnodename
				#measure the nodegrowth and edge growth
		nodegrowth = len(graph.nodes())
		edgegrowth = len(graph.edges())
		if verbose:
			print 'Graph has ' + str(nodegrowth) + ' nodes.'
		if verbose:
		    print 'Graph has ' + str(edgegrowth) + ' edges.'

		#here I make a few random graphs to compare the growth of the real graph. They are all the same size and same edges, but different controls.		

		# Reverse the direction of the edges. The in-degree distribution in this graph is power-law, but the out-degree is exponential tailed. So this is just a check that degree distribution is irrelevant.
		if reverserandom == True: 
			random_graph =  graph.reverse()
			random = True
		# Keep the number of outgoing links for each node the same, but randomly allocating their destinations. This should break modularity, put preserves out-degree.
		if outgoingrandom == True:
			random_graph = graph.copy()
			for edge in random_graph.edges():
				parent = edge[0]
				child = edge[1]
				random_graph.remove_edge(parent,child)
				newchild = parent
				while newchild == parent: #so that we don't get a self loop.
					newchild = np.random.choice(graph.nodes())
				random_graph.add_edge(parent,newchild)
				random = True
		# Same thing, but this time fixing the number of incoming links and randomly allocating their origins. Likewise, but preserves in-degree.
		if incomingrandom ==True:
			random_graph = graph.copy()
			for edge in random_graph.edges():
				parent = edge[0]
				child = edge[1]
				random_graph.remove_edge(parent,child)
				newparent = child
				while newparent == child:
					newparent = np.random.choice(graph.nodes())
				random_graph.add_edge(newparent,child)
				random = True
		#gives a graph picked randomly out of the set of all graphs with n nodes and m edges. This preserves overall degree, and number of nodes/edges, but is completeley random to outdegree/indegree. 
		if totalrandom == True:
			numrandomedges = graph.number_of_edges()
			numrandomnodes = graph.number_of_nodes()
			random_graph = nx.dense_gnm_random_graph(numrandomnodes, numrandomedges)
			random_graph = random_graph.to_directed()
			random = True
				
		    #here is where we measure everything about the graph 
		if nodegrowth in sparsemeasurements: #we only measure every now and then in sparse.
			start_time = time()
			if nodegrowth > 5:
				modgraph = nx.Graph(graph) #make it into a undirected networkx graph. This measures moduilarity on undirected version of graph! 
				partition = best_partition(modgraph) #find the partition with maximal modularity
				modval = modularity(partition,modgraph) #calculate modularity with that partition
				sleep(2)
				if random == True:
					random_modgraph = random_graph.to_undirected() #make it into a undirected networkx graph. This measures moduilarity on undirected version of graph! 
					random_partition = best_partition(random_modgraph) #find the partition with maximal modularity
					random_modval = modularity(random_partition,random_modgraph) #calculate modularity with that partition
			#option to use python nx to moralize and triangulate
			if usenx == True:
				moralized = moral_graph(graph)
				if random == True:
					random_moralized = moral_graph(random_graph)
			
			else: #use Octave to run Matlab Bayes Net Toolbox, NOT SET UP FOR RANDOM GRAPHS YET.
				moralized = nx.to_numpy_matrix(graph) # make nx graph into a simple matrix
				ismoral = False
				tries = 0
				while ismoral == False and tries < 5: #sometimes oct2py takes too long to return I think
				    try:
				        moralized, moral_edges = octave.moralize(moralized)
				        ismoral = True
				    except:
				        ismoral = False
				        print 'Octave Error, trying again!'
				        tries = tries + 1
			
			order = range(len(moralized)+1) # BNT needs order of nodes to triangulate
			order = order[1:]
			
			random_order = range(len(random_moralized)+1) # BNT needs order of nodes to triangulate
			random_order = random_order[1:] # I think you have to shift the space because you are going from 0 index to 1 idndex
			
			istriangulated = False
			tries = 0
			while istriangulated == False and tries < 5: #sometimes oct2py takes too long to return I think
				try:
				    moralized = nx.to_numpy_matrix(moralized)  # have to make it into matrix.
				    triangulated, cliques, fill_ins = octave.triangulate(moralized,order)
				    if random == True:
				    	random_moralized = nx.to_numpy_matrix(random_moralized) # have to make it into matrix.
				    	random_triangulated, random_cliques, random_fill_ins = octave.triangulate(random_moralized, random_order)
				    istriangulated = True
				    tries = 5
				except:
				    istriangulated = False
				    print 'Octave Error, trying again!'
				    tries = tries + 1 
			#loop through cliques and get the size of them
			cliquesizes = [] #empty array to keep clique sizes in
			for x in cliques:
			    size = len(x)
			    cliquesizes.append(size)
			maxclique = max(cliquesizes) #get the biggest clique
			avgclique = np.mean(cliquesizes) # get the mean of the clique sizes

			#do the same for random graph cliques
			if random == True:
				random_cliquesizes = [] #empty array to keep clique sizes in
				#loop through cliques and get the size of them
				for x in random_cliques:
					size = len(x)
					random_cliquesizes.append(size)
				random_maxclique = max(random_cliquesizes) #get the biggest clique
				random_avgclique = np.mean(random_cliquesizes) # get the mean of the clique sizes
		
			end_time = time() #get end time 
			run_time = end_time - start_time #time how long all the took
			
			#store the data! 

			if random == True:
				data.append([nodegrowth, edgegrowth, modval, random_modval, maxclique, random_maxclique, avgclique, random_avgclique, run_time]) #store results

			if random == False:
				data.append([nodegrowth, edgegrowth, modval, maxclique,avgclique,run_time]) #store results
			
			sparsemeasurements.remove(nodegrowth) #so we don't calculate clique size more than once!
			if verbose:
			    print 'took ' + str(run_time) + ' to run last computation'
			
			#this will always print, basic status update everytime clique size is measured.
			if random == True:
				print str(nodegrowth) + ' nodes, ', str(edgegrowth) + ' edges; ' + 'Modularity: ' + str(modval) + ', Random Modularity: ' +str(random_modval) + ', Largest Clique: ' + str(maxclique) + ', Largest Random Clique: ' + str(random_maxclique)
			if random == False:
				print str(nodegrowth) + ' nodes, ', str(edgegrowth) + ' edges; ' + 'Modularity: ' + str(modval) + ', Largest Clique: ' + str(maxclique)
			

			#this will redraw the plot everytime a computation is done.
			if plot == True:
				if random == False:
					df = pd.DataFrame(data, columns= ('nodegrowth','edgegrowth', 'modularity','maxclique','avgclique','run_time'))
					plt.close()
					fig = plt.figure(figsize=(24,16))
					ax = fig.add_subplot(1,1,1)
					ax2 = ax.twinx()
					y1 = df[ploty] #user input, default to clique size
					y2 = df[ploty2] #user input, default to modularity
					x = df[plotx] #user input, default to nodes in graph.
					ax.set_xlabel('%s in Graph' %(plotx),fontsize=20)
					line1, = ax.plot(x, y1, label = ploty)
					line2, = ax2.plot(x, y2, label = ploty2, color = 'red')
					ax.set_ylabel(ploty,fontsize=20)
					ax2.set_ylabel(ploty2, fontsize=20)
					plt.suptitle('Graph Growth', fontsize=30)
					plt.legend((line1,line2),(ploty , ploty2), loc='upper center', frameon=False, fontsize=20)
					plt.show()

				if random == True:
					df = pd.DataFrame(data, columns= ('nodegrowth', 'edgegrowth', 'modval','random_modval', 'maxclique', 'random_maxclique', 'avgclique', 'random_avgclique', 'run_time'))
					plt.close()
					fig = plt.figure(figsize=(18,10))
					ax = fig.add_subplot(1,1,1)
					ax2 = ax.twinx()
					ax3 = ax.twinx()
					y1 = df[ploty]
					y2 = df[ploty2]
					y3def = str('random_'+ploty) # i just add random to whatever the user inputs as the y stuff they want to plot
					y3 = df[y3def]
					y4def = str('random_'+ploty2)
					y4 = df[y4def]
					x = df[plotx]
					ax.set_xlabel('%s in Graph' %(plotx),fontsize=20)
					line1, = ax.plot(x, y1, label = ploty, color = 'blue')
					line2, = ax2.plot(x, y2, label = ploty2, color = 'green')
					line3, = ax.plot(x,y3, label = y3def, color = 'red')
					line4, = ax3.plot(x,y4, label = y4def, color = 'cyan')
					ax.set_ylabel(ploty,fontsize=20)
					ax2.set_ylabel(ploty2, fontsize=20)
					plt.suptitle('Graph Growth', fontsize=30)
					plt.legend((line1,line2,line3,line4), (ploty,ploty2,y3def,y4def),loc='upper center', frameon=False, fontsize=20)#
					plt.show()
			#draw the graph 
			if draw == True:
				if drawgraph == 'triangulated':
					G = nx.from_numpy_matrix(triangulated)
				elif drawgraph == 'moralized':
					G = nx.from_numpy_matrix(moralized)
				elif drawgraph == 'directed':
					G = graph
				plt.close()
				
				if drawspectral == True:
					nx.draw_random(G, prog='neato')
				else:
					pos = nx.random_layout(G)
					# find node near center (0.5,0.5)
					dmin=1
					ncenter=0
					for n in pos:
					    x,y=pos[n]
					    d=(x-0.5)**2+(y-0.5)**2
					    if d<dmin:
					        ncenter=n
					        dmin=d

					# color by path length from node near center
					p=nx.single_source_shortest_path_length(G,ncenter)
					plt.figure(figsize=(15,15))
					plt.suptitle(drawgraph + ' graph')
					nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.2)
					nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),
					                       node_size=20,
					                       node_color=p.values(),
					                       cmap=plt.cm.Reds_r)

					plt.xlim(-0.05,1.05)
					plt.ylim(-0.05,1.05)
					plt.axis('off')

				plt.show()
	if random == True:
		df = pd.DataFrame(data, columns= ('nodegrowth', 'edgegrowth', 'modval','random_modval', 'maxclique', 'random_maxclique', 'avgclique', 'random_avgclique', 'run_time'))
	if random == False:
		df = pd.DataFrame(data, columns= ('nodegrowth','edgegrowth', 'modval','maxclique','avgclique','run_time'))
	if getgraph == True:
		return (graph, df)
	else:
		return(df)
Beispiel #26
0
data = []
if graph_db.size < 2:
	print 'Your graph is empty. Please load a graph into the database.'
	1/0
if sparse == True:
    sparsemeasurements = [6,7,8,9,growthfactor]
    measurements = np.logspace(1, (int(len(str(growthfactor))))-1, num_measurements)
    for x in measurements:
        sparsemeasurements.append(int(x))
else:
	sparsemeasurements = range(2,growthfactor)

if directed:
	graph = nx.DiGraph()
else:
	graph = nx.graph()

nodegrowth = graph.size()

edgegrowth = 0
if wholegrowth == True:
    growthfactor = len(nodes)


initial_node = random.choice(nodes.keys()) #randomly get a node to add from dictionary
if verbose:
    print 'Starting from ' + initial_node
while nodegrowth < growthfactor:
        if nodegrowth > 1:
            possiblenodes = graph.nodes() # get all nodes in graph.
            fromnode = random.choice(possiblenodes) #pick random node in graph to grow from.
Beispiel #27
0
            
edges = []
for node in data:
    for edge in node['timelineItems']['edges']:
        crnode = edge['node']
        if not crnode['isCrossRepository']:
            edges.append((node['number'], crnode['source']['number']))
            
edges
refcounts
refcounts.sum()
len(edges)
nodes
G = nx.graph()
import networkx as nx
G = nx.graph()
G = nx.Graph()
G
G.add_nodes(nodes)
G.add_nodes_from(nodes)
G.nodes
G.add_edges_from(edges)
G.nodes
nx.draw(G)
plt.show()
import matplotlib.pyplot as plt
plt.show()
nx.draw(G, node_size=100)
plt.show()
G
G.edges
Beispiel #28
0
def modMST(graph):
	result = nx.graph()
	sort = sorted(g.edges(data=True), key = lambda t: t[2].get('weight'),reverse=True)
Beispiel #29
0
def grow_graph(reverserandom=False,
               outgoingrandom=False,
               incomingrandom=False,
               totalrandom=False,
               usenx=True,
               force_connected=True,
               sparse=True,
               plot=False,
               directed=True,
               randomgrowth=False,
               wholegrowth=False,
               growthfactor=100,
               num_measurements=10,
               verbose=True,
               connected=True,
               plotx='nodegrowth',
               ploty='maxclique',
               ploty2='modval',
               drawgraph='triangulated',
               draw=True,
               drawspectral=True,
               getgraph=True):

    random = False  #set as false, gets made to truth later if the person passes a type of random graph they want

    #make sure user does not want to draw and plot at the same time.
    if plot == True:
        draw = False
    if draw == True:
        plot = False
    #you cannot plot and draw at the same time

    # initialize database
    graph_db = database()

    #get the pickled dictionary of nodes and node names (for the database) that is made while the csv file is loaded.
    try:
        nodes = open('nodes.p', 'r')
        nodes = pickle.load(nodes)
    except:
        print 'Your graph is empty. Please load a graph into the database.'
        1 / 0
    data = []
    if graph_db.size < 2:
        print 'Your graph is empty. Please load a graph into the database.'
        1 / 0
    # this will grow the whole graph. Takes a while for big ones!
    if wholegrowth == True:
        growthfactor = len(nodes)

    #make a list of all the nodes in the database to randomly choose from, but only if force connected graph option is false.
    if force_connected == False:
        possiblenodes = nodes
        print 'Graph will not be fully connected, use "force_connected = True" if you want it to be fully connected'

    # here we figure out at what points to measure if the user wants a spare measumement or to measure every time a node is added. This speeds up big graph growths a lot!
    if sparse == True:
        sparsemeasurements = [10, 15, growthfactor]
        measurements = np.logspace(1, (int(len(str(growthfactor)))) - 1,
                                   num_measurements)
        for x in measurements:
            sparsemeasurements.append(int(x))
    else:
        sparsemeasurements = range(2, growthfactor)

    # this will actually only work for directed graph because we are moralizing, but I want to leave the option for later. Perhaps I can just skip moralization for undirected graphs.
    if directed:
        graph = nx.DiGraph()
    else:
        graph = nx.graph()

    # pick an initial node to start from
    initial_node = np.random.choice(
        nodes.keys())  #randomly get a node to add from dictionary
    if verbose:
        print 'Starting from ' + initial_node
    initial_used = 0  #keep track of how many times the initial node was used
    nodegrowth = graph.number_of_nodes()
    edgegrowth = graph.number_of_edges()
    while nodegrowth < growthfactor:  # make sure we aren't above how many nodes we want to measure in the graph
        #start off finding a node to add to the graph.
        if force_connected == True:
            try:
                possiblenodes = graph.nodes()  # get all nodes in graph.
                fromnode = Random.choice(
                    possiblenodes
                )  #pick random node in graph to grow from(add one of its nieghbors). This uses the random module, not np.random
                if verbose:
                    print 'Using ' + str(fromnode) + ' to find new node'
            except:  #this is because you can't do random from one node at the start.
                fromnode = initial_node
                graph.add_node(fromnode)
                if verbose:
                    print 'using initial node'
                    initial_used = initial_used + 1
                    if initial_used > 5:
                        1 / 0

            fromnode = nodes[fromnode]  #get DB version of the node.
            #get all relationships in graph DB for that node so we can pick
            new_node_rels = list(
                graph_db.match(end_node=fromnode, bidirectional=True))
            new_rel = Random.choice(
                new_node_rels
            )  #randomly pick one of them, thus picking a node to add to graph. This uses the random module, not np.random

            # is the new node a parent or child of the node we are growing from?
            if new_rel.end_node == fromnode:
                new_node = new_rel.start_node
            if new_rel.start_node == fromnode:
                new_node = new_rel.end_node
            assert new_node != fromnode

        if force_connected == False:  # if not connected, we can just pick from the pickled dictionary of nodes in the database
            new_node = np.random.choice(possiblenodes.values())

        print 'adding' + str(new_node)
        #add the nodes to the graph, connecting it to nodes in the graph that it is connected to.
        # go through the list of edges that have the new node as a part of it, and only add the edge if they are between the new node and a node in the graph already.
        rels = list(graph_db.match(
            start_node=new_node))  #query graph for edges from that node
        for edge in rels:
            #get the string name of the node
            newnodename = edge.start_node.get_properties()
            newnodename = newnodename.get('name')
            newnodename = newnodename.encode()
            endnodename = edge.end_node.get_properties()
            endnodename = endnodename.get('name')
            endnodename = endnodename.encode()
            if newnodename not in graph:  #check to see if new node is in graph
                graph.add_node(newnodename)  # add if not
                if verbose:
                    print 'added ' + str(newnodename)
            if endnodename in graph:  #check to see if end node is in graph
                graph.add_edge(newnodename, endnodename)  #add it if it is
                if verbose:
                    print 'connected ' + newnodename + ' to ' + endnodename

        rels = list(graph_db.match(
            end_node=new_node))  #query graph for edges to that node
        for edge in rels:
            newnodename = edge.end_node.get_properties()
            newnodename = newnodename.get('name')
            newnodename = newnodename.encode()
            startnodename = edge.start_node.get_properties()
            startnodename = startnodename.get('name')
            startnodename = startnodename.encode()
            if newnodename not in graph:  #check to see if new node is in graph
                graph.add_node(newnodename)  # add if not
                if verbose:
                    print 'added ' + str(newnodename)
            if startnodename in graph:  #check to see if end node is in graph
                graph.add_edge(startnodename, newnodename)  #add it if it is
                if verbose:
                    print 'connected ' + startnodename + ' to ' + newnodename
        #measure the nodegrowth and edge growth
        nodegrowth = len(graph.nodes())
        edgegrowth = len(graph.edges())
        if verbose:
            print 'Graph has ' + str(nodegrowth) + ' nodes.'
        if verbose:
            print 'Graph has ' + str(edgegrowth) + ' edges.'

        #here I make a few random graphs to compare the growth of the real graph. They are all the same size and same edges, but different controls.

        # Reverse the direction of the edges. The in-degree distribution in this graph is power-law, but the out-degree is exponential tailed. So this is just a check that degree distribution is irrelevant.
        if reverserandom == True:
            random_graph = graph.reverse()
            random = True
        # Keep the number of outgoing links for each node the same, but randomly allocating their destinations. This should break modularity, put preserves out-degree.
        if outgoingrandom == True:
            random_graph = graph.copy()
            for edge in random_graph.edges():
                parent = edge[0]
                child = edge[1]
                random_graph.remove_edge(parent, child)
                newchild = parent
                while newchild == parent:  #so that we don't get a self loop.
                    newchild = np.random.choice(graph.nodes())
                random_graph.add_edge(parent, newchild)
                random = True
        # Same thing, but this time fixing the number of incoming links and randomly allocating their origins. Likewise, but preserves in-degree.
        if incomingrandom == True:
            random_graph = graph.copy()
            for edge in random_graph.edges():
                parent = edge[0]
                child = edge[1]
                random_graph.remove_edge(parent, child)
                newparent = child
                while newparent == child:
                    newparent = np.random.choice(graph.nodes())
                random_graph.add_edge(newparent, child)
                random = True
        #gives a graph picked randomly out of the set of all graphs with n nodes and m edges. This preserves overall degree, and number of nodes/edges, but is completeley random to outdegree/indegree.
        if totalrandom == True:
            numrandomedges = graph.number_of_edges()
            numrandomnodes = graph.number_of_nodes()
            random_graph = nx.dense_gnm_random_graph(numrandomnodes,
                                                     numrandomedges)
            random_graph = random_graph.to_directed()
            random = True

        #here is where we measure everything about the graph
        if nodegrowth in sparsemeasurements:  #we only measure every now and then in sparse.
            start_time = time()
            if nodegrowth > 5:
                modgraph = nx.Graph(
                    graph
                )  #make it into a undirected networkx graph. This measures moduilarity on undirected version of graph!
                partition = best_partition(
                    modgraph)  #find the partition with maximal modularity
                modval = modularity(
                    partition,
                    modgraph)  #calculate modularity with that partition
                sleep(2)
                if random == True:
                    random_modgraph = random_graph.to_undirected(
                    )  #make it into a undirected networkx graph. This measures moduilarity on undirected version of graph!
                    random_partition = best_partition(
                        random_modgraph
                    )  #find the partition with maximal modularity
                    random_modval = modularity(
                        random_partition, random_modgraph
                    )  #calculate modularity with that partition
            #option to use python nx to moralize and triangulate
            if usenx == True:
                moralized = moral_graph(graph)
                if random == True:
                    random_moralized = moral_graph(random_graph)

            else:  #use Octave to run Matlab Bayes Net Toolbox, NOT SET UP FOR RANDOM GRAPHS YET.
                moralized = nx.to_numpy_matrix(
                    graph)  # make nx graph into a simple matrix
                ismoral = False
                tries = 0
                while ismoral == False and tries < 5:  #sometimes oct2py takes too long to return I think
                    try:
                        moralized, moral_edges = octave.moralize(moralized)
                        ismoral = True
                    except:
                        ismoral = False
                        print 'Octave Error, trying again!'
                        tries = tries + 1

            order = range(len(moralized) +
                          1)  # BNT needs order of nodes to triangulate
            order = order[1:]

            random_order = range(len(random_moralized) +
                                 1)  # BNT needs order of nodes to triangulate
            random_order = random_order[
                1:]  # I think you have to shift the space because you are going from 0 index to 1 idndex

            istriangulated = False
            tries = 0
            while istriangulated == False and tries < 5:  #sometimes oct2py takes too long to return I think
                try:
                    moralized = nx.to_numpy_matrix(
                        moralized)  # have to make it into matrix.
                    triangulated, cliques, fill_ins = octave.triangulate(
                        moralized, order)
                    if random == True:
                        random_moralized = nx.to_numpy_matrix(
                            random_moralized)  # have to make it into matrix.
                        random_triangulated, random_cliques, random_fill_ins = octave.triangulate(
                            random_moralized, random_order)
                    istriangulated = True
                    tries = 5
                except:
                    istriangulated = False
                    print 'Octave Error, trying again!'
                    tries = tries + 1
            #loop through cliques and get the size of them
            cliquesizes = []  #empty array to keep clique sizes in
            for x in cliques:
                size = len(x)
                cliquesizes.append(size)
            maxclique = max(cliquesizes)  #get the biggest clique
            avgclique = np.mean(
                cliquesizes)  # get the mean of the clique sizes

            #do the same for random graph cliques
            if random == True:
                random_cliquesizes = []  #empty array to keep clique sizes in
                #loop through cliques and get the size of them
                for x in random_cliques:
                    size = len(x)
                    random_cliquesizes.append(size)
                random_maxclique = max(
                    random_cliquesizes)  #get the biggest clique
                random_avgclique = np.mean(
                    random_cliquesizes)  # get the mean of the clique sizes

            end_time = time()  #get end time
            run_time = end_time - start_time  #time how long all the took

            #store the data!

            if random == True:
                data.append([
                    nodegrowth, edgegrowth, modval, random_modval, maxclique,
                    random_maxclique, avgclique, random_avgclique, run_time
                ])  #store results

            if random == False:
                data.append([
                    nodegrowth, edgegrowth, modval, maxclique, avgclique,
                    run_time
                ])  #store results

            sparsemeasurements.remove(
                nodegrowth)  #so we don't calculate clique size more than once!
            if verbose:
                print 'took ' + str(run_time) + ' to run last computation'

            #this will always print, basic status update everytime clique size is measured.
            if random == True:
                print str(nodegrowth) + ' nodes, ', str(
                    edgegrowth) + ' edges; ' + 'Modularity: ' + str(
                        modval) + ', Random Modularity: ' + str(
                            random_modval) + ', Largest Clique: ' + str(
                                maxclique) + ', Largest Random Clique: ' + str(
                                    random_maxclique)
            if random == False:
                print str(nodegrowth) + ' nodes, ', str(
                    edgegrowth) + ' edges; ' + 'Modularity: ' + str(
                        modval) + ', Largest Clique: ' + str(maxclique)

            #this will redraw the plot everytime a computation is done.
            if plot == True:
                if random == False:
                    df = pd.DataFrame(data,
                                      columns=('nodegrowth', 'edgegrowth',
                                               'modularity', 'maxclique',
                                               'avgclique', 'run_time'))
                    plt.close()
                    fig = plt.figure(figsize=(24, 16))
                    ax = fig.add_subplot(1, 1, 1)
                    ax2 = ax.twinx()
                    y1 = df[ploty]  #user input, default to clique size
                    y2 = df[ploty2]  #user input, default to modularity
                    x = df[plotx]  #user input, default to nodes in graph.
                    ax.set_xlabel('%s in Graph' % (plotx), fontsize=20)
                    line1, = ax.plot(x, y1, label=ploty)
                    line2, = ax2.plot(x, y2, label=ploty2, color='red')
                    ax.set_ylabel(ploty, fontsize=20)
                    ax2.set_ylabel(ploty2, fontsize=20)
                    plt.suptitle('Graph Growth', fontsize=30)
                    plt.legend((line1, line2), (ploty, ploty2),
                               loc='upper center',
                               frameon=False,
                               fontsize=20)
                    plt.show()

                if random == True:
                    df = pd.DataFrame(data,
                                      columns=('nodegrowth', 'edgegrowth',
                                               'modval', 'random_modval',
                                               'maxclique', 'random_maxclique',
                                               'avgclique', 'random_avgclique',
                                               'run_time'))
                    plt.close()
                    fig = plt.figure(figsize=(18, 10))
                    ax = fig.add_subplot(1, 1, 1)
                    ax2 = ax.twinx()
                    ax3 = ax.twinx()
                    y1 = df[ploty]
                    y2 = df[ploty2]
                    y3def = str(
                        'random_' + ploty
                    )  # i just add random to whatever the user inputs as the y stuff they want to plot
                    y3 = df[y3def]
                    y4def = str('random_' + ploty2)
                    y4 = df[y4def]
                    x = df[plotx]
                    ax.set_xlabel('%s in Graph' % (plotx), fontsize=20)
                    line1, = ax.plot(x, y1, label=ploty, color='blue')
                    line2, = ax2.plot(x, y2, label=ploty2, color='green')
                    line3, = ax.plot(x, y3, label=y3def, color='red')
                    line4, = ax3.plot(x, y4, label=y4def, color='cyan')
                    ax.set_ylabel(ploty, fontsize=20)
                    ax2.set_ylabel(ploty2, fontsize=20)
                    plt.suptitle('Graph Growth', fontsize=30)
                    plt.legend((line1, line2, line3, line4),
                               (ploty, ploty2, y3def, y4def),
                               loc='upper center',
                               frameon=False,
                               fontsize=20)  #
                    plt.show()
            #draw the graph
            if draw == True:
                if drawgraph == 'triangulated':
                    G = nx.from_numpy_matrix(triangulated)
                elif drawgraph == 'moralized':
                    G = nx.from_numpy_matrix(moralized)
                elif drawgraph == 'directed':
                    G = graph
                plt.close()

                if drawspectral == True:
                    nx.draw_random(G, prog='neato')
                else:
                    pos = nx.random_layout(G)
                    # find node near center (0.5,0.5)
                    dmin = 1
                    ncenter = 0
                    for n in pos:
                        x, y = pos[n]
                        d = (x - 0.5)**2 + (y - 0.5)**2
                        if d < dmin:
                            ncenter = n
                            dmin = d

                    # color by path length from node near center
                    p = nx.single_source_shortest_path_length(G, ncenter)
                    plt.figure(figsize=(15, 15))
                    plt.suptitle(drawgraph + ' graph')
                    nx.draw_networkx_edges(G,
                                           pos,
                                           nodelist=[ncenter],
                                           alpha=0.2)
                    nx.draw_networkx_nodes(G,
                                           pos,
                                           nodelist=p.keys(),
                                           node_size=20,
                                           node_color=p.values(),
                                           cmap=plt.cm.Reds_r)

                    plt.xlim(-0.05, 1.05)
                    plt.ylim(-0.05, 1.05)
                    plt.axis('off')

                plt.show()
    if random == True:
        df = pd.DataFrame(data,
                          columns=('nodegrowth', 'edgegrowth', 'modval',
                                   'random_modval', 'maxclique',
                                   'random_maxclique', 'avgclique',
                                   'random_avgclique', 'run_time'))
    if random == False:
        df = pd.DataFrame(data,
                          columns=('nodegrowth', 'edgegrowth', 'modval',
                                   'maxclique', 'avgclique', 'run_time'))
    if getgraph == True:
        return (graph, df)
    else:
        return (df)