nickil commited on
Commit
19da5be
β€’
1 Parent(s): e3b35ec

Update graph

Browse files
Files changed (3) hide show
  1. app.py +9 -7
  2. network.py +30 -15
  3. requirements.txt +2 -1
app.py CHANGED
@@ -15,16 +15,18 @@ from network import analysis
15
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
16
  # with gr.Tab("πŸ“ About"):
17
  # # gr.Markdown(ABOUT_TEXT)
 
 
 
 
 
 
 
18
  with gr.Row():
19
  with gr.Column():
20
- gr.Label("πŸ“Š Visually explore witches family data")
21
  gr.Markdown("You can use drag-and-drop operations to explore the data, start your analysis now! In the given query, we see where the accused witches reside after filtering on the gender label (here, Female) and we show a breakdown of the class!")
22
  pyg_app = get_html_on_gradio(df, spec="./config.json")
23
  gr.HTML(pyg_app)
24
- with gr.Row():
25
- with gr.Column():
26
- gr.Label("πŸ‘ͺ Visualize witches family connections")
27
- gr.Markdown("You can see the various parent-child-sibling-spouse relationships of the accused witches!")
28
- gr.HTML(analysis)
29
-
30
  demo.launch(app_kwargs={"routes": [PYGWALKER_ROUTE]}, share=True).queue()
 
15
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
16
  # with gr.Tab("πŸ“ About"):
17
  # # gr.Markdown(ABOUT_TEXT)
18
+
19
+ with gr.Row():
20
+ with gr.Column():
21
+ gr.Label("πŸ‘ͺ Visualize witches family connections", show_label=False)
22
+ gr.Markdown("You can see the various parent-child-sibling-spouse relationships of the accused witches!")
23
+ gr.HTML(analysis)
24
+
25
  with gr.Row():
26
  with gr.Column():
27
+ gr.Label("πŸ“Š Visually explore witches family data", show_label=False)
28
  gr.Markdown("You can use drag-and-drop operations to explore the data, start your analysis now! In the given query, we see where the accused witches reside after filtering on the gender label (here, Female) and we show a breakdown of the class!")
29
  pyg_app = get_html_on_gradio(df, spec="./config.json")
30
  gr.HTML(pyg_app)
31
+
 
 
 
 
 
32
  demo.launch(app_kwargs={"routes": [PYGWALKER_ROUTE]}, share=True).queue()
network.py CHANGED
@@ -9,19 +9,33 @@ from data import df
9
  def analysis():
10
  G = nx.DiGraph()
11
 
12
- # Define the columns to consider for relationships
13
  relationship_columns = ['father', 'sibling', 'spouse', 'mother', 'child']
14
 
15
- G.add_nodes_from(df["itemLabel"])
16
-
17
  for index, row in df.iterrows():
18
- main_entity = row['itemLabel']
19
- for relationship in relationship_columns:
20
- if pd.notna(row[relationship]):
21
- G.add_edge(main_entity, row[relationship], relationship=str(relationship), label=relationship)
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- plt.figure(figsize=(20, 10)) # Set the size of the plot
24
- # pos = nx.kamada_kawai_layout(G) # Compute the positions of the nodes
 
 
 
 
25
 
26
  # # Draw the nodes and edges with labels
27
  # nx.draw(G, pos, with_labels=True, node_size=20, node_color='skyblue', font_size=10, font_color='black', font_weight='bold', alpha=0.7)
@@ -35,27 +49,28 @@ def analysis():
35
  # plt.show()
36
 
37
 
38
- net = Network(width="1200px", height="800px", bgcolor="#222222", font_color="white", directed=True)
39
  net.from_nx(G)
40
 
41
  for node in net.nodes:
42
- node["title"] = node["id"]
43
  node["value"] = len(G[node["id"]])
44
-
45
 
46
  for edge in net.edges:
47
  # Set edge title to the relationship from your graph
48
- relationship = G[edge["from"]][edge["to"]].get("relationship", "Unknown")
49
  edge["title"] = relationship # This will show as a tooltip
50
  edge["color"] = "blue"
51
  edge["width"] = 2 if relationship != "Unknown" else 1
52
 
 
53
  html = net.generate_html()
54
  #need to remove ' from HTML
55
  html = html.replace("'", "\"")
56
 
57
- return f"""<iframe style="width: 100%; height: 800px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
58
  display-capture; encrypted-media;" sandbox="allow-modals allow-forms
59
  allow-scripts allow-same-origin allow-popups
60
  allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
61
- allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
 
9
  def analysis():
10
  G = nx.DiGraph()
11
 
 
12
  relationship_columns = ['father', 'sibling', 'spouse', 'mother', 'child']
13
 
14
+ # G.add_nodes_from(df["itemLabel"])
15
+ inf_labels = ["residence", "occupation", "class"]
16
  for index, row in df.iterrows():
17
+ if row["itemLabel"] not in G.nodes():
18
+ G.add_node(row["itemLabel"])
19
+ information = ""
20
+ for i in inf_labels:
21
+ if pd.notna(row[i]):
22
+ information+=f"{i[:-5]}: {row[i]}\n"
23
+ G.nodes[row["itemLabel"]]["attr"]=information
24
+
25
+ for index, row in df.iterrows():
26
+ main_entity = row['itemLabel']
27
+ for relationship in relationship_columns:
28
+ if pd.notna(row[relationship]) and not G.has_edge(main_entity, row[relationship]) and not G.has_edge(row[relationship], main_entity):
29
+ # if pd.notna(row[relationship]):
30
+
31
+ G.add_edge(row[relationship], main_entity, label=str(relationship)+ "_of")
32
 
33
+ for x in G.nodes():
34
+ if "attr" not in G.nodes[x]:
35
+ G.nodes[x]["attr"]=""
36
+
37
+ # plt.figure(figsize=(50, 20)) # Set the size of the plot
38
+ pos = nx.kamada_kawai_layout(G) # Compute the positions of the nodes
39
 
40
  # # Draw the nodes and edges with labels
41
  # nx.draw(G, pos, with_labels=True, node_size=20, node_color='skyblue', font_size=10, font_color='black', font_weight='bold', alpha=0.7)
 
49
  # plt.show()
50
 
51
 
52
+ net = Network(width="1500px", height="1000px", bgcolor="#FFFFFF", font_color="black", directed=True)
53
  net.from_nx(G)
54
 
55
  for node in net.nodes:
56
+ node["title"] = G.nodes[node["id"]]["attr"]
57
  node["value"] = len(G[node["id"]])
58
+ node["font"]={"size": 20}
59
 
60
  for edge in net.edges:
61
  # Set edge title to the relationship from your graph
62
+ relationship = G[edge["from"]][edge["to"]].get("label", "Unknown")
63
  edge["title"] = relationship # This will show as a tooltip
64
  edge["color"] = "blue"
65
  edge["width"] = 2 if relationship != "Unknown" else 1
66
 
67
+
68
  html = net.generate_html()
69
  #need to remove ' from HTML
70
  html = html.replace("'", "\"")
71
 
72
+ return f"""<iframe style="width: 100%; height: 800px; margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
73
  display-capture; encrypted-media;" sandbox="allow-modals allow-forms
74
  allow-scripts allow-same-origin allow-popups
75
  allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
76
+ allowpaymentrequest="" frameBorder="0" srcdoc='{html}'></iframe>"""
requirements.txt CHANGED
@@ -4,4 +4,5 @@ gradio
4
  pygwalker
5
  datasets
6
  pyvis
7
- networkx
 
 
4
  pygwalker
5
  datasets
6
  pyvis
7
+ networkx
8
+ scipy