mardi 26 avril 2016

Using Python Networkx and Numpy.Choice & Getting ValueError

Language: Python 2.x Packages: Networkx, Numpy

My code picks a random node from my graph then does the following:

1- if it's an isolate, go back to the beginning of the while loop and pick another random node

2- if it is not an isolate, get it's neighbors

3 - if it has only 1 neighbor then the random neighbor you pick is that only neighbor

4-if it has more than one neighbor, then pick a random neighbor out of the choices available

5- that random choice, check to see if it has neighbors. Just like above.. if it has one than set the neighbor of neighbor to be that one neighbor

6-if it had more than one then pick a random neighbor of neighbor

7- connect the neighbor of my neighbor to me (the original random node)

This code HAD been working until I introduced some minor changes in another part of my program. I know all the syntax works, but I simply cannot get passed the error. Below is my code and the error.

while i <= self.iterations:
            node_list = nx.nodes(self.origin_network)


            random_node = numpy.random.choice(node_list)
            #print (" This random node has no neighbors:", self.origin_network.neighbors(random_node))
            if nx.is_isolate(self.origin_network, random_node) == True:
                i += 1
                print (" This random node has no neighbors:", self.origin_network.neighbors(random_node), "on turn", i)
                continue
            else:
                Neighbs = self.origin_network.neighbors(random_node)

                if len(Neighbs) == 1:
                    print ("The neighbor has no additional neighbors", "on turn", i)
                    random_neighb = Neighbs
                else:
                    random_neighb = numpy.random.choice(Neighbs) ***#This is line 108 which the error references***

                neighbs_of_neighb = self.origin_network.neighbors(random_neighb)
                if len(neighbs_of_neighb) == 1:
                    print ("This neighbor has only the original neighbor on turn", i)
                    random_NofN = neighbs_of_neighb
                    self.origin_network.add_edge(random_node, random_NofN)
                else:
                    random_NofN = numpy.random.choice(neighbs_of_neighb)
                    self.origin_network.add_edge(random_node, random_NofN)
                    print "success"
            i += 1

The error I receive is:

self.triadic_method(self.origin_network , iteration, sim_number)

File "D:\network.py", line 108, in triadic_method random_neighb = numpy.random.choice(Neighbs) File "mtrand.pyx", line 1121, in mtrand.RandomState.choice (numpy\random\mtrand\mtrand.c:12473) File "mtrand.pyx", line 945, in mtrand.RandomState.randint (numpy\random\mtrand\mtrand.c:10732) ValueError: low >= high

line 108 which the error references is this one:

random_neighb = numpy.random.choice(Neighbs) #This is line 108 which the error references




Aucun commentaire:

Enregistrer un commentaire