예제 #1
0
 def test_read_sparse6(self):
     data = """:Q___eDcdFcDeFcE`GaJ`IaHbKNbLM"""
     G = nx.parse_sparse6(data)
     fh = StringIO(data)
     Gin = nx.read_sparse6(fh)
     assert_equal(sorted(G.nodes()), sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()), sorted(Gin.edges()))
예제 #2
0
 def test_read_sparse6(self):
     data=""":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM"""
     G=nx.parse_sparse6(data)
     fh = StringIO(data)
     Gin=nx.read_sparse6(fh)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
예제 #3
0
 def test_read_sparse6(self):
     data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM'
     G = nx.from_sparse6_bytes(data)
     fh = BytesIO(data)
     Gin = nx.read_sparse6(fh)
     assert_nodes_equal(G.nodes(), Gin.nodes())
     assert_edges_equal(G.edges(), Gin.edges())
예제 #4
0
 def test_read_sparse6(self):
     data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM'
     G = nx.from_sparse6_bytes(data)
     fh = BytesIO(data)
     Gin = nx.read_sparse6(fh)
     assert_nodes_equal(G.nodes(), Gin.nodes())
     assert_edges_equal(G.edges(), Gin.edges())
예제 #5
0
 def test_read_many_graph6(self):
     # Read many graphs into list
     data=':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n'+\
         ':Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM'
     fh = StringIO(data)
     glist = nx.read_sparse6(fh)
     assert_equal(len(glist), 2)
     for G in glist:
         assert_equal(
             sorted(G.nodes()),
             [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
예제 #6
0
 def test_read_many_graph6(self):
     # Read many graphs into list
     data=':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n'+\
         ':Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM'
     fh = StringIO(data)
     glist=nx.read_sparse6(fh)
     assert_equal(len(glist),2)
     for G in glist:
         assert_equal(sorted(G.nodes()),
                      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                       10, 11, 12, 13, 14, 15, 16, 17])
예제 #7
0
 def test_read_many_graph6(self):
     # Read many graphs into list
     data = (b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n'
             b':Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM')
     fh = BytesIO(data)
     glist = nx.read_sparse6(fh)
     assert len(glist) == 2
     for G in glist:
         assert_nodes_equal(
             G.nodes(),
             [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
예제 #8
0
 def test_read_sparse6(self):
     data=""":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM"""
     G=nx.parse_sparse6(data)
     (fd,fname)=tempfile.mkstemp()
     fh=open(fname,'w')
     b=fh.write(data)
     fh.close()
     Gin=nx.read_sparse6(fname)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
     os.close(fd)
     os.unlink(fname)
예제 #9
0
 def test_read_sparse6(self):
     data=""":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM"""
     G=nx.parse_sparse6(data)
     (fd,fname)=tempfile.mkstemp()
     fh=open(fname,'w')
     b=fh.write(data)
     fh.close()
     Gin=nx.read_sparse6(fname)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
     os.close(fd)
     os.unlink(fname)
예제 #10
0
# create the MarkovChain with the given transition matrix
mc = mkm.MarkovChain(P)
    
# add some initial distributions
for i in [0,500,999]:
	mc.add_distributions(mkm.delta_distribution(1000,x=i))
        
# plot the total variation mixing
mc.plot_tv_mixing(y_tol=0.01, threshold=1e-5)


####################### NBRW EXAMPLE #########################
import matplotlib.pyplot as plt

# load a 6-regular graph with 50.000 nodes from file
G_6_regular = nx.read_sparse6('6_regular.s6')

# get the adjacency matrix
A = nx.to_scipy_sparse_matrix(G_6_regular)

# transition matrix for NBRW on the graph from the adjacency matrix 
P = mkm.graph_nbrw_transition_matrix(A)

# Markov chain with the transition marix
mc = mkm.MarkovChain(P)

# the stationary distribution is uniform
mc.set_stationary(mkm.uniform_distribution(mc.get_n()))

# add a random starting position to the Markov chain
mc.add_random_delta_distributions(1)
예제 #11
0
def main(args):
	
	file_name = ' '.join(args.graph_file)
	
	# read graph file to networkx.DiGraph object
	ext = file_name.split('.')[-1]
	if ext == 'graphml':
		graph = nx.read_graphml(file_name)
	elif ext == 'gml':
		graph = nx.read_gml(file_name)
	elif ext == 'gexf':
		graph = nx.read_gexf(file_name)
	elif ext == 'g6':
		graph = nx.read_graph6(file_name)
	elif ext == 's6':
		graph = nx.read_sparse6(file_name)
	elif ext == 'gpickle' or ext == 'p':
		graph = nx.read_gpickle(file_name)
	elif ext == 'yaml':
		graph = nx.read_yaml(file_name)
	else:
		print "Graph file format not supported. Supported fileformats: graphml (recommended), gexf, gml, g6, s6, gpickle, yaml"

	for n in graph.nodes():
		# from nicholas' tulip output
		if not 'x' in graph.node[n].keys():
			if 'graphics' in graph.node[n].keys():
				g = graph.node[n].pop('graphics')
				graph.node[n]['x'] = g['x']
				graph.node[n]['y'] = g['y']
				if 'h' in g.keys() and not 'node_type' in graph.node[n].keys():
					if g['h'] == 1:
						graph.node[n]['node_type'] = 'reaction'
					elif g['h'] == 2.5:
						graph.node[n]['node_type'] = 'species'
					else:
						graph.node[n]['node_type'] = 'ignore'
		if not 'label' in graph.node[n].keys():
			graph.node[n]['label'] = n

	if compatible_graph(graph):
		# get dictionary with layout info
		d = read_graph(graph)

		# get font
		font = ImageFont.truetype(args.font_file, 1000)

		# add cofactors
		if args.add_cofactors_from_sbml:
			sbml_file = ' '.join(args.add_cofactors_from_sbml)
			cofactors = get_cofactors_from_sbml(d, sbml_file)
			for r in cofactors:
				for s in cofactors[r]:
					d['edge_type'][(r,s)]=cofactors[r][s]['role']
		else:
			cofactors = None
		
		# get the data to assemble the svg file (editable version)
		svg_data = get_svgdata(
			d= d,
			font=font, 
			font_size= args.font_size, 
			scale=args.scale,
			padding=args.padding,
			padding_labels= args.padding_labels,
			normalize=args.normalize,
			overlap=args.overlap,
			defdir = args.r_direction,
			cofactors = cofactors,
			reverse_cof = args.reverse_cof)
		
		# assemble svg file and save (editable version)
		doc = get_svgdoc(**svg_data)
		doc.save(args.svg_name)
		print 'output svg saved in', args.svg_name
예제 #12
0
import markovmixing as mkm
import networkx as nx
import matplotlib.pyplot as plt

# load a random 6-regular graph with 50.000 nodes from file
G_6_regular = nx.read_sparse6('6_regular.s6')

# get the adjacency matrix
A = nx.to_scipy_sparse_matrix(G_6_regular)

# transition matrix for SRW on the graph from the adjacency matrix 
P = mkm.graph_srw_transition_matrix(A)

# Markov chain with the transition marix
mc = mkm.MarkovChain(P)

# stationary distribution of SRW on a graph is deg(x)/2*|E|
mc.set_stationary_distribution(mkm.graph_srw_stationary_distribution(A))

# add a random starting position to the Markov chain
mc.add_distributions(mkm.random_delta_distributions(mc.get_n(),1))

# determine the mixing in total variation 
mc.compute_tv_mixing()

# plot the mixing
(x,tv) = mc.distribution_tv_mixing(0)
plt.plot(x, tv)
plt.xlabel("t")
plt.ylabel("Distance to stationary distribution in total variation")
plt.show()	
	
elif(input_file_type=='Pajek'):
	while True:
		try:
			G = nx.read_pajek(file_path)
			break
	#if the file format isin ---Pajek---- read the graph and put that in G variable which is later used to write  graph
		except IOError:
			print("Error while READING the file ")
		
elif(input_file_type=='SparseGraph6'):
	a=input("enter 1.for Sparse6 \n2.for graph6 format")
	if(a==1):
	  while True:
		try:
			G = nx.read_sparse6(file_path)
			break
		except IOError:
			print("Error while READING the file ")
		
	else :
	  while True:
		try:
			G=nx.read_graph6(file_path)
			break
	#if the file format isin ---SparseGraph6---- read the graph and put that in G variable which is later used to write  graph
		except IOError:
			print("Error while READING the file ")
		
elif(input_file_type=='GISShapefile'):
	while True:
예제 #14
0
# import networkx as nx
from sklearn import cluster
import networkx as nx
from collections import defaultdict
import matplotlib.pyplot as plt
from matplotlib import cm, colors
import seaborn as sns
import pandas as pd
import numpy as np
import random
from sklearn.metrics.cluster import normalized_mutual_info_score
from sklearn.metrics.cluster import adjusted_rand_score  # never have to reinvent the wheel.
G = nx.read_sparse6("randomFuck.s6")
pos = nx.spring_layout(G)


# s0=nx.clustering(G)
# for x in s0:
#   print(x)
def draw_communities(G, membership, pos):
    """Draws the nodes to a plot with assigned colors for each individual cluster
    Parameters
    ----------
    G : networkx graph
    membership : list
        A list where the position is the student and the value at the position is the student club membership.
        E.g. `print(membership[8]) --> 1` means that student #8 is a member of club 1.
    pos : positioning as a networkx spring layout
        E.g. nx.spring_layout(G)
    """
    fig, ax = plt.subplots(figsize=(16, 9))