def get_graphs(): directories = sorted(os.listdir('out'), key=lambda x:max([0]+[os.stat(os.path.join('out', x, y))[stat.ST_MTIME] for y in os.listdir(os.path.join('out', x))]), reverse=True) for directory in directories: for filename in os.listdir(os.path.join('out', directory)): filename = os.path.join('out', directory, filename) if filename.endswith('.xml'): try: yield directory, read_graphml(filename) except Exception: traceback.print_exc(file=sys.stderr) pass
def main(): # Read the graph and generate a adjacency matrix g = read_graphml("data/Intellifiber.graphml") matrix = g.get_adjacency_matrix() """ Initialize the genetics module with parameters 1: Number of individuals in a population 2: Number of elements in one individual 3: Percentage of parents to retain 4: Mutation rate 5: Random selection rate """ gen = Genetics(100, 3, 0.1, 0.05, 0.01) # Generate initial population based on available graph vertices p = gen.population(g.get_nodes()) generation_history = [] # Evolve for i in xrange(1000): p = gen.evolve(p, matrix, g.get_nodes()) grade = gen.grade(p, matrix) print("Generation (" + str(i) + ") ==> " + str(grade)) generation_history.append(grade) # Print best individual across all generations print(str(gen.optimum) + " with a value of " + str(gen.optimum_value)) # Plot grade over generation x = range(0, len(generation_history)) plot.plot(x, generation_history, linewidth=2) plot.xlabel("Generation") plot.ylabel("Average Fitness") plot.grid(True) plot.savefig("Result.png") plot.show()
from operator import itemgetter # {cmd} journal regex assert len(sys.argv) == 3 _, journal, regex = sys.argv assert os.path.isfile(journal + ".gml") print "Compiling regex: %s" % regex p = re.compile(regex, re.U) print "Loading GraphML network graph for %s" % journal g = read_graphml(journal + ".gml") print "Creating a directional graph to hold just the citation chains" filtered_g = nx.DiGraph() filtered_g.level = {} filtered_g.position = {} filtered_g.year = {} rows = {} # unescape '_' in the Journal journal = " ".join(journal.split("_")) print "Scanning nodes for title regex matches" hits = set()
import matplotlib.pyplot as plt try: from pylab import * except: pass from graphml import read_graphml import sys, os if len(sys.argv) == 2: fn = sys.argv[1] print "Reading in %s" % fn g = read_graphml(fn) print "Generating a generalized laplacian" l = nx.generalized_laplacian(g) print "Calculating eigenvalues" e = eigenvalues(l) print ("Largest eigenvalue:", max(e)) print ("Smallest eigenvalue:", min(e)) # plot with matplotlib if we have it # shows "semicircle" distribution of eigenvalues try: print "Trying to plot semicircle of eigenvalues" hist(e, bins=100) # histogram with 100 bins xlim(0, 2) # eigenvalues between 0 and 2 show() # plt.savefig("%s.eigen.png" % fn) except:
from operator import itemgetter # {cmd} journal regex assert len(sys.argv) == 3 _, journal, regex = sys.argv assert os.path.isfile(journal+".gml") print "Compiling regex: %s" % regex p = re.compile(regex, re.U) print "Loading GraphML network graph for %s" % journal g = read_graphml(journal+".gml") print "Creating a directional graph to hold just the citation chains" filtered_g = nx.DiGraph() filtered_g.level = {} filtered_g.position = {} filtered_g.year = {} rows = {} # unescape '_' in the Journal journal = " ".join(journal.split("_")) print "Scanning nodes for title regex matches" hits = set()
#!/usr/bin/env python try: import matplotlib.pyplot as plt except: raise import networkx as nx from graphml import read_graphml import sys, os if len(sys.argv) == 2: fn = sys.argv[1] print "Reading in %s" % fn g = read_graphml(fn) print "Simulating spring network..." pos = nx.spring_layout(g, iterations=50) print "Drawing a simple graph" plt.figure(figsize=(10, 10)) #nx.draw(g,pos,node_size=40) nx.draw_circular(g, node_size=40) #plt.savefig("%s.spring_no_label.png" % fn) # save as png plt.show() # display