mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-07-12 23:10:08 +02:00
Added comments, descriptions and cleaned up.
This commit is contained in:
@ -1,21 +1,31 @@
|
|||||||
import pandas as pd
|
"""Old Module for NetworkX Graphs."""
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
|
import pandas as pd
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
from dash import Dash, Input, Output, dcc, html
|
from dash import Dash, Input, Output, dcc, html
|
||||||
|
|
||||||
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||||
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
||||||
|
|
||||||
test_company = 13 #2213 # 13
|
test_company = 13 # 2213 # 13
|
||||||
|
|
||||||
|
|
||||||
def find_all_company_relations() -> pd.DataFrame:
|
def find_all_company_relations() -> pd.DataFrame:
|
||||||
|
"""Searches for all companies and their relation in the DB.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
pd.DataFrame: _description_
|
||||||
|
"""
|
||||||
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
query_companies = session.query(entities.Company) #.all()
|
query_companies = session.query(entities.Company) # .all()
|
||||||
query_relations = session.query(entities.CompanyRelation) # .all()
|
query_relations = session.query(entities.CompanyRelation) # .all()
|
||||||
|
|
||||||
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
||||||
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
||||||
# print(companies_relations_df)
|
# print(companies_relations_df)
|
||||||
companies_relations_df = companies_relations_df[["relation_id","company_relation_company2_id"]]
|
companies_relations_df = companies_relations_df[
|
||||||
|
["relation_id", "company_relation_company2_id"]
|
||||||
|
]
|
||||||
# print(companies_relations_df)
|
# print(companies_relations_df)
|
||||||
company_name = []
|
company_name = []
|
||||||
connected_company_name = []
|
connected_company_name = []
|
||||||
@ -23,14 +33,22 @@ def find_all_company_relations() -> pd.DataFrame:
|
|||||||
companies_relations_df = companies_relations_df.head()
|
companies_relations_df = companies_relations_df.head()
|
||||||
# print(companies_relations_df)
|
# print(companies_relations_df)
|
||||||
|
|
||||||
for _, row in companies_relations_df.iterrows():
|
for _, row in companies_relations_df.iterrows():
|
||||||
# print(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
# print(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
||||||
# print("TEst")
|
# print("TEst")
|
||||||
company_name.append(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
company_name.append(
|
||||||
|
companies_df.loc[companies_df["company_id"] == row["relation_id"]][
|
||||||
connected_company_name.append(companies_df.loc[companies_df["company_id"] == row["company_relation_company2_id"]]["company_name"].values[0])
|
"company_name"
|
||||||
|
].values[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
connected_company_name.append(
|
||||||
|
companies_df.loc[
|
||||||
|
companies_df["company_id"] == row["company_relation_company2_id"]
|
||||||
|
]["company_name"].values[0]
|
||||||
|
)
|
||||||
# print(connected_company_name)
|
# print(connected_company_name)
|
||||||
|
|
||||||
# print(company_name)
|
# print(company_name)
|
||||||
companies_relations_df["company_name"] = company_name
|
companies_relations_df["company_name"] = company_name
|
||||||
companies_relations_df["connected_company_name"] = connected_company_name
|
companies_relations_df["connected_company_name"] = connected_company_name
|
||||||
@ -38,10 +56,19 @@ def find_all_company_relations() -> pd.DataFrame:
|
|||||||
# print(companies_relations_df)
|
# print(companies_relations_df)
|
||||||
return companies_relations_df
|
return companies_relations_df
|
||||||
|
|
||||||
|
|
||||||
# Plotly figure
|
# Plotly figure
|
||||||
def networkGraph(EGDE_VAR: None) -> go.Figure:
|
def networkGraph(EGDE_VAR: None) -> go.Figure:
|
||||||
|
"""Create a NetworkX Graph.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
EGDE_VAR (None): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Figure: _description_
|
||||||
|
"""
|
||||||
# find_all_company_relations()
|
# find_all_company_relations()
|
||||||
|
|
||||||
edges = []
|
edges = []
|
||||||
for index, row in find_all_company_relations().iterrows():
|
for index, row in find_all_company_relations().iterrows():
|
||||||
edges.append([row["company_name"], row["connected_company_name"]])
|
edges.append([row["company_name"], row["connected_company_name"]])
|
||||||
@ -113,12 +140,11 @@ def networkGraph(EGDE_VAR: None) -> go.Figure:
|
|||||||
"mirror": True,
|
"mirror": True,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
print(nx.eigenvector_centrality(network_graph))
|
print(nx.eigenvector_centrality(network_graph))
|
||||||
measure_vector = {}
|
measure_vector = {}
|
||||||
network_metrics_df = pd.DataFrame()
|
network_metrics_df = pd.DataFrame()
|
||||||
|
|
||||||
|
|
||||||
measure_vector = nx.eigenvector_centrality(network_graph)
|
measure_vector = nx.eigenvector_centrality(network_graph)
|
||||||
network_metrics_df["eigenvector"] = measure_vector.values()
|
network_metrics_df["eigenvector"] = measure_vector.values()
|
||||||
|
|
||||||
@ -150,15 +176,14 @@ app = Dash(__name__)
|
|||||||
app.title = "Dash Networkx"
|
app.title = "Dash Networkx"
|
||||||
# className="networkx_style"
|
# className="networkx_style"
|
||||||
app.layout = html.Div(
|
app.layout = html.Div(
|
||||||
|
style={"width": "49%"},
|
||||||
style={'width': '49%'},
|
children=[
|
||||||
children = [
|
|
||||||
html.I("Write your EDGE_VAR"),
|
html.I("Write your EDGE_VAR"),
|
||||||
html.Br(),
|
html.Br(),
|
||||||
# dcc.Dropdown(['eigenvector', 'degree', 'betweeness', 'closeness'], 'eigenvector', id='metric-dropdown'),
|
# dcc.Dropdown(['eigenvector', 'degree', 'betweeness', 'closeness'], 'eigenvector', id='metric-dropdown'),
|
||||||
dcc.Input(id="EGDE_VAR", type="text", value="K", debounce=True),
|
dcc.Input(id="EGDE_VAR", type="text", value="K", debounce=True),
|
||||||
dcc.Graph(id="my-graph", style={'width': '49%'}),
|
dcc.Graph(id="my-graph", style={"width": "49%"}),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -167,9 +192,18 @@ app.layout = html.Div(
|
|||||||
# Input('metric-dropdown', 'value'),
|
# Input('metric-dropdown', 'value'),
|
||||||
[Input("EGDE_VAR", "value")],
|
[Input("EGDE_VAR", "value")],
|
||||||
)
|
)
|
||||||
def update_output(EGDE_VAR: None) -> None:
|
def update_output(EGDE_VAR: None) -> go.Figure:
|
||||||
|
"""Just Returns the go Figure of Plotly.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
EGDE_VAR (None): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Figure: _description_
|
||||||
|
"""
|
||||||
return networkGraph(EGDE_VAR)
|
return networkGraph(EGDE_VAR)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
"""Main Method to test this page."""
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
@ -4,7 +4,8 @@
|
|||||||
margin-left: 10px;
|
margin-left: 10px;
|
||||||
margin-right: 20px;
|
margin-right: 20px;
|
||||||
border: 1px solid;
|
border: 1px solid;
|
||||||
border-color: blue;
|
border-color: var(--raisin-black);
|
||||||
|
border-radius: 20px;
|
||||||
width: 57%;
|
width: 57%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
@ -15,6 +16,94 @@
|
|||||||
margin-left: 20px;
|
margin-left: 20px;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
border: 1px solid;
|
border: 1px solid;
|
||||||
|
border-color: var(--raisin-black);
|
||||||
|
border-radius: 20px;
|
||||||
width: 37%;
|
width: 37%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
.networkx_style .filter-wrapper {
|
||||||
|
float: left;
|
||||||
|
width: 100%;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .filter-wrapper .filter-wrapper-item {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 10px;
|
||||||
|
width: 31%;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .filter-wrapper .filter-wrapper-item .dropdown_style {
|
||||||
|
padding-bottom: 10px;
|
||||||
|
margin-top: 1px;
|
||||||
|
padding-left: 10px;
|
||||||
|
width: 100%;
|
||||||
|
font-size: '30%'
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .header {
|
||||||
|
text-align: center;
|
||||||
|
align-items: center;
|
||||||
|
align-content: center;
|
||||||
|
vertical-align: middle;
|
||||||
|
padding: 20px;
|
||||||
|
margin: 0px;
|
||||||
|
/* margin: 5px; */
|
||||||
|
color: var(--raisin-black);
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .filter-wrapper .filter-wrapper-item .filter-description {
|
||||||
|
padding: 5px;
|
||||||
|
padding-left: 10px;
|
||||||
|
margin: 0px;
|
||||||
|
/* margin: 5px; */
|
||||||
|
color: var(--raisin-black);
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .switch-style {
|
||||||
|
align-self: left;
|
||||||
|
float: none;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
margin-top: 1px;
|
||||||
|
padding-left: 10px;
|
||||||
|
width: 80%;
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .switch-style .jAjsxw {
|
||||||
|
align-self: left;
|
||||||
|
float: none;
|
||||||
|
display: inline;
|
||||||
|
padding: 0px;
|
||||||
|
margin: 0px;
|
||||||
|
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .graph-style {
|
||||||
|
margin-top: 10px;
|
||||||
|
padding-bottom: 0px;
|
||||||
|
display: inline;
|
||||||
|
margin: 0px;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.networkx_style .graph-style .canvas {
|
||||||
|
padding: 0px;
|
||||||
|
margin: 0px;
|
||||||
|
width: 100%
|
||||||
|
/* background-color: var(--raisin-black); */
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
import dash_cytoscape as cyto
|
|
||||||
from dash import Dash, html
|
|
||||||
|
|
||||||
app = Dash(__name__)
|
|
||||||
|
|
||||||
app.layout = html.Div(
|
|
||||||
[
|
|
||||||
html.P("Dash Cytoscape:"),
|
|
||||||
cyto.Cytoscape(
|
|
||||||
id="cytoscape",
|
|
||||||
elements=[
|
|
||||||
{"data": {"id": "ca", "label": "Canada"}},
|
|
||||||
{"data": {"id": "on", "label": "Ontario"}},
|
|
||||||
{"data": {"id": "qc", "label": "Quebec"}},
|
|
||||||
{"data": {"source": "ca", "target": "on"}},
|
|
||||||
{"data": {"source": "ca", "target": "qc"}},
|
|
||||||
],
|
|
||||||
layout={"name": "breadthfirst"},
|
|
||||||
style={"width": "400px", "height": "500px"},
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run_server(debug=True)
|
|
@ -1,11 +0,0 @@
|
|||||||
# https://pypi.org/project/dashvis/
|
|
||||||
|
|
||||||
import dash
|
|
||||||
from dash import html
|
|
||||||
from dashvis import DashNetwork
|
|
||||||
|
|
||||||
app = dash.Dash()
|
|
||||||
app.layout = html.Div([DashNetwork(1)])
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run_server(debug=True)
|
|
@ -1,13 +1,24 @@
|
|||||||
import pandas as pd
|
"""Old NetworkX Graph which needs to be discarded in the next commits."""
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
|
import pandas as pd
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
from dash import Dash, Input, Output, dcc, html, callback
|
from dash import dcc, html
|
||||||
|
|
||||||
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||||
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
||||||
|
|
||||||
test_company = 13 #2213 # 13
|
test_company = 13 # 2213 # 13
|
||||||
|
|
||||||
|
|
||||||
def find_company_relations(company_id: int) -> pd.DataFrame:
|
def find_company_relations(company_id: int) -> pd.DataFrame:
|
||||||
|
"""_summary_.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
company_id (int): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
pd.DataFrame: _description_
|
||||||
|
"""
|
||||||
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
query_companies = session.query(entities.Company)
|
query_companies = session.query(entities.Company)
|
||||||
query_relations = session.query(entities.CompanyRelation)
|
query_relations = session.query(entities.CompanyRelation)
|
||||||
@ -15,24 +26,43 @@ def find_company_relations(company_id: int) -> pd.DataFrame:
|
|||||||
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
||||||
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
||||||
|
|
||||||
companies_relations_df = companies_relations_df.loc[companies_relations_df["relation_id"] == company_id,:][["relation_id","company_relation_company2_id"]]
|
companies_relations_df = companies_relations_df.loc[
|
||||||
|
companies_relations_df["relation_id"] == company_id, :
|
||||||
|
][["relation_id", "company_relation_company2_id"]]
|
||||||
|
|
||||||
company_name = []
|
company_name = []
|
||||||
connected_company_name = []
|
connected_company_name = []
|
||||||
|
|
||||||
for _, row in companies_relations_df.iterrows():
|
for _, row in companies_relations_df.iterrows():
|
||||||
# print(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
# print(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
||||||
company_name.append(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
company_name.append(
|
||||||
connected_company_name.append(companies_df.loc[companies_df["company_id"] == row["company_relation_company2_id"]]["company_name"].values[0])
|
companies_df.loc[companies_df["company_id"] == row["relation_id"]][
|
||||||
|
"company_name"
|
||||||
|
].values[0]
|
||||||
|
)
|
||||||
|
connected_company_name.append(
|
||||||
|
companies_df.loc[
|
||||||
|
companies_df["company_id"] == row["company_relation_company2_id"]
|
||||||
|
]["company_name"].values[0]
|
||||||
|
)
|
||||||
|
|
||||||
# print(company_name)
|
# print(company_name)
|
||||||
companies_relations_df["company_name"] = company_name
|
companies_relations_df["company_name"] = company_name
|
||||||
companies_relations_df["connected_company_name"] = connected_company_name
|
companies_relations_df["connected_company_name"] = connected_company_name
|
||||||
# print(companies_relations_df)
|
# print(companies_relations_df)
|
||||||
return companies_relations_df
|
return companies_relations_df
|
||||||
|
|
||||||
|
|
||||||
# Plotly figure
|
# Plotly figure
|
||||||
def networkGraph(company_id: int) -> go.Figure:
|
def networkGraph(company_id: int) -> go.Figure:
|
||||||
|
"""_summary_.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
company_id (int): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Figure: _description_
|
||||||
|
"""
|
||||||
# df = find_company_relations(test_company)
|
# df = find_company_relations(test_company)
|
||||||
edges = []
|
edges = []
|
||||||
for index, row in find_company_relations(company_id).iterrows():
|
for index, row in find_company_relations(company_id).iterrows():
|
||||||
@ -109,13 +139,18 @@ def networkGraph(company_id: int) -> go.Figure:
|
|||||||
# figure
|
# figure
|
||||||
return go.Figure(data=[edge_trace, node_trace], layout=layout)
|
return go.Figure(data=[edge_trace, node_trace], layout=layout)
|
||||||
|
|
||||||
def networkx_component(company_id: int):
|
|
||||||
|
|
||||||
layout = html.Div(
|
|
||||||
[
|
|
||||||
|
|
||||||
dcc.Graph(id="my-graph", figure=networkGraph(company_id)),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return layout
|
|
||||||
|
|
||||||
|
def networkx_component(company_id: int) -> html.Div:
|
||||||
|
"""Retruns the Layout with a Graph.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
company_id (int): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
any: _description_
|
||||||
|
"""
|
||||||
|
return html.Div(
|
||||||
|
[
|
||||||
|
dcc.Graph(id="my-graph", figure=networkGraph(company_id)),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
@ -1,34 +1,38 @@
|
|||||||
"""Content of home page."""
|
"""Content of home page."""
|
||||||
import dash
|
import dash
|
||||||
|
import dash_daq as daq
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
from dash import Input, Output, callback, html, dcc, dash_table, ctx
|
from dash import Input, Output, callback, dash_table, dcc, html
|
||||||
import dash_daq as daq
|
|
||||||
|
|
||||||
|
|
||||||
from aki_prj23_transparenzregister.utils.networkx.networkx_data import (
|
|
||||||
find_all_company_relations,
|
|
||||||
find_top_companies,
|
|
||||||
get_all_person_relations,
|
|
||||||
get_all_company_relations,
|
|
||||||
filter_relation_type,
|
|
||||||
filter_relation_with_more_than_one_connection,
|
|
||||||
create_edge_and_node_list
|
|
||||||
)
|
|
||||||
from aki_prj23_transparenzregister.utils.networkx.network_3d import (
|
|
||||||
initialize_network,
|
|
||||||
create_3d_graph,
|
|
||||||
)
|
|
||||||
|
|
||||||
from aki_prj23_transparenzregister.utils.networkx.network_2d import (
|
from aki_prj23_transparenzregister.utils.networkx.network_2d import (
|
||||||
create_2d_graph,
|
create_2d_graph,
|
||||||
)
|
)
|
||||||
|
from aki_prj23_transparenzregister.utils.networkx.network_3d import (
|
||||||
|
# initialize_network,
|
||||||
|
create_3d_graph,
|
||||||
|
)
|
||||||
|
from aki_prj23_transparenzregister.utils.networkx.network_base import (
|
||||||
|
initialize_network,
|
||||||
|
)
|
||||||
|
from aki_prj23_transparenzregister.utils.networkx.networkx_data import (
|
||||||
|
create_edge_and_node_list,
|
||||||
|
filter_relation_type,
|
||||||
|
filter_relation_with_more_than_one_connection,
|
||||||
|
find_top_companies,
|
||||||
|
get_all_company_relations,
|
||||||
|
get_all_person_relations,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Get Data
|
# Get Data
|
||||||
person_relation = filter_relation_type(get_all_person_relations(), "HAFTENDER_GESELLSCHAFTER")
|
person_relation = filter_relation_type(
|
||||||
company_relation = filter_relation_with_more_than_one_connection(get_all_company_relations(), "id_company_to", "id_company_from")
|
get_all_person_relations(), "HAFTENDER_GESELLSCHAFTER"
|
||||||
|
)
|
||||||
|
company_relation = filter_relation_with_more_than_one_connection(
|
||||||
|
get_all_company_relations(), "id_company_to", "id_company_from"
|
||||||
|
)
|
||||||
|
|
||||||
dash.register_page(
|
dash.register_page(
|
||||||
__name__,
|
__name__,
|
||||||
@ -50,56 +54,105 @@ def networkGraph(EGDE_VAR: None) -> go.Figure:
|
|||||||
graph, metrices = initialize_network(nodes = nodes, edges = edges)
|
graph, metrices = initialize_network(nodes = nodes, edges = edges)
|
||||||
# print(graph)
|
# print(graph)
|
||||||
metric = None
|
metric = None
|
||||||
network = create_3d_graph(graph, nodes, edges, metrices, metric)
|
network = create_3d_graph(graph, nodes, edges, metrics, metric)
|
||||||
|
|
||||||
|
# Get the possible Filter values for the Dropdowns.
|
||||||
company_relation_type_filter = get_all_person_relations()["relation_type"].unique()
|
company_relation_type_filter = get_all_person_relations()["relation_type"].unique()
|
||||||
print(company_relation_type_filter)
|
|
||||||
person_relation_type_filter = get_all_company_relations()["relation_type"].unique()
|
person_relation_type_filter = get_all_company_relations()["relation_type"].unique()
|
||||||
|
|
||||||
|
top_companies_df = find_top_companies()
|
||||||
df = find_top_companies()
|
# with open("src/aki_prj23_transparenzregister/ui/assets/network_graph.html") as file:
|
||||||
with open("src/aki_prj23_transparenzregister/ui/assets/network_graph.html") as file:
|
# html_content = file.read()
|
||||||
html_content = file.read()
|
|
||||||
|
|
||||||
|
|
||||||
layout = html.Div(
|
layout = html.Div(
|
||||||
# children=[
|
children=html.Div(
|
||||||
# # NOTE lib dir created by NetworkX has to be placed in assets
|
|
||||||
# # html.Iframe(
|
|
||||||
# # src="assets/network_graph.html",
|
|
||||||
# # style={"height": "100vh", "width": "100vw"},
|
|
||||||
# # allow="*",
|
|
||||||
# # )
|
|
||||||
# ]
|
|
||||||
children = html.Div(
|
|
||||||
children=[
|
children=[
|
||||||
html.Div(
|
html.Div(
|
||||||
className="top_companytable_style",
|
className="top_companytable_style",
|
||||||
children=[
|
children=[
|
||||||
html.Title(title="Top Ten Unternehmen", style={"align": "mid"}),
|
html.Title(title="Top Ten Unternehmen", style={"align": "mid"}),
|
||||||
dash_table.DataTable(df.to_dict('records'), [{"name": i, "id": i} for i in df.columns])
|
dash_table.DataTable(
|
||||||
]
|
top_companies_df.to_dict("records"),
|
||||||
|
[{"name": i, "id": i} for i in top_companies_df.columns],
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
html.Div(
|
html.Div(
|
||||||
className="networkx_style",
|
className="networkx_style",
|
||||||
children=[
|
children=[
|
||||||
html.Header(title="Social Graph"),
|
html.H1(className="header", children=["Social Graph"]),
|
||||||
"Company Relation Type Filter:",
|
html.Div(
|
||||||
dcc.Dropdown(company_relation_type_filter, company_relation_type_filter[0], id='dropdown_companyrelation_filter'),
|
className="filter-wrapper",
|
||||||
"Person Relation Type Filter:",
|
children=[
|
||||||
dcc.Dropdown(person_relation_type_filter, person_relation_type_filter[0], id='dropdown_personrelation_filter'),
|
html.Div(
|
||||||
"Choose Graph Metric:",
|
className="filter-wrapper-item",
|
||||||
dcc.Dropdown(['None','eigenvector', 'degree', 'betweeness', 'closeness'], 'None', id='dropdown'),
|
children=[
|
||||||
# "Text",
|
html.H5(
|
||||||
# dcc.Input(id="EGDE_VAR", type="text", value="K", debounce=True),
|
className="filter-description",
|
||||||
daq.BooleanSwitch(id='switch', on=False),
|
children=["Company Relation Type Filter:"],
|
||||||
# html.Div(id='switch'),
|
),
|
||||||
# dcc.Dropdown(['eigenvector', 'degree', 'betweeness', 'closeness'], 'eigenvector', id='metric-dropdown'),
|
dcc.Dropdown(
|
||||||
# dcc.Graph(id="my-graph"),
|
company_relation_type_filter,
|
||||||
dcc.Graph(figure = network, id='my-graph'),
|
company_relation_type_filter[0],
|
||||||
# html.Div(id='my-graph'),
|
id="dropdown_companyrelation_filter",
|
||||||
]
|
className="dropdown_style",
|
||||||
)
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
html.Div(
|
||||||
|
className="filter-wrapper-item",
|
||||||
|
children=[
|
||||||
|
html.H5(
|
||||||
|
className="filter-description",
|
||||||
|
children=["Person Relation Type Filter:"],
|
||||||
|
),
|
||||||
|
dcc.Dropdown(
|
||||||
|
person_relation_type_filter,
|
||||||
|
person_relation_type_filter[0],
|
||||||
|
id="dropdown_personrelation_filter",
|
||||||
|
className="dropdown_style",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
html.Div(
|
||||||
|
className="filter-wrapper-item",
|
||||||
|
children=[
|
||||||
|
html.H5(
|
||||||
|
className="filter-description",
|
||||||
|
children=["Choose Graph Metric:"],
|
||||||
|
),
|
||||||
|
dcc.Dropdown(
|
||||||
|
[
|
||||||
|
"None",
|
||||||
|
"eigenvector",
|
||||||
|
"degree",
|
||||||
|
"betweeness",
|
||||||
|
"closeness",
|
||||||
|
],
|
||||||
|
"None",
|
||||||
|
id="dropdown",
|
||||||
|
className="dropdown_style",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
html.Div(
|
||||||
|
className="filter-wrapper-item",
|
||||||
|
children=[
|
||||||
|
html.H5(
|
||||||
|
className="filter-description",
|
||||||
|
children=["Switch to 2D Diagramm"],
|
||||||
|
),
|
||||||
|
html.Div(
|
||||||
|
className="switch-style",
|
||||||
|
children=[daq.BooleanSwitch(id="switch", on=False)],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
dcc.Graph(figure=network, id="my-graph", className="graph-style"),
|
||||||
|
],
|
||||||
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -107,78 +160,65 @@ layout = html.Div(
|
|||||||
|
|
||||||
@callback(
|
@callback(
|
||||||
Output("my-graph", "figure"),
|
Output("my-graph", "figure"),
|
||||||
# Input('metric-dropdown', 'value'),
|
[
|
||||||
[Input("dropdown", "value"),
|
Input("dropdown", "value"),
|
||||||
Input("switch", "on"),
|
Input("switch", "on"),
|
||||||
Input("dropdown_companyrelation_filter", "value"),
|
Input("dropdown_companyrelation_filter", "value"),
|
||||||
Input("dropdown_personrelation_filter", "value")],
|
Input("dropdown_personrelation_filter", "value"),
|
||||||
|
],
|
||||||
prevent_initial_call=True,
|
prevent_initial_call=True,
|
||||||
allow_duplicate=True
|
allow_duplicate=True,
|
||||||
)
|
)
|
||||||
def update_figure(selected_value, on, c_relation_filter, p_relation_filter):
|
def update_figure(
|
||||||
triggered_id = ctx.triggered_id
|
selected_metric: str,
|
||||||
|
switch_value: bool,
|
||||||
|
c_relation_filter_value: str,
|
||||||
|
p_relation_filter_value: str,
|
||||||
|
) -> go.Figure:
|
||||||
|
"""In this Callback the Value of the Dropdown is used to filter the Data. In Addition it takes the filter for the Graph metrics and creates a new graph, or switches between 3D and 2D.
|
||||||
|
|
||||||
find_top_companies()
|
Args:
|
||||||
|
selected_metric (_type_): _description_
|
||||||
|
switch_value (bool): _description_
|
||||||
|
c_relation_filter_value (_type_): _description_
|
||||||
|
p_relation_filter_value (_type_): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Network Graph(Plotly Figure): Plotly Figure in 3 or 2D
|
||||||
|
"""
|
||||||
|
_ = c_relation_filter_value, p_relation_filter_value
|
||||||
|
|
||||||
|
# find_top_companies()
|
||||||
|
|
||||||
|
metric = None if selected_metric == "None" else selected_metric
|
||||||
|
|
||||||
if selected_value == "None":
|
|
||||||
metric = None
|
|
||||||
else:
|
|
||||||
metric = selected_value
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if triggered_id == 'switch':
|
|
||||||
|
|
||||||
if on:
|
|
||||||
return update_mode(on, selected_value)
|
|
||||||
else:
|
|
||||||
return create_3d_graph(graph, nodes, edges, metrices, metric)
|
|
||||||
elif triggered_id == 'dropdown':
|
|
||||||
|
|
||||||
if on:
|
|
||||||
return update_mode(on, selected_value)
|
|
||||||
else:
|
|
||||||
return create_3d_graph(graph, nodes, edges, metrices, metric)
|
|
||||||
|
|
||||||
# print(c_relation_filter)
|
|
||||||
# print(p_relation_filter)
|
|
||||||
# if triggered_id == 'dropdown_companyrelation_filter' or triggered_id == 'dropdown_personrelation_filter':
|
# if triggered_id == 'dropdown_companyrelation_filter' or triggered_id == 'dropdown_personrelation_filter':
|
||||||
# print("Hallo")
|
|
||||||
# print(selected_value)
|
# print(selected_value)
|
||||||
# graph, metrices = update_graph_data(person_relation_type= p_relation_filter, company_relation_type= c_relation_filter)
|
# print(metrics)
|
||||||
# if on:
|
# print(graph)
|
||||||
# return update_mode(on, selected_value)
|
# graph, metrics = update_graph_data(person_relation_type= p_relation_filter, company_relation_type= c_relation_filter)
|
||||||
# else:
|
|
||||||
# return create_3d_graph(graph, nodes, edges, metrices, metric)
|
|
||||||
# print(metrices)
|
|
||||||
# print(graph)
|
|
||||||
|
|
||||||
def update_mode(value, metric):
|
if switch_value:
|
||||||
|
return create_2d_graph(graph, nodes, edges, metrics, metric)
|
||||||
if metric == "None":
|
else:
|
||||||
metric = None
|
return create_3d_graph(graph, nodes, edges, metrics, metric)
|
||||||
if value == True:
|
|
||||||
return create_2d_graph(graph, nodes, edges, metrices, metric)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def update_graph_data(
|
||||||
def update_graph_data(person_relation_type = "HAFTENDER_GESELLSCHAFTER", company_relation_type = "GESCHAEFTSFUEHRER"):
|
person_relation_type: str = "HAFTENDER_GESELLSCHAFTER",
|
||||||
|
company_relation_type: str = "GESCHAEFTSFUEHRER",
|
||||||
|
) -> tuple[nx.Graph, pd.DataFrame]:
|
||||||
# Get Data
|
# Get Data
|
||||||
person_df = get_all_person_relations()
|
person_df = get_all_person_relations()
|
||||||
company_df = get_all_company_relations()
|
company_df = get_all_company_relations()
|
||||||
|
|
||||||
person_relation = filter_relation_type(person_df, person_relation_type)
|
person_relation = filter_relation_type(person_df, person_relation_type)
|
||||||
company_relation = filter_relation_type(company_df, company_relation_type)
|
company_relation = filter_relation_type(company_df, company_relation_type)
|
||||||
print(company_relation)
|
|
||||||
|
|
||||||
# company_relation = filter_relation_with_more_than_one_connection(company_relation, "id_company_to", "id_company_from")
|
# company_relation = filter_relation_with_more_than_one_connection(company_relation, "id_company_to", "id_company_from")
|
||||||
# print(company_relation)
|
|
||||||
#Create Edge and Node List from data
|
|
||||||
nodes, edges = create_edge_and_node_list(person_relation, company_relation)
|
|
||||||
# print(edges)
|
|
||||||
|
|
||||||
graph, metrices = initialize_network(nodes = nodes, edges = edges)
|
# Create Edge and Node List from data
|
||||||
return graph, metrices
|
nodes, edges = create_edge_and_node_list(person_relation, company_relation)
|
||||||
|
|
||||||
|
graph, metrics = initialize_network(nodes=nodes, edges=edges)
|
||||||
|
return graph, metrics
|
||||||
|
@ -1795,7 +1795,7 @@
|
|||||||
"import networkx as nx\n",
|
"import networkx as nx\n",
|
||||||
"\n",
|
"\n",
|
||||||
"G = graph\n",
|
"G = graph\n",
|
||||||
"pos=nx.fruchterman_reingold_layout(G)\n",
|
"pos = nx.fruchterman_reingold_layout(G)\n",
|
||||||
"pos"
|
"pos"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -1929,7 +1929,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"nodes = pd.DataFrame(pos)\n",
|
"nodes = pd.DataFrame(pos)\n",
|
||||||
"nodes\n"
|
"nodes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1938,41 +1938,45 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
"Xv = [pos[k][0] for k in range(N)]\n",
|
||||||
"Xv=[pos[k][0] for k in range(N)]\n",
|
"Yv = [pos[k][1] for k in range(N)]\n",
|
||||||
"Yv=[pos[k][1] for k in range(N)]\n",
|
"Xed = []\n",
|
||||||
"Xed=[]\n",
|
"Yed = []\n",
|
||||||
"Yed=[]\n",
|
|
||||||
"for edge in E:\n",
|
"for edge in E:\n",
|
||||||
" Xed+=[pos[edge[0]][0],pos[edge[1]][0], None]\n",
|
" Xed += [pos[edge[0]][0], pos[edge[1]][0], None]\n",
|
||||||
" Yed+=[pos[edge[0]][1],pos[edge[1]][1], None]\n",
|
" Yed += [pos[edge[0]][1], pos[edge[1]][1], None]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"trace3=Scatter(x=Xed,\n",
|
"trace3 = Scatter(\n",
|
||||||
" y=Yed,\n",
|
" x=Xed,\n",
|
||||||
" mode='lines',\n",
|
" y=Yed,\n",
|
||||||
" line=dict(color='rgb(210,210,210)', width=1),\n",
|
" mode=\"lines\",\n",
|
||||||
" hoverinfo='none'\n",
|
" line=dict(color=\"rgb(210,210,210)\", width=1),\n",
|
||||||
" )\n",
|
" hoverinfo=\"none\",\n",
|
||||||
"trace4=Scatter(x=Xv,\n",
|
")\n",
|
||||||
" y=Yv,\n",
|
"trace4 = Scatter(\n",
|
||||||
" mode='markers',\n",
|
" x=Xv,\n",
|
||||||
" name='net',\n",
|
" y=Yv,\n",
|
||||||
" marker=dict(symbol='circle-dot',\n",
|
" mode=\"markers\",\n",
|
||||||
" size=5,\n",
|
" name=\"net\",\n",
|
||||||
" color='#6959CD',\n",
|
" marker=dict(\n",
|
||||||
" line=dict(color='rgb(50,50,50)', width=0.5)\n",
|
" symbol=\"circle-dot\",\n",
|
||||||
" ),\n",
|
" size=5,\n",
|
||||||
" text=labels,\n",
|
" color=\"#6959CD\",\n",
|
||||||
" hoverinfo='text'\n",
|
" line=dict(color=\"rgb(50,50,50)\", width=0.5),\n",
|
||||||
" )\n",
|
" ),\n",
|
||||||
|
" text=labels,\n",
|
||||||
|
" hoverinfo=\"text\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"annot=\"This networkx.Graph has the Fruchterman-Reingold layout<br>Code:\"+\\\n",
|
"annot = (\n",
|
||||||
"\"<a href='http://nbviewer.ipython.org/gist/empet/07ea33b2e4e0b84193bd'> [2]</a>\"\n",
|
" \"This networkx.Graph has the Fruchterman-Reingold layout<br>Code:\"\n",
|
||||||
|
" + \"<a href='http://nbviewer.ipython.org/gist/empet/07ea33b2e4e0b84193bd'> [2]</a>\"\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"data1=[trace3, trace4]\n",
|
"data1 = [trace3, trace4]\n",
|
||||||
"fig1=Figure(data=data1, layout=layout)\n",
|
"fig1 = Figure(data=data1, layout=layout)\n",
|
||||||
"fig1['layout']['annotations'][0]['text']=annot\n",
|
"fig1[\"layout\"][\"annotations\"][0][\"text\"] = annot\n",
|
||||||
"py.iplot(fig1, filename='Coautorship-network-nx')"
|
"py.iplot(fig1, filename=\"Coautorship-network-nx\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1998,12 +2002,12 @@
|
|||||||
"import networkx as nx\n",
|
"import networkx as nx\n",
|
||||||
"\n",
|
"\n",
|
||||||
"G = graph\n",
|
"G = graph\n",
|
||||||
"pos=nx.fruchterman_reingold_layout(G)\n",
|
"pos = nx.fruchterman_reingold_layout(G)\n",
|
||||||
"edge_x = []\n",
|
"edge_x = []\n",
|
||||||
"edge_y = []\n",
|
"edge_y = []\n",
|
||||||
"for edge in G.edges():\n",
|
"for edge in G.edges():\n",
|
||||||
" x0, y0 = G.nodes[edge[0]]['pos']\n",
|
" x0, y0 = G.nodes[edge[0]][\"pos\"]\n",
|
||||||
" x1, y1 = G.nodes[edge[1]]['pos']\n",
|
" x1, y1 = G.nodes[edge[1]][\"pos\"]\n",
|
||||||
" edge_x.append(x0)\n",
|
" edge_x.append(x0)\n",
|
||||||
" edge_x.append(x1)\n",
|
" edge_x.append(x1)\n",
|
||||||
" edge_x.append(None)\n",
|
" edge_x.append(None)\n",
|
||||||
@ -2012,39 +2016,41 @@
|
|||||||
" edge_y.append(None)\n",
|
" edge_y.append(None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"edge_trace = go.Scatter(\n",
|
"edge_trace = go.Scatter(\n",
|
||||||
" x=edge_x, y=edge_y,\n",
|
" x=edge_x,\n",
|
||||||
" line=dict(width=0.5, color='#888'),\n",
|
" y=edge_y,\n",
|
||||||
" hoverinfo='none',\n",
|
" line=dict(width=0.5, color=\"#888\"),\n",
|
||||||
" mode='lines')\n",
|
" hoverinfo=\"none\",\n",
|
||||||
|
" mode=\"lines\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_x = []\n",
|
"node_x = []\n",
|
||||||
"node_y = []\n",
|
"node_y = []\n",
|
||||||
"for node in G.nodes():\n",
|
"for node in G.nodes():\n",
|
||||||
" x, y = G.nodes[node]['pos']\n",
|
" x, y = G.nodes[node][\"pos\"]\n",
|
||||||
" node_x.append(x)\n",
|
" node_x.append(x)\n",
|
||||||
" node_y.append(y)\n",
|
" node_y.append(y)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_trace = go.Scatter(\n",
|
"node_trace = go.Scatter(\n",
|
||||||
" x=node_x, y=node_y,\n",
|
" x=node_x,\n",
|
||||||
" mode='markers',\n",
|
" y=node_y,\n",
|
||||||
" hoverinfo='text',\n",
|
" mode=\"markers\",\n",
|
||||||
|
" hoverinfo=\"text\",\n",
|
||||||
" marker=dict(\n",
|
" marker=dict(\n",
|
||||||
" showscale=True,\n",
|
" showscale=True,\n",
|
||||||
" # colorscale options\n",
|
" # colorscale options\n",
|
||||||
" #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |\n",
|
" #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |\n",
|
||||||
" #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |\n",
|
" #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |\n",
|
||||||
" #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |\n",
|
" #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |\n",
|
||||||
" colorscale='YlGnBu',\n",
|
" colorscale=\"YlGnBu\",\n",
|
||||||
" reversescale=True,\n",
|
" reversescale=True,\n",
|
||||||
" color=[],\n",
|
" color=[],\n",
|
||||||
" size=10,\n",
|
" size=10,\n",
|
||||||
" colorbar=dict(\n",
|
" colorbar=dict(\n",
|
||||||
" thickness=15,\n",
|
" thickness=15, title=\"Node Connections\", xanchor=\"left\", titleside=\"right\"\n",
|
||||||
" title='Node Connections',\n",
|
|
||||||
" xanchor='left',\n",
|
|
||||||
" titleside='right'\n",
|
|
||||||
" ),\n",
|
" ),\n",
|
||||||
" line_width=2))"
|
" line_width=2,\n",
|
||||||
|
" ),\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -2057,7 +2063,7 @@
|
|||||||
"node_text = []\n",
|
"node_text = []\n",
|
||||||
"for node, adjacencies in enumerate(G.adjacency()):\n",
|
"for node, adjacencies in enumerate(G.adjacency()):\n",
|
||||||
" node_adjacencies.append(len(adjacencies[1]))\n",
|
" node_adjacencies.append(len(adjacencies[1]))\n",
|
||||||
" node_text.append('# of connections: '+str(len(adjacencies[1])))\n",
|
" node_text.append(\"# of connections: \" + str(len(adjacencies[1])))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_trace.marker.color = node_adjacencies\n",
|
"node_trace.marker.color = node_adjacencies\n",
|
||||||
"node_trace.text = node_text"
|
"node_trace.text = node_text"
|
||||||
@ -2069,21 +2075,28 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"fig = go.Figure(data=[edge_trace, node_trace],\n",
|
"fig = go.Figure(\n",
|
||||||
" layout=go.Layout(\n",
|
" data=[edge_trace, node_trace],\n",
|
||||||
" title='<br>Network graph made with Python',\n",
|
" layout=go.Layout(\n",
|
||||||
" titlefont_size=16,\n",
|
" title=\"<br>Network graph made with Python\",\n",
|
||||||
" showlegend=False,\n",
|
" titlefont_size=16,\n",
|
||||||
" hovermode='closest',\n",
|
" showlegend=False,\n",
|
||||||
" margin=dict(b=20,l=5,r=5,t=40),\n",
|
" hovermode=\"closest\",\n",
|
||||||
" annotations=[ dict(\n",
|
" margin=dict(b=20, l=5, r=5, t=40),\n",
|
||||||
" text=\"Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>\",\n",
|
" annotations=[\n",
|
||||||
" showarrow=False,\n",
|
" dict(\n",
|
||||||
" xref=\"paper\", yref=\"paper\",\n",
|
" text=\"Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>\",\n",
|
||||||
" x=0.005, y=-0.002 ) ],\n",
|
" showarrow=False,\n",
|
||||||
" xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
" xref=\"paper\",\n",
|
||||||
" yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))\n",
|
" yref=\"paper\",\n",
|
||||||
" )\n",
|
" x=0.005,\n",
|
||||||
|
" y=-0.002,\n",
|
||||||
|
" )\n",
|
||||||
|
" ],\n",
|
||||||
|
" xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
||||||
|
" yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
||||||
|
" ),\n",
|
||||||
|
")\n",
|
||||||
"fig.show()"
|
"fig.show()"
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -780,7 +780,9 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"relations_df = pd.DataFrame(relations_query.all())\n",
|
"relations_df = pd.DataFrame(relations_query.all())\n",
|
||||||
"relations_df[\"person_name\"] = relations_df[\"lastname\"] + relations_df[\"firstname\"]\n",
|
"relations_df[\"person_name\"] = relations_df[\"lastname\"] + relations_df[\"firstname\"]\n",
|
||||||
"relations_df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)\n",
|
"relations_df.rename(\n",
|
||||||
|
" columns={\"oldName1\": \"newName1\", \"oldName2\": \"newName2\"}, inplace=True\n",
|
||||||
|
")\n",
|
||||||
"relations_df"
|
"relations_df"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -791,22 +793,21 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"node_template = {\n",
|
"node_template = {\n",
|
||||||
" \"id\": \"(company|person)_\\d\",\n",
|
" \"id\": \"(company|person)_\\d\",\n",
|
||||||
" \"label\": \"Name from entries\",\n",
|
" \"label\": \"Name from entries\",\n",
|
||||||
" \"type\": \"Company|Person\",\n",
|
" \"type\": \"Company|Person\",\n",
|
||||||
" \"shape\": \"dot\",\n",
|
" \"shape\": \"dot\",\n",
|
||||||
" \"color\": \"#729b79ff\",\n",
|
" \"color\": \"#729b79ff\",\n",
|
||||||
" # TODO add title for hover effect in graph \"title\": \"\"\n",
|
" # TODO add title for hover effect in graph \"title\": \"\"\n",
|
||||||
" }\n",
|
"}\n",
|
||||||
"nodes = relations_df\n",
|
"nodes = relations_df\n",
|
||||||
"for index in relations_df.index:\n",
|
"for index in relations_df.index:\n",
|
||||||
" \n",
|
|
||||||
" nodes[\"index\"] = {\n",
|
" nodes[\"index\"] = {\n",
|
||||||
" \"label\": company_2.name,\n",
|
" \"label\": company_2.name,\n",
|
||||||
" \"type\": \"Company\",\n",
|
" \"type\": \"Company\",\n",
|
||||||
" \"shape\": \"dot\",\n",
|
" \"shape\": \"dot\",\n",
|
||||||
" \"color\": \"#729b79ff\",\n",
|
" \"color\": \"#729b79ff\",\n",
|
||||||
" }"
|
" }"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -879,15 +880,10 @@
|
|||||||
" else:\n",
|
" else:\n",
|
||||||
" size = measure_vector[node_id] * 50\n",
|
" size = measure_vector[node_id] * 50\n",
|
||||||
" next(\n",
|
" next(\n",
|
||||||
" (\n",
|
" (node.update({\"size\": size}) for node in net.nodes if node[\"id\"] == node_id),\n",
|
||||||
" node.update({\"size\": size})\n",
|
|
||||||
" for node in net.nodes\n",
|
|
||||||
" if node[\"id\"] == node_id\n",
|
|
||||||
" ),\n",
|
|
||||||
" None,\n",
|
" None,\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"net.repulsion()\n",
|
"net.repulsion()\n",
|
||||||
"net.show_buttons(filter_=[\"physics\"])\n",
|
"net.show_buttons(filter_=[\"physics\"])\n",
|
||||||
@ -895,7 +891,7 @@
|
|||||||
"# net.show_buttons()\n",
|
"# net.show_buttons()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# save graph as HTML\n",
|
"# save graph as HTML\n",
|
||||||
"net.save_graph(\"./tmp.html\")\n"
|
"net.save_graph(\"./tmp.html\")"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
File diff suppressed because one or more lines are too long
@ -680,8 +680,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"person_relations['id_company'] = person_relations['id_company'].apply(lambda x: f\"c_{x}\")\n",
|
"person_relations[\"id_company\"] = person_relations[\"id_company\"].apply(\n",
|
||||||
"person_relations['id_person'] = person_relations['id_person'].apply(lambda x: f\"p_{x}\")\n",
|
" lambda x: f\"c_{x}\"\n",
|
||||||
|
")\n",
|
||||||
|
"person_relations[\"id_person\"] = person_relations[\"id_person\"].apply(lambda x: f\"p_{x}\")\n",
|
||||||
"person_relations.head()"
|
"person_relations.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -10172,7 +10174,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"person_relations_df[[\"from_x\", \"from_y\"]] = (company_df.set_index(\"id\", drop=True)).loc[person_relations_df[\"company_id\"], [\"x\", \"y\"]]\n",
|
"person_relations_df[[\"from_x\", \"from_y\"]] = (company_df.set_index(\"id\", drop=True)).loc[\n",
|
||||||
|
" person_relations_df[\"company_id\"], [\"x\", \"y\"]\n",
|
||||||
|
"]\n",
|
||||||
"person_relations_df.head().T"
|
"person_relations_df.head().T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -10202,7 +10206,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"df_temp_df: pd.DataFrame = company_df.set_index(\"id\", drop=True)\n",
|
"df_temp_df: pd.DataFrame = company_df.set_index(\"id\", drop=True)\n",
|
||||||
"person_relations_df[[\"company_from_x\", \"company_from_y\"]] = df_temp_df.loc[person_relations_df[\"company_id\"] , [\"x\", \"y\"]].reset_index(drop=True)"
|
"person_relations_df[[\"company_from_x\", \"company_from_y\"]] = df_temp_df.loc[\n",
|
||||||
|
" person_relations_df[\"company_id\"], [\"x\", \"y\"]\n",
|
||||||
|
"].reset_index(drop=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -10213,7 +10219,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"df_person_tmp: pd.DataFrame = person_df.set_index(\"id\", drop=True)\n",
|
"df_person_tmp: pd.DataFrame = person_df.set_index(\"id\", drop=True)\n",
|
||||||
"person_relations_df[[\"to_person_x\", \"to_person_y\"]] = df_person_tmp.loc[person_relations_df[\"person_id\"] , [\"x\", \"y\"]].reset_index(drop=True)\n"
|
"person_relations_df[[\"to_person_x\", \"to_person_y\"]] = df_person_tmp.loc[\n",
|
||||||
|
" person_relations_df[\"person_id\"], [\"x\", \"y\"]\n",
|
||||||
|
"].reset_index(drop=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -10242,10 +10250,14 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"df_temp_df: pd.DataFrame = company_df.set_index(\"id\", drop=True)\n",
|
"df_temp_df: pd.DataFrame = company_df.set_index(\"id\", drop=True)\n",
|
||||||
"company_relations_df[[\"company_from_x\", \"company_from_y\"]] = df_temp_df.loc[company_relations_df[\"company_from_id\"] , [\"x\", \"y\"]].reset_index(drop=True)\n",
|
"company_relations_df[[\"company_from_x\", \"company_from_y\"]] = df_temp_df.loc[\n",
|
||||||
|
" company_relations_df[\"company_from_id\"], [\"x\", \"y\"]\n",
|
||||||
|
"].reset_index(drop=True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"company_relations_df[[\"company_to_x\", \"company_to_y\"]] = df_temp_df.loc[company_relations_df[\"company_to_id\"] , [\"x\", \"y\"]].reset_index(drop=True)\n",
|
"company_relations_df[[\"company_to_x\", \"company_to_y\"]] = df_temp_df.loc[\n",
|
||||||
|
" company_relations_df[\"company_to_id\"], [\"x\", \"y\"]\n",
|
||||||
|
"].reset_index(drop=True)\n",
|
||||||
"company_relations_df"
|
"company_relations_df"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -10278,39 +10290,45 @@
|
|||||||
"fig = go.Figure()\n",
|
"fig = go.Figure()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Scatter plot for df1\n",
|
"# Scatter plot for df1\n",
|
||||||
"fig.add_trace(go.Scatter(\n",
|
"fig.add_trace(\n",
|
||||||
" x=df1['x'],\n",
|
" go.Scatter(\n",
|
||||||
" y=df1['y'],\n",
|
" x=df1[\"x\"],\n",
|
||||||
" mode='markers',\n",
|
" y=df1[\"y\"],\n",
|
||||||
" name='DataFrame 1',\n",
|
" mode=\"markers\",\n",
|
||||||
" hovertext=df1[\"name\"]\n",
|
" name=\"DataFrame 1\",\n",
|
||||||
"))\n",
|
" hovertext=df1[\"name\"],\n",
|
||||||
|
" )\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Scatter plot for df2\n",
|
"# Scatter plot for df2\n",
|
||||||
"fig.add_trace(go.Scatter(\n",
|
"fig.add_trace(\n",
|
||||||
" x=df2['x'],\n",
|
" go.Scatter(\n",
|
||||||
" y=df2['y'],\n",
|
" x=df2[\"x\"],\n",
|
||||||
" mode='markers',\n",
|
" y=df2[\"y\"],\n",
|
||||||
" name='DataFrame 2',\n",
|
" mode=\"markers\",\n",
|
||||||
" hovertext=df2[\"firstname\"]\n",
|
" name=\"DataFrame 2\",\n",
|
||||||
"))\n",
|
" hovertext=df2[\"firstname\"],\n",
|
||||||
|
" )\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Scatter plot for df3\n",
|
"# Scatter plot for df3\n",
|
||||||
"for i in range(len(df1)):\n",
|
"for i in range(len(df1)):\n",
|
||||||
" fig.add_trace(go.Scatter(\n",
|
" fig.add_trace(\n",
|
||||||
" x=[df3['company_from_x'][i], df3['to_person_x'][i]],\n",
|
" go.Scatter(\n",
|
||||||
" y=[df3['company_from_y'][i], df3['to_person_y'][i]],\n",
|
" x=[df3[\"company_from_x\"][i], df3[\"to_person_x\"][i]],\n",
|
||||||
" mode='lines',\n",
|
" y=[df3[\"company_from_y\"][i], df3[\"to_person_y\"][i]],\n",
|
||||||
" name='Line DataFrame 1',\n",
|
" mode=\"lines\",\n",
|
||||||
" line=dict(color='blue')\n",
|
" name=\"Line DataFrame 1\",\n",
|
||||||
" ))\n",
|
" line=dict(color=\"blue\"),\n",
|
||||||
|
" )\n",
|
||||||
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Customize the layout of the plot\n",
|
"# Customize the layout of the plot\n",
|
||||||
"fig.update_layout(\n",
|
"fig.update_layout(\n",
|
||||||
" title='Overlayed Scatter Plot',\n",
|
" title=\"Overlayed Scatter Plot\",\n",
|
||||||
" xaxis=dict(title='X-axis Label'),\n",
|
" xaxis=dict(title=\"X-axis Label\"),\n",
|
||||||
" yaxis=dict(title='Y-axis Label'),\n",
|
" yaxis=dict(title=\"Y-axis Label\"),\n",
|
||||||
" showlegend=True # Show legend with dataframe names\n",
|
" showlegend=True, # Show legend with dataframe names\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Show the plot\n",
|
"# Show the plot\n",
|
||||||
@ -10324,13 +10342,15 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"for i in range(len(df1)):\n",
|
"for i in range(len(df1)):\n",
|
||||||
" fig.add_trace(go.Scatter(\n",
|
" fig.add_trace(\n",
|
||||||
" x=[df1['x0_column'][i], df1['x1_column'][i]],\n",
|
" go.Scatter(\n",
|
||||||
" y=[df1['y0_column'][i], df1['y1_column'][i]],\n",
|
" x=[df1[\"x0_column\"][i], df1[\"x1_column\"][i]],\n",
|
||||||
" mode='lines',\n",
|
" y=[df1[\"y0_column\"][i], df1[\"y1_column\"][i]],\n",
|
||||||
" name='Line DataFrame 1',\n",
|
" mode=\"lines\",\n",
|
||||||
" line=dict(color='blue')\n",
|
" name=\"Line DataFrame 1\",\n",
|
||||||
" ))"
|
" line=dict(color=\"blue\"),\n",
|
||||||
|
" )\n",
|
||||||
|
" )"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -129718,7 +129738,7 @@
|
|||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Show the plot\n",
|
"# Show the plot\n",
|
||||||
"fig.show()\n"
|
"fig.show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
@ -48,7 +48,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"if not os.path.exists(\"src\"):\n",
|
"if not os.path.exists(\"src\"):\n",
|
||||||
" %cd \"../\"\n",
|
" %cd \"../\"\n",
|
||||||
"os.path.abspath(\".\") "
|
"os.path.abspath(\".\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -675,8 +675,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"company_relations['id_company_from'] = company_relations['id_company_from'].apply(lambda x: f\"c_{x}\")\n",
|
"company_relations[\"id_company_from\"] = company_relations[\"id_company_from\"].apply(\n",
|
||||||
"company_relations['id_company_to'] = company_relations['id_company_to'].apply(lambda x: f\"c_{x}\")\n",
|
" lambda x: f\"c_{x}\"\n",
|
||||||
|
")\n",
|
||||||
|
"company_relations[\"id_company_to\"] = company_relations[\"id_company_to\"].apply(\n",
|
||||||
|
" lambda x: f\"c_{x}\"\n",
|
||||||
|
")\n",
|
||||||
"company_relations.head()"
|
"company_relations.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -793,8 +797,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"person_relations['id_company'] = person_relations['id_company'].apply(lambda x: f\"c_{x}\")\n",
|
"person_relations[\"id_company\"] = person_relations[\"id_company\"].apply(\n",
|
||||||
"person_relations['id_person'] = person_relations['id_person'].apply(lambda x: f\"p_{x}\")\n",
|
" lambda x: f\"c_{x}\"\n",
|
||||||
|
")\n",
|
||||||
|
"person_relations[\"id_person\"] = person_relations[\"id_person\"].apply(lambda x: f\"p_{x}\")\n",
|
||||||
"person_relations.head()"
|
"person_relations.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -1051,10 +1057,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import pandas as pd\n",
|
"import pandas as pd\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def filter_relation_type(df: pd.DataFrame, selected_relation_type):\n",
|
"def filter_relation_type(df: pd.DataFrame, selected_relation_type):\n",
|
||||||
" df = df.loc[df[\"relation_type\"] == selected_relation_type]\n",
|
" df = df.loc[df[\"relation_type\"] == selected_relation_type]\n",
|
||||||
" return df\n",
|
" return df\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"relation_type_set = set(company_relations[\"relation_type\"].unique())\n",
|
"relation_type_set = set(company_relations[\"relation_type\"].unique())\n",
|
||||||
"relation_type_set\n",
|
"relation_type_set\n",
|
||||||
"# {'HAFTENDER_GESELLSCHAFTER', 'INHABER', 'KOMMANDITIST', 'LIQUIDATOR'}\n",
|
"# {'HAFTENDER_GESELLSCHAFTER', 'INHABER', 'KOMMANDITIST', 'LIQUIDATOR'}\n",
|
||||||
@ -1247,9 +1255,11 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"def filter_relation_with_more_than_one_connection(df: pd.DataFrame, id_column_name_to, id_column_name_from):\n",
|
"def filter_relation_with_more_than_one_connection(\n",
|
||||||
|
" df: pd.DataFrame, id_column_name_to, id_column_name_from\n",
|
||||||
|
"):\n",
|
||||||
" # print(df.columns.values)\n",
|
" # print(df.columns.values)\n",
|
||||||
" tmp_df = pd.DataFrame(columns= df.columns.values)\n",
|
" tmp_df = pd.DataFrame(columns=df.columns.values)\n",
|
||||||
" # print(tmp_df)\n",
|
" # print(tmp_df)\n",
|
||||||
" for _index, row in df.iterrows():\n",
|
" for _index, row in df.iterrows():\n",
|
||||||
" count = 0\n",
|
" count = 0\n",
|
||||||
@ -1261,7 +1271,7 @@
|
|||||||
" count = count + 1\n",
|
" count = count + 1\n",
|
||||||
" if count > 1:\n",
|
" if count > 1:\n",
|
||||||
" break\n",
|
" break\n",
|
||||||
" \n",
|
"\n",
|
||||||
" if count > 1:\n",
|
" if count > 1:\n",
|
||||||
" # tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+\n",
|
" # tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+\n",
|
||||||
" tmp_df.loc[len(tmp_df)] = row\n",
|
" tmp_df.loc[len(tmp_df)] = row\n",
|
||||||
@ -1274,7 +1284,10 @@
|
|||||||
" count = 0\n",
|
" count = 0\n",
|
||||||
" return tmp_df\n",
|
" return tmp_df\n",
|
||||||
"\n",
|
"\n",
|
||||||
"tmp_df = filter_relation_with_more_than_one_connection(company_relations, \"id_company_to\", \"id_company_from\")\n",
|
"\n",
|
||||||
|
"tmp_df = filter_relation_with_more_than_one_connection(\n",
|
||||||
|
" company_relations, \"id_company_to\", \"id_company_from\"\n",
|
||||||
|
")\n",
|
||||||
"tmp_df\n",
|
"tmp_df\n",
|
||||||
"# Für Companys 10 sekunden\n",
|
"# Für Companys 10 sekunden\n",
|
||||||
"# tmp_df = filter_relation_with_more_than_one_connection(person_relations, \"id_company\", \"id_person\")\n",
|
"# tmp_df = filter_relation_with_more_than_one_connection(person_relations, \"id_company\", \"id_person\")\n",
|
||||||
@ -1412,43 +1425,57 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def create_edge_and_node_list(person_relations: pd.DataFrame, company_relations:pd.DataFrame):\n",
|
"def create_edge_and_node_list(\n",
|
||||||
|
" person_relations: pd.DataFrame, company_relations: pd.DataFrame\n",
|
||||||
|
"):\n",
|
||||||
" nodes = {}\n",
|
" nodes = {}\n",
|
||||||
" edges = []\n",
|
" edges = []\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Iterate over person relations\n",
|
" # Iterate over person relations\n",
|
||||||
" for _index, row in person_relations.iterrows():\n",
|
" for _index, row in person_relations.iterrows():\n",
|
||||||
" if node:= nodes.get(row['id_company']) is None:\n",
|
" if node := nodes.get(row[\"id_company\"]) is None:\n",
|
||||||
" nodes[row['id_company']] = {\n",
|
" nodes[row[\"id_company\"]] = {\n",
|
||||||
" \"id\": row['id_company'],\n",
|
" \"id\": row[\"id_company\"],\n",
|
||||||
" 'name': row['name_company'],\n",
|
" \"name\": row[\"name_company\"],\n",
|
||||||
" 'color': COLOR_COMPANY\n",
|
" \"color\": COLOR_COMPANY,\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
" if node:= nodes.get(row['id_person']) is None:\n",
|
" if node := nodes.get(row[\"id_person\"]) is None:\n",
|
||||||
" nodes[row['id_person']] = {\n",
|
" nodes[row[\"id_person\"]] = {\n",
|
||||||
" \"id\": row['id_person'],\n",
|
" \"id\": row[\"id_person\"],\n",
|
||||||
" 'firstname': row['firstname'],\n",
|
" \"firstname\": row[\"firstname\"],\n",
|
||||||
" 'lastname': row['lastname'],\n",
|
" \"lastname\": row[\"lastname\"],\n",
|
||||||
" 'date_of_birth': row['date_of_birth'],\n",
|
" \"date_of_birth\": row[\"date_of_birth\"],\n",
|
||||||
" 'color': COLOR_PERSON\n",
|
" \"color\": COLOR_PERSON,\n",
|
||||||
" } \n",
|
" }\n",
|
||||||
" edges.append({'from': row['id_person'], 'to': row['id_company'], 'type': row['relation_type']})\n",
|
" edges.append(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"from\": row[\"id_person\"],\n",
|
||||||
|
" \"to\": row[\"id_company\"],\n",
|
||||||
|
" \"type\": row[\"relation_type\"],\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" for _index, row in company_relations.iterrows():\n",
|
" for _index, row in company_relations.iterrows():\n",
|
||||||
" if node:= nodes.get(row['id_company_from']) is None:\n",
|
" if node := nodes.get(row[\"id_company_from\"]) is None:\n",
|
||||||
" nodes[row['id_company_from']] = {\n",
|
" nodes[row[\"id_company_from\"]] = {\n",
|
||||||
" \"id\": row['id_company_from'],\n",
|
" \"id\": row[\"id_company_from\"],\n",
|
||||||
" 'name': row['name_company_from'],\n",
|
" \"name\": row[\"name_company_from\"],\n",
|
||||||
" 'color': COLOR_COMPANY\n",
|
" \"color\": COLOR_COMPANY,\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
" if node:= nodes.get(row['id_company_to']) is None:\n",
|
" if node := nodes.get(row[\"id_company_to\"]) is None:\n",
|
||||||
" nodes[row['id_company_to']] = {\n",
|
" nodes[row[\"id_company_to\"]] = {\n",
|
||||||
" \"id\": row['id_company_to'],\n",
|
" \"id\": row[\"id_company_to\"],\n",
|
||||||
" 'name': row['name_company_to'],\n",
|
" \"name\": row[\"name_company_to\"],\n",
|
||||||
" 'color': COLOR_COMPANY\n",
|
" \"color\": COLOR_COMPANY,\n",
|
||||||
" } \n",
|
" }\n",
|
||||||
" edges.append({'from': row['id_company_from'], 'to': row['id_company_to'], 'type': row['relation_type']})\n",
|
" edges.append(\n",
|
||||||
" return nodes, edges\n"
|
" {\n",
|
||||||
|
" \"from\": row[\"id_company_from\"],\n",
|
||||||
|
" \"to\": row[\"id_company_to\"],\n",
|
||||||
|
" \"type\": row[\"relation_type\"],\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
|
" return nodes, edges"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1458,7 +1485,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"tmp_company = filter_relation_with_more_than_one_connection(company_relations, \"id_company_to\", \"id_company_from\")\n",
|
"tmp_company = filter_relation_with_more_than_one_connection(\n",
|
||||||
|
" company_relations, \"id_company_to\", \"id_company_from\"\n",
|
||||||
|
")\n",
|
||||||
"tmp_persons = filter_relation_type(person_relations, \"HAFTENDER_GESELLSCHAFTER\")"
|
"tmp_persons = filter_relation_type(person_relations, \"HAFTENDER_GESELLSCHAFTER\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -2257,7 +2286,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"measure_type = \"degree\"\n",
|
"measure_type = \"degree\"\n",
|
||||||
"measure_vector = {}\n",
|
"measure_vector = {}\n",
|
||||||
"df = pd.DataFrame(columns=[\"degree\", \"eigenvector\", \"degree\", \"betweeness\", \"closeness\", \"pagerank\"])\n",
|
"df = pd.DataFrame(\n",
|
||||||
|
" columns=[\"degree\", \"eigenvector\", \"degree\", \"betweeness\", \"closeness\", \"pagerank\"]\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# if measure_type == \"eigenvector\":\n",
|
"# if measure_type == \"eigenvector\":\n",
|
||||||
"# measure_vector = nx.eigenvector_centrality(graph)\n",
|
"# measure_vector = nx.eigenvector_centrality(graph)\n",
|
||||||
@ -2275,7 +2306,6 @@
|
|||||||
"# df[\"pagerank\"] = measure_vector.values()\n",
|
"# df[\"pagerank\"] = measure_vector.values()\n",
|
||||||
"# if measure_type == \"average_degree\":\n",
|
"# if measure_type == \"average_degree\":\n",
|
||||||
"# measure_vector = nx.average_degree_connectivity(graph)\n",
|
"# measure_vector = nx.average_degree_connectivity(graph)\n",
|
||||||
" \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"df[\"eigenvector\"] = nx.eigenvector_centrality(graph).values()\n",
|
"df[\"eigenvector\"] = nx.eigenvector_centrality(graph).values()\n",
|
||||||
@ -2301,6 +2331,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import igraph as ig\n",
|
"import igraph as ig\n",
|
||||||
|
"\n",
|
||||||
"# convert to igraph\n",
|
"# convert to igraph\n",
|
||||||
"h = ig.Graph.from_networkx(graph)"
|
"h = ig.Graph.from_networkx(graph)"
|
||||||
]
|
]
|
||||||
@ -2376,7 +2407,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Nur wenn Pos vorher gesetzt wurde\n",
|
"# Nur wenn Pos vorher gesetzt wurde\n",
|
||||||
"pos2 = nx.get_node_attributes(graph,'pos')\n",
|
"pos2 = nx.get_node_attributes(graph, \"pos\")\n",
|
||||||
"print(pos2)"
|
"print(pos2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -2411,6 +2442,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import plotly.graph_objects as go\n",
|
"import plotly.graph_objects as go\n",
|
||||||
|
"\n",
|
||||||
"edge_x = []\n",
|
"edge_x = []\n",
|
||||||
"edge_y = []\n",
|
"edge_y = []\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -2427,25 +2459,31 @@
|
|||||||
" edge_y.append(y1)\n",
|
" edge_y.append(y1)\n",
|
||||||
" edge_y.append(None)\n",
|
" edge_y.append(None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # edge_weight_x.append(x1 + x1 - x0) \n",
|
" # edge_weight_x.append(x1 + x1 - x0)\n",
|
||||||
" # edge_weight_y.append(y1 + y1 - y0)\n",
|
" # edge_weight_y.append(y1 + y1 - y0)\n",
|
||||||
" # edge_weight_x.append(x0 + x0 - x1) \n",
|
" # edge_weight_x.append(x0 + x0 - x1)\n",
|
||||||
" # edge_weight_y.append(y0 + y0 - y1)\n",
|
" # edge_weight_y.append(y0 + y0 - y1)\n",
|
||||||
" edge_weight_x.append(x0 + ((x1 - x0) / 2))\n",
|
" edge_weight_x.append(x0 + ((x1 - x0) / 2))\n",
|
||||||
" edge_weight_y.append(y0 + ((y1 - y0) / 2))\n",
|
" edge_weight_y.append(y0 + ((y1 - y0) / 2))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"edge_trace = go.Scatter(\n",
|
"edge_trace = go.Scatter(\n",
|
||||||
" x=edge_x, y=edge_y,\n",
|
" x=edge_x,\n",
|
||||||
" line=dict(width=0.5, color='#888'),\n",
|
" y=edge_y,\n",
|
||||||
" hoverinfo='none',\n",
|
" line=dict(width=0.5, color=\"#888\"),\n",
|
||||||
" mode='lines')\n",
|
" hoverinfo=\"none\",\n",
|
||||||
|
" mode=\"lines\",\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"edge_weights_trace = go.Scatter(\n",
|
||||||
|
" x=edge_weight_x,\n",
|
||||||
|
" y=edge_weight_y,\n",
|
||||||
|
" mode=\"text\",\n",
|
||||||
|
" marker_size=1,\n",
|
||||||
|
" text=[0.45, 0.7, 0.34],\n",
|
||||||
|
" textposition=\"top center\",\n",
|
||||||
|
" hovertemplate=\"weight: %{text}<extra></extra>\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"edge_weights_trace = go.Scatter(x=edge_weight_x,y= edge_weight_y, mode='text',\n",
|
|
||||||
" marker_size=1,\n",
|
|
||||||
" text=[0.45, 0.7, 0.34],\n",
|
|
||||||
" textposition='top center',\n",
|
|
||||||
" hovertemplate='weight: %{text}<extra></extra>')\n",
|
|
||||||
" \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"node_x = []\n",
|
"node_x = []\n",
|
||||||
"node_y = []\n",
|
"node_y = []\n",
|
||||||
@ -2455,26 +2493,26 @@
|
|||||||
" node_y.append(y)\n",
|
" node_y.append(y)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_trace = go.Scatter(\n",
|
"node_trace = go.Scatter(\n",
|
||||||
" x=node_x, y=node_y,\n",
|
" x=node_x,\n",
|
||||||
" mode='markers',\n",
|
" y=node_y,\n",
|
||||||
" hoverinfo='text',\n",
|
" mode=\"markers\",\n",
|
||||||
|
" hoverinfo=\"text\",\n",
|
||||||
" marker=dict(\n",
|
" marker=dict(\n",
|
||||||
" showscale=True,\n",
|
" showscale=True,\n",
|
||||||
" # colorscale options\n",
|
" # colorscale options\n",
|
||||||
" #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |\n",
|
" #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |\n",
|
||||||
" #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |\n",
|
" #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |\n",
|
||||||
" #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |\n",
|
" #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |\n",
|
||||||
" colorscale='YlGnBu',\n",
|
" colorscale=\"YlGnBu\",\n",
|
||||||
" reversescale=True,\n",
|
" reversescale=True,\n",
|
||||||
" color=[],\n",
|
" color=[],\n",
|
||||||
" size=10,\n",
|
" size=10,\n",
|
||||||
" colorbar=dict(\n",
|
" colorbar=dict(\n",
|
||||||
" thickness=15,\n",
|
" thickness=15, title=\"Node Connections\", xanchor=\"left\", titleside=\"right\"\n",
|
||||||
" title='Node Connections',\n",
|
|
||||||
" xanchor='left',\n",
|
|
||||||
" titleside='right'\n",
|
|
||||||
" ),\n",
|
" ),\n",
|
||||||
" line_width=2))"
|
" line_width=2,\n",
|
||||||
|
" ),\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -2511,8 +2549,8 @@
|
|||||||
" node_adjacencies.append(len(adjacencies[1]))\n",
|
" node_adjacencies.append(len(adjacencies[1]))\n",
|
||||||
" # node_text.append('# of connections: '+str(len(adjacencies[1])))\n",
|
" # node_text.append('# of connections: '+str(len(adjacencies[1])))\n",
|
||||||
" # node_adjacencies.append(nodes[node].color)\n",
|
" # node_adjacencies.append(nodes[node].color)\n",
|
||||||
" node_text.append('# of connections: '+str(len(adjacencies[1])))\n",
|
" node_text.append(\"# of connections: \" + str(len(adjacencies[1])))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"print(node_adjacencies)\n",
|
"print(node_adjacencies)\n",
|
||||||
"# node_trace.marker.color = node_adjacencies\n",
|
"# node_trace.marker.color = node_adjacencies\n",
|
||||||
"# node_trace.text = node_text\n",
|
"# node_trace.text = node_text\n",
|
||||||
@ -2543,13 +2581,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"#Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!\n",
|
"# Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!\n",
|
||||||
"colors = list(nx.get_node_attributes(graph, \"color\").values())\n",
|
"colors = list(nx.get_node_attributes(graph, \"color\").values())\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_names = []\n",
|
"node_names = []\n",
|
||||||
"for key, value in nodes.items():\n",
|
"for key, value in nodes.items():\n",
|
||||||
" \n",
|
" if \"name\" in value.keys():\n",
|
||||||
" if 'name' in value.keys():\n",
|
|
||||||
" node_names.append(value[\"name\"])\n",
|
" node_names.append(value[\"name\"])\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" node_names.append(value[\"firstname\"] + \" \" + value[\"lastname\"])\n",
|
" node_names.append(value[\"firstname\"] + \" \" + value[\"lastname\"])\n",
|
||||||
@ -2561,8 +2598,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"node_trace.marker.color = colors\n",
|
"node_trace.marker.color = colors\n",
|
||||||
"node_trace.text = node_names\n",
|
"node_trace.text = node_names\n",
|
||||||
"print(list(df[\"eigenvector\"]*100))\n",
|
"print(list(df[\"eigenvector\"] * 100))\n",
|
||||||
"node_trace.marker.size = list(df[\"eigenvector\"]*100)"
|
"node_trace.marker.size = list(df[\"eigenvector\"] * 100)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -12520,21 +12557,28 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"fig = go.Figure(data=[edge_trace, edge_weights_trace, node_trace],\n",
|
"fig = go.Figure(\n",
|
||||||
" layout=go.Layout(\n",
|
" data=[edge_trace, edge_weights_trace, node_trace],\n",
|
||||||
" title='<br>Network graph made with Python',\n",
|
" layout=go.Layout(\n",
|
||||||
" titlefont_size=16,\n",
|
" title=\"<br>Network graph made with Python\",\n",
|
||||||
" showlegend=False,\n",
|
" titlefont_size=16,\n",
|
||||||
" hovermode='closest',\n",
|
" showlegend=False,\n",
|
||||||
" margin=dict(b=20,l=5,r=5,t=40),\n",
|
" hovermode=\"closest\",\n",
|
||||||
" annotations=[ dict(\n",
|
" margin=dict(b=20, l=5, r=5, t=40),\n",
|
||||||
" text=\"Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>\",\n",
|
" annotations=[\n",
|
||||||
" showarrow=False,\n",
|
" dict(\n",
|
||||||
" xref=\"paper\", yref=\"paper\",\n",
|
" text=\"Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>\",\n",
|
||||||
" x=0.005, y=-0.002 ) ],\n",
|
" showarrow=False,\n",
|
||||||
" xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
" xref=\"paper\",\n",
|
||||||
" yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))\n",
|
" yref=\"paper\",\n",
|
||||||
" )\n",
|
" x=0.005,\n",
|
||||||
|
" y=-0.002,\n",
|
||||||
|
" )\n",
|
||||||
|
" ],\n",
|
||||||
|
" xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
||||||
|
" yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
|
||||||
|
" ),\n",
|
||||||
|
")\n",
|
||||||
"fig.show()"
|
"fig.show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -13219,6 +13263,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
"# Extract node and edge positions from the layout\n",
|
"# Extract node and edge positions from the layout\n",
|
||||||
"node_xyz = np.array([pos[v] for v in sorted(graph)])\n",
|
"node_xyz = np.array([pos[v] for v in sorted(graph)])\n",
|
||||||
"edge_xyz = np.array([(pos[u], pos[v]) for u, v in graph.edges()])\n",
|
"edge_xyz = np.array([(pos[u], pos[v]) for u, v in graph.edges()])\n",
|
||||||
@ -13234,6 +13279,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import plotly.graph_objects as go\n",
|
"import plotly.graph_objects as go\n",
|
||||||
|
"\n",
|
||||||
"edge_x = []\n",
|
"edge_x = []\n",
|
||||||
"edge_y = []\n",
|
"edge_y = []\n",
|
||||||
"edge_z = []\n",
|
"edge_z = []\n",
|
||||||
@ -13257,23 +13303,22 @@
|
|||||||
" edge_z.append(z1)\n",
|
" edge_z.append(z1)\n",
|
||||||
" # edge_z.append(z2)\n",
|
" # edge_z.append(z2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # edge_weight_x.append(x1 + x1 - x0) \n",
|
" # edge_weight_x.append(x1 + x1 - x0)\n",
|
||||||
" # edge_weight_y.append(y1 + y1 - y0)\n",
|
" # edge_weight_y.append(y1 + y1 - y0)\n",
|
||||||
" # edge_weight_x.append(x0 + x0 - x1) \n",
|
" # edge_weight_x.append(x0 + x0 - x1)\n",
|
||||||
" # edge_weight_y.append(y0 + y0 - y1)\n",
|
" # edge_weight_y.append(y0 + y0 - y1)\n",
|
||||||
" # edge_weight_x.append(x0 + ((x1 - x0) / 2))\n",
|
" # edge_weight_x.append(x0 + ((x1 - x0) / 2))\n",
|
||||||
" # edge_weight_y.append(y0 + ((y1 - y0) / 2))\n",
|
" # edge_weight_y.append(y0 + ((y1 - y0) / 2))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"edge_trace = go.Scatter3d(\n",
|
||||||
"\n",
|
" x=edge_x,\n",
|
||||||
"edge_trace=go.Scatter3d(x=edge_x,\n",
|
" y=edge_y,\n",
|
||||||
" y=edge_y,\n",
|
" z=edge_z,\n",
|
||||||
" z=edge_z,\n",
|
" mode=\"lines\",\n",
|
||||||
" mode='lines',\n",
|
" line=dict(color=\"rgb(125,125,125)\", width=1),\n",
|
||||||
" line=dict(color='rgb(125,125,125)', width=1),\n",
|
" hoverinfo=\"none\",\n",
|
||||||
" hoverinfo='none'\n",
|
")\n",
|
||||||
" )\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"node_x = []\n",
|
"node_x = []\n",
|
||||||
"node_y = []\n",
|
"node_y = []\n",
|
||||||
@ -13284,58 +13329,58 @@
|
|||||||
" node_y.append(y)\n",
|
" node_y.append(y)\n",
|
||||||
" node_z.append(z)\n",
|
" node_z.append(z)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_trace=go.Scatter3d(x=node_x,\n",
|
"node_trace = go.Scatter3d(\n",
|
||||||
" y=node_y,\n",
|
" x=node_x,\n",
|
||||||
" z=node_z,\n",
|
" y=node_y,\n",
|
||||||
" mode='markers',\n",
|
" z=node_z,\n",
|
||||||
" name='actors',\n",
|
" mode=\"markers\",\n",
|
||||||
" marker=dict(symbol='circle',\n",
|
" name=\"actors\",\n",
|
||||||
" size=6,\n",
|
" marker=dict(\n",
|
||||||
" color=\"blue\",\n",
|
" symbol=\"circle\",\n",
|
||||||
" colorscale='Viridis',\n",
|
" size=6,\n",
|
||||||
" line=dict(color='rgb(50,50,50)', width=0.5)\n",
|
" color=\"blue\",\n",
|
||||||
" ),\n",
|
" colorscale=\"Viridis\",\n",
|
||||||
" # text=labels,\n",
|
" line=dict(color=\"rgb(50,50,50)\", width=0.5),\n",
|
||||||
" hoverinfo='text'\n",
|
" ),\n",
|
||||||
" )\n",
|
" # text=labels,\n",
|
||||||
|
" hoverinfo=\"text\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"axis=dict(showbackground=False,\n",
|
"axis = dict(\n",
|
||||||
" showline=False,\n",
|
" showbackground=False,\n",
|
||||||
" zeroline=False,\n",
|
" showline=False,\n",
|
||||||
" showgrid=False,\n",
|
" zeroline=False,\n",
|
||||||
" showticklabels=False,\n",
|
" showgrid=False,\n",
|
||||||
" title=''\n",
|
" showticklabels=False,\n",
|
||||||
" )\n",
|
" title=\"\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"layout = go.Layout(\n",
|
"layout = go.Layout(\n",
|
||||||
" title=\"Network of coappearances of characters in Victor Hugo's novel<br> Les Miserables (3D visualization)\",\n",
|
" title=\"Network of coappearances of characters in Victor Hugo's novel<br> Les Miserables (3D visualization)\",\n",
|
||||||
" width=1000,\n",
|
" width=1000,\n",
|
||||||
" height=1000,\n",
|
" height=1000,\n",
|
||||||
" showlegend=False,\n",
|
" showlegend=False,\n",
|
||||||
" scene=dict(\n",
|
" scene=dict(\n",
|
||||||
" xaxis=dict(axis),\n",
|
" xaxis=dict(axis),\n",
|
||||||
" yaxis=dict(axis),\n",
|
" yaxis=dict(axis),\n",
|
||||||
" zaxis=dict(axis),\n",
|
" zaxis=dict(axis),\n",
|
||||||
" ),\n",
|
|
||||||
" margin=dict(\n",
|
|
||||||
" t=100\n",
|
|
||||||
" ),\n",
|
" ),\n",
|
||||||
" hovermode='closest',\n",
|
" margin=dict(t=100),\n",
|
||||||
|
" hovermode=\"closest\",\n",
|
||||||
" annotations=[\n",
|
" annotations=[\n",
|
||||||
" dict(\n",
|
" dict(\n",
|
||||||
" showarrow=False,\n",
|
" showarrow=False,\n",
|
||||||
" text=\"Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>\",\n",
|
" text=\"Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>\",\n",
|
||||||
" xref='paper',\n",
|
" xref=\"paper\",\n",
|
||||||
" yref='paper',\n",
|
" yref=\"paper\",\n",
|
||||||
" x=0,\n",
|
" x=0,\n",
|
||||||
" y=0.1,\n",
|
" y=0.1,\n",
|
||||||
" xanchor='left',\n",
|
" xanchor=\"left\",\n",
|
||||||
" yanchor='bottom',\n",
|
" yanchor=\"bottom\",\n",
|
||||||
" font=dict(\n",
|
" font=dict(size=14),\n",
|
||||||
" size=14\n",
|
" )\n",
|
||||||
" )\n",
|
" ],\n",
|
||||||
" )\n",
|
")"
|
||||||
" ], )"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -13353,13 +13398,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"#Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!\n",
|
"# Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!\n",
|
||||||
"colors = list(nx.get_node_attributes(graph, \"color\").values())\n",
|
"colors = list(nx.get_node_attributes(graph, \"color\").values())\n",
|
||||||
"\n",
|
"\n",
|
||||||
"node_names = []\n",
|
"node_names = []\n",
|
||||||
"for key, value in nodes.items():\n",
|
"for key, value in nodes.items():\n",
|
||||||
" \n",
|
" if \"name\" in value.keys():\n",
|
||||||
" if 'name' in value.keys():\n",
|
|
||||||
" node_names.append(value[\"name\"])\n",
|
" node_names.append(value[\"name\"])\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" node_names.append(value[\"firstname\"] + \" \" + value[\"lastname\"])\n",
|
" node_names.append(value[\"firstname\"] + \" \" + value[\"lastname\"])\n",
|
||||||
@ -13396,9 +13440,9 @@
|
|||||||
"edge_colors = []\n",
|
"edge_colors = []\n",
|
||||||
"for row in edges:\n",
|
"for row in edges:\n",
|
||||||
" if row[\"type\"] == \"HAFTENDER_GESELLSCHAFTER\":\n",
|
" if row[\"type\"] == \"HAFTENDER_GESELLSCHAFTER\":\n",
|
||||||
" edge_colors.append('rgb(255,0,0)')\n",
|
" edge_colors.append(\"rgb(255,0,0)\")\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" edge_colors.append('rgb(255,105,180)')\n",
|
" edge_colors.append(\"rgb(255,105,180)\")\n",
|
||||||
"print(edge_colors)\n",
|
"print(edge_colors)\n",
|
||||||
"edge_trace.line = dict(color=edge_colors, width=2)"
|
"edge_trace.line = dict(color=edge_colors, width=2)"
|
||||||
]
|
]
|
||||||
@ -22038,9 +22082,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# edge_trace, \n",
|
"# edge_trace,\n",
|
||||||
"data=[edge_trace, node_trace]\n",
|
"data = [edge_trace, node_trace]\n",
|
||||||
"fig=go.Figure(data=data, layout=layout)\n",
|
"fig = go.Figure(data=data, layout=layout)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"fig.show()"
|
"fig.show()"
|
||||||
]
|
]
|
File diff suppressed because one or more lines are too long
@ -1,128 +1,133 @@
|
|||||||
|
"""This Module contains the Method to create a 2D Network Graph with NetworkX."""
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
|
|
||||||
def initialize_network(edges: list, nodes: list):
|
|
||||||
# create edges from dataframe
|
|
||||||
df_edges = pd.DataFrame(edges)
|
|
||||||
graph = nx.from_pandas_edgelist(df_edges, source="from", target="to", edge_attr="type")
|
|
||||||
|
|
||||||
# update node attributes from dataframe
|
def create_2d_graph(
|
||||||
nx.set_node_attributes(graph, nodes)
|
graph: nx.Graph, nodes: dict, edges: list, metrics: pd.DataFrame, metric: str | None
|
||||||
|
) -> go.Figure:
|
||||||
|
"""This Method creates a 2d Network in Plotly with a Scatter Graph and retuns it.
|
||||||
|
|
||||||
metrices = pd.DataFrame(columns=["degree", "eigenvector", "betweeness", "closeness", "pagerank"])
|
Args:
|
||||||
|
graph (_type_): NetworkX Graph.
|
||||||
metrices["eigenvector"] = nx.eigenvector_centrality(graph).values()
|
nodes (_type_): List of Nodes
|
||||||
metrices["degree"] = nx.degree_centrality(graph).values()
|
edges (_type_): List of Edges
|
||||||
metrices["betweeness"] = nx.betweenness_centrality(graph).values()
|
metrics (_type_): DataFrame with the MEtrics
|
||||||
metrices["closeness"] = nx.closeness_centrality(graph).values()
|
metric (_type_): Selected Metric
|
||||||
metrices["pagerank"] = nx.pagerank(graph).values()
|
|
||||||
|
|
||||||
return graph, metrices
|
|
||||||
|
|
||||||
def create_2d_graph(graph, nodes, edges,metrices, metric):
|
|
||||||
edge_x = []
|
|
||||||
edge_y = []
|
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: Plotly Figure
|
||||||
|
"""
|
||||||
|
# Set 2D Layout
|
||||||
pos = nx.spring_layout(graph)
|
pos = nx.spring_layout(graph)
|
||||||
|
|
||||||
|
# Initialize Variables to set the Position of the Edges.
|
||||||
|
edge_x = []
|
||||||
|
edge_y = []
|
||||||
|
# Initialize Node Position Variables.
|
||||||
|
node_x = []
|
||||||
|
node_y = []
|
||||||
|
# Initialize Position Variables for the Description Text of the edges.
|
||||||
edge_weight_x = []
|
edge_weight_x = []
|
||||||
edge_weight_y = []
|
edge_weight_y = []
|
||||||
G = graph
|
|
||||||
for edge in G.edges():
|
# Getting the Positions from NetworkX and assign them to the variables.
|
||||||
|
for edge in graph.edges():
|
||||||
x0, y0 = pos[edge[0]]
|
x0, y0 = pos[edge[0]]
|
||||||
x1, y1 = pos[edge[1]]
|
x1, y1 = pos[edge[1]]
|
||||||
edge_x.append(x0)
|
edge_x.append(x0)
|
||||||
edge_x.append(x1)
|
edge_x.append(x1)
|
||||||
edge_x.append(None)
|
edge_x.append(None)
|
||||||
|
|
||||||
edge_y.append(y0)
|
edge_y.append(y0)
|
||||||
edge_y.append(y1)
|
edge_y.append(y1)
|
||||||
edge_y.append(None)
|
edge_y.append(None)
|
||||||
|
|
||||||
# edge_weight_x.append(x1 + x1 - x0)
|
|
||||||
# edge_weight_y.append(y1 + y1 - y0)
|
|
||||||
# edge_weight_x.append(x0 + x0 - x1)
|
|
||||||
# edge_weight_y.append(y0 + y0 - y1)
|
|
||||||
edge_weight_x.append(x0 + ((x1 - x0) / 2))
|
edge_weight_x.append(x0 + ((x1 - x0) / 2))
|
||||||
edge_weight_y.append(y0 + ((y1 - y0) / 2))
|
edge_weight_y.append(y0 + ((y1 - y0) / 2))
|
||||||
|
edge_weight_y.append(None)
|
||||||
|
# Add the Edges to the scatter plot according to their Positions.
|
||||||
edge_trace = go.Scatter(
|
edge_trace = go.Scatter(
|
||||||
x=edge_x, y=edge_y,
|
x=edge_x,
|
||||||
line=dict(width=0.5, color='#888'),
|
y=edge_y,
|
||||||
hoverinfo='none',
|
line={"width": 0.5, "color": "#888"},
|
||||||
mode='lines')
|
hoverinfo="none",
|
||||||
|
mode="lines",
|
||||||
|
)
|
||||||
|
# Add the Edgedescriptiontext to the scatter plot according to its Position.
|
||||||
|
edge_weights_trace = go.Scatter(
|
||||||
|
x=edge_weight_x,
|
||||||
|
y=edge_weight_y,
|
||||||
|
mode="text",
|
||||||
|
marker_size=1,
|
||||||
|
text=[0.45, 0.7, 0.34],
|
||||||
|
textposition="top center",
|
||||||
|
hovertemplate="weight: %{text}<extra></extra>",
|
||||||
|
)
|
||||||
|
|
||||||
edge_weights_trace = go.Scatter(x=edge_weight_x,y= edge_weight_y, mode='text',
|
# Getting the Positions from NetworkX and assign it to the variables.
|
||||||
marker_size=1,
|
for node in graph.nodes():
|
||||||
text=[0.45, 0.7, 0.34],
|
|
||||||
textposition='top center',
|
|
||||||
hovertemplate='weight: %{text}<extra></extra>')
|
|
||||||
|
|
||||||
|
|
||||||
node_x = []
|
|
||||||
node_y = []
|
|
||||||
for node in G.nodes():
|
|
||||||
x, y = pos[node]
|
x, y = pos[node]
|
||||||
node_x.append(x)
|
node_x.append(x)
|
||||||
node_y.append(y)
|
node_y.append(y)
|
||||||
|
# Add the Nodes to the scatter plot according to their Positions.
|
||||||
node_trace = go.Scatter(
|
node_trace = go.Scatter(
|
||||||
x=node_x, y=node_y,
|
x=node_x,
|
||||||
mode='markers',
|
y=node_y,
|
||||||
hoverinfo='text',
|
mode="markers",
|
||||||
marker=dict(
|
hoverinfo="text",
|
||||||
showscale=True,
|
marker={
|
||||||
colorscale='YlGnBu',
|
"showscale": True,
|
||||||
reversescale=True,
|
"colorscale": "YlGnBu",
|
||||||
color=[],
|
"reversescale": True,
|
||||||
size=10,
|
"color": [],
|
||||||
colorbar=dict(
|
"size": 10,
|
||||||
thickness=15,
|
"colorbar": {
|
||||||
title='Node Connections',
|
"thickness": 15,
|
||||||
xanchor='left',
|
"title": "Node Connections",
|
||||||
titleside='right'
|
"xanchor": "left",
|
||||||
),
|
"titleside": "right",
|
||||||
line_width=2))
|
},
|
||||||
|
"line_width": 2,
|
||||||
#Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!
|
},
|
||||||
colors = list(nx.get_node_attributes(graph, "color").values())
|
)
|
||||||
|
|
||||||
|
# Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!
|
||||||
|
colors = list(nx.get_node_attributes(graph, "color").values())
|
||||||
|
# Get the Node Text
|
||||||
node_names = []
|
node_names = []
|
||||||
for key, value in nodes.items():
|
for key, value in nodes.items():
|
||||||
|
if "name" in value:
|
||||||
if 'name' in value.keys():
|
|
||||||
node_names.append(value["name"])
|
node_names.append(value["name"])
|
||||||
else:
|
else:
|
||||||
node_names.append(value["firstname"] + " " + value["lastname"])
|
node_names.append(value["firstname"] + " " + value["lastname"])
|
||||||
|
# Add Color and Names to the Scatter Plot.
|
||||||
|
|
||||||
node_trace.marker.color = colors
|
node_trace.marker.color = colors
|
||||||
node_trace.text = node_names
|
node_trace.text = node_names
|
||||||
|
|
||||||
if metric != None:
|
# Highlight the Node Size in regards to the selected Metric.
|
||||||
node_trace.marker.size = list(metrices[metric]*500)
|
if metric is not None:
|
||||||
# print(list(metrices[metric]*500))
|
node_trace.marker.size = list(metrics[metric] * 500)
|
||||||
|
|
||||||
|
|
||||||
|
# Add Relation_Type as a Descriptin for the edges.
|
||||||
edge_type_list = []
|
edge_type_list = []
|
||||||
|
|
||||||
for row in edges:
|
for row in edges:
|
||||||
edge_type_list.append(row["type"])
|
edge_type_list.append(row["type"])
|
||||||
|
|
||||||
edge_weights_trace.text = edge_type_list
|
edge_weights_trace.text = edge_type_list
|
||||||
|
|
||||||
return go.Figure(data=[edge_trace, edge_weights_trace, node_trace],
|
# Return the Plotly Figure
|
||||||
layout=go.Layout(
|
return go.Figure(
|
||||||
title='<br>Network graph made with Python',
|
data=[edge_trace, edge_weights_trace, node_trace],
|
||||||
titlefont_size=16,
|
layout=go.Layout(
|
||||||
showlegend=False,
|
title="<br>Network graph made with Python",
|
||||||
hovermode='closest',
|
titlefont_size=16,
|
||||||
margin=dict(b=20,l=5,r=5,t=40),
|
showlegend=False,
|
||||||
annotations=[ dict(
|
hovermode="closest",
|
||||||
text="Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>",
|
margin={"b": 20, "l": 5, "r": 5, "t": 20},
|
||||||
showarrow=False,
|
# annotations=,
|
||||||
xref="paper", yref="paper",
|
xaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
|
||||||
x=0.005, y=-0.002 ) ],
|
yaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
|
||||||
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
),
|
||||||
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
)
|
||||||
)
|
|
||||||
|
@ -1,33 +1,38 @@
|
|||||||
|
"""This Module contains the Method to create a 3D Network Graph with NetworkX."""
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
|
|
||||||
def initialize_network(edges: list, nodes: list):
|
|
||||||
# create edges from dataframe
|
|
||||||
df_edges = pd.DataFrame(edges)
|
|
||||||
graph = nx.from_pandas_edgelist(df_edges, source="from", target="to", edge_attr="type")
|
|
||||||
|
|
||||||
# update node attributes from dataframe
|
def create_3d_graph(
|
||||||
nx.set_node_attributes(graph, nodes)
|
graph: nx.Graph, nodes: dict, edges: list, metrics: pd.DataFrame, metric: str | None
|
||||||
|
) -> go.Figure:
|
||||||
|
"""This Method creates a 3D Network in Plotly with a Scatter Graph and retuns it.
|
||||||
|
|
||||||
metrices = pd.DataFrame(columns=["degree", "eigenvector", "betweeness", "closeness", "pagerank"])
|
Args:
|
||||||
|
graph (_type_): NetworkX Graph.
|
||||||
|
nodes (_type_): List of Nodes
|
||||||
|
edges (_type_): List of Edges
|
||||||
|
metrics (_type_): DataFrame with the MEtrics
|
||||||
|
metric (_type_): Selected Metric
|
||||||
|
|
||||||
metrices["eigenvector"] = nx.eigenvector_centrality(graph).values()
|
Returns:
|
||||||
metrices["degree"] = nx.degree_centrality(graph).values()
|
_type_: Plotly Figure
|
||||||
metrices["betweeness"] = nx.betweenness_centrality(graph).values()
|
"""
|
||||||
metrices["closeness"] = nx.closeness_centrality(graph).values()
|
# 3d spring layout
|
||||||
metrices["pagerank"] = nx.pagerank(graph).values()
|
pos = nx.spring_layout(graph, dim=3, seed=779)
|
||||||
|
|
||||||
return graph, metrices
|
# Initialize Variables to set the Position of the Edges.
|
||||||
|
|
||||||
def create_3d_graph(graph, nodes, edges,metrices, metric):
|
|
||||||
edge_x = []
|
edge_x = []
|
||||||
edge_y = []
|
edge_y = []
|
||||||
edge_z = []
|
edge_z = []
|
||||||
|
|
||||||
# 3d spring layout
|
# Initialize Node Position Variables.
|
||||||
pos = nx.spring_layout(graph, dim=3, seed=779)
|
node_x = []
|
||||||
|
node_y = []
|
||||||
|
node_z = []
|
||||||
|
|
||||||
|
# Getting the Positions from NetworkX and assign them to the variables.
|
||||||
for edge in graph.edges():
|
for edge in graph.edges():
|
||||||
x0, y0, z0 = pos[edge[0]]
|
x0, y0, z0 = pos[edge[0]]
|
||||||
x1, y1, z1 = pos[edge[1]]
|
x1, y1, z1 = pos[edge[1]]
|
||||||
@ -41,102 +46,101 @@ def create_3d_graph(graph, nodes, edges,metrices, metric):
|
|||||||
edge_z.append(z0)
|
edge_z.append(z0)
|
||||||
edge_z.append(z1)
|
edge_z.append(z1)
|
||||||
|
|
||||||
edge_trace=go.Scatter3d(x=edge_x,
|
# Add the Edges to the scatter plot according to their Positions.
|
||||||
y=edge_y,
|
edge_trace = go.Scatter3d(
|
||||||
z=edge_z,
|
x=edge_x,
|
||||||
mode='lines',
|
y=edge_y,
|
||||||
line=dict(color='rgb(125,125,125)', width=1),
|
z=edge_z,
|
||||||
hoverinfo='none'
|
mode="lines",
|
||||||
)
|
line={"color": "rgb(125,125,125)", "width": 1},
|
||||||
|
hoverinfo="none",
|
||||||
node_x = []
|
)
|
||||||
node_y = []
|
|
||||||
node_z = []
|
|
||||||
|
|
||||||
|
# Getting the Positions from NetworkX and assign it to the variables.
|
||||||
for node in graph.nodes():
|
for node in graph.nodes():
|
||||||
x, y, z = pos[node]
|
x, y, z = pos[node]
|
||||||
node_x.append(x)
|
node_x.append(x)
|
||||||
node_y.append(y)
|
node_y.append(y)
|
||||||
node_z.append(z)
|
node_z.append(z)
|
||||||
|
|
||||||
node_trace=go.Scatter3d(x=node_x,
|
# Add the Nodes to the scatter plot according to their Positions.
|
||||||
y=node_y,
|
node_trace = go.Scatter3d(
|
||||||
z=node_z,
|
x=node_x,
|
||||||
mode='markers',
|
y=node_y,
|
||||||
name='actors',
|
z=node_z,
|
||||||
marker=dict(symbol='circle',
|
mode="markers",
|
||||||
size=6,
|
name="actors",
|
||||||
color="blue",
|
marker={
|
||||||
colorscale='Viridis',
|
"symbol": "circle",
|
||||||
line=dict(color='rgb(50,50,50)', width=0.5)
|
"size": 6,
|
||||||
),
|
"color": "blue",
|
||||||
# text=labels,
|
"colorscale": "Viridis",
|
||||||
hoverinfo='text'
|
"line": {"color": "rgb(50,50,50)", "width": 0.5},
|
||||||
)
|
},
|
||||||
|
# text=labels,
|
||||||
|
hoverinfo="text",
|
||||||
|
)
|
||||||
|
|
||||||
axis=dict(showbackground=False,
|
axis = {
|
||||||
showline=False,
|
"showbackground": False,
|
||||||
zeroline=False,
|
"showline": False,
|
||||||
showgrid=False,
|
"zeroline": False,
|
||||||
showticklabels=False,
|
"showgrid": False,
|
||||||
title=''
|
"showticklabels": False,
|
||||||
)
|
"title": "",
|
||||||
|
}
|
||||||
|
|
||||||
layout = go.Layout(
|
layout = go.Layout(
|
||||||
title="Social Graph",
|
title="Social Graph",
|
||||||
|
showlegend=False,
|
||||||
showlegend=False,
|
scene={
|
||||||
scene=dict(
|
"xaxis": dict(axis),
|
||||||
xaxis=dict(axis),
|
"yaxis": dict(axis),
|
||||||
yaxis=dict(axis),
|
"zaxis": dict(axis),
|
||||||
zaxis=dict(axis),
|
},
|
||||||
),
|
margin={"t": 10},
|
||||||
margin=dict(
|
hovermode="closest",
|
||||||
t=10
|
|
||||||
),
|
|
||||||
hovermode='closest',
|
|
||||||
annotations=[
|
annotations=[
|
||||||
dict(
|
{
|
||||||
showarrow=False,
|
"showarrow": False,
|
||||||
text="Companies (Blue) & Person (Red) Relation",
|
"text": "Companies (Blue) & Person (Red) Relation",
|
||||||
xref='paper',
|
"xref": "paper",
|
||||||
yref='paper',
|
"yref": "paper",
|
||||||
x=0,
|
"x": 0,
|
||||||
y=0.1,
|
"y": 0.1,
|
||||||
xanchor='left',
|
"xanchor": "left",
|
||||||
yanchor='bottom',
|
"yanchor": "bottom",
|
||||||
font=dict(
|
"font": {"size": 14},
|
||||||
size=14
|
}
|
||||||
)
|
],
|
||||||
)
|
)
|
||||||
], )
|
|
||||||
|
|
||||||
#Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!
|
# Set Color by using the nodes DataFrame with its Color Attribute. The sequence matters!
|
||||||
colors = list(nx.get_node_attributes(graph, "color").values())
|
colors = list(nx.get_node_attributes(graph, "color").values())
|
||||||
|
|
||||||
node_names = []
|
node_names = []
|
||||||
for key, value in nodes.items():
|
for key, value in nodes.items():
|
||||||
|
if "name" in value:
|
||||||
if 'name' in value.keys():
|
|
||||||
node_names.append(value["name"])
|
node_names.append(value["name"])
|
||||||
else:
|
else:
|
||||||
node_names.append(value["firstname"] + " " + value["lastname"])
|
node_names.append(value["firstname"] + " " + value["lastname"])
|
||||||
|
# Add Color and Names to the Scatter Plot.
|
||||||
node_trace.marker.color = colors
|
node_trace.marker.color = colors
|
||||||
node_trace.text = node_names
|
node_trace.text = node_names
|
||||||
|
|
||||||
if metric != None:
|
# Highlight the Node Size in regards to the selected Metric.
|
||||||
node_trace.marker.size = list(metrices[metric]*500)
|
if metric is not None:
|
||||||
print("Test")
|
node_trace.marker.size = list(metrics[metric] * 500)
|
||||||
|
|
||||||
|
# Set the Color and width of Edges for better highlighting.
|
||||||
edge_colors = []
|
edge_colors = []
|
||||||
for row in edges:
|
for row in edges:
|
||||||
if row["type"] == "HAFTENDER_GESELLSCHAFTER":
|
if row["type"] == "HAFTENDER_GESELLSCHAFTER":
|
||||||
edge_colors.append('rgb(255,0,0)')
|
edge_colors.append("rgb(255,0,0)")
|
||||||
else:
|
else:
|
||||||
edge_colors.append('rgb(255,105,180)')
|
edge_colors.append("rgb(255,105,180)")
|
||||||
edge_trace.line = dict(color=edge_colors, width=2)
|
edge_trace.line = {"color": edge_colors, "width": 2}
|
||||||
|
|
||||||
|
# Return the Plotly Figure
|
||||||
data=[edge_trace, node_trace]
|
data = [edge_trace, node_trace]
|
||||||
return go.Figure(data=data, layout=layout)
|
return go.Figure(data=data, layout=layout)
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
"""This Module has a Method to initialize a Network with NetworkX which can be used for 2D and 3D Graphs."""
|
||||||
|
import networkx as nx
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_network(edges: list, nodes: dict) -> tuple[nx.Graph, pd.DataFrame]:
|
||||||
|
"""This Method creates a Network from the Framework NetworkX with the help of a Node and Edge List. Furthemore it creates a DataFrame with the most important Metrics.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
edges (list): List with the connections between Nodes.
|
||||||
|
nodes (list): List with all Nodes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Graph: Plotly Figure
|
||||||
|
Metrices: DataFrame with Metrics
|
||||||
|
"""
|
||||||
|
# create edge dataframe
|
||||||
|
df_edges = pd.DataFrame(edges)
|
||||||
|
graph = nx.from_pandas_edgelist(
|
||||||
|
df_edges, source="from", target="to", edge_attr="type"
|
||||||
|
)
|
||||||
|
|
||||||
|
# update node attributes from dataframe
|
||||||
|
nx.set_node_attributes(graph, nodes)
|
||||||
|
|
||||||
|
# Create a DataFrame with all Metrics
|
||||||
|
metrices = pd.DataFrame(
|
||||||
|
columns=["degree", "eigenvector", "betweeness", "closeness", "pagerank"]
|
||||||
|
)
|
||||||
|
metrices["eigenvector"] = nx.eigenvector_centrality(graph).values()
|
||||||
|
metrices["degree"] = nx.degree_centrality(graph).values()
|
||||||
|
metrices["betweeness"] = nx.betweenness_centrality(graph).values()
|
||||||
|
metrices["closeness"] = nx.closeness_centrality(graph).values()
|
||||||
|
metrices["pagerank"] = nx.pagerank(graph).values()
|
||||||
|
|
||||||
|
return graph, metrices
|
@ -1,11 +1,12 @@
|
|||||||
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
"""Module to receive and filter Data for working with NetworkX."""
|
||||||
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
import pandas as pd
|
|
||||||
from sqlalchemy import func, text
|
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||||
|
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
||||||
from aki_prj23_transparenzregister.utils.sql.connector import get_session
|
from aki_prj23_transparenzregister.utils.sql.connector import get_session
|
||||||
|
|
||||||
|
# Gets the Session Key for the DB Connection.
|
||||||
session = get_session(JsonFileConfigProvider("secrets.json"))
|
session = get_session(JsonFileConfigProvider("secrets.json"))
|
||||||
|
|
||||||
# Alias for Company table for the base company
|
# Alias for Company table for the base company
|
||||||
@ -14,54 +15,68 @@ to_company = aliased(entities.Company, name="to_company")
|
|||||||
# Alias for Company table for the head company
|
# Alias for Company table for the head company
|
||||||
from_company = aliased(entities.Company, name="from_company")
|
from_company = aliased(entities.Company, name="from_company")
|
||||||
|
|
||||||
|
|
||||||
def find_all_company_relations() -> pd.DataFrame:
|
def find_all_company_relations() -> pd.DataFrame:
|
||||||
"""_summary_
|
"""_summary_.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
pd.DataFrame: _description_
|
pd.DataFrame: _description_
|
||||||
"""
|
"""
|
||||||
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
query_companies = session.query(entities.Company) #.all()
|
query_companies = session.query(entities.Company) # .all()
|
||||||
query_relations = session.query(entities.CompanyRelation) # .all()
|
query_relations = session.query(entities.CompanyRelation) # .all()
|
||||||
|
|
||||||
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
||||||
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
companies_relations_df: pd.DataFrame = pd.read_sql(str(query_relations), session.bind) # type: ignore
|
||||||
companies_relations_df = companies_relations_df[["relation_id","company_relation_company2_id"]]
|
companies_relations_df = companies_relations_df[
|
||||||
|
["relation_id", "company_relation_company2_id"]
|
||||||
|
]
|
||||||
company_name = []
|
company_name = []
|
||||||
connected_company_name = []
|
connected_company_name = []
|
||||||
|
|
||||||
companies_relations_df = companies_relations_df.head()
|
companies_relations_df = companies_relations_df.head()
|
||||||
|
|
||||||
for _, row in companies_relations_df.iterrows():
|
for _, row in companies_relations_df.iterrows():
|
||||||
company_name.append(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
company_name.append(
|
||||||
connected_company_name.append(companies_df.loc[companies_df["company_id"] == row["company_relation_company2_id"]]["company_name"].values[0])
|
companies_df.loc[companies_df["company_id"] == row["relation_id"]][
|
||||||
|
"company_name"
|
||||||
|
].values[0]
|
||||||
|
)
|
||||||
|
connected_company_name.append(
|
||||||
|
companies_df.loc[
|
||||||
|
companies_df["company_id"] == row["company_relation_company2_id"]
|
||||||
|
]["company_name"].values[0]
|
||||||
|
)
|
||||||
|
|
||||||
companies_relations_df["company_name"] = company_name
|
companies_relations_df["company_name"] = company_name
|
||||||
companies_relations_df["connected_company_name"] = connected_company_name
|
companies_relations_df["connected_company_name"] = connected_company_name
|
||||||
|
|
||||||
return companies_relations_df
|
return companies_relations_df
|
||||||
|
|
||||||
|
|
||||||
def find_top_companies() -> pd.DataFrame:
|
def find_top_companies() -> pd.DataFrame:
|
||||||
"""_summary_
|
"""_summary_.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
pd.DataFrame: _description_
|
pd.DataFrame: _description_
|
||||||
"""
|
"""
|
||||||
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
query_companies = session.query(entities.Company) #.all()
|
query_companies = session.query(entities.Company) # .all()
|
||||||
|
|
||||||
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
|
||||||
companies_df = companies_df.head()
|
companies_df = companies_df.head()
|
||||||
companies_df = companies_df[["company_name"]]
|
companies_df = companies_df[["company_name"]]
|
||||||
companies_df["Platzierung"] = [1,2,3,4,5]
|
companies_df["Platzierung"] = [1, 2, 3, 4, 5]
|
||||||
companies_df["Umsatz M€"] = [1,2,3,4,5]
|
companies_df["Umsatz M€"] = [1, 2, 3, 4, 5]
|
||||||
companies_df = companies_df[['Platzierung', 'company_name', 'Umsatz M€']]
|
return companies_df[["Platzierung", "company_name", "Umsatz M€"]]
|
||||||
# print(companies_df)
|
|
||||||
return companies_df
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_company_relations() -> pd.DataFrame:
|
||||||
|
"""This Methods makes a Database Request for all Companies and their relations, modifies the ID Column and returns the Result as an DataFrame.
|
||||||
|
|
||||||
def get_all_company_relations():
|
Returns:
|
||||||
|
DataFrame: DataFrame with all Relations between Companies.
|
||||||
|
"""
|
||||||
# Query to fetch relations between companies
|
# Query to fetch relations between companies
|
||||||
relations_company_query = (
|
relations_company_query = (
|
||||||
session.query(
|
session.query(
|
||||||
@ -83,12 +98,22 @@ def get_all_company_relations():
|
|||||||
str(relations_company_query)
|
str(relations_company_query)
|
||||||
company_relations = pd.read_sql_query(str(relations_company_query), session.bind)
|
company_relations = pd.read_sql_query(str(relations_company_query), session.bind)
|
||||||
|
|
||||||
company_relations['id_company_from'] = company_relations['id_company_from'].apply(lambda x: f"c_{x}")
|
company_relations["id_company_from"] = company_relations["id_company_from"].apply(
|
||||||
company_relations['id_company_to'] = company_relations['id_company_to'].apply(lambda x: f"c_{x}")
|
lambda x: f"c_{x}"
|
||||||
|
)
|
||||||
|
company_relations["id_company_to"] = company_relations["id_company_to"].apply(
|
||||||
|
lambda x: f"c_{x}"
|
||||||
|
)
|
||||||
|
|
||||||
return company_relations
|
return company_relations
|
||||||
|
|
||||||
def get_all_person_relations():
|
|
||||||
|
def get_all_person_relations() -> pd.DataFrame:
|
||||||
|
"""This Methods makes a Database Request for all Persons and their relations, modifies the ID Column and returns the Result as an DataFrame.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
DataFrame: DataFrame with all Relations between Persons and Companies.
|
||||||
|
"""
|
||||||
relations_person_query = (
|
relations_person_query = (
|
||||||
session.query(
|
session.query(
|
||||||
entities.Company.id.label("id_company"),
|
entities.Company.id.label("id_company"),
|
||||||
@ -110,83 +135,129 @@ def get_all_person_relations():
|
|||||||
)
|
)
|
||||||
person_relations = pd.read_sql_query(str(relations_person_query), session.bind)
|
person_relations = pd.read_sql_query(str(relations_person_query), session.bind)
|
||||||
|
|
||||||
person_relations['id_company'] = person_relations['id_company'].apply(lambda x: f"c_{x}")
|
person_relations["id_company"] = person_relations["id_company"].apply(
|
||||||
person_relations['id_person'] = person_relations['id_person'].apply(lambda x: f"p_{x}")
|
lambda x: f"c_{x}"
|
||||||
|
)
|
||||||
|
person_relations["id_person"] = person_relations["id_person"].apply(
|
||||||
|
lambda x: f"p_{x}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
return person_relations
|
return person_relations
|
||||||
|
|
||||||
|
|
||||||
def filter_relation_type(df: pd.DataFrame, selected_relation_type):
|
def filter_relation_type(
|
||||||
df = df.loc[df["relation_type"] == selected_relation_type]
|
relation_dataframe: pd.DataFrame, selected_relation_type: str
|
||||||
return df
|
) -> pd.DataFrame:
|
||||||
|
"""This Method filters the given DataFrame based on the selected Relation Type and returns it.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
relation_dataframe (pd.DataFrame): DataFrame must contain a column named "relation_type".
|
||||||
|
selected_relation_type (str): String with the filter value.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
relation_dataframe (pd.DataFrame): The filtered DataFrame which now only contains entries with the selected Relation Type.
|
||||||
|
"""
|
||||||
|
return relation_dataframe.loc[
|
||||||
|
relation_dataframe["relation_type"] == selected_relation_type
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def filter_relation_with_more_than_one_connection(df: pd.DataFrame, id_column_name_to, id_column_name_from):
|
def filter_relation_with_more_than_one_connection(
|
||||||
# print(df.columns.values)
|
relation_dataframe: pd.DataFrame, id_column_name_to: str, id_column_name_from: str
|
||||||
tmp_df = pd.DataFrame(columns= df.columns.values)
|
) -> pd.DataFrame:
|
||||||
# print(tmp_df)
|
"""Method to filter all Entries in an DataFrame which has more than one connection.
|
||||||
for _index, row in df.iterrows():
|
|
||||||
|
Args:
|
||||||
|
relation_dataframe (pd.DataFrame): _description_
|
||||||
|
id_column_name_to (_type_): _description_
|
||||||
|
id_column_name_from (_type_): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
relation_dataframe (pd.DataFrame): The DataFrame which now only contains entries with more than one connection.
|
||||||
|
"""
|
||||||
|
tmp_df = pd.DataFrame(columns=relation_dataframe.columns.values)
|
||||||
|
for _index, row in relation_dataframe.iterrows():
|
||||||
count = 0
|
count = 0
|
||||||
id = row[id_column_name_to]
|
id = row[id_column_name_to]
|
||||||
for _index_sub, row_sub in df.iterrows():
|
for _index_sub, row_sub in relation_dataframe.iterrows():
|
||||||
if id == row_sub[id_column_name_to]:
|
if id == row_sub[id_column_name_to]:
|
||||||
count = count + 1
|
count = count + 1
|
||||||
if id == row_sub[id_column_name_from]:
|
if id == row_sub[id_column_name_from]:
|
||||||
count = count + 1
|
count = count + 1
|
||||||
if count > 1:
|
if count > 1:
|
||||||
break
|
break
|
||||||
|
|
||||||
if count > 1:
|
if count > 1:
|
||||||
# tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+
|
# tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+
|
||||||
tmp_df.loc[len(tmp_df)] = row
|
tmp_df.loc[len(tmp_df)] = row
|
||||||
# print(row)
|
|
||||||
count = 0
|
count = 0
|
||||||
else:
|
else:
|
||||||
count = 0
|
count = 0
|
||||||
continue
|
continue
|
||||||
# print(tmp_df)
|
|
||||||
count = 0
|
count = 0
|
||||||
return tmp_df
|
return tmp_df
|
||||||
|
|
||||||
|
|
||||||
def create_edge_and_node_list(person_relations: pd.DataFrame, company_relations:pd.DataFrame):
|
def create_edge_and_node_list(
|
||||||
nodes = {}
|
person_relations: pd.DataFrame, company_relations: pd.DataFrame
|
||||||
edges = []
|
) -> tuple[dict, list]:
|
||||||
|
"""In this Method the given DataFrame with the relation will be morphed to Edge and Node lists and enhanced by a coloring for companies and person Nodes.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
person_relations (pd.DataFrame): _description_
|
||||||
|
company_relations (pd.DataFrame): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: _description_
|
||||||
|
"""
|
||||||
|
nodes: dict = {}
|
||||||
|
edges: list = []
|
||||||
|
|
||||||
COLOR_COMPANY = "blue"
|
COLOR_COMPANY = "blue"
|
||||||
COLOR_PERSON = "red"
|
COLOR_PERSON = "red"
|
||||||
|
|
||||||
# Iterate over person relations
|
# Iterate over person relations
|
||||||
for _index, row in person_relations.iterrows():
|
for _index, row in person_relations.iterrows():
|
||||||
if node:= nodes.get(row['id_company']) is None:
|
if node := nodes.get(row["id_company"]) is None:
|
||||||
nodes[row['id_company']] = {
|
nodes[row["id_company"]] = {
|
||||||
"id": row['id_company'],
|
"id": row["id_company"],
|
||||||
'name': row['name_company'],
|
"name": row["name_company"],
|
||||||
'color': COLOR_COMPANY
|
"color": COLOR_COMPANY,
|
||||||
}
|
}
|
||||||
if node:= nodes.get(row['id_person']) is None:
|
if node := nodes.get(row["id_person"]) is None:
|
||||||
nodes[row['id_person']] = {
|
nodes[row["id_person"]] = {
|
||||||
"id": row['id_person'],
|
"id": row["id_person"],
|
||||||
'firstname': row['firstname'],
|
"firstname": row["firstname"],
|
||||||
'lastname': row['lastname'],
|
"lastname": row["lastname"],
|
||||||
'date_of_birth': row['date_of_birth'],
|
"date_of_birth": row["date_of_birth"],
|
||||||
'color': COLOR_PERSON
|
"color": COLOR_PERSON,
|
||||||
}
|
}
|
||||||
edges.append({'from': row['id_person'], 'to': row['id_company'], 'type': row['relation_type']})
|
edges.append(
|
||||||
|
{
|
||||||
|
"from": row["id_person"],
|
||||||
|
"to": row["id_company"],
|
||||||
|
"type": row["relation_type"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
for _index, row in company_relations.iterrows():
|
for _index, row in company_relations.iterrows():
|
||||||
if node:= nodes.get(row['id_company_from']) is None:
|
if node := nodes.get(row["id_company_from"]) is None:
|
||||||
nodes[row['id_company_from']] = {
|
nodes[row["id_company_from"]] = {
|
||||||
"id": row['id_company_from'],
|
"id": row["id_company_from"],
|
||||||
'name': row['name_company_from'],
|
"name": row["name_company_from"],
|
||||||
'color': COLOR_COMPANY
|
"color": COLOR_COMPANY,
|
||||||
}
|
}
|
||||||
if node:= nodes.get(row['id_company_to']) is None:
|
if node := nodes.get(row["id_company_to"]) is None:
|
||||||
nodes[row['id_company_to']] = {
|
nodes[row["id_company_to"]] = {
|
||||||
"id": row['id_company_to'],
|
"id": row["id_company_to"],
|
||||||
'name': row['name_company_to'],
|
"name": row["name_company_to"],
|
||||||
'color': COLOR_COMPANY
|
"color": COLOR_COMPANY,
|
||||||
}
|
}
|
||||||
edges.append({'from': row['id_company_from'], 'to': row['id_company_to'], 'type': row['relation_type']})
|
edges.append(
|
||||||
return nodes, edges
|
{
|
||||||
|
"from": row["id_company_from"],
|
||||||
|
"to": row["id_company_to"],
|
||||||
|
"type": row["relation_type"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return nodes, edges
|
||||||
|
Reference in New Issue
Block a user