mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-05-13 12:58:45 +02:00
On branch feature/visualize-verflechtungen
This commit is contained in:
parent
d3158c4592
commit
6585a0ee11
@ -76,6 +76,9 @@ torchvision = {version = "*", source = "torch-cpu"}
|
|||||||
tqdm = "^4.66.1"
|
tqdm = "^4.66.1"
|
||||||
transformers = {version = "*", extras = ["torch"]}
|
transformers = {version = "*", extras = ["torch"]}
|
||||||
xmltodict = "^0.13.0"
|
xmltodict = "^0.13.0"
|
||||||
|
dash_cytoscape = "^0.2.0"
|
||||||
|
dashvis = "^0.1.3"
|
||||||
|
|
||||||
|
|
||||||
[tool.poetry.extras]
|
[tool.poetry.extras]
|
||||||
ingest = ["selenium", "deutschland", "xmltodict"]
|
ingest = ["selenium", "deutschland", "xmltodict"]
|
||||||
|
25
src/aki_prj23_transparenzregister/ui/cytoscape_dash.py
Normal file
25
src/aki_prj23_transparenzregister/ui/cytoscape_dash.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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)
|
11
src/aki_prj23_transparenzregister/ui/dashvis_networkx.py
Normal file
11
src/aki_prj23_transparenzregister/ui/dashvis_networkx.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# 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)
|
139
src/aki_prj23_transparenzregister/ui/networkx_dash.py
Normal file
139
src/aki_prj23_transparenzregister/ui/networkx_dash.py
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import networkx as nx
|
||||||
|
import plotly.graph_objects as go
|
||||||
|
from dash import Dash, Input, Output, dcc, html
|
||||||
|
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||||
|
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
||||||
|
|
||||||
|
test_company = 13 #2213 # 13
|
||||||
|
|
||||||
|
def find_company_relations(company_id: int) -> pd.DataFrame:
|
||||||
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
|
query_companies = session.query(entities.Company)
|
||||||
|
query_relations = session.query(entities.CompanyRelation)
|
||||||
|
|
||||||
|
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 = companies_relations_df.loc[companies_relations_df["relation_id"] == company_id,:][["relation_id","company_relation_company2_id"]]
|
||||||
|
|
||||||
|
company_name = []
|
||||||
|
connected_company_name = []
|
||||||
|
|
||||||
|
for _, row in companies_relations_df.iterrows():
|
||||||
|
# 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])
|
||||||
|
connected_company_name.append(companies_df.loc[companies_df["company_id"] == row["company_relation_company2_id"]]["company_name"].values[0])
|
||||||
|
|
||||||
|
# print(company_name)
|
||||||
|
companies_relations_df["company_name"] = company_name
|
||||||
|
companies_relations_df["connected_company_name"] = connected_company_name
|
||||||
|
# print(companies_relations_df)
|
||||||
|
return companies_relations_df
|
||||||
|
|
||||||
|
# Plotly figure
|
||||||
|
def networkGraph(EGDE_VAR: None) -> go.Figure:
|
||||||
|
# df = find_company_relations(test_company)
|
||||||
|
edges = []
|
||||||
|
for index, row in find_company_relations(test_company).iterrows():
|
||||||
|
edges.append([row["company_name"], row["connected_company_name"]])
|
||||||
|
# print(row["company_name"], row["connected_company_name"])
|
||||||
|
# print(edges)
|
||||||
|
# edges = df[["relation_id","company_relation_company2_id"]]
|
||||||
|
# edges = [[EGDE_VAR, "B"], ["B", "C"], ["B", "D"]]
|
||||||
|
network_graph = nx.Graph()
|
||||||
|
network_graph.add_edges_from(edges)
|
||||||
|
pos = nx.spring_layout(network_graph)
|
||||||
|
|
||||||
|
# edges trace
|
||||||
|
edge_x = []
|
||||||
|
edge_y = []
|
||||||
|
for edge in network_graph.edges():
|
||||||
|
x0, y0 = pos[edge[0]]
|
||||||
|
x1, y1 = pos[edge[1]]
|
||||||
|
edge_x.append(x0)
|
||||||
|
edge_x.append(x1)
|
||||||
|
edge_x.append(None)
|
||||||
|
edge_y.append(y0)
|
||||||
|
edge_y.append(y1)
|
||||||
|
edge_y.append(None)
|
||||||
|
|
||||||
|
edge_trace = go.Scatter(
|
||||||
|
x=edge_x,
|
||||||
|
y=edge_y,
|
||||||
|
line={"color": "black", "width": 1},
|
||||||
|
hoverinfo="none",
|
||||||
|
showlegend=False,
|
||||||
|
mode="lines",
|
||||||
|
)
|
||||||
|
|
||||||
|
# nodes trace
|
||||||
|
node_x = []
|
||||||
|
node_y = []
|
||||||
|
text = []
|
||||||
|
for node in network_graph.nodes():
|
||||||
|
x, y = pos[node]
|
||||||
|
node_x.append(x)
|
||||||
|
node_y.append(y)
|
||||||
|
text.append(node)
|
||||||
|
|
||||||
|
node_trace = go.Scatter(
|
||||||
|
x=node_x,
|
||||||
|
y=node_y,
|
||||||
|
text=text,
|
||||||
|
mode="markers+text",
|
||||||
|
showlegend=False,
|
||||||
|
hoverinfo="none",
|
||||||
|
marker={"color": "pink", "size": 50, "line": {"color": "black", "width": 1}},
|
||||||
|
)
|
||||||
|
|
||||||
|
# layout
|
||||||
|
layout = {
|
||||||
|
"plot_bgcolor": "white",
|
||||||
|
"paper_bgcolor": "white",
|
||||||
|
"margin": {"t": 10, "b": 10, "l": 10, "r": 10, "pad": 0},
|
||||||
|
"xaxis": {
|
||||||
|
"linecolor": "black",
|
||||||
|
"showgrid": False,
|
||||||
|
"showticklabels": False,
|
||||||
|
"mirror": True,
|
||||||
|
},
|
||||||
|
"yaxis": {
|
||||||
|
"linecolor": "black",
|
||||||
|
"showgrid": False,
|
||||||
|
"showticklabels": False,
|
||||||
|
"mirror": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
# figure
|
||||||
|
return go.Figure(data=[edge_trace, node_trace], layout=layout)
|
||||||
|
|
||||||
|
|
||||||
|
# Dash App
|
||||||
|
|
||||||
|
|
||||||
|
app = Dash(__name__)
|
||||||
|
|
||||||
|
app.title = "Dash Networkx"
|
||||||
|
|
||||||
|
app.layout = html.Div(
|
||||||
|
[
|
||||||
|
html.I("Write your EDGE_VAR"),
|
||||||
|
html.Br(),
|
||||||
|
dcc.Input(id="EGDE_VAR", type="text", value="K", debounce=True),
|
||||||
|
dcc.Graph(id="my-graph"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
Output("my-graph", "figure"),
|
||||||
|
[Input("EGDE_VAR", "value")],
|
||||||
|
)
|
||||||
|
def update_output(EGDE_VAR: None) -> None:
|
||||||
|
return networkGraph(EGDE_VAR)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(debug=True)
|
173
src/aki_prj23_transparenzregister/ui/networkx_dash_overall.py
Normal file
173
src/aki_prj23_transparenzregister/ui/networkx_dash_overall.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import networkx as nx
|
||||||
|
import plotly.graph_objects as go
|
||||||
|
from dash import Dash, Input, Output, dcc, html
|
||||||
|
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||||
|
from aki_prj23_transparenzregister.utils.sql import connector, entities
|
||||||
|
|
||||||
|
test_company = 13 #2213 # 13
|
||||||
|
|
||||||
|
def find_all_company_relations() -> pd.DataFrame:
|
||||||
|
session = connector.get_session(JsonFileConfigProvider("./secrets.json"))
|
||||||
|
query_companies = session.query(entities.Company) #.all()
|
||||||
|
query_relations = session.query(entities.CompanyRelation) # .all()
|
||||||
|
|
||||||
|
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
|
||||||
|
# print(companies_relations_df)
|
||||||
|
companies_relations_df = companies_relations_df[["relation_id","company_relation_company2_id"]]
|
||||||
|
# print(companies_relations_df)
|
||||||
|
company_name = []
|
||||||
|
connected_company_name = []
|
||||||
|
|
||||||
|
companies_relations_df = companies_relations_df.head()
|
||||||
|
# print(companies_relations_df)
|
||||||
|
|
||||||
|
for _, row in companies_relations_df.iterrows():
|
||||||
|
# print(companies_df.loc[companies_df["company_id"] == row["relation_id"]]["company_name"].values[0])
|
||||||
|
# print("TEst")
|
||||||
|
company_name.append(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(connected_company_name)
|
||||||
|
|
||||||
|
# print(company_name)
|
||||||
|
companies_relations_df["company_name"] = company_name
|
||||||
|
companies_relations_df["connected_company_name"] = connected_company_name
|
||||||
|
# print("Test")
|
||||||
|
# print(companies_relations_df)
|
||||||
|
return companies_relations_df
|
||||||
|
|
||||||
|
# Plotly figure
|
||||||
|
def networkGraph(EGDE_VAR: None) -> go.Figure:
|
||||||
|
# find_all_company_relations()
|
||||||
|
|
||||||
|
edges = []
|
||||||
|
for index, row in find_all_company_relations().iterrows():
|
||||||
|
edges.append([row["company_name"], row["connected_company_name"]])
|
||||||
|
# print(row["company_name"], row["connected_company_name"])
|
||||||
|
# print(edges)
|
||||||
|
# edges = df[["relation_id","company_relation_company2_id"]]
|
||||||
|
# edges = [[EGDE_VAR, "B"], ["B", "C"], ["B", "D"]]
|
||||||
|
network_graph = nx.Graph()
|
||||||
|
network_graph.add_edges_from(edges)
|
||||||
|
pos = nx.spring_layout(network_graph)
|
||||||
|
|
||||||
|
# edges trace
|
||||||
|
edge_x = []
|
||||||
|
edge_y = []
|
||||||
|
for edge in network_graph.edges():
|
||||||
|
x0, y0 = pos[edge[0]]
|
||||||
|
x1, y1 = pos[edge[1]]
|
||||||
|
edge_x.append(x0)
|
||||||
|
edge_x.append(x1)
|
||||||
|
edge_x.append(None)
|
||||||
|
edge_y.append(y0)
|
||||||
|
edge_y.append(y1)
|
||||||
|
edge_y.append(None)
|
||||||
|
|
||||||
|
edge_trace = go.Scatter(
|
||||||
|
x=edge_x,
|
||||||
|
y=edge_y,
|
||||||
|
line={"color": "black", "width": 1},
|
||||||
|
hoverinfo="none",
|
||||||
|
showlegend=False,
|
||||||
|
mode="lines",
|
||||||
|
)
|
||||||
|
|
||||||
|
# nodes trace
|
||||||
|
node_x = []
|
||||||
|
node_y = []
|
||||||
|
text = []
|
||||||
|
for node in network_graph.nodes():
|
||||||
|
x, y = pos[node]
|
||||||
|
node_x.append(x)
|
||||||
|
node_y.append(y)
|
||||||
|
text.append(node)
|
||||||
|
|
||||||
|
node_trace = go.Scatter(
|
||||||
|
x=node_x,
|
||||||
|
y=node_y,
|
||||||
|
text=text,
|
||||||
|
mode="markers+text",
|
||||||
|
showlegend=False,
|
||||||
|
hoverinfo="none",
|
||||||
|
marker={"color": "pink", "size": 50, "line": {"color": "black", "width": 1}},
|
||||||
|
)
|
||||||
|
|
||||||
|
# layout
|
||||||
|
layout = {
|
||||||
|
"plot_bgcolor": "white",
|
||||||
|
"paper_bgcolor": "white",
|
||||||
|
"margin": {"t": 10, "b": 10, "l": 10, "r": 10, "pad": 0},
|
||||||
|
"xaxis": {
|
||||||
|
"linecolor": "black",
|
||||||
|
"showgrid": False,
|
||||||
|
"showticklabels": False,
|
||||||
|
"mirror": True,
|
||||||
|
},
|
||||||
|
"yaxis": {
|
||||||
|
"linecolor": "black",
|
||||||
|
"showgrid": False,
|
||||||
|
"showticklabels": False,
|
||||||
|
"mirror": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
print(nx.eigenvector_centrality(network_graph))
|
||||||
|
measure_vector = {}
|
||||||
|
network_metrics_df = pd.DataFrame()
|
||||||
|
|
||||||
|
|
||||||
|
measure_vector = nx.eigenvector_centrality(network_graph)
|
||||||
|
network_metrics_df["eigenvector"] = measure_vector.values()
|
||||||
|
|
||||||
|
measure_vector = nx.degree_centrality(network_graph)
|
||||||
|
network_metrics_df["degree"] = measure_vector.values()
|
||||||
|
|
||||||
|
measure_vector = nx.betweenness_centrality(network_graph)
|
||||||
|
network_metrics_df["betweeness"] = measure_vector.values()
|
||||||
|
|
||||||
|
measure_vector = nx.closeness_centrality(network_graph)
|
||||||
|
network_metrics_df["closeness"] = measure_vector.values()
|
||||||
|
|
||||||
|
# measure_vector = nx.pagerank(network_graph)
|
||||||
|
# network_metrics_df["pagerank"] = measure_vector.values()
|
||||||
|
|
||||||
|
# measure_vector = nx.average_degree_connectivity(network_graph)
|
||||||
|
# network_metrics_df["average_degree"] = measure_vector.values()
|
||||||
|
print(network_metrics_df)
|
||||||
|
|
||||||
|
# figure
|
||||||
|
return go.Figure(data=[edge_trace, node_trace], layout=layout)
|
||||||
|
|
||||||
|
|
||||||
|
# Dash App
|
||||||
|
|
||||||
|
|
||||||
|
app = Dash(__name__)
|
||||||
|
|
||||||
|
app.title = "Dash Networkx"
|
||||||
|
|
||||||
|
app.layout = html.Div(
|
||||||
|
[
|
||||||
|
html.I("Write your EDGE_VAR"),
|
||||||
|
html.Br(),
|
||||||
|
# dcc.Dropdown(['eigenvector', 'degree', 'betweeness', 'closeness'], 'eigenvector', id='metric-dropdown'),
|
||||||
|
dcc.Input(id="EGDE_VAR", type="text", value="K", debounce=True),
|
||||||
|
dcc.Graph(id="my-graph"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
Output("my-graph", "figure"),
|
||||||
|
# Input('metric-dropdown', 'value'),
|
||||||
|
[Input("EGDE_VAR", "value")],
|
||||||
|
)
|
||||||
|
def update_output(EGDE_VAR: None) -> None:
|
||||||
|
return networkGraph(EGDE_VAR)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(debug=True)
|
6
tests/ui/cytoscape_dash_test.py
Normal file
6
tests/ui/cytoscape_dash_test.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
"""Test for the NetworkX Component."""
|
||||||
|
|
||||||
|
|
||||||
|
# def networkGraph(Edges) -> None:
|
||||||
|
# """Checks if an import co company_stats_dash can be made."""
|
||||||
|
# assert networkx_dash is not None
|
6
tests/ui/dashvis_networkx_test.py
Normal file
6
tests/ui/dashvis_networkx_test.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
"""Test for the NetworkX Component."""
|
||||||
|
|
||||||
|
|
||||||
|
# def networkGraph(Edges) -> None:
|
||||||
|
# """Checks if an import co company_stats_dash can be made."""
|
||||||
|
# assert networkx_dash is not None
|
7
tests/ui/networkx_dash_test.py
Normal file
7
tests/ui/networkx_dash_test.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""Test for the NetworkX Component."""
|
||||||
|
from aki_prj23_transparenzregister.ui import networkx_dash
|
||||||
|
|
||||||
|
|
||||||
|
def networkGraph(Edges: None) -> None:
|
||||||
|
"""Checks if an import co company_stats_dash can be made."""
|
||||||
|
assert networkx_dash is not None
|
Loading…
x
Reference in New Issue
Block a user