|
import pandas as pd |
|
import networkx as nx |
|
import matplotlib.pyplot as plt |
|
from pyvis.network import Network |
|
|
|
from data import df |
|
|
|
|
|
def analysis(): |
|
G = nx.DiGraph() |
|
|
|
relationship_columns = ['father', 'sibling', 'spouse', 'mother', 'child'] |
|
|
|
|
|
inf_labels = ["residence", "occupation", "class"] |
|
for index, row in df.iterrows(): |
|
if row["itemLabel"] not in G.nodes(): |
|
G.add_node(row["itemLabel"]) |
|
information = "" |
|
for i in inf_labels: |
|
if pd.notna(row[i]): |
|
information+=f"{i}: {row[i]}\n" |
|
G.nodes[row["itemLabel"]]["attr"]=information |
|
|
|
for index, row in df.iterrows(): |
|
main_entity = row['itemLabel'] |
|
for relationship in relationship_columns: |
|
if pd.notna(row[relationship]) and not G.has_edge(main_entity, row[relationship]) and not G.has_edge(row[relationship], main_entity): |
|
|
|
|
|
G.add_edge(row[relationship], main_entity, label=str(relationship)+ "_of") |
|
|
|
for x in G.nodes(): |
|
if "attr" not in G.nodes[x]: |
|
G.nodes[x]["attr"]="" |
|
|
|
|
|
pos = nx.kamada_kawai_layout(G) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
net = Network(width="1500px", height="1000px", bgcolor="#FFFFFF", font_color="black", directed=True) |
|
net.from_nx(G) |
|
|
|
for node in net.nodes: |
|
node["title"] = G.nodes[node["id"]]["attr"] |
|
node["value"] = len(G[node["id"]]) |
|
node["font"]={"size": 20} |
|
|
|
for edge in net.edges: |
|
|
|
relationship = G[edge["from"]][edge["to"]].get("label", "Unknown") |
|
edge["title"] = relationship |
|
edge["color"] = "blue" |
|
edge["width"] = 2 if relationship != "Unknown" else 1 |
|
|
|
html = net.generate_html() |
|
|
|
html = html.replace("'", "\"") |
|
|
|
return f"""<iframe style="width: 100%; height: 800px; margin:0 auto" name="result" allow="midi; geolocation; microphone; camera; |
|
display-capture; encrypted-media;" sandbox="allow-modals allow-forms |
|
allow-scripts allow-same-origin allow-popups |
|
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen="" |
|
allowpaymentrequest="" frameBorder="0" srcdoc='{html}'></iframe>""" |
|
|