Hello I have another netowrkx question in python. I am attempting to use the following function to create a visualization but getting keyerror whenever I try to run the code below:
G = nx.Graph()
G = nx.from_pandas_edgelist(
df_edges,'user_id_x','user_id_y')
node_attr = df_nodes.set_index('user_id')
nx.set_node_attributes(G, node_attr)
communities = community.greedy_modularity_communities(G)
modularity_dict = {} # Create a blank dictionary
for i,c in enumerate(communities): # Loop through the list of communities, keeping track of the number for the community
for name in c: # Loop through each person in a community
modularity_dict[name] = i # Create an entry in the dictionary for the person, where the value is which group they belong to.
# Now you can add modularity information like we did the other metrics
nx.set_node_attributes(G, modularity_dict, 'modularity')
def visualize(identity_id,html_name):
#create subgraph using the provided identity_id
classx = [n for n in G.nodes() if G.nodes[n]['modularity'] ==
G.nodes[identity_id]['modularity']]
SG = G.subgraph(classx)
#instantiate the Network object
N = Network(height='800px', width='100%', bgcolor='#ffffff',
font_color='black',notebook = True, directed=False)
#this line effects the physics of the html File
N.barnes_hut(spring_strength=0.006)
for n in SG:
if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE')): # assign color to nodes based on cust status
color = 'green'
shape = 'square'
if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='CLOSED')): # assign color to nodes based on cust status
color = 'red'
shape = 'square'
elif SG.nodes[n]['category']=='app':# assign shape to nodes based on cust versus app
color = 'blue'
shape = 'triangle'
N.add_node(n, label=n, color=color,shape = shape)
for e in SG.edges:
if e in SG.edges: # add the edges to the graph
color = 'black'
width = 2
N.add_edge(e[0],e[1],color=color, width=width)
N.show_buttons(filter_=True)
N.show(f'subgraph_{html_name}.html') # save a html file in current dir
print(f"Number of nodes in this customer's device based network: {SG.number_of_nodes()}")
print('\nList of associated customers and applications based on device:\n')
display(df_nodes[(df_nodes['user_id'].isin(list(SG.nodes)))])#print list of customer_ids that are associated with the customer
network_df= df_nodes[(df_nodes['user_id'].isin(list(SG.nodes)))]
visualize('b572f4d3-2c69-48fa-91fe-849fa55d1be7','customerA')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-124-d0c830d4c4a1> in <module>
----> 1 visualize('b572f4d3-2c69-48fa-91fe-849fa55d1be7','customerA')
<ipython-input-123-5d1d5e9d24a4> in visualize(identity_id, html_name)
14
15 for n in SG:
---> 16 if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE')): # assign color to nodes based on cust status
17 color = 'green'
18 shape = 'square'
KeyError: 'category'
This has been very frustrating as this block of code worked perfectly last week, And I am positive I did not change anything critical.
And for those of you that are curious this is what the df_nodes DataFrame looks like:
It is particularly strange because when I print out the node attribute outside of the function it returns the expected result.
Thank you in advance!