Original ticket https://networkx.lanl.gov/trac/ticket/47
Reported 2006-06-07 by ddiamantaras, assigned to @hagberg.
The following is an error message produced by the code below. Background: I am an economist, and learning how to use python for social network formation simulation. I am working currently with a co-author on a particular network formation process, using Networkx 0.29, with numpy 0.9.5 installed, on a Fedora Core 5 machine. Lots of stuff goes well, but we have the problem that when the network has nodes with degree zero, the degree() and clustering() give errors. As an illustration, below (with clustering we get the same kind of error).
I have not reported a bug to an open source program before---apologies if this is not optimal. In such a case, I will be happy to provide missing details. I highly appreciate your work on this module.
Dimitrios Diamantaras
'''
File "cbnetsim.py", line 77, in ?
node_degree = NX.degree(sps,k)
File "buil/bdist.linux-i686/egg/networkx/base.py", line 228, in degree
File "buil/bdist.linux-i686/egg/networkx/base.py", line 813, in degree
TypeError: iteration over non-sequence
'''
Input was -5, 10; this resulted in a network with some nodes with degree zero; the first such node encountered terminated output with the error shown above.
Code:
'''
!/usr/bin/env python
import networkx as NX
import networkx.drawing as NXD
from pylab import *
import random as R
"""
Main simulation process. Takes input from a file called
cbninput.txt. Simulates network formation under case-based
decision making.
"""
n = 25 # for now
"""
Ask: generate randomly input values, or read them from input.txt?
If yes, then run the random processes below, which writes input.txt.
Read input.txt to proceed.
"""
def createinput(m = n):
mm = float(raw_input("Enter average for delta: "))
sd = float(raw_input("Enter standard deviation for delta: "))
nodedata = zeros((m, m), Float)
infile = open('input.txt', 'w')
for i in xrange(m):
for j in xrange(m):
nodedata[i,j] = R.gauss(mm, sd)
if i == m-1 and j == m-1:
infile.write(repr(nodedata[i,j]))
else:
infile.write(repr(nodedata[i,j])+ ",")
infile.close()
"""Ask if input should be created"""
answer = raw_input("Do you want to create a random input file? [y,n]: ")
if answer == 'y':
createinput()
else:
pass
"""Read the deltas from the file"""
delta = zeros((n,n), Float)
deltathree = []
inputter = open('input.txt', 'r')
deltaone = inputter.read()
inputter.close()
deltatwo = deltaone.split(',')
for h in xrange(len(deltatwo)):
deltathree.append(float(deltatwo[h]))
delta = array(deltathree)
delta.shape = (n,n)
Now compute the strongly pairwise stable network from the delta data
sps = NX.Graph()
for i in xrange(n):
for j in xrange(i,n):
if delta[i,j] < 0:
pass
elif delta[j,i] < 0:
pass
else:
sps.add_edge(i,j)
NXD.draw_spectral(sps)
show()
NX.write_edgelist(sps, 'sps-output.txt')
out = open('sps-output.txt', 'a')
out.writelines(['\nThe array of delta values is:\n\n', repr(delta), '\n\n'])
degree_sum = 0
sps_value = 0
for k in xrange(0,n):
node_degree = NX.degree(sps,k)
node_clust = NX.clustering(sps,k)
out.writelines(['Node ', str(k), ' has degree ', str(node_degree), ' and clustering ', str(node_clust), '.\n'])
degree_sum += node_degree
for h in xrange(k+1,n):
if sps.has_edge(k,h):
sps_value = delta[k,h] + delta[h,k] + sps_value
out.writelines(['\n', 'The average node degree is ', str(degree_sum/float(n)), '.\n'])
out.writelines(['\n', 'The clustering for the network is ', str(NX.average_clustering(sps)), '.\n\n', 'The value of the SPS network is ', str(sps_value), '\n\nThe edge percentage of the SPS network is ', str(sps.size()/(0.5_n_(n-1)))])
out.close()
'''