FDSRashid commited on
Commit
e144c2d
·
verified ·
1 Parent(s): a08c056

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -27
app.py CHANGED
@@ -40,39 +40,73 @@ def subsetEdges(city, fstyear, lstyear):
40
  info = taraf_info[(taraf_info['Year'] >= fstyear) & (taraf_info['City'] == city) & (taraf_info['Year'] <= lstyear)]
41
  narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
42
  return narrators
43
- def splitIsnad(dataframe):
44
- teacher_student =dataframe['Edge_Name'].str.split(' TO ')
45
- dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
46
- dataframe['Student'] = teacher_student.apply(lambda x: x[1])
47
- return dataframe
48
 
49
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def network_visualizer(yaxis, city, fstyear,lastyr, num_nodes):
51
  edges = subsetEdges(city, fstyear, lastyr)
52
- edges_split = splitIsnad(edges).reset_index()
 
 
 
 
 
 
 
53
  #.groupby(['Teacher', 'Student']).sum()
54
- if edges_split.shape[0] > num_nodes:
55
- edge_15 = edges_split.sort_values(by=yaxis, ascending=False).head(num_nodes)
56
- else:
57
- edge_15 = edges_split.copy()
58
-
59
- net = Network(directed =True, select_menu=True, cdn_resources='remote', filter_menu=True)
60
-
61
-
 
 
 
 
 
 
 
 
 
62
 
63
- for _, row in edge_15.iterrows():
64
- source = row['Teacher']
65
- target = row['Student']
66
- attribute_value = row[yaxis]
67
- edge_color = value_to_hex(attribute_value)
68
- teacher_info = narrator_bios[narrator_bios['Rawi ID'] == row['Teacher_ID']]
69
- student_info = narrator_bios[narrator_bios['Rawi ID'] == row['Student_ID']]
70
- teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
71
- student_narrations = student_info['Number of Narrations'].to_list()[0]
72
 
73
- net.add_node(source, color=value_to_hex(teacher_narrations), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{teacher_narrations}")
74
- net.add_node(target, color=value_to_hex(student_narrations), font = {'size': 20, 'color': 'red'}, label = f"{target}\n{student_narrations}")
75
- net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{yaxis}:{attribute_value}")
76
 
77
 
78
  net.barnes_hut(gravity=-5000, central_gravity=0.1, spring_length=200)
 
40
  info = taraf_info[(taraf_info['Year'] >= fstyear) & (taraf_info['City'] == city) & (taraf_info['Year'] <= lstyear)]
41
  narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
42
  return narrators
 
 
 
 
 
43
 
44
+ def get_node_info(node):
45
+ node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)]
46
+ student_narrations = node_info['Number of Narrations'].to_list()
47
+ if len(student_narrations):
48
+ student_narrations = student_narrations[0]
49
+ else:
50
+ student_narrations = 1
51
+ student_gen = node_info['Generation'].to_list()
52
+ if len(student_gen):
53
+ student_gen = student_gen[0]
54
+ else:
55
+ student_gen = -1
56
+ student_rank = node_info["Narrator Rank"].to_list()
57
+ if len(student_rank):
58
+ student_rank = student_rank[0]
59
+ else:
60
+ student_rank = 'فلان'
61
+ node_name = node_info['Famous Name'].to_list()
62
+ if len(node_name):
63
+ node_name = node_name[0]
64
+ else:
65
+ node_name = 'فلان'
66
+ return node_info,student_narrations,student_gen, student_rank, node_name
67
+
68
  def network_visualizer(yaxis, city, fstyear,lastyr, num_nodes):
69
  edges = subsetEdges(city, fstyear, lastyr)
70
+
71
+ G = nx.from_pandas_edgelist(edges, source = 'Teacher_ID', target = 'Student_ID', create_using = nx.DiGraph())
72
+ nodes = list(G.nodes)
73
+ node_reports = [narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)['Number of Narrations'] for x in nodes]
74
+ nodes_df = pd.DataFrame({'Node': nodes, 'Report': node_reports}).sort_values('Report').head(num_nodes)
75
+ nodes_remove = list(set(nodes) - set(nodes_df['Node'].to_list()))
76
+ [G.remove_nodes_from(nodes_remove)]
77
+
78
  #.groupby(['Teacher', 'Student']).sum()
79
+ # if edges_split.shape[0] > num_nodes:
80
+ # edge_15 = edges_split.sort_values(by=yaxis, ascending=False).head(num_nodes)
81
+ # else:
82
+ # edge_15 = edges_split.copy()
83
+
84
+ net = Network(directed =True, select_menu=True, cdn_resources='remote')
85
+ for node in G.nodes:
86
+ node_info,student_narrations,student_gen, student_rank, node_name = get_node_info(node)
87
+ if node == 99999:
88
+ net.add_node(node, font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', size= 70)
89
+ else:
90
+ net.add_node(node, font = {'size':30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{node_name} \n {student_rank} \n ID: {node} - Gen {student_gen}', size= 50)
91
+ for edge in G.edges:
92
+ row = edges[(edges['Source'] == edge[0]) & (edges['Destination'] == edge[1])].iloc[0]
93
+ source = row['Teacher_ID']
94
+ target = row['Student_ID']
95
+ net.add_edge(source, target, color = value_to_hex(int(row[yaxis])), label = f"{row[yaxis]}")
96
 
97
+ # for _, row in edge_15.iterrows():
98
+ # source = row['Teacher']
99
+ # target = row['Student']
100
+ # attribute_value = row[yaxis]
101
+ # edge_color = value_to_hex(attribute_value)
102
+ # teacher_info = narrator_bios[narrator_bios['Rawi ID'] == row['Teacher_ID']]
103
+ # student_info = narrator_bios[narrator_bios['Rawi ID'] == row['Student_ID']]
104
+ # teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
105
+ # student_narrations = student_info['Number of Narrations'].to_list()[0]
106
 
107
+ # net.add_node(source, color=value_to_hex(teacher_narrations), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{teacher_narrations}")
108
+ # net.add_node(target, color=value_to_hex(student_narrations), font = {'size': 20, 'color': 'red'}, label = f"{target}\n{student_narrations}")
109
+ # net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{yaxis}:{attribute_value}")
110
 
111
 
112
  net.barnes_hut(gravity=-5000, central_gravity=0.1, spring_length=200)