Advertisement
Guest User

Untitled

a guest
May 29th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. from __future__ import division
  3.  
  4. import sys
  5. import csv
  6. import numpy # MATLAB-like numerical library
  7. import networkx as nx # To plot the graph
  8. import matplotlib.pyplot as plt
  9. from matplotlib.patches import FancyArrowPatch, Circle
  10.  
  11. # Example
  12.  
  13. # 1,A,B,C,-,-,-,-
  14. # 2,D,E,F,A,C,-,-
  15. # 3,B,C,F,-,-,-,-
  16. # 4,A,B,C,D,E,F,-
  17.  
  18. # [[ 0. 2. 3. 2. 2. 2.]
  19. # [ 2. 0. 3. 1. 1. 2.]
  20. # [ 3. 3. 0. 2. 2. 3.]
  21. # [ 2. 1. 2. 0. 2. 2.]
  22. # [ 2. 1. 2. 2. 0. 2.]
  23. # [ 2. 2. 3. 2. 2. 0.]]
  24.  
  25. try:
  26. # sys.argv is like argv in the main function of a C program
  27. # argv[0] is the name of the command and argv[1] is the first parameter
  28. filename = sys.argv[1]
  29. except:
  30. raise ValueError("You must provide a CSV file to work on!")
  31.  
  32. # Keep track of author names and papers count
  33. authors = []
  34.  
  35. # We don't know the number of author a priori so we use
  36. # a matrix of size 10 that we will grow (exponentially) when needed
  37. N = 10
  38.  
  39. def grow(A, papers):
  40. global N
  41.  
  42. next_N = N*10
  43.  
  44. # Create an empty matrix
  45. B = numpy.zeros((next_N, next_N))
  46. p = numpy.zeros(next_N, dtype=numpy.intc)
  47.  
  48. # And copy existing data
  49. B[:N,:N] = A
  50. p[:N] = papers
  51.  
  52. N = next_N
  53.  
  54. return B, p
  55.  
  56. def add_author(name, inc=False):
  57. """ Helper function which adds a name to the list
  58. when not already in it, and returns the corresponding index
  59. """
  60. if name not in authors:
  61. authors.append(name)
  62. idx = authors.index(name)
  63. return idx
  64.  
  65. def draw_network(G,pos,ax,sg=None):
  66.  
  67. for n in G:
  68. c=Circle(pos[n],radius=0.01,alpha=1)
  69. ax.add_patch(c)
  70. G.node[n]['patch']=c
  71. x,y=pos[n]
  72. seen={}
  73. for (u,v,d) in G.edges(data=True):
  74. n1=G.node[u]['patch']
  75. n2=G.node[v]['patch']
  76. rad=0.1
  77. if (u,v) in seen:
  78. rad=seen.get((u,v))
  79. rad=(rad+np.sign(rad)*0.1)*-1
  80. alpha=1
  81. color='k'
  82.  
  83. e = FancyArrowPatch(n1.center,n2.center,patchA=n1,patchB=n2,
  84. arrowstyle='-|>',
  85. connectionstyle='arc3,rad=%s'%rad,
  86. mutation_scale=10.0,
  87. lw=2,
  88. alpha=alpha,
  89. color=color)
  90. seen[(u,v)]=rad
  91. ax.add_patch(e)
  92. return e
  93.  
  94. # Open the CSV file as f. It will be closed automatically
  95. # when leaving this block
  96. with open(filename) as f:
  97.  
  98. # Our adjacency matrix
  99. A = numpy.zeros((N, N))
  100. papers = numpy.zeros(N)
  101.  
  102. # The file is read line by line
  103. reader = csv.reader(f)
  104. for row in reader: # Easy to do a loop
  105. # Each line is a list of strings, corresponding to the values
  106. # between the comas in the file
  107.  
  108. # Only keep the actual names
  109. author_names = filter(lambda x:x != "", row[1:])
  110.  
  111. # When we need to know the index of the current value when looping,
  112. # use the `enumerate` function. Here k is the index.
  113. for k, author_name in enumerate(author_names):
  114. i = add_author(author_name, inc=True)
  115. if i > N-1:
  116. A, papers = grow(A, papers)
  117. papers[i] += 1
  118. for coauthors in author_names[k+1:]:
  119. j = add_author(coauthors)
  120. if j > N-1:
  121. A, papers = grow(A, papers)
  122.  
  123. # Fill 'er up! (not trying to be smart with symmetry)
  124. A[i,j] += 1
  125. A[j,i] += 1
  126.  
  127. # Clip the matrix to the actual number of authors
  128. K = len(authors)
  129. A = A[:K, :K]
  130. papers = papers[:K]
  131.  
  132. with open("table_data.txt", "w") as table_file:
  133. formatted_authors = map(lambda s:s.replace(" ", "-"), authors)
  134. table_file.write("-,{0}\n".format(",".join(formatted_authors)))
  135. for i in range(K):
  136. table_file.write(formatted_authors[i])
  137. for j in range(K):
  138. table_file.write(",{0}".format(A[i,j] if j > i else 0))
  139. table_file.write("\n")
  140.  
  141. #idx = numpy.argsort(papers)
  142. #papers = papers[idx]
  143. #authors = [authors[idx[k]] for k in range(K)]
  144. #P = numpy.zeros((K, K))
  145. #for k in range(K):
  146. #P[idx[k], k] = 1
  147.  
  148. #A = numpy.dot(P.T, numpy.dot(A, P))
  149.  
  150.  
  151.  
  152. # Generate the graph and plot it
  153. #g = nx.from_numpy_matrix(A)
  154.  
  155. #node_labels = dict(zip(range(K), authors))
  156. #g = nx.relabel_nodes(g, node_labels)
  157.  
  158. #pos = nx.graphviz_layout(g)
  159. #pos = { k:(numpy.cos(2*numpy.pi*i/K), numpy.sin(2*numpy.pi*i/K)) for i,k in enumerate(authors) }
  160.  
  161. #edge_labels=dict([((u,v,),int(d['weight'])) for u,v,d in g.edges(data=True)])
  162.  
  163. #nx.draw_networkx_edge_labels(g,pos, edge_labels=edge_labels)
  164. #nx.draw_networkx(g, pos=pos, node_color="white", node_size=0, font_size=9)
  165.  
  166. #ax=plt.gca()
  167. #draw_network(g,pos,ax)
  168. #ax.autoscale()
  169.  
  170. #plt.axis('equal')
  171. #plt.axis('off')
  172. #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement