-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakeGroups_interactive.py
64 lines (60 loc) · 1.62 KB
/
MakeGroups_interactive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# %%
# Import necessary modules
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from itertools import repeat
from networkx.algorithms import community
# Import dataframe with 2 columns
df = pd.read_json("testdata.json")
df.columns = ['a', 'b']
df.head()
# %%
# Converting the df to a list for network edges
edgesList = df.values.tolist()
print(edgesList)
# Converting the df to a dictionary for network nodes
nodeDict = df.to_dict('list')
print(nodeDict)
# %%
# Creating an empty graph
G = nx.Graph()
# Adding nodes from both lists in the dictionary
# Effectively a merge of the two lists
G.add_nodes_from(nodeDict['a'])
G.add_nodes_from(nodeDict['b'])
# printing out the nodes
G.nodes()
# %%
# Adding the edges from the edges list
G.add_edges_from(edgesList)
G.edges()
# %%
# Draw the graph to screen
nx.draw(G)
plt.show()
# %%
# Generates groups
# TODO check that Size of smallest clique, here 2 is dynamic
comm = list(community.k_clique_communities(G, 2))
print(comm)
# %%
# Splits the communities into two lists
comm1 = list(comm[0])
comm2 = list(comm[1])
print(comm1)
print(comm2)
# %%
# Generates list of the group name
# Replicates the group name to match community list length
group1 = list(repeat(comm1[0], len(comm1)))
group2 = list(repeat(comm2[0], len(comm2)))
# %%
# combines each group name and community list into a dataframe
dfGroup1 = pd.DataFrame(list(zip(group1, comm1)), columns=['Group', 'key'])
dfGroup2 = pd.DataFrame(list(zip(group2, comm2)), columns=['Group', 'key'])
dfGroup1
dfGroup2
# Combines both dataframes into the final dataframe
dfOutput = dfGroup1.append(dfGroup2, ignore_index=True)
dfOutput