FDSRashid commited on
Commit
5b4a284
ยท
verified ยท
1 Parent(s): 84acc5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -34
app.py CHANGED
@@ -61,37 +61,7 @@ def value_to_hex(value):
61
  return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
62
 
63
 
64
- def visualize_subTaraf(taraf_num, hadith_str, yaxis):
65
- hadith_list = hadith_str.split(',')
66
- hadith_list = [hadith.strip() for hadith in hadith_list]
67
- hadiths = np.array([], dtype=int)
68
- for hadith in hadith_list:
69
- if '-' in hadith:
70
- if hadith.count('-') > 1:
71
- #print('Please use only one Dash mark!')
72
- raise gr.Error('Please use only one Dash mark!')
73
- hadith_multi = hadith.strip().split('-')
74
- if any([not had.isnumeric() for had in hadith_multi]):
75
- #print('Invalid Begining')
76
- raise gr.Error('Invalid Begining')
77
- elif len(hadith_multi) != 2:
78
- #print('Two numbers for a range of Hadith numbers please!')
79
- raise gr.Error('Two numbers for a range of Hadith numbers please!')
80
- hadith_multi = [int(had) for had in hadith_multi]
81
- hadiths = np.append(hadiths, np.arange(hadith_multi[0], hadith_multi[1]))
82
- elif hadith.isnumeric():
83
- hadiths = np.append(hadiths, int(hadith))
84
- else:
85
- #print('Invalid Data format!')
86
- raise gr.Error("Invalid Data format!")
87
-
88
- taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
89
- num_hadith = taraf.shape[0]
90
- taraf['Index'] = np.arange(num_hadith)
91
- sub_taraf = taraf[taraf['Index'].isin(hadiths)]
92
- hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
93
- isnad_hadith = isnad_info[hadith_cleaned]
94
- return hadiths
95
 
96
 
97
  def visualize_isnad(taraf_num, yaxis):
@@ -182,6 +152,96 @@ def visualize_isnad(taraf_num, yaxis):
182
  allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df.drop('Hdth Num', axis=1)
183
 
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  def taraf_booknum(taraf_num):
186
  taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
187
  return taraf[['matn', 'Book_ID', 'Hadith Number', 'Book_Name', 'Author']]
@@ -227,9 +287,6 @@ def visualize_hadith_isnad(df, yaxis):
227
  node_name = 'ูู„ุงู†'
228
  if node == '99999':
229
  net.add_node(node, font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 70)
230
- # elif node in end_nodes:
231
- # end_matn_info = df[df["End Transmitter ID"] == node]
232
- # 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} \n Hadith {" ".join(end_matn_info["Hadith Number"].tolist())}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
233
  else:
234
  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}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
235
  for _, row in isnad_hadith.iterrows():
@@ -256,6 +313,12 @@ with gr.Blocks() as demo:
256
  taraf_num = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
257
  btn_num = gr.Button('Retrieve')
258
  btn_num.click(fn=taraf_booknum, inputs = [taraf_num], outputs= [gr.DataFrame(wrap=True)])
 
 
 
 
 
 
259
  with gr.Tab('Select Hadith Isnad Visualizer'):
260
  yyaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
261
  hadith_selection = gr.Dataframe(
 
61
  return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
62
 
63
 
64
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
 
67
  def visualize_isnad(taraf_num, yaxis):
 
152
  allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df.drop('Hdth Num', axis=1)
153
 
154
 
155
+
156
+ def visualize_subTaraf(taraf_num, hadith_str, yaxis):
157
+ hadith_list = hadith_str.split(',')
158
+ hadith_list = [hadith.strip() for hadith in hadith_list]
159
+ hadiths = np.array([], dtype=int)
160
+ for hadith in hadith_list:
161
+ if '-' in hadith:
162
+ if hadith.count('-') > 1:
163
+ #print('Please use only one Dash mark!')
164
+ raise gr.Error('Please use only one Dash mark!')
165
+ hadith_multi = hadith.strip().split('-')
166
+ if any([not had.isnumeric() for had in hadith_multi]):
167
+ #print('Invalid Begining')
168
+ raise gr.Error('Invalid Begining')
169
+ elif len(hadith_multi) != 2:
170
+ #print('Two numbers for a range of Hadith numbers please!')
171
+ raise gr.Error('Two numbers for a range of Hadith numbers please!')
172
+ hadith_multi = [int(had) for had in hadith_multi]
173
+ hadiths = np.append(hadiths, np.arange(hadith_multi[0], hadith_multi[1]))
174
+ elif hadith.isnumeric():
175
+ hadiths = np.append(hadiths, int(hadith))
176
+ else:
177
+ #print('Invalid Data format!')
178
+ raise gr.Error("Invalid Data format!")
179
+
180
+ taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
181
+ num_hadith = taraf.shape[0]
182
+ if np.max(hadiths) > num_hadith:
183
+ raise gr.Error(f'Hadith index outside of range. Total Number of Hadith in this Taraf: {num_hadith}')
184
+ taraf['Index'] = np.arange(num_hadith)
185
+ sub_taraf = taraf[taraf['Index'].isin(hadiths)]
186
+
187
+ taraf_hadith = sub_taraf['bookid_hadithid'].to_list()
188
+ taraf_hadith_split = [i.split('_') for i in taraf_hadith]
189
+
190
+ hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
191
+ isnad_hadith = isnad_info[hadith_cleaned]
192
+ subtaraf_filter = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split))
193
+ isnad_hadith = isnad_hadith[subtaraf_filter]
194
+
195
+ isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
196
+ isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
197
+ isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
198
+ isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
199
+
200
+ G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
201
+ isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
202
+ x_stretch = 4
203
+ y_stretch = 4
204
+ net = Network(directed =True)
205
+
206
+ for node, pos in isnad_pos.items():
207
+ node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)]
208
+ student_narrations = node_info['Number of Narrations'].to_list()
209
+ if len(student_narrations):
210
+ student_narrations = student_narrations[0]
211
+ else:
212
+ student_narrations = 1
213
+ student_gen = node_info['Generation'].to_list()
214
+ if len(student_gen):
215
+ student_gen = student_gen[0]
216
+ else:
217
+ student_gen = -1
218
+ student_rank = node_info["Narrator Rank"].to_list()
219
+ if len(student_rank):
220
+ student_rank = student_rank[0]
221
+ else:
222
+ student_rank = 'ูู„ุงู†'
223
+ node_name = node_info['Famous Name'].to_list()
224
+ if len(node_name):
225
+ node_name = node_name[0]
226
+ else:
227
+ node_name = 'ูู„ุงู†'
228
+ if node == '99999':
229
+ net.add_node(node, font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 70)
230
+ else:
231
+ 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}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
232
+ for _, row in isnad_hadith.iterrows():
233
+ source = row['Source']
234
+ target = row['Destination']
235
+ net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
236
+ net.toggle_physics(False)
237
+ html = net.generate_html()
238
+ html = html.replace("'", "\"")
239
+ return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
240
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
241
+ allow-scripts allow-same-origin allow-popups
242
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
243
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
244
+
245
  def taraf_booknum(taraf_num):
246
  taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
247
  return taraf[['matn', 'Book_ID', 'Hadith Number', 'Book_Name', 'Author']]
 
287
  node_name = 'ูู„ุงู†'
288
  if node == '99999':
289
  net.add_node(node, font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 70)
 
 
 
290
  else:
291
  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}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
292
  for _, row in isnad_hadith.iterrows():
 
313
  taraf_num = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
314
  btn_num = gr.Button('Retrieve')
315
  btn_num.click(fn=taraf_booknum, inputs = [taraf_num], outputs= [gr.DataFrame(wrap=True)])
316
+ with gr.Tab('Sub Taraf Visualizer'):
317
+ taraf_num = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
318
+ Yaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
319
+ hadith_str = gr.Textbox(label='Hadith Selection', info='Choose which range of Hadith you would like visualized from the Taraf')
320
+ btn_sub = gr.Button('Visualize')
321
+ btn_sub.click(fn=visualize_subTaraf, inputs = [taraf_num, hadith_str, Yaxis], outputs=[gr.HTML()])
322
  with gr.Tab('Select Hadith Isnad Visualizer'):
323
  yyaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
324
  hadith_selection = gr.Dataframe(