I want to generate 10 random graphs with different node attributes but with the same underlying configuration (4 nodes and the same edge arrangements). Randomness is associated with the values of the node attributes only.
Each node has two attributes: expertise_level and innovation_level. The objective is to have 10 graphs, each with a random set of values (High/Medium/Low) for these two attributes.
import numpy as np
import networkx as nx
masterG = [] #to store generated graphs in a list
G = nx.path_graph(4) #creating a specific 4-node graph configuration
def randompicking(levels = ["High", "Medium", "Low"]):
abc = np.random.choice(levels, len(list(G)), replace = True, p=[0.33, 0.33, 0.34])
return abc.tolist()
def initializeG():
expertise_level = dict(list(enumerate(randompicking())))
innovation_level = dict(list(enumerate(randompicking())))
nx.set_node_attributes(G, expertise_level, 'expertise_level')
nx.set_node_attributes(G, innovation_level, 'innovation_level')
for i in range(10):
initializeG()
masterG.append(G)
masterG
The problem: When I am running the code manually for generating each graph by unpacking the code inside intializeG() and moving the code outside of this function (i.e., not creating this function but using the inside code in the global environment), the node attributes generated are random (unique) in each graph as desired. However, when I am running the code using the function initializeG() as above, the node attributes generated for each node in each graph (stored in masterG) are exactly the same.
Can anyone explain where the code is going wrong?