solved errors

This commit is contained in:
Tim 2023-10-25 20:10:29 +02:00
parent 7e8adfafd5
commit 30f9e4506f
6 changed files with 69 additions and 71 deletions

View File

@ -1,10 +1,11 @@
"""Content of home page."""
from functools import lru_cache
import dash
import dash_daq as daq
import networkx as nx
import pandas as pd
import plotly.graph_objects as go
from dash import Input, Output, callback, dash_table, dcc, html
from dash import Input, Output, callback, dash_table, dcc, html, ctx
from aki_prj23_transparenzregister.utils.networkx.network_2d import (
create_2d_graph,
@ -157,53 +158,7 @@ layout = html.Div(
)
)
@callback(
Output("my-graph", "figure"),
[
Input("dropdown", "value"),
Input("switch", "on"),
Input("dropdown_company_relation_filter", "value"),
Input("dropdown_person_relation_filter", "value"),
],
prevent_initial_call=True,
allow_duplicate=True,
)
def update_figure(
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.
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 triggered_id == 'dropdown_companyrelation_filter' or triggered_id == 'dropdown_personrelation_filter':
# print(selected_value)
# print(metrics)
# print(graph)
# graph, metrics = update_graph_data(person_relation_type= p_relation_filter, company_relation_type= c_relation_filter)
if switch_value:
return create_2d_graph(graph, nodes, edges, metrics, metric)
else:
return create_3d_graph(graph, nodes, edges, metrics, metric)
# @lru_cache(200)
def update_graph_data(
person_relation_type: str = "HAFTENDER_GESELLSCHAFTER",
company_relation_type: str = "GESCHAEFTSFUEHRER",
@ -222,3 +177,50 @@ def update_graph_data(
graph, metrics = initialize_network(nodes=nodes, edges=edges)
return graph, metrics
@callback(
Output("my-graph", "figure"),
[
Input("dropdown", "value"),
Input("switch", "on"),
Input("dropdown_company_relation_filter", "value"),
Input("dropdown_person_relation_filter", "value"),
],
prevent_initial_call=True,
allow_duplicate=True,
)
# @lru_cache(20)
def update_figure(
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.
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
# triggered_id = ctx.triggered_id
# if triggered_id == 'dropdown_companyrelation_filter' or triggered_id == 'dropdown_personrelation_filter':
# print(selected_value)
# print(metrics)
# print(graph)
graph, metrics = update_graph_data(person_relation_type= p_relation_filter_value, company_relation_type= c_relation_filter_value)
if switch_value:
return create_2d_graph(graph, nodes, edges, metrics, selected_metric)
else:
return create_3d_graph(graph, nodes, edges, metrics, selected_metric)

View File

@ -1,5 +1,6 @@
"""This Module contains the Method to create a 2D Network Graph with NetworkX."""
import networkx as nx
import numpy as np
import pandas as pd
import plotly.graph_objects as go
@ -107,8 +108,8 @@ def create_2d_graph(
node_trace.text = node_names
# Highlight the Node Size in regards to the selected Metric.
if metric is not None:
node_trace.marker.size = list(metrics[metric] * 500)
if metric != "None":
node_trace.marker.size = list(np.sqrt(metrics[metric]) * 200)
# Add Relation_Type as a Descriptin for the edges.
edge_type_list = []

View File

@ -1,5 +1,6 @@
"""This Module contains the Method to create a 3D Network Graph with NetworkX."""
import networkx as nx
import numpy as np
import pandas as pd
import plotly.graph_objects as go
@ -129,8 +130,8 @@ def create_3d_graph(
node_trace.text = node_names
# Highlight the Node Size in regards to the selected Metric.
if metric is not None:
node_trace.marker.size = list(metrics[metric] * 500)
if metric != "None":
node_trace.marker.size = list(np.cbrt(metrics[metric]) * 200)
# Set the Color and width of Edges for better highlighting.
edge_colors = []

View File

@ -97,7 +97,7 @@ def get_all_company_relations() -> pd.DataFrame:
)
)
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) # type: ignore
company_relations["id_company_from"] = company_relations["id_company_from"].apply(
lambda x: f"c_{x}"
@ -134,7 +134,7 @@ def get_all_person_relations() -> pd.DataFrame:
entities.PersonRelation.person_id == entities.Person.id,
)
)
person_relations = pd.read_sql_query(str(relations_person_query), session.bind)
person_relations = pd.read_sql_query(str(relations_person_query), session.bind) # type: ignore
person_relations["id_company"] = person_relations["id_company"].apply(
lambda x: f"c_{x}"
@ -190,7 +190,7 @@ def filter_relation_with_more_than_one_connection(
if count > 1:
# tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+
tmp_df.loc[len(tmp_df)] = row
tmp_df.loc[len(tmp_df)] = row
count = 0
else:
count = 0
@ -267,9 +267,6 @@ def create_edge_and_node_list(
def find_company_relations(
selected_company_id: int,
) -> tuple[pd.DataFrame, pd.DataFrame()]:
print("In Method:" + str(selected_company_id))
selected_company_id = 312
# print( entities.Company.id == selected_company_id)
relations_company_query = (
session.query(
to_company.id.label("id_company_to"),
@ -286,9 +283,10 @@ def find_company_relations(
from_company,
entities.CompanyRelation.company2_id == from_company.id,
)
.where(to_company.id == selected_company_id)
.filter((from_company.id == selected_company_id) | (to_company.id == selected_company_id))
)
logger.debug(str(relations_company_query))
company_relations = pd.DataFrame(relations_company_query.all())
# logger.debug(str(relations_company_query))
# relations_company_query = relations_company_query.filter(entities.CompanyRelation.company2_id == selected_company_id) # or (entities.CompanyRelation.company2_id == selected_company_id))
# logger.debug(str(relations_company_query))
# tmp = str(relations_company_query) + " WHERE from_company.id = " + str(selected_company_id)
@ -306,11 +304,7 @@ def find_company_relations(
"id_company_from",
],
)
logger.debug(str(relations_company_query))
print("query ergebnis:")
print(company_relations)
company_relations["id_company_from"] = company_relations["id_company_from"].apply(
lambda x: f"c_{x}"
)

View File

@ -40,13 +40,12 @@ def get_engine(conn_args: SQLConnectionString) -> Engine:
database=conn_args.database,
port=conn_args.port,
)
return sa.create_engine(url, echo=True)
return sa.create_engine(url)
if isinstance(conn_args, SQLiteConnectionString):
return sa.create_engine(
str(conn_args),
connect_args={"check_same_thread": True},
poolclass=SingletonThreadPool,
echo=True,
poolclass=SingletonThreadPool
)
raise TypeError("The type of the configuration is invalid.")

View File

@ -8,8 +8,9 @@ import pandas as pd
from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network
tc = TestCase()
import pytest
@pytest.mark.tim
def test_initialize_network() -> None:
edges: list = [
{"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"},
@ -30,8 +31,8 @@ def test_initialize_network() -> None:
},
}
graph, metrics = initialize_network(edges=edges, nodes=nodes)
assert type(graph) is nx.Graph
assert type(metrics) is pd.DataFrame
assert isinstance(graph, nx.Graph)
assert isinstance(metrics, pd.DataFrame)
tc.assertListEqual(
list(metrics.columns),
["degree", "eigenvector", "betweeness", "closeness", "pagerank"],