From 30f9e4506f1e04186928a7dfdda7d3eda07b26dd Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 25 Oct 2023 20:10:29 +0200 Subject: [PATCH] solved errors --- .../ui/pages/home.py | 98 ++++++++++--------- .../utils/networkx/network_2d.py | 5 +- .../utils/networkx/network_3d.py | 5 +- .../utils/networkx/networkx_data.py | 20 ++-- .../utils/sql/connector.py | 5 +- tests/utils/networkx/network_base_test.py | 7 +- 6 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/aki_prj23_transparenzregister/ui/pages/home.py b/src/aki_prj23_transparenzregister/ui/pages/home.py index 21cedb0..865366f 100644 --- a/src/aki_prj23_transparenzregister/ui/pages/home.py +++ b/src/aki_prj23_transparenzregister/ui/pages/home.py @@ -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) + + + diff --git a/src/aki_prj23_transparenzregister/utils/networkx/network_2d.py b/src/aki_prj23_transparenzregister/utils/networkx/network_2d.py index 39da7f3..d6af5f3 100644 --- a/src/aki_prj23_transparenzregister/utils/networkx/network_2d.py +++ b/src/aki_prj23_transparenzregister/utils/networkx/network_2d.py @@ -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 = [] diff --git a/src/aki_prj23_transparenzregister/utils/networkx/network_3d.py b/src/aki_prj23_transparenzregister/utils/networkx/network_3d.py index 980e98b..f3a88a6 100644 --- a/src/aki_prj23_transparenzregister/utils/networkx/network_3d.py +++ b/src/aki_prj23_transparenzregister/utils/networkx/network_3d.py @@ -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 = [] diff --git a/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py b/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py index f71b655..d582def 100644 --- a/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py +++ b/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py @@ -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}" ) diff --git a/src/aki_prj23_transparenzregister/utils/sql/connector.py b/src/aki_prj23_transparenzregister/utils/sql/connector.py index 335e556..7208e0f 100644 --- a/src/aki_prj23_transparenzregister/utils/sql/connector.py +++ b/src/aki_prj23_transparenzregister/utils/sql/connector.py @@ -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.") diff --git a/tests/utils/networkx/network_base_test.py b/tests/utils/networkx/network_base_test.py index ed2fd00..d040182 100644 --- a/tests/utils/networkx/network_base_test.py +++ b/tests/utils/networkx/network_base_test.py @@ -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"],