-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_plot.py
More file actions
55 lines (45 loc) · 1.85 KB
/
Copy pathfiles_plot.py
File metadata and controls
55 lines (45 loc) · 1.85 KB
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
import json
import matplotlib.pyplot as plt
import networkx as nx
from collections import defaultdict
from matplotlib.lines import Line2D
# Load the data
with open('topic_rails.json', 'r') as file:
data = json.load(file)
# Initialize a graph
G = nx.Graph()
# Initialize a dictionary to store related files per user
files_per_user = defaultdict(set)
# Process the data
for topic in data:
entities = topic['entities_in_topic']
for collaborator in topic['collaborators']:
# Extract the user name without the role
user = collaborator.split(' (')[0]
files_per_user[user].update(entities)
for entity in entities:
G.add_edge(user, entity)
# Create labels with the number of related files
labels = {user: f"{user} ({len(files)})" for user, files in files_per_user.items()}
labels.update({file: file for file in G.nodes if file not in labels})
# Assign colors to nodes
node_colors = []
for node in G.nodes:
if node in files_per_user:
node_colors.append('skyblue') # User nodes
else:
node_colors.append('lightgreen') # File nodes
# Create a bipartite graph layout
users = list(files_per_user.keys())
files = [node for node in G.nodes if node not in users]
pos = nx.bipartite_layout(G, users)
# Plotting
plt.figure(figsize=(12, 8))
nx.draw(G, pos, labels=labels, with_labels=True, node_size=3000, node_color=node_colors, font_size=10, font_color='black', font_weight='bold', edge_color='gray')
# Create legend with circle shapes and bigger size
user_patch = Line2D([0], [0], marker='o', color='w', label='User', markersize=15, markerfacecolor='skyblue')
file_patch = Line2D([0], [0], marker='o', color='w', label='File', markersize=15, markerfacecolor='lightgreen')
plt.legend(handles=[user_patch, file_patch], loc='best')
plt.title('Related Files per User')
plt.savefig('related_files_per_user_graph.png')
plt.show()